mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-17 09:08:08 +08:00
simplify the implementation of from_toml
This commit is contained in:
@@ -170,3 +170,22 @@ BOOST_AUTO_TEST_CASE(test_from_toml_tie)
|
|||||||
BOOST_CHECK_EQUAL(ut["val4"].cast<toml::value_t::String >(), "piyo");
|
BOOST_CHECK_EQUAL(ut["val4"].cast<toml::value_t::String >(), "piyo");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(test_from_toml_tuple)
|
||||||
|
{
|
||||||
|
toml::Array a;
|
||||||
|
a.emplace_back(2);
|
||||||
|
a.emplace_back(7);
|
||||||
|
a.emplace_back(1);
|
||||||
|
a.emplace_back(8);
|
||||||
|
a.emplace_back(2);
|
||||||
|
toml::value v(a);
|
||||||
|
|
||||||
|
std::tuple<int, int, int, int, int> t;
|
||||||
|
toml::from_toml(t, v);
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(std::get<0>(t), 2);
|
||||||
|
BOOST_CHECK_EQUAL(std::get<1>(t), 7);
|
||||||
|
BOOST_CHECK_EQUAL(std::get<2>(t), 1);
|
||||||
|
BOOST_CHECK_EQUAL(std::get<3>(t), 8);
|
||||||
|
BOOST_CHECK_EQUAL(std::get<4>(t), 2);
|
||||||
|
}
|
||||||
|
@@ -1,66 +1,14 @@
|
|||||||
#ifndef TOML11_FROM_TOML
|
#ifndef TOML11_FROM_TOML
|
||||||
#define TOML11_FROM_TOML
|
#define TOML11_FROM_TOML
|
||||||
#include "value.hpp"
|
#include "get.hpp"
|
||||||
|
|
||||||
namespace toml
|
namespace toml
|
||||||
{
|
{
|
||||||
|
|
||||||
template<typename T, toml::value_t vT = toml::detail::check_type<T>(),
|
template<typename T>
|
||||||
typename std::enable_if<(vT != toml::value_t::Unknown &&
|
|
||||||
vT != value_t::Empty), std::nullptr_t>::type = nullptr>
|
|
||||||
void from_toml(T& x, const toml::value& v)
|
void from_toml(T& x, const toml::value& v)
|
||||||
{
|
{
|
||||||
if(v.type() != vT)
|
x = toml::get<T>(v);
|
||||||
throw type_error("from_toml: value type: " + stringize(v.type()) +
|
|
||||||
std::string(" is not arguemnt type: ") + stringize(vT));
|
|
||||||
x = v.cast<vT>();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T, toml::value_t vT = toml::detail::check_type<T>(),
|
|
||||||
typename std::enable_if<(vT == toml::value_t::Unknown) &&
|
|
||||||
(!toml::detail::is_map<T>::value) &&
|
|
||||||
toml::detail::is_container<T>::value, std::nullptr_t>::type = nullptr>
|
|
||||||
void from_toml(T& x, const toml::value& v)
|
|
||||||
{
|
|
||||||
// TODO the case of x is not dynamic container case
|
|
||||||
if(v.type() != value_t::Array)
|
|
||||||
throw type_error("from_toml: value type: " + stringize(v.type()) +
|
|
||||||
std::string(" is not argument type: Array"));
|
|
||||||
const auto& ar = v.cast<value_t::Array>();
|
|
||||||
try
|
|
||||||
{
|
|
||||||
toml::resize(x, ar.size());
|
|
||||||
}
|
|
||||||
catch(std::invalid_argument& iv)
|
|
||||||
{
|
|
||||||
throw toml::type_error("toml::from_toml: static array size is not enough");
|
|
||||||
}
|
|
||||||
auto iter = x.begin();
|
|
||||||
for(const auto& val : ar)
|
|
||||||
{
|
|
||||||
typename T::value_type v;
|
|
||||||
from_toml(v, val);
|
|
||||||
*iter = std::move(v);
|
|
||||||
++iter;
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T, toml::value_t vT = toml::detail::check_type<T>(),
|
|
||||||
typename std::enable_if<(vT == toml::value_t::Unknown) &&
|
|
||||||
toml::detail::is_map<T>::value, std::nullptr_t>::type = nullptr>
|
|
||||||
void from_toml(T& x, const toml::value& v)
|
|
||||||
{
|
|
||||||
if(v.type() != value_t::Table)
|
|
||||||
throw type_error("from_toml: value type: " + stringize(v.type()) +
|
|
||||||
std::string(" is not argument type: Table"));
|
|
||||||
x.clear();
|
|
||||||
const auto& tb = v.cast<value_t::Table>();
|
|
||||||
for(const auto& kv : tb)
|
|
||||||
{
|
|
||||||
x.insert(kv);
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user