2018-12-13 11:44:10 +00:00
|
|
|
// Copyright Toru Niina 2017.
|
|
|
|
// Distributed under the MIT License.
|
2019-03-16 05:19:47 +00:00
|
|
|
#ifndef TOML11_GET_HPP
|
|
|
|
#define TOML11_GET_HPP
|
2019-03-16 05:44:04 +00:00
|
|
|
#include "from.hpp"
|
2018-12-12 07:11:37 +00:00
|
|
|
#include "result.hpp"
|
2017-04-21 04:14:53 +00:00
|
|
|
#include "value.hpp"
|
2017-05-06 14:55:15 +00:00
|
|
|
#include <algorithm>
|
2017-04-21 04:14:53 +00:00
|
|
|
|
|
|
|
namespace toml
|
|
|
|
{
|
|
|
|
|
2018-12-10 06:58:20 +00:00
|
|
|
// ============================================================================
|
|
|
|
// exact toml::* type
|
|
|
|
|
2019-06-02 08:31:49 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<detail::is_exact_toml_type<T, basic_value<C, M, V>>::value, T> &
|
2019-06-02 08:31:49 +00:00
|
|
|
get(basic_value<C, M, V>& v)
|
2017-04-21 04:14:53 +00:00
|
|
|
{
|
2019-06-02 08:31:49 +00:00
|
|
|
return v.template cast<detail::type_to_enum<T, basic_value<C, M, V>>::value>();
|
2017-04-21 04:14:53 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 08:31:49 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<detail::is_exact_toml_type<T, basic_value<C, M, V>>::value, T> const&
|
2019-06-02 08:31:49 +00:00
|
|
|
get(const basic_value<C, M, V>& v)
|
2018-05-05 02:42:11 +00:00
|
|
|
{
|
2019-06-02 08:31:49 +00:00
|
|
|
return v.template cast<detail::type_to_enum<T, basic_value<C, M, V>>::value>();
|
2018-12-10 06:58:20 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 08:31:49 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<detail::is_exact_toml_type<T, basic_value<C, M, V>>::value, T> &&
|
2019-06-02 08:31:49 +00:00
|
|
|
get(basic_value<C, M, V>&& v)
|
2018-12-10 06:58:20 +00:00
|
|
|
{
|
2019-06-02 13:09:26 +00:00
|
|
|
return std::move(v).template cast<detail::type_to_enum<T, basic_value<C, M, V>>::value>();
|
2018-05-05 02:42:11 +00:00
|
|
|
}
|
|
|
|
|
2018-12-16 11:50:40 +00:00
|
|
|
// ============================================================================
|
|
|
|
// T == toml::value; identity transformation.
|
|
|
|
|
2019-06-02 08:31:49 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
inline detail::enable_if_t<std::is_same<T, basic_value<C, M, V>>::value, T>&
|
2019-06-02 08:31:49 +00:00
|
|
|
get(basic_value<C, M, V>& v)
|
2018-12-16 11:50:40 +00:00
|
|
|
{
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2019-06-02 08:31:49 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
inline detail::enable_if_t<std::is_same<T, basic_value<C, M, V>>::value, T> const&
|
2019-06-02 08:31:49 +00:00
|
|
|
get(const basic_value<C, M, V>& v)
|
2018-12-16 11:50:40 +00:00
|
|
|
{
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2019-06-02 08:31:49 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
inline detail::enable_if_t<std::is_same<T, basic_value<C, M, V>>::value, T> &&
|
2019-06-02 08:31:49 +00:00
|
|
|
get(basic_value<C, M, V>&& v)
|
2018-12-16 11:50:40 +00:00
|
|
|
{
|
|
|
|
return std::move(v);
|
|
|
|
}
|
|
|
|
|
2019-06-02 10:09:56 +00:00
|
|
|
// ============================================================================
|
|
|
|
// T == toml::basic_value<C2, M2, V2>; basic_value -> basic_value
|
|
|
|
|
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-15 11:20:14 +00:00
|
|
|
inline detail::enable_if_t<detail::conjunction<detail::is_basic_value<T>,
|
|
|
|
detail::negation<std::is_same<T, basic_value<C, M, V>>>
|
|
|
|
>::value, T>
|
2019-06-02 10:09:56 +00:00
|
|
|
get(const basic_value<C, M, V>& v)
|
|
|
|
{
|
|
|
|
return T(v);
|
|
|
|
}
|
|
|
|
|
2018-12-10 06:58:20 +00:00
|
|
|
// ============================================================================
|
|
|
|
// integer convertible from toml::Integer
|
|
|
|
|
2019-06-02 08:31:49 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
inline detail::enable_if_t<detail::conjunction<
|
2018-12-10 06:58:20 +00:00
|
|
|
std::is_integral<T>, // T is integral
|
|
|
|
detail::negation<std::is_same<T, bool>>, // but not bool
|
2019-06-02 08:31:49 +00:00
|
|
|
detail::negation< // but not toml::integer
|
|
|
|
detail::is_exact_toml_type<T, basic_value<C, M, V>>>
|
|
|
|
>::value, T>
|
|
|
|
get(const basic_value<C, M, V>& v)
|
2018-05-05 02:42:11 +00:00
|
|
|
{
|
2019-06-02 08:31:49 +00:00
|
|
|
return static_cast<T>(v.template cast<value_t::integer>());
|
2018-05-05 02:42:11 +00:00
|
|
|
}
|
2018-12-10 06:58:20 +00:00
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// floating point convertible from toml::Float
|
|
|
|
|
2019-06-02 08:31:49 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
inline detail::enable_if_t<detail::conjunction<
|
2018-12-10 06:58:20 +00:00
|
|
|
std::is_floating_point<T>, // T is floating_point
|
2019-06-02 08:31:49 +00:00
|
|
|
detail::negation< // but not toml::floating
|
|
|
|
detail::is_exact_toml_type<T, basic_value<C, M, V>>>
|
|
|
|
>::value, T>
|
|
|
|
get(const basic_value<C, M, V>& v)
|
2017-04-21 04:14:53 +00:00
|
|
|
{
|
2019-06-02 08:31:49 +00:00
|
|
|
return static_cast<T>(v.template cast<value_t::floating>());
|
2018-05-05 02:42:11 +00:00
|
|
|
}
|
2017-12-11 03:04:57 +00:00
|
|
|
|
2018-12-10 06:58:20 +00:00
|
|
|
// ============================================================================
|
|
|
|
// std::string; toml uses its own toml::string, but it should be convertible to
|
|
|
|
// std::string seamlessly
|
|
|
|
|
2019-06-02 08:31:49 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
inline detail::enable_if_t<std::is_same<T, std::string>::value, std::string>&
|
2019-06-02 08:31:49 +00:00
|
|
|
get(basic_value<C, M, V>& v)
|
2018-12-10 06:58:20 +00:00
|
|
|
{
|
2019-06-02 08:31:49 +00:00
|
|
|
return v.template cast<value_t::string>().str;
|
2018-12-10 06:58:20 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 08:31:49 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
inline detail::enable_if_t<std::is_same<T, std::string>::value, std::string> const&
|
2019-06-02 08:31:49 +00:00
|
|
|
get(const basic_value<C, M, V>& v)
|
2018-12-10 06:58:20 +00:00
|
|
|
{
|
2019-06-02 08:31:49 +00:00
|
|
|
return v.template cast<value_t::string>().str;
|
2018-12-10 06:58:20 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 08:31:49 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-09-28 01:31:10 +00:00
|
|
|
inline detail::enable_if_t<std::is_same<T, std::string>::value, std::string>&&
|
2019-06-02 08:31:49 +00:00
|
|
|
get(basic_value<C, M, V>&& v)
|
2018-12-10 06:58:20 +00:00
|
|
|
{
|
2019-06-02 08:31:49 +00:00
|
|
|
return std::move(v.template cast<value_t::string>().str);
|
2018-12-10 06:58:20 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 14:24:23 +00:00
|
|
|
// ============================================================================
|
|
|
|
// std::string_view
|
|
|
|
|
|
|
|
#if __cplusplus >= 201703L
|
2019-06-02 08:31:49 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
inline detail::enable_if_t<std::is_same<T, std::string_view>::value, std::string_view>
|
2019-06-02 08:31:49 +00:00
|
|
|
get(const basic_value<C, M, V>& v)
|
2019-04-23 14:24:23 +00:00
|
|
|
{
|
2019-06-02 08:31:49 +00:00
|
|
|
return std::string_view(v.template cast<value_t::string>().str);
|
2019-04-23 14:24:23 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-12-10 13:06:06 +00:00
|
|
|
// ============================================================================
|
|
|
|
// std::chrono::duration from toml::local_time.
|
|
|
|
|
2019-06-02 08:31:49 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
inline detail::enable_if_t<detail::is_chrono_duration<T>::value, T>
|
2019-06-02 08:31:49 +00:00
|
|
|
get(const basic_value<C, M, V>& v)
|
2018-12-10 13:06:06 +00:00
|
|
|
{
|
|
|
|
return std::chrono::duration_cast<T>(
|
2019-06-02 08:31:49 +00:00
|
|
|
std::chrono::nanoseconds(v.template cast<value_t::local_time>()));
|
2018-12-10 13:06:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// std::chrono::system_clock::time_point from toml::datetime variants
|
|
|
|
|
2019-06-02 08:31:49 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
inline detail::enable_if_t<
|
2019-06-02 08:31:49 +00:00
|
|
|
std::is_same<std::chrono::system_clock::time_point, T>::value, T>
|
|
|
|
get(const basic_value<C, M, V>& v)
|
2018-12-10 13:06:06 +00:00
|
|
|
{
|
|
|
|
switch(v.type())
|
|
|
|
{
|
2019-06-02 08:31:49 +00:00
|
|
|
case value_t::local_date:
|
2018-12-10 13:06:06 +00:00
|
|
|
{
|
|
|
|
return std::chrono::system_clock::time_point(
|
2019-06-02 08:31:49 +00:00
|
|
|
v.template cast<value_t::local_date>());
|
2018-12-10 13:06:06 +00:00
|
|
|
}
|
2019-06-02 08:31:49 +00:00
|
|
|
case value_t::local_datetime:
|
2018-12-10 13:06:06 +00:00
|
|
|
{
|
|
|
|
return std::chrono::system_clock::time_point(
|
2019-06-02 08:31:49 +00:00
|
|
|
v.template cast<value_t::local_datetime>());
|
2018-12-10 13:06:06 +00:00
|
|
|
}
|
2019-06-02 08:31:49 +00:00
|
|
|
case value_t::offset_datetime:
|
2018-12-10 13:06:06 +00:00
|
|
|
{
|
|
|
|
return std::chrono::system_clock::time_point(
|
2019-06-02 08:31:49 +00:00
|
|
|
v.template cast<value_t::offset_datetime>());
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
throw type_error(detail::format_underline("[error] toml::value "
|
|
|
|
"bad_cast to std::chrono::system_clock::time_point", {
|
|
|
|
{std::addressof(detail::get_region(v)),
|
|
|
|
concat_to_string("the actual type is ", v.type())}
|
|
|
|
}));
|
2018-12-10 13:06:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-10 06:58:20 +00:00
|
|
|
|
|
|
|
// ============================================================================
|
2018-12-10 13:06:06 +00:00
|
|
|
// forward declaration to use this recursively. ignore this and go ahead.
|
|
|
|
|
2019-06-02 08:53:08 +00:00
|
|
|
// array-like type with resize(N) method
|
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<detail::conjunction<
|
2019-06-02 08:53:08 +00:00
|
|
|
detail::is_container<T>, // T is container
|
|
|
|
detail::has_resize_method<T>, // T::resize(N) works
|
|
|
|
detail::negation< // but not toml::array
|
|
|
|
detail::is_exact_toml_type<T, basic_value<C, M, V>>>
|
|
|
|
>::value, T>
|
|
|
|
get(const basic_value<C, M, V>&);
|
|
|
|
|
|
|
|
// array-like type with resize(N) method
|
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<detail::conjunction<
|
2018-12-10 06:58:20 +00:00
|
|
|
detail::is_container<T>, // T is container
|
|
|
|
detail::negation<detail::has_resize_method<T>>, // no T::resize() exists
|
2019-06-02 08:53:08 +00:00
|
|
|
detail::negation< // not toml::array
|
|
|
|
detail::is_exact_toml_type<T, basic_value<C, M, V>>>
|
|
|
|
>::value, T>
|
|
|
|
get(const basic_value<C, M, V>&);
|
|
|
|
|
|
|
|
// std::pair<T1, T2>
|
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<detail::is_std_pair<T>::value, T>
|
2019-06-02 08:53:08 +00:00
|
|
|
get(const basic_value<C, M, V>&);
|
|
|
|
|
|
|
|
// std::tuple<T1, T2, ...>
|
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<detail::is_std_tuple<T>::value, T>
|
2019-06-02 08:53:08 +00:00
|
|
|
get(const basic_value<C, M, V>&);
|
|
|
|
|
|
|
|
// map-like classes
|
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<detail::conjunction<
|
2019-06-02 08:53:08 +00:00
|
|
|
detail::is_map<T>, // T is map
|
2019-06-02 09:36:49 +00:00
|
|
|
detail::negation< // but not toml::table
|
2019-06-02 08:53:08 +00:00
|
|
|
detail::is_exact_toml_type<T, basic_value<C, M, V>>>
|
|
|
|
>::value, T>
|
|
|
|
get(const basic_value<C, M, V>&);
|
|
|
|
|
|
|
|
// T.from_toml(v)
|
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<detail::conjunction<
|
2019-06-02 08:53:08 +00:00
|
|
|
detail::negation< // not a toml::* type
|
|
|
|
detail::is_exact_toml_type<T, basic_value<C, M, V>>>,
|
|
|
|
detail::has_from_toml_method<T, C, M, V>, // but has from_toml(toml::value)
|
|
|
|
std::is_default_constructible<T> // and default constructible
|
|
|
|
>::value, T>
|
|
|
|
get(const basic_value<C, M, V>&);
|
|
|
|
|
|
|
|
// toml::from<T>::from_toml(v)
|
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V,
|
|
|
|
std::size_t S = sizeof(::toml::into<T>)>
|
|
|
|
T get(const basic_value<C, M, V>&);
|
2019-03-16 06:52:22 +00:00
|
|
|
|
2018-12-10 06:58:20 +00:00
|
|
|
// ============================================================================
|
|
|
|
// array-like types; most likely STL container, like std::vector, etc.
|
|
|
|
|
2019-06-02 08:53:08 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<detail::conjunction<
|
2019-06-02 08:53:08 +00:00
|
|
|
detail::is_container<T>, // T is container
|
|
|
|
detail::has_resize_method<T>, // T::resize(N) works
|
|
|
|
detail::negation< // but not toml::array
|
|
|
|
detail::is_exact_toml_type<T, basic_value<C, M, V>>>
|
|
|
|
>::value, T>
|
|
|
|
get(const basic_value<C, M, V>& v)
|
2018-05-05 02:42:11 +00:00
|
|
|
{
|
2018-12-10 06:58:20 +00:00
|
|
|
using value_type = typename T::value_type;
|
2019-06-02 08:53:08 +00:00
|
|
|
const auto& ar = v.template cast<value_t::array>();
|
|
|
|
T container;
|
|
|
|
container.resize(ar.size());
|
2018-12-10 06:58:20 +00:00
|
|
|
std::transform(ar.cbegin(), ar.cend(), container.begin(),
|
|
|
|
[](const value& x){return ::toml::get<value_type>(x);});
|
|
|
|
return container;
|
2017-04-21 04:14:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 06:58:20 +00:00
|
|
|
// ============================================================================
|
|
|
|
// array-like types; but does not have resize(); most likely std::array.
|
|
|
|
|
2019-06-02 08:53:08 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<detail::conjunction<
|
2018-12-10 06:58:20 +00:00
|
|
|
detail::is_container<T>, // T is container
|
|
|
|
detail::negation<detail::has_resize_method<T>>, // no T::resize() exists
|
2019-06-02 08:53:08 +00:00
|
|
|
detail::negation< // but not toml::array
|
|
|
|
detail::is_exact_toml_type<T, basic_value<C, M, V>>>
|
|
|
|
>::value, T>
|
|
|
|
get(const basic_value<C, M, V>& v)
|
2017-04-21 04:14:53 +00:00
|
|
|
{
|
2018-12-10 06:58:20 +00:00
|
|
|
using value_type = typename T::value_type;
|
2019-06-02 08:53:08 +00:00
|
|
|
const auto& ar = v.template cast<value_t::array>();
|
2018-12-10 06:58:20 +00:00
|
|
|
|
|
|
|
T container;
|
|
|
|
if(ar.size() != container.size())
|
|
|
|
{
|
2018-12-13 07:13:05 +00:00
|
|
|
throw std::out_of_range(detail::format_underline(concat_to_string(
|
2018-12-10 06:58:20 +00:00
|
|
|
"[erorr] toml::get specified container size is ", container.size(),
|
2019-03-13 15:59:10 +00:00
|
|
|
" but there are ", ar.size(), " elements in toml array."), {
|
|
|
|
{std::addressof(detail::get_region(v)), "here"}
|
|
|
|
}));
|
2018-12-10 06:58:20 +00:00
|
|
|
}
|
|
|
|
std::transform(ar.cbegin(), ar.cend(), container.begin(),
|
|
|
|
[](const value& x){return ::toml::get<value_type>(x);});
|
|
|
|
return container;
|
2017-04-21 04:14:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 06:58:20 +00:00
|
|
|
// ============================================================================
|
|
|
|
// std::pair.
|
|
|
|
|
2019-06-02 08:53:08 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<detail::is_std_pair<T>::value, T>
|
2019-06-02 08:53:08 +00:00
|
|
|
get(const basic_value<C, M, V>& v)
|
2018-05-05 02:46:09 +00:00
|
|
|
{
|
|
|
|
using first_type = typename T::first_type;
|
|
|
|
using second_type = typename T::second_type;
|
2018-12-10 06:58:20 +00:00
|
|
|
|
2019-06-02 08:53:08 +00:00
|
|
|
const auto& ar = v.template cast<value_t::array>();
|
2018-05-05 02:46:09 +00:00
|
|
|
if(ar.size() != 2)
|
|
|
|
{
|
2018-12-13 07:13:05 +00:00
|
|
|
throw std::out_of_range(detail::format_underline(concat_to_string(
|
2018-12-10 06:58:20 +00:00
|
|
|
"[erorr] toml::get specified std::pair but there are ", ar.size(),
|
2019-03-13 15:59:10 +00:00
|
|
|
" elements in toml array."), {
|
|
|
|
{std::addressof(detail::get_region(v)), "here"}
|
|
|
|
}));
|
2018-05-05 02:46:09 +00:00
|
|
|
}
|
2018-12-10 06:58:20 +00:00
|
|
|
return std::make_pair(::toml::get<first_type >(ar.at(0)),
|
|
|
|
::toml::get<second_type>(ar.at(1)));
|
2018-05-05 02:46:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 06:58:20 +00:00
|
|
|
// ============================================================================
|
|
|
|
// std::tuple.
|
|
|
|
|
2018-05-05 02:46:09 +00:00
|
|
|
namespace detail
|
|
|
|
{
|
2019-06-02 08:53:08 +00:00
|
|
|
template<typename T, typename Array, std::size_t ... I>
|
|
|
|
T get_tuple_impl(const Array& a, index_sequence<I...>)
|
2018-05-05 02:46:09 +00:00
|
|
|
{
|
|
|
|
return std::make_tuple(
|
|
|
|
::toml::get<typename std::tuple_element<I, T>::type>(a.at(I))...);
|
|
|
|
}
|
|
|
|
} // detail
|
|
|
|
|
2019-06-02 08:53:08 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<detail::is_std_tuple<T>::value, T>
|
2019-06-02 08:53:08 +00:00
|
|
|
get(const basic_value<C, M, V>& v)
|
2018-05-05 02:46:09 +00:00
|
|
|
{
|
2019-06-02 08:53:08 +00:00
|
|
|
const auto& ar = v.template cast<value_t::array>();
|
2018-05-05 02:46:09 +00:00
|
|
|
if(ar.size() != std::tuple_size<T>::value)
|
|
|
|
{
|
2018-12-13 07:13:05 +00:00
|
|
|
throw std::out_of_range(detail::format_underline(concat_to_string(
|
2018-12-10 06:58:20 +00:00
|
|
|
"[erorr] toml::get specified std::tuple with ",
|
|
|
|
std::tuple_size<T>::value, "elements, but there are ", ar.size(),
|
2019-03-13 15:59:10 +00:00
|
|
|
" elements in toml array."), {
|
|
|
|
{std::addressof(detail::get_region(v)), "here"}
|
|
|
|
}));
|
2018-05-05 02:46:09 +00:00
|
|
|
}
|
|
|
|
return detail::get_tuple_impl<T>(ar,
|
|
|
|
detail::make_index_sequence<std::tuple_size<T>::value>{});
|
|
|
|
}
|
|
|
|
|
2018-12-10 06:58:20 +00:00
|
|
|
// ============================================================================
|
|
|
|
// map-like types; most likely STL map, like std::map or std::unordered_map.
|
|
|
|
|
2019-06-02 08:53:08 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<detail::conjunction<
|
2019-06-02 08:53:08 +00:00
|
|
|
detail::is_map<T>, // T is map
|
|
|
|
detail::negation< // but not toml::array
|
|
|
|
detail::is_exact_toml_type<T, basic_value<C, M, V>>>
|
|
|
|
>::value, T>
|
|
|
|
get(const basic_value<C, M, V>& v)
|
2018-12-10 06:58:20 +00:00
|
|
|
{
|
|
|
|
using key_type = typename T::key_type;
|
|
|
|
using mapped_type = typename T::mapped_type;
|
|
|
|
static_assert(std::is_convertible<std::string, key_type>::value,
|
|
|
|
"toml::get only supports map type of which key_type is "
|
|
|
|
"convertible from std::string.");
|
|
|
|
T map;
|
2019-06-02 08:53:08 +00:00
|
|
|
for(const auto& kv : v.template cast<value_t::table>())
|
2018-12-10 06:58:20 +00:00
|
|
|
{
|
|
|
|
map[key_type(kv.first)] = ::toml::get<mapped_type>(kv.second);
|
|
|
|
}
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2019-03-16 05:44:04 +00:00
|
|
|
// ============================================================================
|
|
|
|
// user-defined, but compatible types.
|
|
|
|
|
2019-06-02 08:53:08 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<detail::conjunction<
|
2019-06-02 08:53:08 +00:00
|
|
|
detail::negation< // not a toml::* type
|
|
|
|
detail::is_exact_toml_type<T, basic_value<C, M, V>>>,
|
|
|
|
detail::has_from_toml_method<T, C, M, V>, // but has from_toml(toml::value) memfn
|
|
|
|
std::is_default_constructible<T> // and default constructible
|
|
|
|
>::value, T>
|
|
|
|
get(const basic_value<C, M, V>& v)
|
2019-03-16 05:44:04 +00:00
|
|
|
{
|
|
|
|
T ud;
|
|
|
|
ud.from_toml(v);
|
|
|
|
return ud;
|
|
|
|
}
|
2019-06-02 08:53:08 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V,
|
|
|
|
std::size_t>
|
|
|
|
T get(const basic_value<C, M, V>& v)
|
2019-03-16 05:44:04 +00:00
|
|
|
{
|
|
|
|
return ::toml::from<T>::from_toml(v);
|
|
|
|
}
|
|
|
|
|
2018-12-12 08:55:34 +00:00
|
|
|
// ============================================================================
|
|
|
|
// find and get
|
|
|
|
|
2019-06-02 09:36:49 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// these overloads do not require to set T. and returns value itself.
|
|
|
|
template<typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
|
|
|
basic_value<C, M, V> const& find(const basic_value<C, M, V>& v, const key& ky)
|
|
|
|
{
|
|
|
|
const auto& tab = v.template cast<value_t::table>();
|
|
|
|
if(tab.count(ky) == 0)
|
|
|
|
{
|
|
|
|
throw std::out_of_range(detail::format_underline(concat_to_string(
|
|
|
|
"[error] key \"", ky, "\" not found"), {
|
|
|
|
{std::addressof(detail::get_region(v)), "in this table"}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
return tab.at(ky);
|
|
|
|
}
|
|
|
|
template<typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
|
|
|
basic_value<C, M, V>& find(basic_value<C, M, V>& v, const key& ky)
|
2018-12-12 08:55:34 +00:00
|
|
|
{
|
2019-06-08 10:53:50 +00:00
|
|
|
auto& tab = v.template cast<value_t::table>();
|
2018-12-12 08:55:34 +00:00
|
|
|
if(tab.count(ky) == 0)
|
|
|
|
{
|
2019-06-02 09:36:49 +00:00
|
|
|
throw std::out_of_range(detail::format_underline(concat_to_string(
|
|
|
|
"[error] key \"", ky, "\" not found"), {
|
|
|
|
{std::addressof(detail::get_region(v)), "in this table"}
|
|
|
|
}));
|
2018-12-12 08:55:34 +00:00
|
|
|
}
|
2019-06-02 09:36:49 +00:00
|
|
|
return tab.at(ky);
|
2018-12-12 08:55:34 +00:00
|
|
|
}
|
2019-06-02 09:36:49 +00:00
|
|
|
template<typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
|
|
|
basic_value<C, M, V>&& find(basic_value<C, M, V>&& v, const key& ky)
|
2018-12-12 08:55:34 +00:00
|
|
|
{
|
2019-06-02 09:36:49 +00:00
|
|
|
auto& tab = v.template cast<value_t::table>();
|
2018-12-12 08:55:34 +00:00
|
|
|
if(tab.count(ky) == 0)
|
|
|
|
{
|
2019-06-02 09:36:49 +00:00
|
|
|
throw std::out_of_range(detail::format_underline(concat_to_string(
|
|
|
|
"[error] key \"", ky, "\" not found"), {
|
|
|
|
{std::addressof(detail::get_region(v)), "in this table"}
|
|
|
|
}));
|
2018-12-12 08:55:34 +00:00
|
|
|
}
|
2019-06-17 03:13:59 +00:00
|
|
|
return std::move(tab.at(ky));
|
2018-12-12 08:55:34 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 09:36:49 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// find<T>(value, key);
|
|
|
|
|
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
|
|
|
decltype(::toml::get<T>(std::declval<basic_value<C, M, V> const&>()))
|
|
|
|
find(const basic_value<C, M, V>& v, const key& ky)
|
2018-12-12 08:55:34 +00:00
|
|
|
{
|
2019-06-02 09:36:49 +00:00
|
|
|
const auto& tab = v.template cast<value_t::table>();
|
2018-12-12 08:55:34 +00:00
|
|
|
if(tab.count(ky) == 0)
|
|
|
|
{
|
2018-12-13 07:13:05 +00:00
|
|
|
throw std::out_of_range(detail::format_underline(concat_to_string(
|
2019-03-13 15:59:10 +00:00
|
|
|
"[error] key \"", ky, "\" not found"), {
|
|
|
|
{std::addressof(detail::get_region(v)), "in this table"}
|
|
|
|
}));
|
2018-12-12 08:55:34 +00:00
|
|
|
}
|
|
|
|
return ::toml::get<T>(tab.at(ky));
|
|
|
|
}
|
2019-06-02 09:36:49 +00:00
|
|
|
|
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
|
|
|
decltype(::toml::get<T>(std::declval<basic_value<C, M, V>&>()))
|
|
|
|
find(basic_value<C, M, V>& v, const key& ky)
|
2018-12-12 08:55:34 +00:00
|
|
|
{
|
2019-06-02 13:09:26 +00:00
|
|
|
auto& tab = v.template cast<value_t::table>();
|
2018-12-12 08:55:34 +00:00
|
|
|
if(tab.count(ky) == 0)
|
|
|
|
{
|
2018-12-13 07:13:05 +00:00
|
|
|
throw std::out_of_range(detail::format_underline(concat_to_string(
|
2019-03-13 15:59:10 +00:00
|
|
|
"[error] key \"", ky, "\" not found"), {
|
|
|
|
{std::addressof(detail::get_region(v)), "in this table"}
|
|
|
|
}));
|
2018-12-12 08:55:34 +00:00
|
|
|
}
|
|
|
|
return ::toml::get<T>(tab.at(ky));
|
|
|
|
}
|
2019-06-02 09:36:49 +00:00
|
|
|
|
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
|
|
|
decltype(::toml::get<T>(std::declval<basic_value<C, M, V>&&>()))
|
|
|
|
find(basic_value<C, M, V>&& v, const key& ky)
|
2018-12-12 08:55:34 +00:00
|
|
|
{
|
2019-06-02 09:36:49 +00:00
|
|
|
auto& tab = v.template cast<value_t::table>();
|
2018-12-12 08:55:34 +00:00
|
|
|
if(tab.count(ky) == 0)
|
|
|
|
{
|
2018-12-13 07:13:05 +00:00
|
|
|
throw std::out_of_range(detail::format_underline(concat_to_string(
|
2019-03-13 15:59:10 +00:00
|
|
|
"[error] key \"", ky, "\" not found"), {
|
|
|
|
{std::addressof(detail::get_region(v)), "in this table"}
|
|
|
|
}));
|
2018-12-12 08:55:34 +00:00
|
|
|
}
|
2019-06-02 09:36:49 +00:00
|
|
|
return ::toml::get<T>(std::move(tab.at(ky)));
|
2018-12-12 08:55:34 +00:00
|
|
|
}
|
|
|
|
|
2019-06-06 15:06:14 +00:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// toml::find(toml::value, toml::key, Ts&& ... keys)
|
2019-06-08 10:40:11 +00:00
|
|
|
|
|
|
|
template<typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V,
|
|
|
|
typename ... Ts>
|
|
|
|
const basic_value<C, M, V>&
|
|
|
|
find(const basic_value<C, M, V>& v, const ::toml::key& ky, Ts&& ... keys)
|
|
|
|
{
|
|
|
|
return ::toml::find(::toml::find(v, ky), std::forward<Ts>(keys)...);
|
|
|
|
}
|
|
|
|
template<typename C,
|
2019-06-08 10:53:50 +00:00
|
|
|
template<typename ...> class M, template<typename ...> class V,
|
|
|
|
typename ... Ts>
|
2019-06-08 10:40:11 +00:00
|
|
|
basic_value<C, M, V>&
|
|
|
|
find(basic_value<C, M, V>& v, const ::toml::key& ky, Ts&& ... keys)
|
|
|
|
{
|
|
|
|
return ::toml::find(::toml::find(v, ky), std::forward<Ts>(keys)...);
|
|
|
|
}
|
|
|
|
template<typename C,
|
2019-06-08 10:53:50 +00:00
|
|
|
template<typename ...> class M, template<typename ...> class V,
|
|
|
|
typename ... Ts>
|
2019-06-08 10:40:11 +00:00
|
|
|
basic_value<C, M, V>&&
|
|
|
|
find(basic_value<C, M, V>&& v, const ::toml::key& ky, Ts&& ... keys)
|
|
|
|
{
|
|
|
|
return ::toml::find(::toml::find(std::move(v), ky), std::forward<Ts>(keys)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V,
|
|
|
|
typename ... Ts>
|
|
|
|
decltype(::toml::get<T>(std::declval<const basic_value<C, M, V>&>()))
|
|
|
|
find(const basic_value<C, M, V>& v, const ::toml::key& ky, Ts&& ... keys)
|
2019-06-06 15:06:14 +00:00
|
|
|
{
|
|
|
|
return ::toml::find<T>(::toml::find(v, ky), std::forward<Ts>(keys)...);
|
|
|
|
}
|
2019-06-08 10:40:11 +00:00
|
|
|
template<typename T, typename C,
|
2019-06-08 10:53:50 +00:00
|
|
|
template<typename ...> class M, template<typename ...> class V,
|
|
|
|
typename ... Ts>
|
2019-06-08 10:40:11 +00:00
|
|
|
decltype(::toml::get<T>(std::declval<basic_value<C, M, V>&>()))
|
|
|
|
find(basic_value<C, M, V>& v, const ::toml::key& ky, Ts&& ... keys)
|
2019-06-06 15:06:14 +00:00
|
|
|
{
|
|
|
|
return ::toml::find<T>(::toml::find(v, ky), std::forward<Ts>(keys)...);
|
|
|
|
}
|
2019-06-08 10:40:11 +00:00
|
|
|
template<typename T, typename C,
|
2019-06-08 10:53:50 +00:00
|
|
|
template<typename ...> class M, template<typename ...> class V,
|
|
|
|
typename ... Ts>
|
2019-06-08 10:40:11 +00:00
|
|
|
decltype(::toml::get<T>(std::declval<basic_value<C, M, V>&&>()))
|
|
|
|
find(basic_value<C, M, V>&& v, const ::toml::key& ky, Ts&& ... keys)
|
2019-06-06 15:06:14 +00:00
|
|
|
{
|
|
|
|
return ::toml::find<T>(::toml::find(std::move(v), ky), std::forward<Ts>(keys)...);
|
|
|
|
}
|
2018-12-12 08:55:34 +00:00
|
|
|
|
2018-12-10 06:58:20 +00:00
|
|
|
// ============================================================================
|
2019-03-18 07:44:36 +00:00
|
|
|
// get_or(value, fallback)
|
2017-12-11 03:04:57 +00:00
|
|
|
|
2019-08-07 05:55:30 +00:00
|
|
|
template<typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
|
|
|
basic_value<C, M, V> const&
|
|
|
|
get_or(const basic_value<C, M, V>& v, const basic_value<C, M, V>&)
|
|
|
|
{
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
template<typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
|
|
|
basic_value<C, M, V>&
|
|
|
|
get_or(basic_value<C, M, V>& v, basic_value<C, M, V>&)
|
|
|
|
{
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
template<typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
|
|
|
basic_value<C, M, V>
|
|
|
|
get_or(basic_value<C, M, V>&& v, basic_value<C, M, V>&&)
|
|
|
|
{
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2019-03-18 07:44:36 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// specialization for the exact toml types (return type becomes lvalue ref)
|
|
|
|
|
2019-06-02 09:47:30 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<
|
|
|
|
detail::is_exact_toml_type<T, basic_value<C, M, V>>::value, T> const&
|
2019-06-02 09:47:30 +00:00
|
|
|
get_or(const basic_value<C, M, V>& v, const T& opt)
|
2018-12-12 08:55:34 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-09-28 02:03:46 +00:00
|
|
|
return get<detail::remove_cvref_t<T>>::type>(v);
|
2018-12-12 08:55:34 +00:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
2019-03-18 07:44:36 +00:00
|
|
|
return opt;
|
2018-12-12 08:55:34 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-02 09:47:30 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<
|
|
|
|
detail::is_exact_toml_type<T, basic_value<C, M, V>>::value, T>&
|
2019-06-02 09:47:30 +00:00
|
|
|
get_or(basic_value<C, M, V>& v, T& opt)
|
2018-12-12 08:55:34 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-09-28 02:03:46 +00:00
|
|
|
return get<detail::remove_cvref_t<T>>::type>(v);
|
2018-12-12 08:55:34 +00:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
2019-03-18 07:44:36 +00:00
|
|
|
return opt;
|
2018-12-12 08:55:34 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-02 09:47:30 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-09-28 02:03:46 +00:00
|
|
|
detail::enable_if_t<detail::is_exact_toml_type<detail::remove_cvref_t<T>,
|
|
|
|
basic_value<C, M, V>>::value, detail::remove_cvref_t<T>>
|
2019-06-02 09:47:30 +00:00
|
|
|
get_or(basic_value<C, M, V>&& v, T&& opt)
|
2018-12-12 08:55:34 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-09-28 02:03:46 +00:00
|
|
|
return get<detail::remove_cvref_t<T>>(std::move(v));
|
2019-03-18 07:44:36 +00:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
2019-09-28 02:03:46 +00:00
|
|
|
return detail::remove_cvref_t<T>(std::forward<T>(opt));
|
2019-03-18 07:44:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// specialization for std::string (return type becomes lvalue ref)
|
|
|
|
|
2019-06-02 09:47:30 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-09-28 02:03:46 +00:00
|
|
|
detail::enable_if_t<std::is_same<detail::remove_cvref_t<T>, std::string>::value,
|
|
|
|
std::string> const&
|
|
|
|
get_or(const basic_value<C, M, V>& v, const T& opt)
|
2019-03-18 07:44:36 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-06-02 09:47:30 +00:00
|
|
|
return v.template cast<value_t::string>().str;
|
2019-03-18 07:44:36 +00:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
return opt;
|
|
|
|
}
|
|
|
|
}
|
2019-06-02 09:47:30 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-03 12:44:11 +00:00
|
|
|
detail::enable_if_t<std::is_same<T, std::string>::value, std::string>&
|
2019-06-02 09:47:30 +00:00
|
|
|
get_or(basic_value<C, M, V>& v, T& opt)
|
2019-03-18 07:44:36 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-06-02 09:47:30 +00:00
|
|
|
return v.template cast<value_t::string>().str;
|
2019-03-18 07:44:36 +00:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
return opt;
|
|
|
|
}
|
|
|
|
}
|
2019-06-02 09:47:30 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-09-28 02:03:46 +00:00
|
|
|
detail::enable_if_t<
|
|
|
|
std::is_same<detail::remove_cvref_t<T>, std::string>::value, std::string>
|
2019-06-02 09:47:30 +00:00
|
|
|
get_or(basic_value<C, M, V>&& v, T&& opt)
|
2019-03-18 07:44:36 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-06-02 09:47:30 +00:00
|
|
|
return std::move(v.template cast<value_t::string>().str);
|
2019-03-18 07:44:36 +00:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
2019-09-28 02:03:46 +00:00
|
|
|
return std::string(std::forward<T>(opt));
|
2019-03-18 07:44:36 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-02 09:47:30 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// specialization for string literal
|
|
|
|
|
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<detail::is_string_literal<
|
2019-06-02 09:47:30 +00:00
|
|
|
typename std::remove_reference<T>::type>::value, std::string>
|
2019-06-03 12:44:11 +00:00
|
|
|
get_or(const basic_value<C, M, V>& v, T&& opt)
|
2019-03-18 07:44:36 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-06-02 09:47:30 +00:00
|
|
|
return std::move(v.template cast<value_t::string>().str);
|
2018-12-12 08:55:34 +00:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
2019-03-18 07:44:36 +00:00
|
|
|
return std::string(opt);
|
2018-12-12 08:55:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 07:44:36 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// others (require type conversion and return type cannot be lvalue reference)
|
|
|
|
|
2019-06-02 09:47:30 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<detail::conjunction<
|
2019-09-28 02:03:46 +00:00
|
|
|
detail::negation<detail::is_exact_toml_type<detail::remove_cvref_t<T>,
|
2019-06-19 10:53:08 +00:00
|
|
|
basic_value<C, M, V>>>,
|
2019-09-28 02:03:46 +00:00
|
|
|
detail::negation<std::is_same<std::string, detail::remove_cvref_t<T>>>,
|
2019-06-19 09:59:12 +00:00
|
|
|
detail::negation<detail::is_string_literal<
|
|
|
|
typename std::remove_reference<T>::type>>
|
2019-09-28 02:03:46 +00:00
|
|
|
>::value, detail::remove_cvref_t<T>>
|
2019-06-02 09:47:30 +00:00
|
|
|
get_or(const basic_value<C, M, V>& v, T&& opt)
|
2019-03-18 07:44:36 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-09-28 02:03:46 +00:00
|
|
|
return get<detail::remove_cvref_t<T>>(v);
|
2019-03-18 07:44:36 +00:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
2019-06-03 12:44:11 +00:00
|
|
|
return std::forward<T>(opt);
|
2019-03-18 07:44:36 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-12 16:29:23 +00:00
|
|
|
|
2019-03-18 08:44:03 +00:00
|
|
|
// ===========================================================================
|
|
|
|
// find_or(value, key, fallback)
|
|
|
|
|
2019-08-07 05:55:30 +00:00
|
|
|
template<typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
|
|
|
basic_value<C, M, V> const&
|
|
|
|
find_or(const basic_value<C, M, V>& v, const key& ky,
|
|
|
|
const basic_value<C, M, V>& 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<typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
|
|
|
basic_value<C, M, V>&
|
|
|
|
find_or(basic_value<C, M, V>& v, const toml::key& ky, basic_value<C, M, V>& opt)
|
|
|
|
{
|
|
|
|
if(!v.is_table()) {return opt;}
|
|
|
|
auto& tab = v.as_table();
|
|
|
|
if(tab.count(ky) == 0) {return opt;}
|
2019-09-28 02:03:14 +00:00
|
|
|
return tab.at(ky);
|
2019-08-07 05:55:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
|
|
|
basic_value<C, M, V>
|
|
|
|
find_or(basic_value<C, M, V>&& v, const toml::key& ky, basic_value<C, M, V>&& opt)
|
|
|
|
{
|
|
|
|
if(!v.is_table()) {return opt;}
|
|
|
|
auto tab = std::move(v).as_table();
|
|
|
|
if(tab.count(ky) == 0) {return opt;}
|
2019-09-28 02:03:14 +00:00
|
|
|
return std::move(tab.at(ky));
|
2019-08-07 05:55:30 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 08:44:03 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// exact types (return type can be a reference)
|
2019-06-02 10:22:17 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<
|
|
|
|
detail::is_exact_toml_type<T, basic_value<C, M, V>>::value, T> const&
|
2019-06-02 10:22:17 +00:00
|
|
|
find_or(const basic_value<C, M, V>& v, const key& ky, const T& opt)
|
2019-03-18 08:44:03 +00:00
|
|
|
{
|
|
|
|
if(!v.is_table()) {return opt;}
|
2019-06-02 10:22:17 +00:00
|
|
|
const auto& tab = v.as_table();
|
2019-03-18 08:44:03 +00:00
|
|
|
if(tab.count(ky) == 0) {return opt;}
|
|
|
|
return get_or(tab.at(ky), opt);
|
|
|
|
}
|
|
|
|
|
2019-06-02 10:22:17 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<
|
|
|
|
detail::is_exact_toml_type<T, basic_value<C, M, V>>::value, T>&
|
2019-06-02 10:22:17 +00:00
|
|
|
find_or(basic_value<C, M, V>& v, const toml::key& ky, T& opt)
|
2019-03-18 08:44:03 +00:00
|
|
|
{
|
|
|
|
if(!v.is_table()) {return opt;}
|
2019-06-02 10:22:17 +00:00
|
|
|
auto& tab = v.as_table();
|
2019-03-18 08:44:03 +00:00
|
|
|
if(tab.count(ky) == 0) {return opt;}
|
2019-09-28 02:03:14 +00:00
|
|
|
return get_or(tab.at(ky), opt);
|
2019-03-18 08:44:03 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 10:22:17 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<
|
|
|
|
detail::is_exact_toml_type<T, basic_value<C, M, V>>::value, T>&&
|
2019-06-02 10:22:17 +00:00
|
|
|
find_or(basic_value<C, M, V>&& v, const toml::key& ky, T&& opt)
|
2019-03-18 08:44:03 +00:00
|
|
|
{
|
|
|
|
if(!v.is_table()) {return opt;}
|
2019-06-02 10:22:17 +00:00
|
|
|
auto tab = std::move(v).as_table();
|
2019-03-18 08:44:03 +00:00
|
|
|
if(tab.count(ky) == 0) {return opt;}
|
2019-09-28 02:03:14 +00:00
|
|
|
return get_or(std::move(tab.at(ky)), std::forward<T>(opt));
|
2019-03-18 08:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// std::string (return type can be a reference)
|
2019-06-02 10:22:17 +00:00
|
|
|
|
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<std::is_same<T, std::string>::value, std::string> const&
|
2019-06-02 10:22:17 +00:00
|
|
|
find_or(const basic_value<C, M, V>& v, const key& ky, const T& opt)
|
2019-03-18 08:44:03 +00:00
|
|
|
{
|
|
|
|
if(!v.is_table()) {return opt;}
|
2019-06-02 10:22:17 +00:00
|
|
|
const auto& tab = v.as_table();
|
2019-03-18 08:44:03 +00:00
|
|
|
if(tab.count(ky) == 0) {return opt;}
|
|
|
|
return get_or(tab.at(ky), opt);
|
|
|
|
}
|
2019-06-02 10:22:17 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<std::is_same<T, std::string>::value, std::string>&
|
2019-06-02 10:22:17 +00:00
|
|
|
find_or(basic_value<C, M, V>& v, const toml::key& ky, T& opt)
|
2019-03-18 08:44:03 +00:00
|
|
|
{
|
|
|
|
if(!v.is_table()) {return opt;}
|
2019-06-02 10:22:17 +00:00
|
|
|
auto& tab = v.as_table();
|
2019-03-18 08:44:03 +00:00
|
|
|
if(tab.count(ky) == 0) {return opt;}
|
2019-06-02 10:22:17 +00:00
|
|
|
return get_or(tab.at(ky), opt);
|
2019-03-18 08:44:03 +00:00
|
|
|
}
|
2019-06-02 10:22:17 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<std::is_same<T, std::string>::value, std::string>
|
2019-06-02 10:22:17 +00:00
|
|
|
find_or(basic_value<C, M, V>&& v, const toml::key& ky, T&& opt)
|
2019-03-18 08:44:03 +00:00
|
|
|
{
|
2019-06-09 12:05:46 +00:00
|
|
|
if(!v.is_table()) {return std::forward<T>(opt);}
|
2019-06-19 10:53:08 +00:00
|
|
|
auto tab = std::move(v).as_table();
|
2019-06-09 12:05:46 +00:00
|
|
|
if(tab.count(ky) == 0) {return std::forward<T>(opt);}
|
2019-06-17 13:40:52 +00:00
|
|
|
return get_or(std::move(tab.at(ky)), std::forward<T>(opt));
|
2019-03-18 08:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// string literal (deduced as std::string)
|
2019-06-02 10:22:17 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<
|
2019-03-18 08:44:03 +00:00
|
|
|
detail::is_string_literal<typename std::remove_reference<T>::type>::value,
|
2019-06-02 10:22:17 +00:00
|
|
|
std::string>
|
|
|
|
find_or(const basic_value<C, M, V>& v, const toml::key& ky, T&& opt)
|
2019-03-18 08:44:03 +00:00
|
|
|
{
|
2019-08-31 04:04:25 +00:00
|
|
|
if(!v.is_table()) {return std::string(opt);}
|
2019-06-02 10:22:17 +00:00
|
|
|
const auto& tab = v.as_table();
|
2019-03-18 08:44:03 +00:00
|
|
|
if(tab.count(ky) == 0) {return std::string(opt);}
|
|
|
|
return get_or(tab.at(ky), std::forward<T>(opt));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// others (require type conversion and return type cannot be lvalue reference)
|
2019-06-02 10:22:17 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
2019-06-02 11:43:08 +00:00
|
|
|
detail::enable_if_t<detail::conjunction<
|
2019-08-31 04:04:25 +00:00
|
|
|
// T is not an exact toml type
|
2019-06-19 09:59:12 +00:00
|
|
|
detail::negation<detail::is_exact_toml_type<
|
2019-09-28 02:03:46 +00:00
|
|
|
detail::remove_cvref_t<T>, basic_value<C, M, V>>>,
|
2019-08-31 04:04:25 +00:00
|
|
|
// T is not std::string
|
2019-09-28 02:03:46 +00:00
|
|
|
detail::negation<std::is_same<std::string, detail::remove_cvref_t<T>>>,
|
2019-08-31 04:04:25 +00:00
|
|
|
// T is not a string literal
|
2019-06-19 09:59:12 +00:00
|
|
|
detail::negation<detail::is_string_literal<
|
|
|
|
typename std::remove_reference<T>::type>>
|
2019-09-28 02:03:46 +00:00
|
|
|
>::value, detail::remove_cvref_t<T>>
|
2019-06-02 10:22:17 +00:00
|
|
|
find_or(const basic_value<C, M, V>& v, const toml::key& ky, T&& opt)
|
2019-03-18 08:44:03 +00:00
|
|
|
{
|
2019-08-31 04:04:25 +00:00
|
|
|
if(!v.is_table()) {return std::forward<T>(opt);}
|
2019-06-02 10:22:17 +00:00
|
|
|
const auto& tab = v.as_table();
|
2019-08-31 04:04:25 +00:00
|
|
|
if(tab.count(ky) == 0) {return std::forward<T>(opt);}
|
2019-03-18 08:44:03 +00:00
|
|
|
return get_or(tab.at(ky), std::forward<T>(opt));
|
|
|
|
}
|
|
|
|
|
2018-12-11 02:35:35 +00:00
|
|
|
// ============================================================================
|
|
|
|
// expect
|
|
|
|
|
2019-06-02 10:02:25 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
|
|
|
result<T, std::string> expect(const basic_value<C, M, V>& v) noexcept
|
2018-12-11 02:35:35 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return ok(get<T>(v));
|
|
|
|
}
|
2018-12-12 08:55:34 +00:00
|
|
|
catch(const std::exception& e)
|
|
|
|
{
|
|
|
|
return err(e.what());
|
|
|
|
}
|
|
|
|
}
|
2019-06-02 10:02:25 +00:00
|
|
|
template<typename T, typename C,
|
|
|
|
template<typename ...> class M, template<typename ...> class V>
|
|
|
|
result<T, std::string>
|
|
|
|
expect(const basic_value<C, M, V>& v, const toml::key& k) noexcept
|
2018-12-12 08:55:34 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2018-12-17 01:57:40 +00:00
|
|
|
return ok(find<T>(v, k));
|
2018-12-12 08:55:34 +00:00
|
|
|
}
|
|
|
|
catch(const std::exception& e)
|
|
|
|
{
|
|
|
|
return err(e.what());
|
|
|
|
}
|
|
|
|
}
|
2019-09-28 02:00:14 +00:00
|
|
|
|
2017-04-21 04:14:53 +00:00
|
|
|
} // toml
|
|
|
|
#endif// TOML11_GET
|