diff --git a/toml/get.hpp b/toml/get.hpp index 147b0b7..cbd48a8 100644 --- a/toml/get.hpp +++ b/toml/get.hpp @@ -71,6 +71,56 @@ T get(const toml::value& v) return tmp; } +// array -> pair +template::value, + std::nullptr_t>::type = nullptr> +T get(const value& v) +{ + using first_type = typename T::first_type; + using second_type = typename T::second_type; + const auto& ar = v.cast(); + if(ar.size() != 2) + { + throw std::out_of_range( + "toml::get: value does not have 2 elements."); + } + + T tmp; + tmp.first = get(ar.at(0)); + tmp.second = get(ar.at(1)); + return tmp; +} + +namespace detail +{ + +template +T get_tuple_impl(const toml::Array& a, index_sequence) +{ + return std::make_tuple( + ::toml::get::type>(a.at(I))...); +} + +} // detail + +// array -> tuple +template::value, + std::nullptr_t>::type = nullptr> +T get(const value& v) +{ + const auto& ar = v.cast(); + if(ar.size() != std::tuple_size::value) + { + throw std::out_of_range( + "toml::get: array value does not have " + + std::to_string(std::tuple_size::value) + + std::string(" elements (array has ") + std::to_string(ar.size()) + + std::string(" elements).")); + } + return detail::get_tuple_impl(ar, + detail::make_index_sequence::value>{}); +} + // get_or ----------------------------------------------------------------- template