osrm-backend/include/util/json_util.hpp

59 lines
1.4 KiB
C++
Raw Normal View History

#ifndef JSON_UTIL_HPP
#define JSON_UTIL_HPP
2016-01-02 11:13:44 -05:00
#include "osrm/json_container.hpp"
#include "util/container.hpp"
#include <cmath>
#include <limits>
namespace osrm
{
2016-01-05 10:51:13 -05:00
namespace util
{
namespace json
{
// Make sure we don't have inf and NaN values
template <typename T> T clamp_float(T d)
{
if (std::isnan(d) || std::numeric_limits<T>::infinity() == d)
{
return std::numeric_limits<T>::max();
}
if (-std::numeric_limits<T>::infinity() == d)
{
return std::numeric_limits<T>::lowest();
}
return d;
}
2016-01-05 10:51:13 -05:00
template <typename... Args> Array make_array(Args... args)
{
2016-01-05 10:51:13 -05:00
Array a;
// TODO: check why a.values.emplace_back(args...); is not an option here
append_to_container(a.values, args...);
return a;
}
// Easy acces to object hierachies
2016-01-28 10:28:44 -05:00
inline Value &get(Value &value) { return value; }
2016-01-07 19:31:57 -05:00
template <typename... Keys> Value &get(Value &value, const char *key, Keys... keys)
{
2016-01-05 10:51:13 -05:00
using recursive_object_t = mapbox::util::recursive_wrapper<Object>;
return get(value.get<recursive_object_t>().get().values[key], keys...);
}
2016-01-07 19:31:57 -05:00
template <typename... Keys> Value &get(Value &value, unsigned key, Keys... keys)
{
2016-01-05 10:51:13 -05:00
using recursive_array_t = mapbox::util::recursive_wrapper<Array>;
return get(value.get<recursive_array_t>().get().values[key], keys...);
}
} // namespace json
2016-01-05 10:51:13 -05:00
} // namespace util
} // namespace osrm
2016-01-05 10:51:13 -05:00
#endif // JSON_UTIL_HPP