enable toml::get to make static array (like std::array)

This commit is contained in:
ToruNiina 2017-05-06 23:55:15 +09:00
parent 58c12a69be
commit 3203d39dad
2 changed files with 33 additions and 27 deletions

View File

@ -10,7 +10,7 @@
#include <unordered_map>
#include <list>
#include <deque>
#include <array>
BOOST_AUTO_TEST_CASE(test_get_exact)
@ -86,33 +86,27 @@ BOOST_AUTO_TEST_CASE(test_get_cast)
toml::value v6(a);
toml::value v7(t);
std::size_t u2 = toml::get<std::size_t>(v2);
float u3 = toml::get<float>(v3);
std::deque<int> u4 = toml::get<std::deque<int>>(v6);
std::list<int> u5 = toml::get<std::list<int> >(v6);
std::map<std::string, toml::value> u6 = toml::get<std::map<std::string, toml::value>>(v7);
const auto u2 = toml::get<std::size_t>(v2);
const auto u3 = toml::get<float>(v3);
const auto u4 = toml::get<std::deque<int>>(v6);
const auto u5 = toml::get<std::list<int> >(v6);
const auto u6 = toml::get<std::array<int,5>>(v6);
std::map<std::string, toml::value> u7 = toml::get<std::map<std::string, toml::value>>(v7);
std::deque<int> r4;
r4.push_back(2);
r4.push_back(7);
r4.push_back(1);
r4.push_back(8);
r4.push_back(2);
std::list<int> r5;
r5.push_back(2);
r5.push_back(7);
r5.push_back(1);
r5.push_back(8);
r5.push_back(2);
std::deque<int> r4{2,7,1,8,2};
std::list<int> r5{2,7,1,8,2};
std::array<int, 5> r6{{2,7,1,8,2}};
BOOST_CHECK_EQUAL(u2, 42ul);
BOOST_CHECK_CLOSE_FRACTION(u3, 3.14, 1e-3);
const bool dq = r4 == u4;
const bool ls = r5 == u5;
const bool ar = r6 == u6;
BOOST_CHECK(dq);
BOOST_CHECK(ls);
BOOST_CHECK_EQUAL(u6.at("val1").cast<toml::value_t::Boolean>(), true);
BOOST_CHECK_EQUAL(u6.at("val2").cast<toml::value_t::Integer>(), 42);
BOOST_CHECK_CLOSE_FRACTION(u6.at("val3").cast<toml::value_t::Float>(),3.14, 1e-3);
BOOST_CHECK_EQUAL(u6.at("val4").cast<toml::value_t::String>(), "piyo");
BOOST_CHECK(ar);
BOOST_CHECK_EQUAL(u7.at("val1").cast<toml::value_t::Boolean>(), true);
BOOST_CHECK_EQUAL(u7.at("val2").cast<toml::value_t::Integer>(), 42);
BOOST_CHECK_CLOSE_FRACTION(u7.at("val3").cast<toml::value_t::Float>(),3.14, 1e-3);
BOOST_CHECK_EQUAL(u7.at("val4").cast<toml::value_t::String>(), "piyo");
}

View File

@ -1,7 +1,7 @@
#ifndef TOML11_GET
#define TOML11_GET
#include "value.hpp"
#include "from_toml.hpp"
#include <algorithm>
namespace toml
{
@ -14,6 +14,7 @@ inline T get(const toml::value& v)
return static_cast<T>(v.cast<vT>());
}
// array case
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) &&
@ -21,10 +22,20 @@ template<typename T, toml::value_t vT = toml::detail::check_type<T>(),
T get(const toml::value& v)
{
if(v.type() != value_t::Array)
throw type_error("from_toml: value type: " + stringize(v.type()) +
throw type_error("get: value type: " + stringize(v.type()) +
std::string(" is not argument type: Array"));
const auto& ar = v.cast<value_t::Array>();
T tmp;
from_toml(tmp, v);
try
{
toml::resize(tmp, ar.size());
}
catch(std::invalid_argument& iv)
{
throw toml::type_error("toml::get: static array size is not enough");
}
std::transform(ar.cbegin(), ar.cend(), tmp.begin(),
[](toml::value const& elem){return get<typename T::value_type>(elem);});
return tmp;
}
@ -34,10 +45,11 @@ template<typename T, toml::value_t vT = toml::detail::check_type<T>(),
T get(const toml::value& v)
{
if(v.type() != value_t::Table)
throw type_error("from_toml: value type: " + stringize(v.type()) +
throw type_error("get: value type: " + stringize(v.type()) +
std::string(" is not argument type: Table"));
T tmp;
from_toml(tmp, v);
const auto& tb = v.cast<value_t::Table>();
for(const auto& kv : tb){tmp.insert(kv);}
return tmp;
}