Use Boost.Optional instead of custom optional monad implementation.

This switches out the `<variant/optional.hpp>` implementation of the
optional monad to the one from Boost.

The following trick makes sure we keep compile times down:

- use `<boost/optional/optional_fwd.hpp>` 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
This commit is contained in:
Daniel J. Hofmann 2015-09-18 14:26:32 +02:00
parent be506f7121
commit 82dd5d8ccf
6 changed files with 71 additions and 61 deletions

View File

@ -45,6 +45,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/optional/optional.hpp>
#include <luabind/luabind.hpp>
@ -53,8 +54,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <tbb/parallel_for.h>
#include <tbb/task_scheduler_init.h>
#include <variant/optional.hpp>
#include <cstdlib>
#include <algorithm>
@ -151,8 +150,7 @@ int extractor::run()
// initialize vectors holding parsed objects
tbb::concurrent_vector<std::pair<std::size_t, ExtractionNode>> resulting_nodes;
tbb::concurrent_vector<std::pair<std::size_t, ExtractionWay>> resulting_ways;
tbb::concurrent_vector<mapbox::util::optional<InputRestrictionContainer>>
resulting_restrictions;
tbb::concurrent_vector<boost::optional<InputRestrictionContainer>> resulting_restrictions;
// setup restriction parser
const RestrictionParser restriction_parser(scripting_environment.get_lua_state());

View File

@ -35,6 +35,10 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "../util/container.hpp"
#include "../util/simple_logger.hpp"
#include <boost/optional/optional.hpp>
#include <osmium/osm.hpp>
#include <osrm/coordinate.hpp>
#include <limits>
@ -65,7 +69,7 @@ void ExtractorCallbacks::ProcessNode(const osmium::Node &input_node,
}
void ExtractorCallbacks::ProcessRestriction(
const mapbox::util::optional<InputRestrictionContainer> &restriction)
const boost::optional<InputRestrictionContainer> &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<EdgeID>(input_way.id()),
{static_cast<EdgeID>(input_way.id()),
static_cast<NodeID>(input_way.nodes().back().ref()),
static_cast<NodeID>(input_way.nodes()[input_way.nodes().size() - 2].ref()),
static_cast<NodeID>(input_way.nodes()[1].ref()),
static_cast<NodeID>(input_way.nodes()[0].ref())
}
);
static_cast<NodeID>(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(),
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,
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<EdgeID>(input_way.id()),
{static_cast<EdgeID>(input_way.id()),
static_cast<NodeID>(input_way.nodes().back().ref()),
static_cast<NodeID>(input_way.nodes()[input_way.nodes().size() - 2].ref()),
static_cast<NodeID>(input_way.nodes()[1].ref()),
static_cast<NodeID>(input_way.nodes()[0].ref())
}
);
static_cast<NodeID>(input_way.nodes()[0].ref())});
}
}

View File

@ -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 <osmium/osm.hpp>
#include <variant/optional.hpp>
#include <boost/optional/optional_fwd.hpp>
#include <string>
#include <unordered_map>
@ -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 &current_node, const ExtractionNode &result_node);
// warning: caller needs to take care of synchronization!
void ProcessRestriction(const mapbox::util::optional<InputRestrictionContainer> &restriction);
void ProcessRestriction(const boost::optional<InputRestrictionContainer> &restriction);
// warning: caller needs to take care of synchronization!
void ProcessWay(const osmium::Way &current_way, const ExtractionWay &result_way);

View File

@ -38,17 +38,20 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <boost/algorithm/string/predicate.hpp>
#include <boost/ref.hpp>
#include <boost/regex.hpp>
#include <boost/optional/optional.hpp>
#include <osmium/osm.hpp>
#include <osmium/tags/regex_filter.hpp>
#include <algorithm>
#include <iterator>
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<T>.
*
* Some restrictions can also be ignored: See the ```get_exceptions``` function
* in the corresponding profile.
*/
mapbox::util::optional<InputRestrictionContainer>
boost::optional<InputRestrictionContainer>
RestrictionParser::TryParse(const osmium::Relation &relation) const
{
// return if turn restrictions should be ignored
if (!use_turn_restrictions)
{
return mapbox::util::optional<InputRestrictionContainer>();
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<InputRestrictionContainer>();
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<InputRestrictionContainer>();
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<InputRestrictionContainer>();
return {};
}
}
}
@ -218,7 +221,7 @@ RestrictionParser::TryParse(const osmium::Relation &relation) const
break;
}
}
return mapbox::util::optional<InputRestrictionContainer>(restriction_container);
return boost::make_optional(std::move(restriction_container));
}
bool RestrictionParser::ShouldIgnoreRestriction(const std::string &except_tag_string) const

View File

@ -30,16 +30,16 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "../data_structures/restriction.hpp"
#include <osmium/osm.hpp>
#include <osmium/tags/regex_filter.hpp>
#include <variant/optional.hpp>
#include <boost/optional/optional_fwd.hpp>
#include <string>
#include <vector>
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<InputRestrictionContainer>
TryParse(const osmium::Relation &relation) const;
boost::optional<InputRestrictionContainer> TryParse(const osmium::Relation &relation) const;
private:
void ReadUseRestrictionsSetting(lua_State *lua_state);

View File

@ -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 <variant/variant.hpp>
#include <cstddef>
#include <algorithm>
#include <deque>
#include <iomanip>
#include <numeric>
#include <utility>
#include <vector>
namespace osrm
{