Skip to content

Commit

Permalink
Support std::optional members for TOML11_DEFINE_CONVERSION_NON_INTRUSIVE
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-matsui committed Jul 23, 2024
1 parent 5727819 commit 1da36d6
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 3 deletions.
83 changes: 81 additions & 2 deletions include/toml11/conversion.hpp
Original file line number Diff line number Diff line change
@@ -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 toml::basic_value<TC>& v, const char* var_name)
{
if constexpr(is_optional_v<T>)
{
try
{
obj = toml::find<typename T::value_type>(v, var_name);
}
catch(...)
{
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, toml::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 toml::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, toml::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
Expand Down Expand Up @@ -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 { \
Expand Down
6 changes: 6 additions & 0 deletions include/toml11/version.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
86 changes: 85 additions & 1 deletion tests/test_user_defined_conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#include <toml.hpp>
#include "utility.hpp"

#if defined(TOML11_HAS_OPTIONAL)
#include <optional>
#endif

namespace extlib
{
struct foo
Expand Down Expand Up @@ -234,7 +238,7 @@ TEST_CASE("test_conversion_by_member_methods")
CHECK(v == v2);
}


{
const toml::value v(toml::table{{"a", 42}, {"b", "baz"}});

Expand Down Expand Up @@ -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

0 comments on commit 1da36d6

Please sign in to comment.