2015-02-27 05:17:19 -05:00
|
|
|
#ifndef JSON_UTIL_HPP
|
|
|
|
#define JSON_UTIL_HPP
|
|
|
|
|
2016-01-02 11:13:44 -05:00
|
|
|
#include "osrm/json_container.hpp"
|
2015-02-27 05:17:19 -05:00
|
|
|
|
|
|
|
#include <cmath>
|
2015-03-03 19:34:45 -05:00
|
|
|
#include <limits>
|
2015-02-27 05:17:19 -05:00
|
|
|
|
|
|
|
namespace osrm
|
|
|
|
{
|
|
|
|
namespace json
|
|
|
|
{
|
|
|
|
|
2015-02-28 12:44:17 -05:00
|
|
|
// Make sure we don't have inf and NaN values
|
|
|
|
template <typename T> T clamp_float(T d)
|
2015-02-27 05:17:19 -05:00
|
|
|
{
|
|
|
|
if (std::isnan(d) || std::numeric_limits<T>::infinity() == d)
|
|
|
|
{
|
|
|
|
return std::numeric_limits<T>::max();
|
|
|
|
}
|
|
|
|
if (-std::numeric_limits<T>::infinity() == d)
|
|
|
|
{
|
2015-03-03 05:53:31 -05:00
|
|
|
return std::numeric_limits<T>::lowest();
|
2015-02-27 05:17:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2015-02-28 12:44:17 -05:00
|
|
|
template <typename... Args> osrm::json::Array make_array(Args... args)
|
2015-02-27 05:17:19 -05:00
|
|
|
{
|
|
|
|
osrm::json::Array a;
|
2015-03-03 19:34:45 -05:00
|
|
|
append_to_container(a.values, args...);
|
2015-02-27 05:17:19 -05:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2015-03-03 05:50:02 -05:00
|
|
|
template <typename T> osrm::json::Array make_array(const std::vector<T> &vector)
|
2015-02-28 12:44:17 -05:00
|
|
|
{
|
|
|
|
osrm::json::Array a;
|
2015-03-03 05:50:02 -05:00
|
|
|
for (const auto &v : vector)
|
|
|
|
{
|
2015-02-28 12:44:17 -05:00
|
|
|
a.values.emplace_back(v);
|
2015-03-03 05:50:02 -05:00
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
// template specialization needed as clang does not play nice
|
|
|
|
template <> osrm::json::Array make_array(const std::vector<bool> &vector)
|
|
|
|
{
|
|
|
|
osrm::json::Array a;
|
|
|
|
for (const bool v : vector)
|
|
|
|
{
|
|
|
|
a.values.emplace_back(v);
|
|
|
|
}
|
2015-02-28 12:44:17 -05:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Easy acces to object hierachies
|
2015-03-03 05:50:02 -05:00
|
|
|
osrm::json::Value &get(osrm::json::Value &value) { return value; }
|
2015-02-28 12:44:17 -05:00
|
|
|
|
2015-03-03 05:50:02 -05:00
|
|
|
template <typename... Keys>
|
|
|
|
osrm::json::Value &get(osrm::json::Value &value, const char *key, Keys... keys)
|
2015-02-28 12:44:17 -05:00
|
|
|
{
|
|
|
|
using recursive_object_t = mapbox::util::recursive_wrapper<osrm::json::Object>;
|
|
|
|
return get(value.get<recursive_object_t>().get().values[key], keys...);
|
|
|
|
}
|
|
|
|
|
2015-03-03 05:50:02 -05:00
|
|
|
template <typename... Keys>
|
|
|
|
osrm::json::Value &get(osrm::json::Value &value, unsigned key, Keys... keys)
|
2015-02-28 12:44:17 -05:00
|
|
|
{
|
|
|
|
using recursive_array_t = mapbox::util::recursive_wrapper<osrm::json::Array>;
|
|
|
|
return get(value.get<recursive_array_t>().get().values[key], keys...);
|
|
|
|
}
|
|
|
|
|
2015-02-27 05:17:19 -05:00
|
|
|
} // namespace json
|
|
|
|
} // namespace osrm
|
2015-03-03 05:50:02 -05:00
|
|
|
#endif // JSON_UTIL_HPP
|