diff --git a/toml/get.hpp b/toml/get.hpp index 80336ce..dc1b495 100644 --- a/toml/get.hpp +++ b/toml/get.hpp @@ -2,6 +2,7 @@ // Distributed under the MIT License. #ifndef TOML11_GET #define TOML11_GET +#include "from.hpp" #include "result.hpp" #include "value.hpp" #include @@ -297,6 +298,31 @@ T get(const toml::value& v) return map; } + +// ============================================================================ +// user-defined, but compatible types. + +template>, // not a toml::value + detail::has_from_toml_method, // but has from_toml(toml::value) memfn + std::is_default_constructible // and default constructible + >::value, std::nullptr_t>::type = nullptr> +T get(const toml::value& v) +{ + T ud; + ud.from_toml(v); + return ud; +} +template> // not a toml::value + >::value, std::nullptr_t>::type = nullptr, + std::size_t = sizeof(::toml::from) // and has from specialization + > +T get(const toml::value& v) +{ + return ::toml::from::from_toml(v); +} + // ============================================================================ // find and get diff --git a/toml/value.hpp b/toml/value.hpp index a866b04..00b790a 100644 --- a/toml/value.hpp +++ b/toml/value.hpp @@ -3,6 +3,7 @@ #ifndef TOML11_VALUE #define TOML11_VALUE #include "traits.hpp" +#include "into.hpp" #include "utility.hpp" #include "exception.hpp" #include "storage.hpp" @@ -533,6 +534,46 @@ class value return *this; } + // user-defined ========================================================= + + // convert using into_toml() method ------------------------------------- + + template>, // not a toml::value + detail::has_into_toml_method // but has `into_toml` method + >::value, std::nullptr_t>::type = nullptr> + value(const T& ud): value(ud.into_toml()) {} + + template>, // not a toml::value + detail::has_into_toml_method // but has `into_toml` method + >::value, std::nullptr_t>::type = nullptr> + value& operator=(const T& ud) + { + *this = ud.into_toml(); + return *this; + } + + // convert using into struct ----------------------------------------- + + template>::value, + std::nullptr_t>::type = nullptr, + std::size_t S = sizeof(::toml::into)> + value(const T& ud): value(::toml::into::into_toml(ud)) {} + + template>::value, + std::nullptr_t>::type = nullptr, + std::size_t S = sizeof(::toml::into)> + value& operator=(const T& ud) + { + *this = ::toml::into::into_toml(ud); + return *this; + } + + // type checking and casting ============================================ + template bool is() const noexcept {return value_traits::type_index == this->type_;} bool is(value_t t) const noexcept {return t == this->type_;}