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

@@ -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;
}