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;
|
|
|
|
}
|
|
|
|
|
2018-05-05 02:46:09 +00:00
|
|
|
// array -> pair
|
|
|
|
template<typename T, typename std::enable_if<detail::is_std_pair<T>::value,
|
|
|
|
std::nullptr_t>::type = nullptr>
|
|
|
|
T get(const value& v)
|
|
|
|
{
|
|
|
|
using first_type = typename T::first_type;
|
|
|
|
using second_type = typename T::second_type;
|
|
|
|
const auto& ar = v.cast<value_t::Array>();
|
|
|
|
if(ar.size() != 2)
|
|
|
|
{
|
|
|
|
throw std::out_of_range(
|
|
|
|
"toml::get<std::pair>: value does not have 2 elements.");
|
|
|
|
}
|
|
|
|
|
|
|
|
T tmp;
|
|
|
|
tmp.first = get<first_type >(ar.at(0));
|
|
|
|
tmp.second = get<second_type>(ar.at(1));
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
|
|
|
|
template<typename T, std::size_t ...I>
|
|
|
|
T get_tuple_impl(const toml::Array& a, index_sequence<I...>)
|
|
|
|
{
|
|
|
|
return std::make_tuple(
|
|
|
|
::toml::get<typename std::tuple_element<I, T>::type>(a.at(I))...);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // detail
|
|
|
|
|
|
|
|
// array -> tuple
|
|
|
|
template<typename T, typename std::enable_if<detail::is_std_tuple<T>::value,
|
|
|
|
std::nullptr_t>::type = nullptr>
|
|
|
|
T get(const value& v)
|
|
|
|
{
|
|
|
|
const auto& ar = v.cast<value_t::Array>();
|
|
|
|
if(ar.size() != std::tuple_size<T>::value)
|
|
|
|
{
|
|
|
|
throw std::out_of_range(
|
|
|
|
"toml::get<std::tuple>: array value does not have " +
|
|
|
|
std::to_string(std::tuple_size<T>::value) +
|
|
|
|
std::string(" elements (array has ") + std::to_string(ar.size()) +
|
|
|
|
std::string(" elements)."));
|
|
|
|
}
|
|
|
|
return detail::get_tuple_impl<T>(ar,
|
|
|
|
detail::make_index_sequence<std::tuple_size<T>::value>{});
|
|
|
|
}
|
|
|
|
|
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
|