Switch from macro based StrongTypedef to template version

This commit is contained in:
Patrick Niklaus 2017-04-21 11:01:35 +00:00 committed by Patrick Niklaus
parent a68435d856
commit c446b017ef
9 changed files with 162 additions and 105 deletions

View File

@ -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)

View File

@ -320,17 +320,19 @@ double getPathDistance(const datafacade::ContiguousInternalMemoryDataFacade<Algo
using util::coordinate_calculation::detail::EARTH_RADIUS;
double distance = 0;
double prev_lat = static_cast<double>(toFloating(source_phantom.location.lat)) * DEGREE_TO_RAD;
double prev_lon = static_cast<double>(toFloating(source_phantom.location.lon)) * DEGREE_TO_RAD;
double prev_lat =
static_cast<double>(util::toFloating(source_phantom.location.lat)) * DEGREE_TO_RAD;
double prev_lon =
static_cast<double>(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<double>(toFloating(current_coordinate.lat)) * DEGREE_TO_RAD;
static_cast<double>(util::toFloating(current_coordinate.lat)) * DEGREE_TO_RAD;
const double current_lon =
static_cast<double>(toFloating(current_coordinate.lon)) * DEGREE_TO_RAD;
static_cast<double>(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<Algo
}
const double current_lat =
static_cast<double>(toFloating(target_phantom.location.lat)) * DEGREE_TO_RAD;
static_cast<double>(util::toFloating(target_phantom.location.lat)) * DEGREE_TO_RAD;
const double current_lon =
static_cast<double>(toFloating(target_phantom.location.lon)) * DEGREE_TO_RAD;
static_cast<double>(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);

View File

@ -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 <boost/assert.hpp>

View File

@ -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

112
include/util/alias.hpp Normal file
View File

@ -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 <functional>
#include <iostream>
#include <type_traits>
namespace osrm
{
template <typename From, typename Tag> struct Alias;
template <typename From, typename Tag>
inline std::ostream &operator<<(std::ostream &stream, const Alias<From, Tag> &inst);
template <typename From, typename Tag> struct Alias final
{
using value_type = From;
static_assert(std::is_arithmetic<From>(), "");
From __value;
friend std::ostream &operator<<<From, Tag>(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<const From>(rhs_)};
}
Alias operator-(const Alias rhs_) const
{
return Alias{__value - static_cast<const From>(rhs_)};
}
Alias operator*(const Alias rhs_) const
{
return Alias{__value * static_cast<const From>(rhs_)};
}
Alias operator/(const Alias rhs_) const
{
return Alias{__value / static_cast<const From>(rhs_)};
}
Alias operator|(const Alias rhs_) const
{
return Alias{__value | static_cast<const From>(rhs_)};
}
Alias operator&(const Alias rhs_) const
{
return Alias{__value & static_cast<const From>(rhs_)};
}
bool operator<(const Alias z_) const { return __value < static_cast<const From>(z_); }
bool operator>(const Alias z_) const { return __value > static_cast<const From>(z_); }
bool operator<=(const Alias z_) const { return __value <= static_cast<const From>(z_); }
bool operator>=(const Alias z_) const { return __value >= static_cast<const From>(z_); }
bool operator==(const Alias z_) const { return __value == static_cast<const From>(z_); }
bool operator!=(const Alias z_) const { return __value != static_cast<const From>(z_); }
};
template <typename From, typename Tag>
inline std::ostream &operator<<(std::ostream &stream, const Alias<From, Tag> &inst)
{
return stream << inst.__value;
}
template <typename T>
struct is_valid_alias
: std::integral_constant<bool,
std::is_trivial<T>::value && std::is_standard_layout<T>::value &&
std::is_pod<T>::value>
{
};
}
namespace std
{
template <typename From, typename Tag> struct hash<osrm::Alias<From, Tag>>
{
typedef osrm::Alias<From, Tag> argument_type;
typedef std::size_t result_type;
result_type operator()(argument_type const &s) const
{
return std::hash<From>()(static_cast<const From>(s));
}
};
}
#endif // OSRM_ALIAS_HPP

View File

@ -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 <boost/numeric/conversion/cast.hpp>
@ -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<std::int32_t, tag::latitude>;
using FixedLongitude = Alias<std::int32_t, tag::longitude>;
using FloatLatitude = Alias<double, tag::latitude>;
using FloatLongitude = Alias<double, tag::longitude>;
static_assert(osrm::is_valid_alias<FixedLatitude>(), "FixedLatitude is not a valid alias");
static_assert(osrm::is_valid_alias<FixedLongitude>(), "FixedLongitude is not a valid alias");
static_assert(osrm::is_valid_alias<FloatLatitude>(), "FloatLatitude is not a valid alias");
static_assert(osrm::is_valid_alias<FloatLongitude>(), "FloatLongitude is not a valid alias");
/**
* Converts a typed latitude from floating to fixed representation.

View File

@ -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 <functional>
#include <iostream>
#include <type_traits>
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>(), ""); \
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<const From>(rhs_)}; } \
To operator-(const To rhs_) const { return To{__value - static_cast<const From>(rhs_)}; } \
To operator*(const To rhs_) const { return To{__value * static_cast<const From>(rhs_)}; } \
To operator/(const To rhs_) const { return To{__value / static_cast<const From>(rhs_)}; } \
bool operator<(const To z_) const { return __value < static_cast<const From>(z_); } \
bool operator>(const To z_) const { return __value > static_cast<const From>(z_); } \
bool operator<=(const To z_) const { return __value <= static_cast<const From>(z_); } \
bool operator>=(const To z_) const { return __value >= static_cast<const From>(z_); } \
bool operator==(const To z_) const { return __value == static_cast<const From>(z_); } \
bool operator!=(const To z_) const { return __value != static_cast<const From>(z_); } \
}; \
static_assert(std::is_trivial<To>(), #To " is not a trivial type"); \
static_assert(std::is_standard_layout<To>(), #To " is not a standart layout"); \
static_assert(std::is_pod<To>(), #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<To> \
{ \
std::size_t operator()(const To &k) const \
{ \
return std::hash<From>()(static_cast<const From>(k)); \
} \
}; \
}
}
#endif // OSRM_STRONG_TYPEDEF_HPP

View File

@ -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 <boost/assert.hpp>
@ -37,11 +37,19 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <limits>
// 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<std::uint64_t, tag::osm_node_id>;
static_assert(osrm::is_valid_alias<OSMNodeID>(), "OSMNodeID is not a valid alias");
using OSMWayID = osrm::Alias<std::uint64_t, tag::osm_way_id>;
static_assert(osrm::is_valid_alias<OSMWayID>(), "OSMWayID is not a valid alias");
static const OSMNodeID SPECIAL_OSM_NODEID = OSMNodeID{std::numeric_limits<std::uint64_t>::max()};
static const OSMWayID SPECIAL_OSM_WAYID = OSMWayID{std::numeric_limits<std::uint32_t>::max()};

View File

@ -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<double>(toFloating(coordinate.lon)));
array.values.push_back(static_cast<double>(toFloating(coordinate.lat)));
array.values.push_back(static_cast<double>(util::toFloating(coordinate.lon)));
array.values.push_back(static_cast<double>(util::toFloating(coordinate.lat)));
return array;
}