#include "osrm/osrm.hpp" #include "engine/algorithm.hpp" #include "engine/api/match_parameters.hpp" #include "engine/api/nearest_parameters.hpp" #include "engine/api/route_parameters.hpp" #include "engine/api/table_parameters.hpp" #include "engine/api/trip_parameters.hpp" #include "engine/engine.hpp" #include "engine/engine_config.hpp" #include "engine/status.hpp" #include namespace osrm { // Pimpl idiom OSRM::OSRM(engine::EngineConfig &config) { using CH = engine::routing_algorithms::ch::Algorithm; using MLD = engine::routing_algorithms::mld::Algorithm; // First, check that necessary core data is available if (!config.use_shared_memory && !config.storage_config.IsValid()) { throw util::exception("Required files are missing, cannot continue. Have all the " "pre-processing steps been run?"); } else if (config.use_shared_memory) { storage::SharedMonitor barrier; using mutex_type = typename decltype(barrier)::mutex_type; boost::interprocess::scoped_lock current_region_lock(barrier.get_mutex()); auto mem = storage::makeSharedMemory(barrier.data().region); auto layout = reinterpret_cast(mem->Ptr()); if (layout->GetBlockSize("/common/names/values") == 0) throw util::exception( "No name data loaded, cannot continue. Have you run osrm-datastore to load data?"); } // Now, check that the algorithm requested can be used with the data // that's available. if (config.algorithm == EngineConfig::Algorithm::CH || config.algorithm == EngineConfig::Algorithm::CoreCH) { if (config.algorithm == EngineConfig::Algorithm::CoreCH) { util::Log(logWARNING) << "Using CoreCH is deprecated. Falling back to CH"; config.algorithm = EngineConfig::Algorithm::CH; } bool ch_compatible = engine::Engine::CheckCompatibility(config); // throw error if dataset is not usable with CH if (config.algorithm == EngineConfig::Algorithm::CH && !ch_compatible) { throw util::exception("Dataset is not compatible with CH"); } } else if (config.algorithm == EngineConfig::Algorithm::MLD) { bool mld_compatible = engine::Engine::CheckCompatibility(config); // throw error if dataset is not usable with MLD if (!mld_compatible) { throw util::exception("Dataset is not compatible with MLD."); } } switch (config.algorithm) { case EngineConfig::Algorithm::CH: engine_ = std::make_unique>(config); break; case EngineConfig::Algorithm::MLD: engine_ = std::make_unique>(config); break; default: util::exception("Algorithm not implemented!"); } } OSRM::~OSRM() = default; OSRM::OSRM(OSRM &&) noexcept = default; OSRM &OSRM::operator=(OSRM &&) noexcept = default; // Forward to implementation engine::Status OSRM::Route(const engine::api::RouteParameters ¶ms, util::json::Object &result) const { return engine_->Route(params, result); } engine::Status OSRM::Table(const engine::api::TableParameters ¶ms, json::Object &result) const { return engine_->Table(params, result); } engine::Status OSRM::Nearest(const engine::api::NearestParameters ¶ms, json::Object &result) const { return engine_->Nearest(params, result); } engine::Status OSRM::Trip(const engine::api::TripParameters ¶ms, json::Object &result) const { return engine_->Trip(params, result); } engine::Status OSRM::Match(const engine::api::MatchParameters ¶ms, json::Object &result) const { return engine_->Match(params, result); } engine::Status OSRM::Tile(const engine::api::TileParameters ¶ms, std::string &result) const { return engine_->Tile(params, result); } } // ns osrm