2017-04-21 04:14:53 +00:00
|
|
|
#ifndef TOML11_GET
|
|
|
|
#define TOML11_GET
|
|
|
|
#include "value.hpp"
|
2017-05-06 14:55:15 +00:00
|
|
|
#include <algorithm>
|
2017-04-21 04:14:53 +00:00
|
|
|
|
|
|
|
namespace toml
|
|
|
|
{
|
|
|
|
|
2018-05-05 02:42:11 +00:00
|
|
|
template<typename T, typename std::enable_if<
|
|
|
|
detail::is_exact_toml_type<T>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
inline T& get(value& v)
|
2017-04-21 04:14:53 +00:00
|
|
|
{
|
2018-05-05 02:42:11 +00:00
|
|
|
constexpr value_t kind = detail::check_type<T>();
|
|
|
|
return v.cast<kind>();
|
2017-04-21 04:14:53 +00:00
|
|
|
}
|
|
|
|
|
2018-05-05 02:42:11 +00:00
|
|
|
template<typename T, typename std::enable_if<
|
|
|
|
detail::is_exact_toml_type<T>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
inline T const& get(const value& v)
|
|
|
|
{
|
|
|
|
constexpr value_t kind = detail::check_type<T>();
|
|
|
|
return v.cast<kind>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename std::enable_if<detail::conjunction<
|
|
|
|
detail::negation<detail::is_exact_toml_type<T>>,
|
|
|
|
detail::negation<std::is_same<T, bool>>, std::is_integral<T>
|
|
|
|
>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
inline T get(const value& v)
|
|
|
|
{
|
|
|
|
return static_cast<T>(v.cast<value_t::Integer>());
|
|
|
|
}
|
|
|
|
template<typename T, typename std::enable_if<detail::conjunction<
|
|
|
|
detail::negation<detail::is_exact_toml_type<T>>, std::is_floating_point<T>
|
|
|
|
>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
inline T get(const value& v)
|
2017-04-21 04:14:53 +00:00
|
|
|
{
|
2018-05-05 02:42:11 +00:00
|
|
|
return static_cast<T>(v.cast<value_t::Float>());
|
|
|
|
}
|
2017-12-11 03:04:57 +00:00
|
|
|
|
2018-05-05 02:42:11 +00:00
|
|
|
// array-like type
|
|
|
|
template<typename T, typename std::enable_if<detail::conjunction<
|
|
|
|
detail::negation<detail::is_exact_toml_type<T>>, detail::is_container<T>
|
|
|
|
>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
T get(const value& v)
|
|
|
|
{
|
2017-05-06 14:55:15 +00:00
|
|
|
const auto& ar = v.cast<value_t::Array>();
|
2017-04-21 04:14:53 +00:00
|
|
|
T tmp;
|
2017-05-06 14:55:15 +00:00
|
|
|
try
|
|
|
|
{
|
2018-05-05 02:42:11 +00:00
|
|
|
::toml::resize(tmp, ar.size());
|
2017-05-06 14:55:15 +00:00
|
|
|
}
|
|
|
|
catch(std::invalid_argument& iv)
|
|
|
|
{
|
2018-05-05 02:42:11 +00:00
|
|
|
throw type_error("toml::get: static array: size is not enough");
|
2017-05-06 14:55:15 +00:00
|
|
|
}
|
|
|
|
std::transform(ar.cbegin(), ar.cend(), tmp.begin(),
|
2018-05-05 02:42:11 +00:00
|
|
|
[](value const& elem){return get<typename T::value_type>(elem);});
|
2017-04-21 04:14:53 +00:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2017-12-11 03:04:57 +00:00
|
|
|
// table-like case
|
2018-05-05 02:42:11 +00:00
|
|
|
template<typename T, typename std::enable_if<detail::conjunction<
|
|
|
|
detail::negation<detail::is_exact_toml_type<T>>, detail::is_map<T>
|
|
|
|
>::value, std::nullptr_t>::type = nullptr>
|
2017-04-21 04:14:53 +00:00
|
|
|
T get(const toml::value& v)
|
|
|
|
{
|
2017-05-06 14:55:15 +00:00
|
|
|
const auto& tb = v.cast<value_t::Table>();
|
2018-05-05 02:42:11 +00:00
|
|
|
T tmp;
|
2017-05-06 14:55:15 +00:00
|
|
|
for(const auto& kv : tb){tmp.insert(kv);}
|
2017-04-21 04:14:53 +00:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2017-12-11 03:04:57 +00:00
|
|
|
// get_or -----------------------------------------------------------------
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline typename std::remove_cv<typename std::remove_reference<T>::type>::type
|
|
|
|
get_or(const toml::Table& tab, const toml::key& ky, T&& opt)
|
|
|
|
{
|
|
|
|
if(tab.count(ky) == 0) {return std::forward<T>(opt);}
|
|
|
|
return get<typename std::remove_cv<
|
|
|
|
typename std::remove_reference<T>::type>::type>(tab.find(ky)->second);
|
|
|
|
}
|
|
|
|
|
2017-04-21 04:14:53 +00:00
|
|
|
} // toml
|
|
|
|
#endif// TOML11_GET
|