mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-17 17:58:09 +08:00
feat: support conversion with external types
This commit is contained in:
26
toml/get.hpp
26
toml/get.hpp
@@ -2,6 +2,7 @@
|
|||||||
// Distributed under the MIT License.
|
// Distributed under the MIT License.
|
||||||
#ifndef TOML11_GET
|
#ifndef TOML11_GET
|
||||||
#define TOML11_GET
|
#define TOML11_GET
|
||||||
|
#include "from.hpp"
|
||||||
#include "result.hpp"
|
#include "result.hpp"
|
||||||
#include "value.hpp"
|
#include "value.hpp"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
@@ -297,6 +298,31 @@ T get(const toml::value& v)
|
|||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
// 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
|
||||||
|
>::value, std::nullptr_t>::type = nullptr>
|
||||||
|
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
|
||||||
|
>::value, std::nullptr_t>::type = nullptr,
|
||||||
|
std::size_t = sizeof(::toml::from<T>) // and has from<T> specialization
|
||||||
|
>
|
||||||
|
T get(const toml::value& v)
|
||||||
|
{
|
||||||
|
return ::toml::from<T>::from_toml(v);
|
||||||
|
}
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// find and get
|
// find and get
|
||||||
|
|
||||||
|
@@ -3,6 +3,7 @@
|
|||||||
#ifndef TOML11_VALUE
|
#ifndef TOML11_VALUE
|
||||||
#define TOML11_VALUE
|
#define TOML11_VALUE
|
||||||
#include "traits.hpp"
|
#include "traits.hpp"
|
||||||
|
#include "into.hpp"
|
||||||
#include "utility.hpp"
|
#include "utility.hpp"
|
||||||
#include "exception.hpp"
|
#include "exception.hpp"
|
||||||
#include "storage.hpp"
|
#include "storage.hpp"
|
||||||
@@ -533,6 +534,46 @@ class value
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// user-defined =========================================================
|
||||||
|
|
||||||
|
// convert using into_toml() method -------------------------------------
|
||||||
|
|
||||||
|
template<typename T, typename std::enable_if<detail::conjunction<
|
||||||
|
detail::negation<detail::is_exact_toml_type<T>>, // not a toml::value
|
||||||
|
detail::has_into_toml_method<T> // but has `into_toml` method
|
||||||
|
>::value, std::nullptr_t>::type = nullptr>
|
||||||
|
value(const T& ud): value(ud.into_toml()) {}
|
||||||
|
|
||||||
|
template<typename T, typename std::enable_if<detail::conjunction<
|
||||||
|
detail::negation<detail::is_exact_toml_type<T>>, // not a toml::value
|
||||||
|
detail::has_into_toml_method<T> // 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<T> struct -----------------------------------------
|
||||||
|
|
||||||
|
template<typename T, typename std::enable_if<
|
||||||
|
detail::negation<detail::is_exact_toml_type<T>>::value,
|
||||||
|
std::nullptr_t>::type = nullptr,
|
||||||
|
std::size_t S = sizeof(::toml::into<T>)>
|
||||||
|
value(const T& ud): value(::toml::into<T>::into_toml(ud)) {}
|
||||||
|
|
||||||
|
template<typename T, typename std::enable_if<
|
||||||
|
detail::negation<detail::is_exact_toml_type<T>>::value,
|
||||||
|
std::nullptr_t>::type = nullptr,
|
||||||
|
std::size_t S = sizeof(::toml::into<T>)>
|
||||||
|
value& operator=(const T& ud)
|
||||||
|
{
|
||||||
|
*this = ::toml::into<T>::into_toml(ud);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// type checking and casting ============================================
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
bool is() const noexcept {return value_traits<T>::type_index == this->type_;}
|
bool is() const noexcept {return value_traits<T>::type_index == this->type_;}
|
||||||
bool is(value_t t) const noexcept {return t == this->type_;}
|
bool is(value_t t) const noexcept {return t == this->type_;}
|
||||||
|
Reference in New Issue
Block a user