mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-15 23:48:11 +08:00
Support std::optional members for TOML11_DEFINE_CONVERSION_NON_INTRUSIVE
This commit is contained in:
@@ -1,9 +1,88 @@
|
||||
#ifndef TOML11_CONVERSION_HPP
|
||||
#define TOML11_CONVERSION_HPP
|
||||
|
||||
#include "find.hpp"
|
||||
#include "from.hpp" // IWYU pragma: keep
|
||||
#include "into.hpp" // IWYU pragma: keep
|
||||
|
||||
#if defined(TOML11_HAS_OPTIONAL)
|
||||
|
||||
#include <optional>
|
||||
|
||||
namespace toml
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template<typename T>
|
||||
inline constexpr bool is_optional_v = false;
|
||||
|
||||
template<typename T>
|
||||
inline constexpr bool is_optional_v<std::optional<T>> = true;
|
||||
|
||||
template<typename T, typename TC>
|
||||
void find_member_variable_from_value(T& obj, const basic_value<TC>& v, const char* var_name)
|
||||
{
|
||||
if constexpr(is_optional_v<T>)
|
||||
{
|
||||
if(v.contains(var_name))
|
||||
{
|
||||
obj = toml::find<typename T::value_type>(v, var_name);
|
||||
}
|
||||
else
|
||||
{
|
||||
obj = std::nullopt;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
obj = toml::find<T>(v, var_name);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T, typename TC>
|
||||
void assign_member_variable_to_value(const T& obj, basic_value<TC>& v, const char* var_name)
|
||||
{
|
||||
if constexpr(is_optional_v<T>)
|
||||
{
|
||||
if(obj.has_value())
|
||||
{
|
||||
v[var_name] = obj.value();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
v[var_name] = obj;
|
||||
}
|
||||
}
|
||||
|
||||
} // detail
|
||||
} // toml
|
||||
|
||||
#else
|
||||
|
||||
namespace toml
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template<typename T, typename TC>
|
||||
void find_member_variable_from_value(T& obj, const basic_value<TC>& v, const char* var_name)
|
||||
{
|
||||
obj = toml::find<T>(v, var_name);
|
||||
}
|
||||
|
||||
template<typename T, typename TC>
|
||||
void assign_member_variable_to_value(const T& obj, basic_value<TC>& v, const char* var_name)
|
||||
{
|
||||
v[var_name] = obj;
|
||||
}
|
||||
|
||||
} // detail
|
||||
} // toml
|
||||
|
||||
#endif // optional
|
||||
|
||||
// use it in the following way.
|
||||
// ```cpp
|
||||
// namespace foo
|
||||
@@ -88,10 +167,10 @@
|
||||
|
||||
|
||||
#define TOML11_FIND_MEMBER_VARIABLE_FROM_VALUE(VAR_NAME)\
|
||||
obj.VAR_NAME = toml::find<decltype(obj.VAR_NAME)>(v, TOML11_STRINGIZE(VAR_NAME));
|
||||
toml::detail::find_member_variable_from_value(obj.VAR_NAME, v, TOML11_STRINGIZE(VAR_NAME));
|
||||
|
||||
#define TOML11_ASSIGN_MEMBER_VARIABLE_TO_VALUE(VAR_NAME)\
|
||||
v[TOML11_STRINGIZE(VAR_NAME)] = obj.VAR_NAME;
|
||||
toml::detail::assign_member_variable_to_value(obj.VAR_NAME, v, TOML11_STRINGIZE(VAR_NAME));
|
||||
|
||||
#define TOML11_DEFINE_CONVERSION_NON_INTRUSIVE(NAME, ...)\
|
||||
namespace toml { \
|
||||
|
@@ -77,6 +77,12 @@
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX17_VALUE
|
||||
# if __has_include(<optional>)
|
||||
# define TOML11_HAS_OPTIONAL 1
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if defined(TOML11_COMPILE_SOURCES)
|
||||
# define TOML11_INLINE
|
||||
#else
|
||||
|
@@ -4,6 +4,10 @@
|
||||
#include <toml.hpp>
|
||||
#include "utility.hpp"
|
||||
|
||||
#if defined(TOML11_HAS_OPTIONAL)
|
||||
#include <optional>
|
||||
#endif
|
||||
|
||||
namespace extlib
|
||||
{
|
||||
struct foo
|
||||
@@ -234,7 +238,7 @@ TEST_CASE("test_conversion_by_member_methods")
|
||||
CHECK(v == v2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
{
|
||||
const toml::value v(toml::table{{"a", 42}, {"b", "baz"}});
|
||||
|
||||
@@ -652,4 +656,84 @@ TEST_CASE("test_conversion_via_macro")
|
||||
CHECK(v2 == v);
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(TOML11_HAS_OPTIONAL)
|
||||
namespace extlib4
|
||||
{
|
||||
struct foo
|
||||
{
|
||||
std::optional<int> a;
|
||||
std::optional<std::string> b;
|
||||
};
|
||||
struct bar
|
||||
{
|
||||
std::optional<int> a;
|
||||
std::optional<std::string> b;
|
||||
std::optional<foo> f;
|
||||
};
|
||||
|
||||
} // extlib4
|
||||
|
||||
TOML11_DEFINE_CONVERSION_NON_INTRUSIVE(extlib4::foo, a, b)
|
||||
TOML11_DEFINE_CONVERSION_NON_INTRUSIVE(extlib4::bar, a, b, f)
|
||||
|
||||
TEST_CASE("test_optional_conversion_via_macro")
|
||||
{
|
||||
{
|
||||
const toml::value v(toml::table{{"a", 42}});
|
||||
|
||||
const auto foo = toml::get<extlib4::foo>(v);
|
||||
CHECK(foo.a.value() == 42);
|
||||
CHECK(foo.b == std::nullopt);
|
||||
|
||||
const toml::value v2(foo);
|
||||
CHECK(v2 == v);
|
||||
}
|
||||
{
|
||||
const toml::ordered_value v(toml::ordered_table{
|
||||
{"b", "baz"}
|
||||
});
|
||||
|
||||
const auto foo = toml::get<extlib4::foo>(v);
|
||||
CHECK(foo.a == std::nullopt);
|
||||
CHECK(foo.b.value() == "baz");
|
||||
|
||||
const toml::ordered_value v2(foo);
|
||||
CHECK(v2 == v);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
{
|
||||
const toml::value v(toml::table{
|
||||
{"b", "bar.b"},
|
||||
{"f", toml::table{{"a", 42}}}
|
||||
});
|
||||
|
||||
const auto bar = toml::get<extlib4::bar>(v);
|
||||
CHECK(bar.a == std::nullopt);
|
||||
CHECK(bar.b.value() == "bar.b");
|
||||
CHECK(bar.f.value().a.value() == 42);
|
||||
CHECK(bar.f.value().b == std::nullopt);
|
||||
|
||||
const toml::value v2(bar);
|
||||
CHECK(v2 == v);
|
||||
}
|
||||
{
|
||||
const toml::ordered_value v(toml::ordered_table{
|
||||
{"a", 42},
|
||||
{"f", toml::ordered_table{{"b", "foo.b"}}}
|
||||
});
|
||||
|
||||
const auto bar = toml::get<extlib4::bar>(v);
|
||||
CHECK(bar.a.value() == 42);
|
||||
CHECK(bar.b == std::nullopt);
|
||||
CHECK(bar.f.value().a == std::nullopt);
|
||||
CHECK(bar.f.value().b.value() == "foo.b");
|
||||
|
||||
const toml::ordered_value v2(bar);
|
||||
CHECK(v2 == v);
|
||||
}
|
||||
}
|
||||
#endif // TOML11_HAS_OPTIONAL
|
||||
#endif // TOML11_WITHOUT_DEFINE_NON_INTRUSIVE
|
||||
|
Reference in New Issue
Block a user