diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d914f074..a96363c02 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -714,7 +714,7 @@ file(GLOB VariantGlob third_party/variant/include/mapbox/*.hpp) file(GLOB LibraryGlob include/osrm/*.hpp) file(GLOB ParametersGlob include/engine/api/*_parameters.hpp) set(EngineHeader include/engine/status.hpp include/engine/engine_config.hpp include/engine/hint.hpp include/engine/bearing.hpp include/engine/phantom_node.hpp) -set(UtilHeader include/util/coordinate.hpp include/util/json_container.hpp include/util/typedefs.hpp include/util/strong_typedef.hpp include/util/exception.hpp) +set(UtilHeader include/util/coordinate.hpp include/util/json_container.hpp include/util/typedefs.hpp include/util/alias.hpp include/util/exception.hpp) set(ExtractorHeader include/extractor/extractor.hpp include/extractor/extractor_config.hpp include/extractor/travel_mode.hpp) set(PartitionerHeader include/partition/partitioner.hpp include/partition/partition_config.hpp) set(ContractorHeader include/contractor/contractor.hpp include/contractor/contractor_config.hpp) diff --git a/include/engine/routing_algorithms/routing_base.hpp b/include/engine/routing_algorithms/routing_base.hpp index a8c829fa5..681d13144 100644 --- a/include/engine/routing_algorithms/routing_base.hpp +++ b/include/engine/routing_algorithms/routing_base.hpp @@ -320,17 +320,19 @@ double getPathDistance(const datafacade::ContiguousInternalMemoryDataFacade(toFloating(source_phantom.location.lat)) * DEGREE_TO_RAD; - double prev_lon = static_cast(toFloating(source_phantom.location.lon)) * DEGREE_TO_RAD; + double prev_lat = + static_cast(util::toFloating(source_phantom.location.lat)) * DEGREE_TO_RAD; + double prev_lon = + static_cast(util::toFloating(source_phantom.location.lon)) * DEGREE_TO_RAD; double prev_cos = std::cos(prev_lat); for (const auto &p : unpacked_path) { const auto current_coordinate = facade.GetCoordinateOfNode(p.turn_via_node); const double current_lat = - static_cast(toFloating(current_coordinate.lat)) * DEGREE_TO_RAD; + static_cast(util::toFloating(current_coordinate.lat)) * DEGREE_TO_RAD; const double current_lon = - static_cast(toFloating(current_coordinate.lon)) * DEGREE_TO_RAD; + static_cast(util::toFloating(current_coordinate.lon)) * DEGREE_TO_RAD; const double current_cos = std::cos(current_lat); const double sin_dlon = std::sin((prev_lon - current_lon) / 2.0); @@ -346,9 +348,9 @@ double getPathDistance(const datafacade::ContiguousInternalMemoryDataFacade(toFloating(target_phantom.location.lat)) * DEGREE_TO_RAD; + static_cast(util::toFloating(target_phantom.location.lat)) * DEGREE_TO_RAD; const double current_lon = - static_cast(toFloating(target_phantom.location.lon)) * DEGREE_TO_RAD; + static_cast(util::toFloating(target_phantom.location.lon)) * DEGREE_TO_RAD; const double current_cos = std::cos(current_lat); const double sin_dlon = std::sin((prev_lon - current_lon) / 2.0); diff --git a/include/extractor/internal_extractor_edge.hpp b/include/extractor/internal_extractor_edge.hpp index ab267e70e..6257b4f6a 100644 --- a/include/extractor/internal_extractor_edge.hpp +++ b/include/extractor/internal_extractor_edge.hpp @@ -6,7 +6,6 @@ #include "extractor/node_based_edge.hpp" #include "extractor/travel_mode.hpp" #include "osrm/coordinate.hpp" -#include "util/strong_typedef.hpp" #include "util/typedefs.hpp" #include diff --git a/include/osrm/coordinate.hpp b/include/osrm/coordinate.hpp index 39b684d01..33ecab533 100644 --- a/include/osrm/coordinate.hpp +++ b/include/osrm/coordinate.hpp @@ -33,6 +33,12 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. namespace osrm { using util::Coordinate; +using util::toFixed; +using util::toFloating; +using util::FixedLatitude; +using util::FixedLongitude; +using util::FloatLatitude; +using util::FloatLongitude; } #endif diff --git a/include/util/alias.hpp b/include/util/alias.hpp new file mode 100644 index 000000000..04e887a44 --- /dev/null +++ b/include/util/alias.hpp @@ -0,0 +1,112 @@ +/* + +Copyright (c) 2016, Project OSRM contributors +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list +of conditions and the following disclaimer. +Redistributions in binary form must reproduce the above copyright notice, this +list of conditions and the following disclaimer in the documentation and/or +other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +*/ + +#ifndef ALIAS_HPP +#define ALIAS_HPP + +#include +#include +#include + +namespace osrm +{ + +template struct Alias; +template +inline std::ostream &operator<<(std::ostream &stream, const Alias &inst); + +template struct Alias final +{ + using value_type = From; + static_assert(std::is_arithmetic(), ""); + + From __value; + friend std::ostream &operator<<(std::ostream &stream, const Alias &inst); + + explicit operator From &() { return __value; } + explicit operator From() const { return __value; } + Alias operator+(const Alias rhs_) const + { + return Alias{__value + static_cast(rhs_)}; + } + Alias operator-(const Alias rhs_) const + { + return Alias{__value - static_cast(rhs_)}; + } + Alias operator*(const Alias rhs_) const + { + return Alias{__value * static_cast(rhs_)}; + } + Alias operator/(const Alias rhs_) const + { + return Alias{__value / static_cast(rhs_)}; + } + Alias operator|(const Alias rhs_) const + { + return Alias{__value | static_cast(rhs_)}; + } + Alias operator&(const Alias rhs_) const + { + return Alias{__value & static_cast(rhs_)}; + } + bool operator<(const Alias z_) const { return __value < static_cast(z_); } + bool operator>(const Alias z_) const { return __value > static_cast(z_); } + bool operator<=(const Alias z_) const { return __value <= static_cast(z_); } + bool operator>=(const Alias z_) const { return __value >= static_cast(z_); } + bool operator==(const Alias z_) const { return __value == static_cast(z_); } + bool operator!=(const Alias z_) const { return __value != static_cast(z_); } +}; + +template +inline std::ostream &operator<<(std::ostream &stream, const Alias &inst) +{ + return stream << inst.__value; +} + +template +struct is_valid_alias + : std::integral_constant::value && std::is_standard_layout::value && + std::is_pod::value> +{ +}; +} + +namespace std +{ +template struct hash> +{ + typedef osrm::Alias argument_type; + typedef std::size_t result_type; + result_type operator()(argument_type const &s) const + { + return std::hash()(static_cast(s)); + } +}; +} + +#endif // OSRM_ALIAS_HPP diff --git a/include/util/coordinate.hpp b/include/util/coordinate.hpp index 13aca3d94..40aea18ff 100644 --- a/include/util/coordinate.hpp +++ b/include/util/coordinate.hpp @@ -28,7 +28,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef COORDINATE_HPP_ #define COORDINATE_HPP_ -#include "util/strong_typedef.hpp" +#include "util/alias.hpp" #include @@ -45,10 +45,24 @@ constexpr const double COORDINATE_PRECISION = 1e6; namespace util { -OSRM_STRONG_TYPEDEF(int32_t, FixedLatitude) -OSRM_STRONG_TYPEDEF(int32_t, FixedLongitude) -OSRM_STRONG_TYPEDEF(double, FloatLatitude) -OSRM_STRONG_TYPEDEF(double, FloatLongitude) +namespace tag +{ +struct latitude +{ +}; +struct longitude +{ +}; +} + +using FixedLatitude = Alias; +using FixedLongitude = Alias; +using FloatLatitude = Alias; +using FloatLongitude = Alias; +static_assert(osrm::is_valid_alias(), "FixedLatitude is not a valid alias"); +static_assert(osrm::is_valid_alias(), "FixedLongitude is not a valid alias"); +static_assert(osrm::is_valid_alias(), "FloatLatitude is not a valid alias"); +static_assert(osrm::is_valid_alias(), "FloatLongitude is not a valid alias"); /** * Converts a typed latitude from floating to fixed representation. diff --git a/include/util/strong_typedef.hpp b/include/util/strong_typedef.hpp deleted file mode 100644 index 02b1d622b..000000000 --- a/include/util/strong_typedef.hpp +++ /dev/null @@ -1,84 +0,0 @@ -/* - -Copyright (c) 2016, Project OSRM contributors -All rights reserved. - -Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - -Redistributions of source code must retain the above copyright notice, this list -of conditions and the following disclaimer. -Redistributions in binary form must reproduce the above copyright notice, this -list of conditions and the following disclaimer in the documentation and/or -other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR -ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -*/ - -#ifndef STRONG_TYPEDEF_HPP -#define STRONG_TYPEDEF_HPP - -#include -#include -#include - -namespace osrm -{ - -/* Creates strongly typed wrappers around scalar types. - * Useful for stopping accidental assignment of lats to lons, - * etc. Also clarifies what this random "int" value is - * being used for. - */ -#define OSRM_STRONG_TYPEDEF(From, To) \ - struct To final \ - { \ - static_assert(std::is_arithmetic(), ""); \ - From __value; \ - friend std::ostream &operator<<(std::ostream &stream, const To &inst); \ - \ - explicit operator From &() { return __value; } \ - explicit operator From() const { return __value; } \ - To operator+(const To rhs_) const { return To{__value + static_cast(rhs_)}; } \ - To operator-(const To rhs_) const { return To{__value - static_cast(rhs_)}; } \ - To operator*(const To rhs_) const { return To{__value * static_cast(rhs_)}; } \ - To operator/(const To rhs_) const { return To{__value / static_cast(rhs_)}; } \ - bool operator<(const To z_) const { return __value < static_cast(z_); } \ - bool operator>(const To z_) const { return __value > static_cast(z_); } \ - bool operator<=(const To z_) const { return __value <= static_cast(z_); } \ - bool operator>=(const To z_) const { return __value >= static_cast(z_); } \ - bool operator==(const To z_) const { return __value == static_cast(z_); } \ - bool operator!=(const To z_) const { return __value != static_cast(z_); } \ - }; \ - static_assert(std::is_trivial(), #To " is not a trivial type"); \ - static_assert(std::is_standard_layout(), #To " is not a standart layout"); \ - static_assert(std::is_pod(), #To " is not a POD layout"); \ - inline std::ostream &operator<<(std::ostream &stream, const To &inst) \ - { \ - return stream << inst.__value; \ - } - -#define OSRM_STRONG_TYPEDEF_HASHABLE(From, To) \ - namespace std \ - { \ - template <> struct hash \ - { \ - std::size_t operator()(const To &k) const \ - { \ - return std::hash()(static_cast(k)); \ - } \ - }; \ - } -} - -#endif // OSRM_STRONG_TYPEDEF_HPP diff --git a/include/util/typedefs.hpp b/include/util/typedefs.hpp index d1156f08b..a4cea4a0a 100644 --- a/include/util/typedefs.hpp +++ b/include/util/typedefs.hpp @@ -28,7 +28,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef TYPEDEFS_H #define TYPEDEFS_H -#include "util/strong_typedef.hpp" +#include "util/alias.hpp" #include @@ -37,11 +37,19 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include // OpenStreetMap node ids are higher than 2^32 -OSRM_STRONG_TYPEDEF(std::uint64_t, OSMNodeID) -OSRM_STRONG_TYPEDEF_HASHABLE(std::uint64_t, OSMNodeID) - -OSRM_STRONG_TYPEDEF(std::uint32_t, OSMWayID) -OSRM_STRONG_TYPEDEF_HASHABLE(std::uint32_t, OSMWayID) +namespace tag +{ +struct osm_node_id +{ +}; +struct osm_way_id +{ +}; +} +using OSMNodeID = osrm::Alias; +static_assert(osrm::is_valid_alias(), "OSMNodeID is not a valid alias"); +using OSMWayID = osrm::Alias; +static_assert(osrm::is_valid_alias(), "OSMWayID is not a valid alias"); static const OSMNodeID SPECIAL_OSM_NODEID = OSMNodeID{std::numeric_limits::max()}; static const OSMWayID SPECIAL_OSM_WAYID = OSMWayID{std::numeric_limits::max()}; diff --git a/src/engine/api/json_factory.cpp b/src/engine/api/json_factory.cpp index 442f8c522..661c50e95 100644 --- a/src/engine/api/json_factory.cpp +++ b/src/engine/api/json_factory.cpp @@ -115,8 +115,8 @@ std::string waypointTypeToString(const guidance::WaypointType waypoint_type) util::json::Array coordinateToLonLat(const util::Coordinate coordinate) { util::json::Array array; - array.values.push_back(static_cast(toFloating(coordinate.lon))); - array.values.push_back(static_cast(toFloating(coordinate.lat))); + array.values.push_back(static_cast(util::toFloating(coordinate.lon))); + array.values.push_back(static_cast(util::toFloating(coordinate.lat))); return array; }