2018-12-13 11:44:10 +00:00
|
|
|
// Copyright Toru Niina 2017.
|
|
|
|
// Distributed under the MIT License.
|
2019-03-16 05:19:47 +00:00
|
|
|
#ifndef TOML11_FROM_TOML_HPP
|
|
|
|
#define TOML11_FROM_TOML_HPP
|
2018-05-05 02:59:34 +00:00
|
|
|
#include "get.hpp"
|
2017-04-21 04:14:53 +00:00
|
|
|
|
|
|
|
namespace toml
|
|
|
|
{
|
|
|
|
|
2018-05-05 02:59:34 +00:00
|
|
|
template<typename T>
|
2017-04-21 04:14:53 +00:00
|
|
|
void from_toml(T& x, const toml::value& v)
|
|
|
|
{
|
2018-12-12 17:24:03 +00:00
|
|
|
x = toml::get<typename std::remove_reference<T>::type>(v);
|
2017-04-21 04:14:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
constexpr toml::value_t determine_castable_type()
|
|
|
|
{
|
|
|
|
return check_type<T>() != toml::value_t::Unknown ? check_type<T>() :
|
|
|
|
toml::detail::is_map<T>::value ? toml::value_t::Table :
|
|
|
|
toml::detail::is_container<T>::value ? toml::value_t::Array :
|
|
|
|
toml::value_t::Unknown;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<std::size_t N, typename ... Ts>
|
|
|
|
struct from_toml_tie_impl
|
|
|
|
{
|
|
|
|
constexpr static std::size_t index = sizeof...(Ts) - N;
|
|
|
|
constexpr static toml::value_t type_index =
|
|
|
|
determine_castable_type<
|
|
|
|
typename std::tuple_element<index, std::tuple<Ts...>>::type>();
|
|
|
|
|
|
|
|
static void invoke(std::tuple<Ts& ...> tie, const toml::value& v)
|
|
|
|
{
|
2017-06-12 13:04:59 +00:00
|
|
|
// static_cast is needed because with intel c++ compiler, operator==
|
|
|
|
// is only defined when the two types are strictly equal, and type_index
|
|
|
|
// is const toml::value_t, while v.type() is toml::value_t.
|
|
|
|
if(static_cast<toml::value_t>(type_index) == v.type())
|
2017-04-21 04:14:53 +00:00
|
|
|
{
|
|
|
|
from_toml(std::get<index>(tie), v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return from_toml_tie_impl<N-1, Ts...>::invoke(tie, v);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename ... Ts>
|
|
|
|
struct from_toml_tie_impl<0, Ts...>
|
|
|
|
{
|
2019-04-06 10:39:13 +00:00
|
|
|
static void invoke(std::tuple<Ts& ...>, const toml::value&)
|
2017-04-21 04:14:53 +00:00
|
|
|
{
|
2017-05-19 05:44:05 +00:00
|
|
|
return;
|
2017-04-21 04:14:53 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // detail
|
|
|
|
|
|
|
|
template<typename ... Ts>
|
|
|
|
void from_toml(std::tuple<Ts& ...> tie, const toml::value& v)
|
|
|
|
{
|
|
|
|
detail::from_toml_tie_impl<sizeof...(Ts), Ts...>::invoke(tie, v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // toml
|
|
|
|
#endif // TOML11_FROM_TOML
|