From 82dd5d8ccf77b4da7f3679c3830517da6ca7c5b3 Mon Sep 17 00:00:00 2001 From: "Daniel J. Hofmann" Date: Fri, 18 Sep 2015 14:26:32 +0200 Subject: [PATCH] Use Boost.Optional instead of custom optional monad implementation. This switches out the `` implementation of the optional monad to the one from Boost. The following trick makes sure we keep compile times down: - use `` to forward declare the optional type in header, then include the full blown optional header only in the implementation file. - do the same for the files we touch, e.g. forward declare osmium types, allowing us to remove the osmium header dependency from our headers: `namespace osmium { class Relation; } and then include the appropriate osmium headers in the implementation file only. We should do this globally... References: - http://www.boost.org/doc/libs/1_59_0/libs/optional/doc/html/index.html - https://github.com/osmcode/libosmium/issues/123 --- extractor/extractor.cpp | 6 +-- extractor/extractor_callbacks.cpp | 71 +++++++++++++++-------------- extractor/extractor_callbacks.hpp | 14 +++--- extractor/restriction_parser.cpp | 23 ++++++---- extractor/restriction_parser.hpp | 13 +++--- routing_algorithms/map_matching.hpp | 5 +- 6 files changed, 71 insertions(+), 61 deletions(-) diff --git a/extractor/extractor.cpp b/extractor/extractor.cpp index 2386f13ee..cc920bab4 100644 --- a/extractor/extractor.cpp +++ b/extractor/extractor.cpp @@ -45,6 +45,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include +#include #include @@ -53,8 +54,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include -#include - #include #include @@ -151,8 +150,7 @@ int extractor::run() // initialize vectors holding parsed objects tbb::concurrent_vector> resulting_nodes; tbb::concurrent_vector> resulting_ways; - tbb::concurrent_vector> - resulting_restrictions; + tbb::concurrent_vector> resulting_restrictions; // setup restriction parser const RestrictionParser restriction_parser(scripting_environment.get_lua_state()); diff --git a/extractor/extractor_callbacks.cpp b/extractor/extractor_callbacks.cpp index 6a50f16c1..14c195bb4 100644 --- a/extractor/extractor_callbacks.cpp +++ b/extractor/extractor_callbacks.cpp @@ -35,6 +35,10 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "../util/container.hpp" #include "../util/simple_logger.hpp" +#include + +#include + #include #include @@ -65,7 +69,7 @@ void ExtractorCallbacks::ProcessNode(const osmium::Node &input_node, } void ExtractorCallbacks::ProcessRestriction( - const mapbox::util::optional &restriction) + const boost::optional &restriction) { if (restriction) { @@ -140,8 +144,8 @@ void ExtractorCallbacks::ProcessWay(const osmium::Way &input_way, const Extracti } } - if (forward_weight_data.type == InternalExtractorEdge::WeightType::INVALID - && backward_weight_data.type == InternalExtractorEdge::WeightType::INVALID) + if (forward_weight_data.type == InternalExtractorEdge::WeightType::INVALID && + backward_weight_data.type == InternalExtractorEdge::WeightType::INVALID) { SimpleLogger().Write(logDEBUG) << "found way with bogus speed, id: " << input_way.id(); return; @@ -169,7 +173,10 @@ void ExtractorCallbacks::ProcessWay(const osmium::Way &input_way, const Extracti std::transform(input_way.nodes().begin(), input_way.nodes().end(), std::back_inserter(external_memory.used_node_id_list), - [](const osmium::NodeRef& ref) { return ref.ref(); }); + [](const osmium::NodeRef &ref) + { + return ref.ref(); + }); const bool is_opposite_way = TRAVEL_MODE_INACCESSIBLE == parsed_way.forward_travel_mode; @@ -182,53 +189,51 @@ void ExtractorCallbacks::ProcessWay(const osmium::Way &input_way, const Extracti [&](const osmium::NodeRef &first_node, const osmium::NodeRef &last_node) { external_memory.all_edges_list.push_back(InternalExtractorEdge( - first_node.ref(), last_node.ref(), name_id, backward_weight_data, - true, false, parsed_way.roundabout, parsed_way.is_access_restricted, + first_node.ref(), last_node.ref(), name_id, + backward_weight_data, true, false, parsed_way.roundabout, + parsed_way.is_access_restricted, parsed_way.backward_travel_mode, false)); }); external_memory.way_start_end_id_list.push_back( - { - static_cast(input_way.id()), - static_cast(input_way.nodes().back().ref()), - static_cast(input_way.nodes()[input_way.nodes().size() - 2].ref()), - static_cast(input_way.nodes()[1].ref()), - static_cast(input_way.nodes()[0].ref()) - } - ); + {static_cast(input_way.id()), + static_cast(input_way.nodes().back().ref()), + static_cast(input_way.nodes()[input_way.nodes().size() - 2].ref()), + static_cast(input_way.nodes()[1].ref()), + static_cast(input_way.nodes()[0].ref())}); } else { - const bool forward_only = split_edge || TRAVEL_MODE_INACCESSIBLE == parsed_way.backward_travel_mode; + const bool forward_only = + split_edge || TRAVEL_MODE_INACCESSIBLE == parsed_way.backward_travel_mode; osrm::for_each_pair(input_way.nodes().cbegin(), input_way.nodes().cend(), [&](const osmium::NodeRef &first_node, const osmium::NodeRef &last_node) { external_memory.all_edges_list.push_back(InternalExtractorEdge( first_node.ref(), last_node.ref(), name_id, forward_weight_data, - true, !forward_only, parsed_way.roundabout, parsed_way.is_access_restricted, - parsed_way.forward_travel_mode, split_edge)); + true, !forward_only, parsed_way.roundabout, + parsed_way.is_access_restricted, parsed_way.forward_travel_mode, + split_edge)); }); if (split_edge) { BOOST_ASSERT(parsed_way.backward_travel_mode != TRAVEL_MODE_INACCESSIBLE); - osrm::for_each_pair(input_way.nodes().cbegin(), input_way.nodes().cend(), - [&](const osmium::NodeRef &first_node, const osmium::NodeRef &last_node) - { - external_memory.all_edges_list.push_back(InternalExtractorEdge( - first_node.ref(), last_node.ref(), name_id, backward_weight_data, - false, true, parsed_way.roundabout, parsed_way.is_access_restricted, - parsed_way.backward_travel_mode, true)); - }); + osrm::for_each_pair( + input_way.nodes().cbegin(), input_way.nodes().cend(), + [&](const osmium::NodeRef &first_node, const osmium::NodeRef &last_node) + { + external_memory.all_edges_list.push_back(InternalExtractorEdge( + first_node.ref(), last_node.ref(), name_id, backward_weight_data, false, + true, parsed_way.roundabout, parsed_way.is_access_restricted, + parsed_way.backward_travel_mode, true)); + }); } external_memory.way_start_end_id_list.push_back( - { - static_cast(input_way.id()), - static_cast(input_way.nodes().back().ref()), - static_cast(input_way.nodes()[input_way.nodes().size() - 2].ref()), - static_cast(input_way.nodes()[1].ref()), - static_cast(input_way.nodes()[0].ref()) - } - ); + {static_cast(input_way.id()), + static_cast(input_way.nodes().back().ref()), + static_cast(input_way.nodes()[input_way.nodes().size() - 2].ref()), + static_cast(input_way.nodes()[1].ref()), + static_cast(input_way.nodes()[0].ref())}); } } diff --git a/extractor/extractor_callbacks.hpp b/extractor/extractor_callbacks.hpp index 25bbbaba1..0026b6fb0 100644 --- a/extractor/extractor_callbacks.hpp +++ b/extractor/extractor_callbacks.hpp @@ -28,12 +28,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef EXTRACTOR_CALLBACKS_HPP #define EXTRACTOR_CALLBACKS_HPP -#include "extraction_way.hpp" #include "../typedefs.h" - -#include - -#include +#include #include #include @@ -42,6 +38,12 @@ struct ExternalMemoryNode; class ExtractionContainers; struct InputRestrictionContainer; struct ExtractionNode; +struct ExtractionWay; +namespace osmium +{ +class Node; +class Way; +} /** * This class is uses by the extractor with the results of the @@ -66,7 +68,7 @@ class ExtractorCallbacks void ProcessNode(const osmium::Node ¤t_node, const ExtractionNode &result_node); // warning: caller needs to take care of synchronization! - void ProcessRestriction(const mapbox::util::optional &restriction); + void ProcessRestriction(const boost::optional &restriction); // warning: caller needs to take care of synchronization! void ProcessWay(const osmium::Way ¤t_way, const ExtractionWay &result_way); diff --git a/extractor/restriction_parser.cpp b/extractor/restriction_parser.cpp index 86174d410..afb194738 100644 --- a/extractor/restriction_parser.cpp +++ b/extractor/restriction_parser.cpp @@ -38,17 +38,20 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include + +#include +#include #include +#include namespace { int lua_error_callback(lua_State *lua_state) { std::string error_msg = lua_tostring(lua_state, -1); - std::ostringstream error_stream; - error_stream << error_msg; - throw osrm::exception("ERROR occured in profile script:\n" + error_stream.str()); + throw osrm::exception("ERROR occured in profile script:\n" + error_msg); } } @@ -104,18 +107,18 @@ void RestrictionParser::ReadRestrictionExceptions(lua_State *lua_state) /** * Tries to parse an relation as turn restriction. This can fail for a number of - * reasons, this the return type is a mapbox::util::optional<>. + * reasons, this the return type is a boost::optional. * * Some restrictions can also be ignored: See the ```get_exceptions``` function * in the corresponding profile. */ -mapbox::util::optional +boost::optional RestrictionParser::TryParse(const osmium::Relation &relation) const { // return if turn restrictions should be ignored if (!use_turn_restrictions) { - return mapbox::util::optional(); + return {}; } osmium::tags::KeyPrefixFilter filter(false); @@ -129,14 +132,14 @@ RestrictionParser::TryParse(const osmium::Relation &relation) const // if it's a restriction, continue; if (std::distance(fi_begin, fi_end) == 0) { - return mapbox::util::optional(); + return {}; } // check if the restriction should be ignored const char *except = relation.get_value_by_key("except"); if (except != nullptr && ShouldIgnoreRestriction(except)) { - return mapbox::util::optional(); + return {}; } bool is_only_restriction = false; @@ -164,7 +167,7 @@ RestrictionParser::TryParse(const osmium::Relation &relation) const if (!is_actually_restricted) { - return mapbox::util::optional(); + return {}; } } } @@ -218,7 +221,7 @@ RestrictionParser::TryParse(const osmium::Relation &relation) const break; } } - return mapbox::util::optional(restriction_container); + return boost::make_optional(std::move(restriction_container)); } bool RestrictionParser::ShouldIgnoreRestriction(const std::string &except_tag_string) const diff --git a/extractor/restriction_parser.hpp b/extractor/restriction_parser.hpp index f99335d2f..097e15d9a 100644 --- a/extractor/restriction_parser.hpp +++ b/extractor/restriction_parser.hpp @@ -30,16 +30,16 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "../data_structures/restriction.hpp" -#include -#include - -#include +#include #include #include struct lua_State; -class ScriptingEnvironment; +namespace osmium +{ +class Relation; +} /** * Parses the relations that represents turn restrictions. @@ -63,8 +63,7 @@ class RestrictionParser { public: RestrictionParser(lua_State *lua_state); - mapbox::util::optional - TryParse(const osmium::Relation &relation) const; + boost::optional TryParse(const osmium::Relation &relation) const; private: void ReadUseRestrictionsSetting(lua_State *lua_state); diff --git a/routing_algorithms/map_matching.hpp b/routing_algorithms/map_matching.hpp index 58b0ad1a8..543900d05 100644 --- a/routing_algorithms/map_matching.hpp +++ b/routing_algorithms/map_matching.hpp @@ -35,11 +35,14 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "../util/json_logger.hpp" #include "../util/matching_debug_info.hpp" -#include +#include #include +#include #include #include +#include +#include namespace osrm {