#ifndef TOML11_GET #define TOML11_GET #include "value.hpp" #include namespace toml { template::value, std::nullptr_t>::type = nullptr> inline T& get(value& v) { constexpr value_t kind = detail::check_type(); return v.cast(); } template::value, std::nullptr_t>::type = nullptr> inline T const& get(const value& v) { constexpr value_t kind = detail::check_type(); return v.cast(); } template>, detail::negation>, std::is_integral >::value, std::nullptr_t>::type = nullptr> inline T get(const value& v) { return static_cast(v.cast()); } template>, std::is_floating_point >::value, std::nullptr_t>::type = nullptr> inline T get(const value& v) { return static_cast(v.cast()); } // array-like type template>, detail::is_container >::value, std::nullptr_t>::type = nullptr> T get(const value& v) { const auto& ar = v.cast(); T tmp; try { ::toml::resize(tmp, ar.size()); } catch(std::invalid_argument& iv) { throw type_error("toml::get: static array: size is not enough"); } std::transform(ar.cbegin(), ar.cend(), tmp.begin(), [](value const& elem){return get(elem);}); return tmp; } // table-like case template>, detail::is_map >::value, std::nullptr_t>::type = nullptr> T get(const toml::value& v) { const auto& tb = v.cast(); T tmp; for(const auto& kv : tb){tmp.insert(kv);} 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 inline typename std::remove_cv::type>::type get_or(const toml::Table& tab, const toml::key& ky, T&& opt) { if(tab.count(ky) == 0) {return std::forward(opt);} return get::type>::type>(tab.find(ky)->second); } } // toml #endif// TOML11_GET