feat(WIP): update toml::get

This commit is contained in:
ToruNiina
2019-06-02 17:31:49 +09:00
parent e1556183d1
commit 725d915ba9

View File

@@ -10,50 +10,60 @@
namespace toml namespace toml
{ {
// C++14 alias
template<bool B, typename T>
using enable_if_t = typename std::enable_if<B, T>::type;
// ============================================================================ // ============================================================================
// exact toml::* type // exact toml::* type
template<typename T, typename std::enable_if< template<typename T, typename C,
detail::is_exact_toml_type<T>::value, std::nullptr_t>::type = nullptr> template<typename ...> class M, template<typename ...> class V>
inline T& get(value& v) enable_if_t<detail::is_exact_toml_type<T, basic_value<C, M, V>>::value, T> &
get(basic_value<C, M, V>& v)
{ {
return v.cast<detail::toml_value_t<T>::value>(); return v.template cast<detail::type_to_enum<T, basic_value<C, M, V>>::value>();
} }
template<typename T, typename std::enable_if< template<typename T, typename C,
detail::is_exact_toml_type<T>::value, std::nullptr_t>::type = nullptr> template<typename ...> class M, template<typename ...> class V>
inline T const& get(const value& v) enable_if_t<detail::is_exact_toml_type<T, basic_value<C, M, V>>::value, T> const&
get(const basic_value<C, M, V>& v)
{ {
return v.cast<detail::toml_value_t<T>::value>(); return v.template cast<detail::type_to_enum<T, basic_value<C, M, V>>::value>();
} }
template<typename T, typename std::enable_if< template<typename T, typename C,
detail::is_exact_toml_type<T>::value, std::nullptr_t>::type = nullptr> template<typename ...> class M, template<typename ...> class V>
inline T&& get(value&& v) enable_if_t<detail::is_exact_toml_type<T, basic_value<C, M, V>>::value, T> &&
get(basic_value<C, M, V>&& v)
{ {
return std::move(v.cast<detail::toml_value_t<T>::value>()); return v.template cast<detail::type_to_enum<T, basic_value<C, M, V>>::value>();
} }
// ============================================================================ // ============================================================================
// T == toml::value; identity transformation. // T == toml::value; identity transformation.
template<typename T, typename std::enable_if< template<typename T, typename C,
std::is_same<T, ::toml::value>::value, std::nullptr_t>::type = nullptr> template<typename ...> class M, template<typename ...> class V>
inline T& get(value& v) inline enable_if_t<std::is_same<T, basic_value<C, M, V>>::value, T>&
get(basic_value<C, M, V>& v)
{ {
return v; return v;
} }
template<typename T, typename std::enable_if< template<typename T, typename C,
std::is_same<T, ::toml::value>::value, std::nullptr_t>::type = nullptr> template<typename ...> class M, template<typename ...> class V>
inline T const& get(const value& v) inline enable_if_t<std::is_same<T, basic_value<C, M, V>>::value, T> const&
get(const basic_value<C, M, V>& v)
{ {
return v; return v;
} }
template<typename T, typename std::enable_if< template<typename T, typename C,
std::is_same<T, ::toml::value>::value, std::nullptr_t>::type = nullptr> template<typename ...> class M, template<typename ...> class V>
inline T&& get(value&& v) inline enable_if_t<std::is_same<T, basic_value<C, M, V>>::value, T> &&
get(basic_value<C, M, V>&& v)
{ {
return std::move(v); return std::move(v);
} }
@@ -61,104 +71,125 @@ inline T&& get(value&& v)
// ============================================================================ // ============================================================================
// integer convertible from toml::Integer // integer convertible from toml::Integer
template<typename T, typename std::enable_if<detail::conjunction< template<typename T, typename C,
template<typename ...> class M, template<typename ...> class V>
inline enable_if_t<detail::conjunction<
std::is_integral<T>, // T is integral std::is_integral<T>, // T is integral
detail::negation<std::is_same<T, bool>>, // but not bool detail::negation<std::is_same<T, bool>>, // but not bool
detail::negation<detail::is_exact_toml_type<T>> // but not toml::integer detail::negation< // but not toml::integer
>::value, std::nullptr_t>::type = nullptr> detail::is_exact_toml_type<T, basic_value<C, M, V>>>
inline T get(const value& v) >::value, T>
get(const basic_value<C, M, V>& v)
{ {
return static_cast<T>(v.cast<value_t::Integer>()); return static_cast<T>(v.template cast<value_t::integer>());
} }
// ============================================================================ // ============================================================================
// floating point convertible from toml::Float // floating point convertible from toml::Float
template<typename T, typename std::enable_if<detail::conjunction< template<typename T, typename C,
template<typename ...> class M, template<typename ...> class V>
inline enable_if_t<detail::conjunction<
std::is_floating_point<T>, // T is floating_point std::is_floating_point<T>, // T is floating_point
detail::negation<detail::is_exact_toml_type<T>> // but not toml::Float detail::negation< // but not toml::floating
>::value, std::nullptr_t>::type = nullptr> detail::is_exact_toml_type<T, basic_value<C, M, V>>>
inline T get(const value& v) >::value, T>
get(const basic_value<C, M, V>& v)
{ {
return static_cast<T>(v.cast<value_t::Float>()); return static_cast<T>(v.template cast<value_t::floating>());
} }
// ============================================================================ // ============================================================================
// std::string; toml uses its own toml::string, but it should be convertible to // std::string; toml uses its own toml::string, but it should be convertible to
// std::string seamlessly // std::string seamlessly
template<typename T, typename std::enable_if< template<typename T, typename C,
std::is_same<T, std::string>::value, std::nullptr_t>::type = nullptr> template<typename ...> class M, template<typename ...> class V>
inline std::string& get(value& v) inline enable_if_t<std::is_same<T, std::string>::value, std::string>&
get(basic_value<C, M, V>& v)
{ {
return v.cast<value_t::String>().str; return v.template cast<value_t::string>().str;
} }
template<typename T, typename std::enable_if< template<typename T, typename C,
std::is_same<T, std::string>::value, std::nullptr_t>::type = nullptr> template<typename ...> class M, template<typename ...> class V>
inline std::string const& get(const value& v) inline enable_if_t<std::is_same<T, std::string>::value, std::string> const&
get(const basic_value<C, M, V>& v)
{ {
return v.cast<value_t::String>().str; return v.template cast<value_t::string>().str;
} }
template<typename T, typename std::enable_if< template<typename T, typename C,
std::is_same<T, std::string>::value, std::nullptr_t>::type = nullptr> template<typename ...> class M, template<typename ...> class V>
inline std::string get(value&& v) inline enable_if_t<std::is_same<T, std::string>::value, std::string> const&
get(basic_value<C, M, V>&& v)
{ {
return std::move(v.cast<value_t::String>().str); return std::move(v.template cast<value_t::string>().str);
} }
// ============================================================================ // ============================================================================
// std::string_view // std::string_view
#if __cplusplus >= 201703L #if __cplusplus >= 201703L
template<typename T, typename std::enable_if< template<typename T, typename C,
std::is_same<T, std::string_view>::value, std::nullptr_t>::type = nullptr> template<typename ...> class M, template<typename ...> class V>
inline std::string_view get(const value& v) inline enable_if_t<std::is_same<T, std::string_view>::value, std::string_view>
get(const basic_value<C, M, V>& v)
{ {
return std::string_view(v.cast<value_t::String>().str); return std::string_view(v.template cast<value_t::string>().str);
} }
#endif #endif
// ============================================================================ // ============================================================================
// std::chrono::duration from toml::local_time. // std::chrono::duration from toml::local_time.
template<typename T, typename std::enable_if< template<typename T, typename C,
detail::is_chrono_duration<T>::value, std::nullptr_t>::type = nullptr> template<typename ...> class M, template<typename ...> class V>
inline T get(const value& v) inline enable_if_t<detail::is_chrono_duration<T>::value, T>
get(const basic_value<C, M, V>& v)
{ {
return std::chrono::duration_cast<T>( return std::chrono::duration_cast<T>(
std::chrono::nanoseconds(v.cast<value_t::LocalTime>())); std::chrono::nanoseconds(v.template cast<value_t::local_time>()));
} }
// ============================================================================ // ============================================================================
// std::chrono::system_clock::time_point from toml::datetime variants // std::chrono::system_clock::time_point from toml::datetime variants
template<typename T, typename std::enable_if< template<typename T, typename C,
std::is_same<std::chrono::system_clock::time_point, T>::value, template<typename ...> class M, template<typename ...> class V>
std::nullptr_t>::type = nullptr> inline enable_if_t<
inline T get(const value& v) std::is_same<std::chrono::system_clock::time_point, T>::value, T>
get(const basic_value<C, M, V>& v)
{ {
switch(v.type()) switch(v.type())
{ {
case value_t::LocalDate: case value_t::local_date:
{ {
return std::chrono::system_clock::time_point( return std::chrono::system_clock::time_point(
v.cast<value_t::LocalDate>()); v.template cast<value_t::local_date>());
} }
case value_t::LocalDatetime: case value_t::local_datetime:
{ {
return std::chrono::system_clock::time_point( return std::chrono::system_clock::time_point(
v.cast<value_t::LocalDatetime>()); v.template cast<value_t::local_datetime>());
}
case value_t::offset_datetime:
{
return std::chrono::system_clock::time_point(
v.template cast<value_t::offset_datetime>());
} }
default: default:
{ {
return std::chrono::system_clock::time_point( throw type_error(detail::format_underline("[error] toml::value "
v.cast<value_t::OffsetDatetime>()); "bad_cast to std::chrono::system_clock::time_point", {
{std::addressof(detail::get_region(v)),
concat_to_string("the actual type is ", v.type())}
}));
} }
} }
} }
/*
// ============================================================================ // ============================================================================
// forward declaration to use this recursively. ignore this and go ahead. // forward declaration to use this recursively. ignore this and go ahead.
@@ -768,6 +799,6 @@ result<T, std::string> expect(const toml::table& t, const toml::key& k,
return err(e.what()); return err(e.what());
} }
} }
*/
} // toml } // toml
#endif// TOML11_GET #endif// TOML11_GET