// Copyright Toru Niina 2017. // Distributed under the MIT License. #ifndef TOML11_GET_HPP #define TOML11_GET_HPP #include #include "from.hpp" #include "result.hpp" #include "value.hpp" namespace toml { // ============================================================================ // exact toml::* type template class M, template class V> detail::enable_if_t>::value, T> & get(basic_value& v) { return v.template cast>::value>(); } template class M, template class V> detail::enable_if_t>::value, T> const& get(const basic_value& v) { return v.template cast>::value>(); } template class M, template class V> detail::enable_if_t>::value, T> get(basic_value&& v) { return T(std::move(v).template cast>::value>()); } // ============================================================================ // T == toml::value; identity transformation. template class M, template class V> inline detail::enable_if_t>::value, T>& get(basic_value& v) { return v; } template class M, template class V> inline detail::enable_if_t>::value, T> const& get(const basic_value& v) { return v; } template class M, template class V> inline detail::enable_if_t>::value, T> get(basic_value&& v) { return basic_value(std::move(v)); } // ============================================================================ // T == toml::basic_value; basic_value -> basic_value template class M, template class V> inline detail::enable_if_t, detail::negation>> >::value, T> get(const basic_value& v) { return T(v); } // ============================================================================ // integer convertible from toml::Integer template class M, template class V> inline detail::enable_if_t, // T is integral detail::negation>, // but not bool detail::negation< // but not toml::integer detail::is_exact_toml_type>> >::value, T> get(const basic_value& v) { return static_cast(v.as_integer()); } // ============================================================================ // floating point convertible from toml::Float template class M, template class V> inline detail::enable_if_t, // T is floating_point detail::negation< // but not toml::floating detail::is_exact_toml_type>> >::value, T> get(const basic_value& v) { return static_cast(v.as_floating()); } // ============================================================================ // std::string; toml uses its own toml::string, but it should be convertible to // std::string seamlessly template class M, template class V> inline detail::enable_if_t::value, std::string>& get(basic_value& v) { return v.as_string().str; } template class M, template class V> inline detail::enable_if_t::value, std::string> const& get(const basic_value& v) { return v.as_string().str; } template class M, template class V> inline detail::enable_if_t::value, std::string> get(basic_value&& v) { return std::string(std::move(v.as_string().str)); } // ============================================================================ // std::string_view #if defined(TOML11_USING_STRING_VIEW) && TOML11_USING_STRING_VIEW>0 template class M, template class V> inline detail::enable_if_t::value, std::string_view> get(const basic_value& v) { return std::string_view(v.as_string().str); } #endif // ============================================================================ // std::chrono::duration from toml::local_time. template class M, template class V> inline detail::enable_if_t::value, T> get(const basic_value& v) { return std::chrono::duration_cast( std::chrono::nanoseconds(v.as_local_time())); } // ============================================================================ // std::chrono::system_clock::time_point from toml::datetime variants template class M, template class V> inline detail::enable_if_t< std::is_same::value, T> get(const basic_value& v) { switch(v.type()) { case value_t::local_date: { return std::chrono::system_clock::time_point(v.as_local_date()); } case value_t::local_datetime: { return std::chrono::system_clock::time_point(v.as_local_datetime()); } case value_t::offset_datetime: { return std::chrono::system_clock::time_point(v.as_offset_datetime()); } default: { throw type_error(detail::format_underline("toml::value: " "bad_cast to std::chrono::system_clock::time_point", { {v.location(), concat_to_string("the actual type is ", v.type())} }), v.location()); } } } // ============================================================================ // forward declaration to use this recursively. ignore this and go ahead. // array-like type with push_back(value) method template class M, template class V> detail::enable_if_t, // T is a container detail::has_push_back_method, // T::push_back(value) works detail::negation< // but not toml::array detail::is_exact_toml_type>> >::value, T> get(const basic_value&); // array-like type without push_back(value) method template class M, template class V> detail::enable_if_t, // T is a container detail::negation>, // w/o push_back(...) detail::negation>, // T does not have special conversion detail::negation< // not toml::array detail::is_exact_toml_type>> >::value, T> get(const basic_value&); // std::pair template class M, template class V> detail::enable_if_t::value, T> get(const basic_value&); // std::tuple template class M, template class V> detail::enable_if_t::value, T> get(const basic_value&); // map-like classes template class M, template class V> detail::enable_if_t, // T is map detail::negation< // but not toml::table detail::is_exact_toml_type>> >::value, T> get(const basic_value&); // T.from_toml(v) template class M, template class V> detail::enable_if_t>>, detail::has_from_toml_method, // but has from_toml(toml::value) std::is_default_constructible // and default constructible >::value, T> get(const basic_value&); // toml::from::from_toml(v) template class M, template class V> detail::enable_if_t::value, T> get(const basic_value&); // T(const toml::value&) and T is not toml::basic_value, // and it does not have `from` nor `from_toml`. template class M, template class V> detail::enable_if_t>, std::is_constructible&>, detail::negation>, detail::negation> >::value, T> get(const basic_value&); // ============================================================================ // array-like types; most likely STL container, like std::vector, etc. template class M, template class V> detail::enable_if_t, // T is a container detail::has_push_back_method, // container.push_back(elem) works detail::negation< // but not toml::array detail::is_exact_toml_type>> >::value, T> get(const basic_value& v) { using value_type = typename T::value_type; const auto& ary = v.as_array(); T container; try_reserve(container, ary.size()); for(const auto& elem : ary) { container.push_back(get(elem)); } return container; } // ============================================================================ // std::forward_list does not have push_back, insert, or emplace. // It has insert_after, emplace_after, push_front. template class M, template class V> detail::enable_if_t::value, T> get(const basic_value& v) { using value_type = typename T::value_type; T container; for(const auto& elem : v.as_array()) { container.push_front(get(elem)); } container.reverse(); return container; } // ============================================================================ // array-like types, without push_back(). most likely [std|boost]::array. template class M, template class V> detail::enable_if_t, // T is a container detail::negation>, // w/o push_back detail::negation>, // T does not have special conversion detail::negation< // T is not toml::array detail::is_exact_toml_type>> >::value, T> get(const basic_value& v) { using value_type = typename T::value_type; const auto& ar = v.as_array(); T container; if(ar.size() != container.size()) { throw std::out_of_range(detail::format_underline(concat_to_string( "toml::get: specified container size is ", container.size(), " but there are ", ar.size(), " elements in toml array."), { {v.location(), "here"} })); } for(std::size_t i=0; i(ar[i]); } return container; } // ============================================================================ // std::pair. template class M, template class V> detail::enable_if_t::value, T> get(const basic_value& v) { using first_type = typename T::first_type; using second_type = typename T::second_type; const auto& ar = v.as_array(); if(ar.size() != 2) { throw std::out_of_range(detail::format_underline(concat_to_string( "toml::get: specified std::pair but there are ", ar.size(), " elements in toml array."), {{v.location(), "here"}})); } return std::make_pair(::toml::get(ar.at(0)), ::toml::get(ar.at(1))); } // ============================================================================ // std::tuple. namespace detail { template T get_tuple_impl(const Array& a, index_sequence) { return std::make_tuple( ::toml::get::type>(a.at(I))...); } } // detail template class M, template class V> detail::enable_if_t::value, T> get(const basic_value& v) { const auto& ar = v.as_array(); if(ar.size() != std::tuple_size::value) { throw std::out_of_range(detail::format_underline(concat_to_string( "toml::get: specified std::tuple with ", std::tuple_size::value, " elements, but there are ", ar.size(), " elements in toml array."), {{v.location(), "here"}})); } return detail::get_tuple_impl(ar, detail::make_index_sequence::value>{}); } // ============================================================================ // map-like types; most likely STL map, like std::map or std::unordered_map. template class M, template class V> detail::enable_if_t, // T is map detail::negation< // but not toml::array detail::is_exact_toml_type>> >::value, T> get(const basic_value& v) { using key_type = typename T::key_type; using mapped_type = typename T::mapped_type; static_assert(std::is_convertible::value, "toml::get only supports map type of which key_type is " "convertible from std::string."); T map; for(const auto& kv : v.as_table()) { map.emplace(key_type(kv.first), get(kv.second)); } return map; } // ============================================================================ // user-defined, but compatible types. template class M, template class V> detail::enable_if_t>>, detail::has_from_toml_method, // but has from_toml(toml::value) memfn std::is_default_constructible // and default constructible >::value, T> get(const basic_value& v) { T ud; ud.from_toml(v); return ud; } template class M, template class V> detail::enable_if_t::value, T> get(const basic_value& v) { return ::toml::from::from_toml(v); } template class M, template class V> detail::enable_if_t>, // T is not a toml::value std::is_constructible&>, // T is constructible from toml::value detail::negation>, // and T does not have T.from_toml(v); detail::negation> // and T does not have toml::from{}; >::value, T> get(const basic_value& v) { return T(v); } // ============================================================================ // find // ---------------------------------------------------------------------------- // these overloads do not require to set T. and returns value itself. template class M, template class V> basic_value const& find(const basic_value& v, const key& ky) { const auto& tab = v.as_table(); if(tab.count(ky) == 0) { detail::throw_key_not_found_error(v, ky); } return tab.at(ky); } template class M, template class V> basic_value& find(basic_value& v, const key& ky) { auto& tab = v.as_table(); if(tab.count(ky) == 0) { detail::throw_key_not_found_error(v, ky); } return tab.at(ky); } template class M, template class V> basic_value find(basic_value&& v, const key& ky) { typename basic_value::table_type tab = std::move(v).as_table(); if(tab.count(ky) == 0) { detail::throw_key_not_found_error(v, ky); } return basic_value(std::move(tab.at(ky))); } // ---------------------------------------------------------------------------- // find(value, idx) template class M, template class V> basic_value const& find(const basic_value& v, const std::size_t idx) { const auto& ary = v.as_array(); if(ary.size() <= idx) { throw std::out_of_range(detail::format_underline(concat_to_string( "index ", idx, " is out of range"), {{v.location(), "in this array"}})); } return ary.at(idx); } template class M, template class V> basic_value& find(basic_value& v, const std::size_t idx) { auto& ary = v.as_array(); if(ary.size() <= idx) { throw std::out_of_range(detail::format_underline(concat_to_string( "index ", idx, " is out of range"), {{v.location(), "in this array"}})); } return ary.at(idx); } template class M, template class V> basic_value find(basic_value&& v, const std::size_t idx) { auto& ary = v.as_array(); if(ary.size() <= idx) { throw std::out_of_range(detail::format_underline(concat_to_string( "index ", idx, " is out of range"), {{v.location(), "in this array"}})); } return basic_value(std::move(ary.at(idx))); } // ---------------------------------------------------------------------------- // find(value, key); template class M, template class V> decltype(::toml::get(std::declval const&>())) find(const basic_value& v, const key& ky) { const auto& tab = v.as_table(); if(tab.count(ky) == 0) { detail::throw_key_not_found_error(v, ky); } return ::toml::get(tab.at(ky)); } template class M, template class V> decltype(::toml::get(std::declval&>())) find(basic_value& v, const key& ky) { auto& tab = v.as_table(); if(tab.count(ky) == 0) { detail::throw_key_not_found_error(v, ky); } return ::toml::get(tab.at(ky)); } template class M, template class V> decltype(::toml::get(std::declval&&>())) find(basic_value&& v, const key& ky) { typename basic_value::table_type tab = std::move(v).as_table(); if(tab.count(ky) == 0) { detail::throw_key_not_found_error(v, ky); } return ::toml::get(std::move(tab.at(ky))); } // ---------------------------------------------------------------------------- // find(value, idx) template class M, template class V> decltype(::toml::get(std::declval const&>())) find(const basic_value& v, const std::size_t idx) { const auto& ary = v.as_array(); if(ary.size() <= idx) { throw std::out_of_range(detail::format_underline(concat_to_string( "index ", idx, " is out of range"), {{v.location(), "in this array"}})); } return ::toml::get(ary.at(idx)); } template class M, template class V> decltype(::toml::get(std::declval&>())) find(basic_value& v, const std::size_t idx) { auto& ary = v.as_array(); if(ary.size() <= idx) { throw std::out_of_range(detail::format_underline(concat_to_string( "index ", idx, " is out of range"), {{v.location(), "in this array"}})); } return ::toml::get(ary.at(idx)); } template class M, template class V> decltype(::toml::get(std::declval&&>())) find(basic_value&& v, const std::size_t idx) { typename basic_value::array_type ary = std::move(v).as_array(); if(ary.size() <= idx) { throw std::out_of_range(detail::format_underline(concat_to_string( "index ", idx, " is out of range"), {{v.location(), "in this array"}})); } return ::toml::get(std::move(ary.at(idx))); } // -------------------------------------------------------------------------- // toml::find(toml::value, toml::key, Ts&& ... keys) namespace detail { // It suppresses warnings by -Wsign-conversion. Let's say we have the following // code. // ```cpp // const auto x = toml::find(data, "array", 0); // ``` // Here, the type of literal number `0` is `int`. `int` is a signed integer. // `toml::find` takes `std::size_t` as an index. So it causes implicit sign // conversion and `-Wsign-conversion` warns about it. Using `0u` instead of `0` // suppresses the warning, but it makes user code messy. // To suppress this warning, we need to be aware of type conversion caused // by `toml::find(v, key1, key2, ... keys)`. But the thing is that the types of // keys can be any combination of {string-like, size_t-like}. Of course we can't // write down all the combinations. Thus we need to use some function that // recognize the type of argument and cast it into `std::string` or // `std::size_t` depending on the context. // `key_cast` does the job. It has 2 overloads. One is invoked when the // argument type is an integer and cast the argument into `std::size_t`. The // other is invoked when the argument type is not an integer, possibly one of // std::string, const char[N] or const char*, and construct std::string from // the argument. // `toml::find(v, k1, k2, ... ks)` uses `key_cast` before passing `ks` to // `toml::find(v, k)` to suppress -Wsign-conversion. template enable_if_t>, negation, bool>>>::value, std::size_t> key_cast(T&& v) noexcept { return std::size_t(v); } template enable_if_t>, negation, bool>>>>::value, std::string> key_cast(T&& v) noexcept { return std::string(std::forward(v)); } } // detail template class M, template class V, typename Key1, typename Key2, typename ... Keys> const basic_value& find(const basic_value& v, Key1&& k1, Key2&& k2, Keys&& ... keys) { return ::toml::find(::toml::find(v, detail::key_cast(k1)), detail::key_cast(k2), std::forward(keys)...); } template class M, template class V, typename Key1, typename Key2, typename ... Keys> basic_value& find(basic_value& v, Key1&& k1, Key2&& k2, Keys&& ... keys) { return ::toml::find(::toml::find(v, detail::key_cast(k1)), detail::key_cast(k2), std::forward(keys)...); } template class M, template class V, typename Key1, typename Key2, typename ... Keys> basic_value find(basic_value&& v, Key1&& k1, Key2&& k2, Keys&& ... keys) { return ::toml::find(::toml::find(std::move(v), std::forward(k1)), detail::key_cast(k2), std::forward(keys)...); } template class M, template class V, typename Key1, typename Key2, typename ... Keys> decltype(::toml::get(std::declval&>())) find(const basic_value& v, Key1&& k1, Key2&& k2, Keys&& ... keys) { return ::toml::find(::toml::find(v, detail::key_cast(k1)), detail::key_cast(k2), std::forward(keys)...); } template class M, template class V, typename Key1, typename Key2, typename ... Keys> decltype(::toml::get(std::declval&>())) find(basic_value& v, Key1&& k1, Key2&& k2, Keys&& ... keys) { return ::toml::find(::toml::find(v, detail::key_cast(k1)), detail::key_cast(k2), std::forward(keys)...); } template class M, template class V, typename Key1, typename Key2, typename ... Keys> decltype(::toml::get(std::declval&&>())) find(basic_value&& v, Key1&& k1, Key2&& k2, Keys&& ... keys) { return ::toml::find(::toml::find(std::move(v), detail::key_cast(k1)), detail::key_cast(k2), std::forward(keys)...); } // ============================================================================ // get_or(value, fallback) template class M, template class V> basic_value const& get_or(const basic_value& v, const basic_value&) { return v; } template class M, template class V> basic_value& get_or(basic_value& v, basic_value&) { return v; } template class M, template class V> basic_value get_or(basic_value&& v, basic_value&&) { return v; } // ---------------------------------------------------------------------------- // specialization for the exact toml types (return type becomes lvalue ref) template class M, template class V> detail::enable_if_t< detail::is_exact_toml_type>::value, T> const& get_or(const basic_value& v, const T& opt) { try { return get>(v); } catch(...) { return opt; } } template class M, template class V> detail::enable_if_t< detail::is_exact_toml_type>::value, T>& get_or(basic_value& v, T& opt) { try { return get>(v); } catch(...) { return opt; } } template class M, template class V> detail::enable_if_t, basic_value>::value, detail::remove_cvref_t> get_or(basic_value&& v, T&& opt) { try { return get>(std::move(v)); } catch(...) { return detail::remove_cvref_t(std::forward(opt)); } } // ---------------------------------------------------------------------------- // specialization for std::string (return type becomes lvalue ref) template class M, template class V> detail::enable_if_t, std::string>::value, std::string> const& get_or(const basic_value& v, const T& opt) { try { return v.as_string().str; } catch(...) { return opt; } } template class M, template class V> detail::enable_if_t::value, std::string>& get_or(basic_value& v, T& opt) { try { return v.as_string().str; } catch(...) { return opt; } } template class M, template class V> detail::enable_if_t< std::is_same, std::string>::value, std::string> get_or(basic_value&& v, T&& opt) { try { return std::move(v.as_string().str); } catch(...) { return std::string(std::forward(opt)); } } // ---------------------------------------------------------------------------- // specialization for string literal template class M, template class V> detail::enable_if_t::type>::value, std::string> get_or(const basic_value& v, T&& opt) { try { return std::move(v.as_string().str); } catch(...) { return std::string(std::forward(opt)); } } // ---------------------------------------------------------------------------- // others (require type conversion and return type cannot be lvalue reference) template class M, template class V> detail::enable_if_t, basic_value>>, detail::negation>>, detail::negation::type>> >::value, detail::remove_cvref_t> get_or(const basic_value& v, T&& opt) { try { return get>(v); } catch(...) { return detail::remove_cvref_t(std::forward(opt)); } } // =========================================================================== // find_or(value, key, fallback) template class M, template class V> basic_value const& find_or(const basic_value& v, const key& ky, const basic_value& opt) { if(!v.is_table()) {return opt;} const auto& tab = v.as_table(); if(tab.count(ky) == 0) {return opt;} return tab.at(ky); } template class M, template class V> basic_value& find_or(basic_value& v, const toml::key& ky, basic_value& opt) { if(!v.is_table()) {return opt;} auto& tab = v.as_table(); if(tab.count(ky) == 0) {return opt;} return tab.at(ky); } template class M, template class V> basic_value find_or(basic_value&& v, const toml::key& ky, basic_value&& opt) { if(!v.is_table()) {return opt;} auto tab = std::move(v).as_table(); if(tab.count(ky) == 0) {return opt;} return basic_value(std::move(tab.at(ky))); } // --------------------------------------------------------------------------- // exact types (return type can be a reference) template class M, template class V> detail::enable_if_t< detail::is_exact_toml_type>::value, T> const& find_or(const basic_value& v, const key& ky, const T& opt) { if(!v.is_table()) {return opt;} const auto& tab = v.as_table(); if(tab.count(ky) == 0) {return opt;} return get_or(tab.at(ky), opt); } template class M, template class V> detail::enable_if_t< detail::is_exact_toml_type>::value, T>& find_or(basic_value& v, const toml::key& ky, T& opt) { if(!v.is_table()) {return opt;} auto& tab = v.as_table(); if(tab.count(ky) == 0) {return opt;} return get_or(tab.at(ky), opt); } template class M, template class V> detail::enable_if_t< detail::is_exact_toml_type>::value, detail::remove_cvref_t> find_or(basic_value&& v, const toml::key& ky, T&& opt) { if(!v.is_table()) {return std::forward(opt);} auto tab = std::move(v).as_table(); if(tab.count(ky) == 0) {return std::forward(opt);} return get_or(std::move(tab.at(ky)), std::forward(opt)); } // --------------------------------------------------------------------------- // std::string (return type can be a reference) template class M, template class V> detail::enable_if_t::value, std::string> const& find_or(const basic_value& v, const key& ky, const T& opt) { if(!v.is_table()) {return opt;} const auto& tab = v.as_table(); if(tab.count(ky) == 0) {return opt;} return get_or(tab.at(ky), opt); } template class M, template class V> detail::enable_if_t::value, std::string>& find_or(basic_value& v, const toml::key& ky, T& opt) { if(!v.is_table()) {return opt;} auto& tab = v.as_table(); if(tab.count(ky) == 0) {return opt;} return get_or(tab.at(ky), opt); } template class M, template class V> detail::enable_if_t::value, std::string> find_or(basic_value&& v, const toml::key& ky, T&& opt) { if(!v.is_table()) {return std::forward(opt);} auto tab = std::move(v).as_table(); if(tab.count(ky) == 0) {return std::forward(opt);} return get_or(std::move(tab.at(ky)), std::forward(opt)); } // --------------------------------------------------------------------------- // string literal (deduced as std::string) template class M, template class V> detail::enable_if_t< detail::is_string_literal::type>::value, std::string> find_or(const basic_value& v, const toml::key& ky, T&& opt) { if(!v.is_table()) {return std::string(opt);} const auto& tab = v.as_table(); if(tab.count(ky) == 0) {return std::string(opt);} return get_or(tab.at(ky), std::forward(opt)); } // --------------------------------------------------------------------------- // others (require type conversion and return type cannot be lvalue reference) template class M, template class V> detail::enable_if_t, basic_value>>, // T is not std::string detail::negation>>, // T is not a string literal detail::negation::type>> >::value, detail::remove_cvref_t> find_or(const basic_value& v, const toml::key& ky, T&& opt) { if(!v.is_table()) {return std::forward(opt);} const auto& tab = v.as_table(); if(tab.count(ky) == 0) {return std::forward(opt);} return get_or(tab.at(ky), std::forward(opt)); } // --------------------------------------------------------------------------- // recursive find-or with type deduction (find_or(value, keys, opt)) template 1), std::nullptr_t> = nullptr> // here we need to add SFINAE in the template parameter to avoid // infinite recursion in type deduction on gcc auto find_or(Value&& v, const toml::key& ky, Ks&& ... keys) -> decltype(find_or(std::forward(v), ky, detail::last_one(std::forward(keys)...))) { if(!v.is_table()) { return detail::last_one(std::forward(keys)...); } auto&& tab = std::forward(v).as_table(); if(tab.count(ky) == 0) { return detail::last_one(std::forward(keys)...); } return find_or(std::forward(tab).at(ky), std::forward(keys)...); } // --------------------------------------------------------------------------- // recursive find_or with explicit type specialization, find_or(value, keys...) template 1), std::nullptr_t> = nullptr> // here we need to add SFINAE in the template parameter to avoid // infinite recursion in type deduction on gcc auto find_or(Value&& v, const toml::key& ky, Ks&& ... keys) -> decltype(find_or(std::forward(v), ky, detail::last_one(std::forward(keys)...))) { if(!v.is_table()) { return detail::last_one(std::forward(keys)...); } auto&& tab = std::forward(v).as_table(); if(tab.count(ky) == 0) { return detail::last_one(std::forward(keys)...); } return find_or(std::forward(tab).at(ky), std::forward(keys)...); } // ============================================================================ // expect template class M, template class V> result expect(const basic_value& v) noexcept { try { return ok(get(v)); } catch(const std::exception& e) { return err(e.what()); } } template class M, template class V> result expect(const basic_value& v, const toml::key& k) noexcept { try { return ok(find(v, k)); } catch(const std::exception& e) { return err(e.what()); } } } // toml #endif// TOML11_GET