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
|
|
|
|
|
2018-05-05 02:42:11 +00:00
|
|
|
template<typename T, typename std::enable_if<
|
|
|
|
detail::is_exact_toml_type<T>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
inline T& get(value& v)
|
2017-04-21 04:14:53 +00:00
|
|
|
{
|
2018-12-10 06:58:20 +00:00
|
|
|
return v.cast<detail::toml_value_t<T>::value>();
|
2017-04-21 04:14:53 +00:00
|
|
|
}
|
|
|
|
|
2018-05-05 02:42:11 +00:00
|
|
|
template<typename T, typename std::enable_if<
|
|
|
|
detail::is_exact_toml_type<T>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
inline T const& get(const value& v)
|
|
|
|
{
|
2018-12-10 06:58:20 +00:00
|
|
|
return v.cast<detail::toml_value_t<T>::value>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename std::enable_if<
|
|
|
|
detail::is_exact_toml_type<T>::value, std::nullptr_t>::type = nullptr>
|
2018-12-12 08:22:41 +00:00
|
|
|
inline T&& get(value&& v)
|
2018-12-10 06:58:20 +00:00
|
|
|
{
|
2018-12-12 08:22:41 +00:00
|
|
|
return std::move(v.cast<detail::toml_value_t<T>::value>());
|
2018-05-05 02:42:11 +00:00
|
|
|
}
|
|
|
|
|
2018-12-16 11:50:40 +00:00
|
|
|
// ============================================================================
|
|
|
|
// T == toml::value; identity transformation.
|
|
|
|
|
|
|
|
template<typename T, typename std::enable_if<
|
|
|
|
std::is_same<T, ::toml::value>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
inline T& get(value& v)
|
|
|
|
{
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename std::enable_if<
|
|
|
|
std::is_same<T, ::toml::value>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
inline T const& get(const value& v)
|
|
|
|
{
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename std::enable_if<
|
|
|
|
std::is_same<T, ::toml::value>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
inline T&& get(value&& v)
|
|
|
|
{
|
|
|
|
return std::move(v);
|
|
|
|
}
|
|
|
|
|
2018-12-10 06:58:20 +00:00
|
|
|
// ============================================================================
|
|
|
|
// integer convertible from toml::Integer
|
|
|
|
|
2018-05-05 02:42:11 +00:00
|
|
|
template<typename T, typename std::enable_if<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
|
|
|
|
detail::negation<detail::is_exact_toml_type<T>> // but not toml::integer
|
2018-05-05 02:42:11 +00:00
|
|
|
>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
inline T get(const value& v)
|
|
|
|
{
|
|
|
|
return static_cast<T>(v.cast<value_t::Integer>());
|
|
|
|
}
|
2018-12-10 06:58:20 +00:00
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// floating point convertible from toml::Float
|
|
|
|
|
2018-05-05 02:42:11 +00:00
|
|
|
template<typename T, typename std::enable_if<detail::conjunction<
|
2018-12-10 06:58:20 +00:00
|
|
|
std::is_floating_point<T>, // T is floating_point
|
|
|
|
detail::negation<detail::is_exact_toml_type<T>> // but not toml::Float
|
2018-05-05 02:42:11 +00:00
|
|
|
>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
inline T get(const value& v)
|
2017-04-21 04:14:53 +00:00
|
|
|
{
|
2018-05-05 02:42:11 +00:00
|
|
|
return static_cast<T>(v.cast<value_t::Float>());
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
template<typename T, typename std::enable_if<
|
|
|
|
std::is_same<T, std::string>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
inline std::string& get(value& v)
|
|
|
|
{
|
|
|
|
return v.cast<value_t::String>().str;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename std::enable_if<
|
|
|
|
std::is_same<T, std::string>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
inline std::string const& get(const value& v)
|
|
|
|
{
|
|
|
|
return v.cast<value_t::String>().str;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename std::enable_if<
|
|
|
|
std::is_same<T, std::string>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
inline std::string get(value&& v)
|
|
|
|
{
|
|
|
|
return std::move(v.cast<value_t::String>().str);
|
|
|
|
}
|
|
|
|
|
2018-12-10 13:06:06 +00:00
|
|
|
// ============================================================================
|
|
|
|
// std::chrono::duration from toml::local_time.
|
|
|
|
|
|
|
|
template<typename T, typename std::enable_if<
|
|
|
|
detail::is_chrono_duration<T>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
inline T get(value& v)
|
|
|
|
{
|
|
|
|
return std::chrono::duration_cast<T>(
|
2018-12-13 03:49:53 +00:00
|
|
|
std::chrono::nanoseconds(v.cast<value_t::LocalTime>()));
|
2018-12-10 13:06:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// std::chrono::system_clock::time_point from toml::datetime variants
|
|
|
|
|
|
|
|
template<typename T, typename std::enable_if<
|
|
|
|
std::is_same<std::chrono::system_clock::time_point, T>::value,
|
|
|
|
std::nullptr_t>::type = nullptr>
|
|
|
|
inline T get(value& v)
|
|
|
|
{
|
|
|
|
switch(v.type())
|
|
|
|
{
|
|
|
|
case value_t::LocalDate:
|
|
|
|
{
|
|
|
|
return std::chrono::system_clock::time_point(
|
|
|
|
v.cast<value_t::LocalDate>());
|
|
|
|
}
|
|
|
|
case value_t::LocalDatetime:
|
|
|
|
{
|
|
|
|
return std::chrono::system_clock::time_point(
|
|
|
|
v.cast<value_t::LocalDatetime>());
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
return std::chrono::system_clock::time_point(
|
|
|
|
v.cast<value_t::OffsetDatetime>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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.
|
|
|
|
|
2018-12-10 06:58:20 +00:00
|
|
|
template<typename T, typename std::enable_if<detail::conjunction<
|
|
|
|
detail::is_container<T>, // T is container
|
|
|
|
detail::has_resize_method<T>, // T::resize(N) works
|
|
|
|
detail::negation<detail::is_exact_toml_type<T>> // but not toml::array
|
|
|
|
>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
T get(const value& v);
|
|
|
|
template<typename T, typename std::enable_if<detail::conjunction<
|
|
|
|
detail::is_container<T>, // T is container
|
|
|
|
detail::negation<detail::has_resize_method<T>>, // no T::resize() exists
|
|
|
|
detail::negation<detail::is_exact_toml_type<T>> // not toml::array
|
|
|
|
>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
T get(const value& v);
|
|
|
|
template<typename T, typename std::enable_if<
|
|
|
|
detail::is_std_pair<T>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
T get(const value& v);
|
|
|
|
template<typename T, typename std::enable_if<
|
|
|
|
detail::is_std_tuple<T>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
T get(const value& v);
|
|
|
|
template<typename T, typename std::enable_if<detail::conjunction<
|
|
|
|
detail::is_map<T>, // T is map
|
|
|
|
detail::negation<detail::is_exact_toml_type<T>> // but not toml::table
|
|
|
|
>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
T get(const toml::value& v);
|
|
|
|
|
2019-03-16 06:52:22 +00:00
|
|
|
template<typename T, typename std::enable_if<detail::conjunction<
|
|
|
|
detail::negation<detail::is_exact_toml_type<T>>, // not a toml::value
|
|
|
|
detail::has_from_toml_method<T>, // but has from_toml(toml::value) memfn
|
|
|
|
std::is_default_constructible<T> // and default constructible
|
|
|
|
>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
T get(const toml::value& v);
|
|
|
|
|
|
|
|
template<typename T, typename std::enable_if<detail::conjunction<
|
|
|
|
detail::negation<detail::is_exact_toml_type<T>> // not a toml::value
|
|
|
|
>::value, std::nullptr_t>::type = nullptr,
|
|
|
|
std::size_t = sizeof(::toml::from<T>) // and has from<T> specialization
|
|
|
|
>
|
|
|
|
T get(const toml::value& v);
|
|
|
|
|
2018-12-10 06:58:20 +00:00
|
|
|
// ============================================================================
|
|
|
|
// array-like types; most likely STL container, like std::vector, etc.
|
|
|
|
|
2018-05-05 02:42:11 +00:00
|
|
|
template<typename T, typename std::enable_if<detail::conjunction<
|
2018-12-10 06:58:20 +00:00
|
|
|
detail::is_container<T>, // T is container
|
|
|
|
detail::has_resize_method<T>, // T::resize(N) works
|
|
|
|
detail::negation<detail::is_exact_toml_type<T>> // but not toml::array
|
2018-12-12 09:58:54 +00:00
|
|
|
>::value, std::nullptr_t>::type>
|
2018-05-05 02:42:11 +00:00
|
|
|
T get(const value& v)
|
|
|
|
{
|
2018-12-10 06:58:20 +00:00
|
|
|
using value_type = typename T::value_type;
|
2017-05-06 14:55:15 +00:00
|
|
|
const auto& ar = v.cast<value_t::Array>();
|
2018-12-10 06:58:20 +00:00
|
|
|
|
|
|
|
T container; container.resize(ar.size());
|
|
|
|
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.
|
|
|
|
|
2018-05-05 02:42:11 +00:00
|
|
|
template<typename T, typename std::enable_if<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
|
|
|
|
detail::negation<detail::is_exact_toml_type<T>> // not toml::array
|
2018-12-12 09:58:54 +00:00
|
|
|
>::value, std::nullptr_t>::type>
|
2018-12-10 06:58:20 +00:00
|
|
|
T get(const value& v)
|
2017-04-21 04:14:53 +00:00
|
|
|
{
|
2018-12-10 06:58:20 +00:00
|
|
|
using value_type = typename T::value_type;
|
|
|
|
const auto& ar = v.cast<value_t::Array>();
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
template<typename T, typename std::enable_if<
|
2018-12-12 09:58:54 +00:00
|
|
|
detail::is_std_pair<T>::value, std::nullptr_t>::type>
|
2018-05-05 02:46:09 +00:00
|
|
|
T get(const value& v)
|
|
|
|
{
|
|
|
|
using first_type = typename T::first_type;
|
|
|
|
using second_type = typename T::second_type;
|
2018-12-10 06:58:20 +00:00
|
|
|
|
2018-05-05 02:46:09 +00:00
|
|
|
const auto& ar = v.cast<value_t::Array>();
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
|
|
|
template<typename T, std::size_t ...I>
|
|
|
|
T get_tuple_impl(const toml::Array& a, index_sequence<I...>)
|
|
|
|
{
|
|
|
|
return std::make_tuple(
|
|
|
|
::toml::get<typename std::tuple_element<I, T>::type>(a.at(I))...);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // detail
|
|
|
|
|
2018-12-10 06:58:20 +00:00
|
|
|
template<typename T, typename std::enable_if<
|
2018-12-12 09:58:54 +00:00
|
|
|
detail::is_std_tuple<T>::value, std::nullptr_t>::type>
|
2018-05-05 02:46:09 +00:00
|
|
|
T get(const value& v)
|
|
|
|
{
|
|
|
|
const auto& ar = v.cast<value_t::Array>();
|
|
|
|
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.
|
|
|
|
|
|
|
|
template<typename T, typename std::enable_if<detail::conjunction<
|
|
|
|
detail::is_map<T>, // T is map
|
|
|
|
detail::negation<detail::is_exact_toml_type<T>> // but not toml::table
|
2018-12-12 09:58:54 +00:00
|
|
|
>::value, std::nullptr_t>::type>
|
2018-12-10 06:58:20 +00:00
|
|
|
T get(const toml::value& v)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
for(const auto& kv : v.cast<value_t::Table>())
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
|
|
|
|
template<typename T, typename std::enable_if<detail::conjunction<
|
|
|
|
detail::negation<detail::is_exact_toml_type<T>>, // not a toml::value
|
|
|
|
detail::has_from_toml_method<T>, // but has from_toml(toml::value) memfn
|
|
|
|
std::is_default_constructible<T> // and default constructible
|
2019-03-16 06:52:22 +00:00
|
|
|
>::value, std::nullptr_t>::type>
|
2019-03-16 05:44:04 +00:00
|
|
|
T get(const toml::value& v)
|
|
|
|
{
|
|
|
|
T ud;
|
|
|
|
ud.from_toml(v);
|
|
|
|
return ud;
|
|
|
|
}
|
|
|
|
template<typename T, typename std::enable_if<detail::conjunction<
|
|
|
|
detail::negation<detail::is_exact_toml_type<T>> // not a toml::value
|
2019-03-16 07:24:10 +00:00
|
|
|
>::value, std::nullptr_t>::type, std::size_t> // and has from<T>
|
2019-03-16 05:44:04 +00:00
|
|
|
T get(const toml::value& v)
|
|
|
|
{
|
|
|
|
return ::toml::from<T>::from_toml(v);
|
|
|
|
}
|
|
|
|
|
2018-12-12 08:55:34 +00:00
|
|
|
// ============================================================================
|
|
|
|
// find and get
|
|
|
|
|
2018-12-27 06:58:50 +00:00
|
|
|
template<typename T = ::toml::value>
|
2018-12-12 08:55:34 +00:00
|
|
|
decltype(::toml::get<T>(std::declval<const ::toml::value&>()))
|
2018-12-12 17:00:13 +00:00
|
|
|
find(const toml::table& tab, const toml::key& ky,
|
|
|
|
std::string tablename = "unknown table")
|
2018-12-12 08:55:34 +00:00
|
|
|
{
|
|
|
|
if(tab.count(ky) == 0)
|
|
|
|
{
|
|
|
|
throw std::out_of_range(concat_to_string("[error] key \"", ky,
|
|
|
|
"\" not found in ", tablename));
|
|
|
|
}
|
|
|
|
return ::toml::get<T>(tab.at(ky));
|
|
|
|
}
|
2018-12-27 06:58:50 +00:00
|
|
|
template<typename T = ::toml::value>
|
2018-12-12 08:55:34 +00:00
|
|
|
decltype(::toml::get<T>(std::declval<::toml::value&>()))
|
2018-12-12 17:00:13 +00:00
|
|
|
find(toml::table& tab, const toml::key& ky,
|
|
|
|
std::string tablename = "unknown table")
|
2018-12-12 08:55:34 +00:00
|
|
|
{
|
|
|
|
if(tab.count(ky) == 0)
|
|
|
|
{
|
|
|
|
throw std::out_of_range(concat_to_string("[error] key \"", ky,
|
|
|
|
"\" not found in ", tablename));
|
|
|
|
}
|
|
|
|
return ::toml::get<T>(tab[ky]);
|
|
|
|
}
|
2018-12-27 06:58:50 +00:00
|
|
|
template<typename T = ::toml::value>
|
2018-12-12 08:55:34 +00:00
|
|
|
decltype(::toml::get<T>(std::declval<::toml::value&&>()))
|
2018-12-12 17:00:13 +00:00
|
|
|
find(toml::table&& tab, const toml::key& ky,
|
|
|
|
std::string tablename = "unknown table")
|
2018-12-12 08:55:34 +00:00
|
|
|
{
|
|
|
|
if(tab.count(ky) == 0)
|
|
|
|
{
|
|
|
|
throw std::out_of_range(concat_to_string("[error] key \"", ky,
|
|
|
|
"\" not found in ", tablename));
|
|
|
|
}
|
|
|
|
return ::toml::get<T>(std::move(tab[ky]));
|
|
|
|
}
|
|
|
|
|
2018-12-27 06:58:50 +00:00
|
|
|
template<typename T = ::toml::value>
|
2018-12-12 08:55:34 +00:00
|
|
|
decltype(::toml::get<T>(std::declval<const ::toml::value&>()))
|
2018-12-12 17:00:13 +00:00
|
|
|
find(const toml::value& v, const toml::key& ky)
|
2018-12-12 08:55:34 +00:00
|
|
|
{
|
|
|
|
const auto& tab = ::toml::get<toml::table>(v);
|
|
|
|
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));
|
|
|
|
}
|
2018-12-27 06:58:50 +00:00
|
|
|
template<typename T = ::toml::value>
|
2018-12-12 08:55:34 +00:00
|
|
|
decltype(::toml::get<T>(std::declval<::toml::value&>()))
|
2018-12-12 17:00:13 +00:00
|
|
|
find(toml::value& v, const toml::key& ky)
|
2018-12-12 08:55:34 +00:00
|
|
|
{
|
|
|
|
auto& tab = ::toml::get<toml::table>(v);
|
|
|
|
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));
|
|
|
|
}
|
2018-12-27 06:58:50 +00:00
|
|
|
template<typename T = ::toml::value>
|
2018-12-12 08:55:34 +00:00
|
|
|
decltype(::toml::get<T>(std::declval<::toml::value&&>()))
|
2018-12-12 17:00:13 +00:00
|
|
|
find(toml::value&& v, const toml::key& ky)
|
2018-12-12 08:55:34 +00:00
|
|
|
{
|
|
|
|
auto tab = ::toml::get<toml::table>(std::move(v));
|
|
|
|
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>(std::move(tab[ky]));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-03-18 07:44:36 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// specialization for the exact toml types (return type becomes lvalue ref)
|
|
|
|
|
|
|
|
template<typename T, typename std::enable_if<
|
|
|
|
detail::is_exact_toml_type<T>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
T const& get_or(const toml::value& v, const T& opt)
|
2018-12-12 08:55:34 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return get<typename std::remove_cv<
|
|
|
|
typename std::remove_reference<T>::type>::type>(v);
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
2019-03-18 07:44:36 +00:00
|
|
|
return opt;
|
2018-12-12 08:55:34 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-18 07:44:36 +00:00
|
|
|
template<typename T, typename std::enable_if<
|
|
|
|
detail::is_exact_toml_type<T>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
T& get_or(toml::value& v, T& opt)
|
2018-12-12 08:55:34 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return get<typename std::remove_cv<
|
|
|
|
typename std::remove_reference<T>::type>::type>(v);
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
2019-03-18 07:44:36 +00:00
|
|
|
return opt;
|
2018-12-12 08:55:34 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-18 07:44:36 +00:00
|
|
|
template<typename T, typename std::enable_if<
|
|
|
|
detail::is_exact_toml_type<T>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
T&& get_or(toml::value&& v, T&& opt)
|
2018-12-12 08:55:34 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return get<typename std::remove_cv<
|
2019-03-18 07:44:36 +00:00
|
|
|
typename std::remove_reference<T>::type>::type>(v);
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
return opt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// specialization for std::string (return type becomes lvalue ref)
|
|
|
|
|
|
|
|
template<typename T, typename std::enable_if<
|
|
|
|
std::is_same<T, std::string>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
std::string const& get_or(const toml::value& v, const T& opt)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return get<std::string>(v);
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
return opt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
template<typename T, typename std::enable_if<
|
|
|
|
std::is_same<T, std::string>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
std::string& get_or(toml::value& v, T& opt)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return get<std::string>(v);
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
return opt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
template<typename T, typename std::enable_if<
|
|
|
|
std::is_same<T, std::string>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
std::string get_or(toml::value&& v, T&& opt)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return get<std::string>(v);
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
return opt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
template<typename T, typename std::enable_if<
|
|
|
|
detail::is_string_literal<typename std::remove_reference<T>::type>::value,
|
|
|
|
std::nullptr_t>::type = nullptr>
|
|
|
|
std::string get_or(const toml::value& v, T&& opt)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return get<std::string>(v);
|
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)
|
|
|
|
|
|
|
|
template<typename T, typename std::enable_if<detail::conjunction<
|
|
|
|
detail::negation<detail::is_exact_toml_type<T>>,
|
|
|
|
detail::negation<std::is_same<T, std::string>>,
|
|
|
|
detail::negation<detail::is_string_literal<typename std::remove_reference<T>::type>>
|
|
|
|
>::value, std::nullptr_t>::type = nullptr>
|
|
|
|
T get_or(const toml::value& v, T&& opt)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return get<typename std::remove_cv<
|
|
|
|
typename std::remove_reference<T>::type>::type>(v);
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
return opt;
|
|
|
|
}
|
|
|
|
}
|
2018-12-12 16:29:23 +00:00
|
|
|
|
2018-12-12 08:55:34 +00:00
|
|
|
template<typename T>
|
|
|
|
auto get_or(const toml::table& tab, const toml::key& ky, T&& opt)
|
|
|
|
-> decltype(get_or(std::declval<value const&>(), std::forward<T>(opt)))
|
2017-12-11 03:04:57 +00:00
|
|
|
{
|
|
|
|
if(tab.count(ky) == 0) {return std::forward<T>(opt);}
|
2018-12-12 08:55:34 +00:00
|
|
|
return ::toml::get_or(tab.at(ky), std::forward<T>(opt));
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
auto get_or(toml::table& tab, const toml::key& ky, T&& opt)
|
|
|
|
-> decltype(get_or(std::declval<value&>(), std::forward<T>(opt)))
|
|
|
|
{
|
|
|
|
if(tab.count(ky) == 0) {return std::forward<T>(opt);}
|
|
|
|
return ::toml::get_or(tab[ky], std::forward<T>(opt));
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
auto get_or(toml::table&& tab, const toml::key& ky, T&& opt)
|
|
|
|
-> decltype(get_or(std::declval<value&&>(), std::forward<T>(opt)))
|
|
|
|
{
|
|
|
|
if(tab.count(ky) == 0) {return std::forward<T>(opt);}
|
|
|
|
return ::toml::get_or(std::move(tab[ky]), std::forward<T>(opt));
|
2017-12-11 03:04:57 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 16:29:23 +00:00
|
|
|
template<typename T>
|
|
|
|
auto get_or(const toml::value& v, const toml::key& ky, T&& opt)
|
|
|
|
-> decltype(get_or(std::declval<value const&>(), std::forward<T>(opt)))
|
|
|
|
{
|
|
|
|
if(v.type() != toml::value_t::Table){return std::forward<T>(opt);}
|
|
|
|
const auto& tab = toml::get<toml::table>(v);
|
|
|
|
if(tab.count(ky) == 0) {return std::forward<T>(opt);}
|
|
|
|
return ::toml::get_or(tab.at(ky), std::forward<T>(opt));
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
auto get_or(toml::value& v, const toml::key& ky, T&& opt)
|
|
|
|
-> decltype(get_or(std::declval<value&>(), std::forward<T>(opt)))
|
|
|
|
{
|
|
|
|
if(v.type() != toml::value_t::Table){return std::forward<T>(opt);}
|
|
|
|
auto& tab = toml::get<toml::table>(v);
|
|
|
|
if(tab.count(ky) == 0) {return std::forward<T>(opt);}
|
|
|
|
return ::toml::get_or(tab[ky], std::forward<T>(opt));
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
auto get_or(toml::value&& v, const toml::key& ky, T&& opt)
|
|
|
|
-> decltype(get_or(std::declval<value&&>(), std::forward<T>(opt)))
|
|
|
|
{
|
|
|
|
if(v.type() != toml::value_t::Table){return std::forward<T>(opt);}
|
|
|
|
auto tab = toml::get<toml::table>(std::move(v));
|
|
|
|
if(tab.count(ky) == 0) {return std::forward<T>(opt);}
|
|
|
|
return ::toml::get_or(std::move(tab[ky]), std::forward<T>(opt));
|
|
|
|
}
|
2018-12-12 08:55:34 +00:00
|
|
|
|
2018-12-11 02:35:35 +00:00
|
|
|
// ============================================================================
|
|
|
|
// expect
|
|
|
|
|
|
|
|
template<typename T>
|
2018-12-17 01:57:40 +00:00
|
|
|
result<T, std::string> expect(const toml::value& 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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
template<typename T>
|
2018-12-17 01:57:40 +00:00
|
|
|
result<T, std::string> expect(const toml::value& 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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
template<typename T>
|
2018-12-17 01:57:40 +00:00
|
|
|
result<T, std::string> expect(const toml::table& t, const toml::key& k,
|
|
|
|
std::string tablename = "unknown table") noexcept
|
2018-12-12 08:55:34 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2018-12-17 01:57:40 +00:00
|
|
|
return ok(find<T>(t, k, std::move(tablename)));
|
2018-12-12 08:55:34 +00:00
|
|
|
}
|
|
|
|
catch(const std::exception& e)
|
|
|
|
{
|
|
|
|
return err(e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 04:14:53 +00:00
|
|
|
} // toml
|
|
|
|
#endif// TOML11_GET
|