Skip to content
11 changes: 11 additions & 0 deletions include/engine/algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ template <typename AlgorithmT> struct HasExcludeFlags final : std::false_type
{
};

// Trait to mark supported routing algorithms
template <typename AlgorithmT> struct IsRoutingAlgorithm final : std::false_type
{
};
template <> struct IsRoutingAlgorithm<ch::Algorithm> final : std::true_type
{
};
template <> struct IsRoutingAlgorithm<mld::Algorithm> final : std::true_type
{
};

// Algorithms supported by Contraction Hierarchies
template <> struct HasAlternativePathSearch<ch::Algorithm> final : std::true_type
{
Expand Down
60 changes: 60 additions & 0 deletions include/engine/concepts.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#ifndef OSRM_ENGINE_CONCEPTS_HPP
#define OSRM_ENGINE_CONCEPTS_HPP

#include "engine/algorithm.hpp"
#include <concepts>

namespace osrm::engine::routing_algorithms
{

/*
* RoutingAlgorithm concept
* ------------------------
* This concept documents and checks the compile-time interface expected
* from the routing algorithm marker types used throughout the engine
* (e.g., routing_algorithms::ch::Algorithm and routing_algorithms::mld::Algorithm).
*
* Required compile-time/public interface (checked by this concept):
* - routing_algorithms::name<Algorithm>() -> const char*
* - routing_algorithms::identifier<Algorithm>() -> const char*
* - trait templates (instantiable):
* HasAlternativePathSearch<Algorithm>
* HasShortestPathSearch<Algorithm>
* HasDirectShortestPathSearch<Algorithm>
* HasMapMatching<Algorithm>
* HasManyToManySearch<Algorithm>
* SupportsDistanceAnnotationType<Algorithm>
* HasGetTileTurns<Algorithm>
* HasExcludeFlags<Algorithm>
* - the above trait specializations must expose a compile-time ::value convertible to bool
*/
template <typename T>
concept RoutingAlgorithm = requires {
/* name() and identifier() are callable and return C strings */
{ name<T>() } -> std::convertible_to<const char *>;
{ identifier<T>() } -> std::convertible_to<const char *>;

Comment thread
DennisOSRM marked this conversation as resolved.
/* trait templates are instantiable */
typename HasAlternativePathSearch<T>;
typename HasShortestPathSearch<T>;
typename HasDirectShortestPathSearch<T>;
typename HasMapMatching<T>;
typename HasManyToManySearch<T>;
typename SupportsDistanceAnnotationType<T>;
typename HasGetTileTurns<T>;
typename HasExcludeFlags<T>;

/* trait values are usable as compile-time booleans */
{ HasAlternativePathSearch<T>::value } -> std::convertible_to<bool>;
{ HasShortestPathSearch<T>::value } -> std::convertible_to<bool>;
{ HasDirectShortestPathSearch<T>::value } -> std::convertible_to<bool>;
{ HasMapMatching<T>::value } -> std::convertible_to<bool>;
{ HasManyToManySearch<T>::value } -> std::convertible_to<bool>;
{ SupportsDistanceAnnotationType<T>::value } -> std::convertible_to<bool>;
{ HasGetTileTurns<T>::value } -> std::convertible_to<bool>;
{ HasExcludeFlags<T>::value } -> std::convertible_to<bool>;
} && IsRoutingAlgorithm<T>::value;

} // namespace osrm::engine::routing_algorithms

#endif // OSRM_ENGINE_CONCEPTS_HPP
4 changes: 3 additions & 1 deletion include/engine/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "engine/api/table_parameters.hpp"
#include "engine/api/tile_parameters.hpp"
#include "engine/api/trip_parameters.hpp"
#include "engine/concepts.hpp"
#include "engine/datafacade_provider.hpp"
#include "engine/engine_config.hpp"
#include "engine/plugins/match.hpp"
Expand Down Expand Up @@ -38,7 +39,8 @@ class EngineInterface
virtual Status Tile(const api::TileParameters &parameters, api::ResultT &result) const = 0;
};

template <typename Algorithm> class Engine final : public EngineInterface
template <routing_algorithms::RoutingAlgorithm Algorithm>
class Engine final : public EngineInterface
Comment thread
DennisOSRM marked this conversation as resolved.
{
public:
explicit Engine(const EngineConfig &config)
Expand Down
16 changes: 9 additions & 7 deletions include/engine/routing_algorithms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define OSRM_ENGINE_ROUTING_ALGORITHM_HPP

#include "engine/algorithm.hpp"
#include "engine/concepts.hpp"
#include "engine/internal_route_result.hpp"
#include "engine/phantom_node.hpp"
#include "engine/routing_algorithms/alternative_path.hpp"
Expand Down Expand Up @@ -61,7 +62,8 @@ class RoutingAlgorithmsInterface
};

// Short-lived object passed to each plugin in request to wrap routing algorithms
template <typename Algorithm> class RoutingAlgorithms final : public RoutingAlgorithmsInterface
template <routing_algorithms::RoutingAlgorithm Algorithm>
class RoutingAlgorithms final : public RoutingAlgorithmsInterface
{
public:
RoutingAlgorithms(SearchEngineData<Algorithm> &heaps,
Expand Down Expand Up @@ -149,15 +151,15 @@ template <typename Algorithm> class RoutingAlgorithms final : public RoutingAlgo
std::shared_ptr<const DataFacade<Algorithm>> facade;
};

template <typename Algorithm>
template <routing_algorithms::RoutingAlgorithm Algorithm>
InternalManyRoutesResult RoutingAlgorithms<Algorithm>::AlternativePathSearch(
const PhantomEndpointCandidates &endpoint_candidates, unsigned number_of_alternatives) const
{
return routing_algorithms::alternativePathSearch(
heaps, *facade, endpoint_candidates, number_of_alternatives);
}

template <typename Algorithm>
template <routing_algorithms::RoutingAlgorithm Algorithm>
InternalRouteResult RoutingAlgorithms<Algorithm>::ShortestPathSearch(
const std::vector<PhantomNodeCandidates> &waypoint_candidates,
const std::optional<bool> continue_straight_at_waypoint) const
Expand All @@ -166,14 +168,14 @@ InternalRouteResult RoutingAlgorithms<Algorithm>::ShortestPathSearch(
heaps, *facade, waypoint_candidates, continue_straight_at_waypoint);
}

template <typename Algorithm>
template <routing_algorithms::RoutingAlgorithm Algorithm>
InternalRouteResult RoutingAlgorithms<Algorithm>::DirectShortestPathSearch(
const PhantomEndpointCandidates &endpoint_candidates) const
{
return routing_algorithms::directShortestPathSearch(heaps, *facade, endpoint_candidates);
}

template <typename Algorithm>
template <routing_algorithms::RoutingAlgorithm Algorithm>
inline routing_algorithms::SubMatchingList RoutingAlgorithms<Algorithm>::MapMatching(
const routing_algorithms::CandidateLists &candidates_list,
const std::vector<util::Coordinate> &trace_coordinates,
Expand All @@ -190,7 +192,7 @@ inline routing_algorithms::SubMatchingList RoutingAlgorithms<Algorithm>::MapMatc
allow_splitting);
}

template <typename Algorithm>
template <routing_algorithms::RoutingAlgorithm Algorithm>
std::pair<std::vector<EdgeDuration>, std::vector<EdgeDistance>>
RoutingAlgorithms<Algorithm>::ManyToManySearch(
const std::vector<PhantomNodeCandidates> &candidates_list,
Expand Down Expand Up @@ -222,7 +224,7 @@ RoutingAlgorithms<Algorithm>::ManyToManySearch(
calculate_distance);
}

template <typename Algorithm>
template <routing_algorithms::RoutingAlgorithm Algorithm>
inline std::vector<routing_algorithms::TurnData> RoutingAlgorithms<Algorithm>::GetTileTurns(
const std::vector<datafacade::BaseDataFacade::RTreeLeaf> &edges,
const std::vector<std::size_t> &sorted_edge_indexes) const
Expand Down
3 changes: 2 additions & 1 deletion include/engine/search_engine_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define SEARCH_ENGINE_DATA_HPP

#include "engine/algorithm.hpp"
#include "engine/concepts.hpp"
#include "util/browse_resistant_cache.hpp"
#include "util/query_heap.hpp"
#include "util/typedefs.hpp"
Expand All @@ -18,7 +19,7 @@ namespace osrm::engine
// - CH algorithms use CH heaps
// - MLD algorithms use MLD heaps

template <typename Algorithm> struct SearchEngineData
template <routing_algorithms::RoutingAlgorithm Algorithm> struct SearchEngineData
{
};

Expand Down
9 changes: 9 additions & 0 deletions unit_tests/engine/offline_facade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ struct Algorithm final
};
} // namespace routing_algorithms::offline

// Mark offline::Algorithm as a supported routing algorithm so it satisfies the RoutingAlgorithm
// concept
namespace routing_algorithms
{
template <> struct IsRoutingAlgorithm<offline::Algorithm> final : std::true_type
{
};
} // namespace routing_algorithms

// Define engine data for offline data facade
template <> struct SearchEngineData<routing_algorithms::offline::Algorithm>
{
Expand Down
Loading