mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-16 16:28:09 +08:00
feat: 💥 drop from_toml support
This commit is contained in:
@@ -24,7 +24,6 @@ set(TEST_NAMES
|
||||
test_comments
|
||||
test_get
|
||||
test_get_related_func
|
||||
test_from_toml
|
||||
test_parse_file
|
||||
test_serialize_file
|
||||
test_parse_unicode
|
||||
|
@@ -1,63 +0,0 @@
|
||||
#define BOOST_TEST_MODULE "test_from_toml"
|
||||
#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#else
|
||||
#define BOOST_TEST_NO_LIB
|
||||
#include <boost/test/included/unit_test.hpp>
|
||||
#endif
|
||||
#include <toml.hpp>
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
#include <list>
|
||||
#include <deque>
|
||||
#include <array>
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_from_toml)
|
||||
{
|
||||
toml::boolean b = false;
|
||||
toml::integer i = 0;
|
||||
toml::floating f = 0.;
|
||||
toml::string s;
|
||||
toml::local_date dt;
|
||||
toml::array a;
|
||||
toml::table t;
|
||||
{
|
||||
toml::value v(true);
|
||||
toml::from_toml(std::tie(b, i, f, s, dt, a, t), v);
|
||||
BOOST_CHECK_EQUAL(b, true);
|
||||
}
|
||||
{
|
||||
toml::value v(42);
|
||||
toml::from_toml(std::tie(b, i, f, s, dt, a, t), v);
|
||||
BOOST_CHECK_EQUAL(i, 42);
|
||||
}
|
||||
{
|
||||
toml::value v(3.14);
|
||||
toml::from_toml(std::tie(b, i, f, s, dt, a, t), v);
|
||||
BOOST_CHECK_EQUAL(f, 3.14);
|
||||
}
|
||||
{
|
||||
toml::value v("foo");
|
||||
|
||||
toml::from_toml(std::tie(b, i, f, s, dt, a, t), v);
|
||||
BOOST_CHECK_EQUAL(s, "foo");
|
||||
}
|
||||
{
|
||||
toml::value v(toml::local_date(2018, toml::month_t::Apr, 22));
|
||||
toml::from_toml(std::tie(b, i, f, s, dt, a, t), v);
|
||||
BOOST_CHECK(dt == toml::local_date(2018, toml::month_t::Apr, 22));
|
||||
}
|
||||
{
|
||||
toml::array ref{toml::value(42), toml::value(54)};
|
||||
toml::value v(ref);
|
||||
toml::from_toml(std::tie(b, i, f, s, dt, a, t), v);
|
||||
BOOST_CHECK(ref == a);
|
||||
}
|
||||
{
|
||||
toml::table ref{{"key1", 42}, {"key2", 3.14}};
|
||||
toml::value v(ref);
|
||||
toml::from_toml(std::tie(b, i, f, s, dt, a, t), v);
|
||||
BOOST_CHECK(ref == t);
|
||||
}
|
||||
}
|
||||
|
@@ -1,71 +0,0 @@
|
||||
// Copyright Toru Niina 2017.
|
||||
// Distributed under the MIT License.
|
||||
#ifndef TOML11_FROM_TOML_HPP
|
||||
#define TOML11_FROM_TOML_HPP
|
||||
#include "get.hpp"
|
||||
|
||||
namespace toml
|
||||
{
|
||||
|
||||
template<typename T>
|
||||
void from_toml(T& x, const toml::value& v)
|
||||
{
|
||||
x = toml::get<typename std::remove_reference<T>::type>(v);
|
||||
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)
|
||||
{
|
||||
// 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())
|
||||
{
|
||||
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...>
|
||||
{
|
||||
static void invoke(std::tuple<Ts& ...>, const toml::value&)
|
||||
{
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
} // 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
|
Reference in New Issue
Block a user