Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/reflection-cpp/reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ constexpr void EnumerateMembers(Callable&& callable)
}

template <typename Object, typename Callable>
requires std::same_as<void, std::invoke_result_t<Callable, std::string, MemberTypeOf<1, Object>>>
requires std::same_as<void, std::invoke_result_t<Callable, std::string, MemberTypeOf<0, Object>>>
void CallOnMembers(Object& object, Callable&& callable)
{
EnumerateMembers<Object>(object,
Expand Down Expand Up @@ -752,7 +752,7 @@ constexpr ResultType FoldMembers(ResultType initialValue, Callable const& callab
///
/// @return The result of the fold
template <typename Object, typename Callable, typename ResultType>
requires std::same_as<ResultType, std::invoke_result_t<Callable, std::string, MemberTypeOf<1, Object>, ResultType>>
requires std::same_as<ResultType, std::invoke_result_t<Callable, std::string, MemberTypeOf<0, Object>, ResultType>>
constexpr ResultType FoldMembers(Object& object, ResultType initialValue, Callable const& callable)
{
// clang-format off
Expand Down
28 changes: 27 additions & 1 deletion test-reflection-cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ enum Color
Blue
};

struct SingleValueRecord
{
int value;
};

TEST_CASE("GetName", "[reflection]")
{
auto const enumValue = Reflection::GetName<Color::Red>();
Expand All @@ -38,13 +43,34 @@ TEST_CASE("GetName", "[reflection]")
auto const enumValue2 = Reflection::GetName<Color::Green>();
CHECK(enumValue2 == "Green");

auto const person = Person { "John Doe", "[email protected]", 42 };
auto const memberName1 = Reflection::GetName<&Person::email>();
CHECK(memberName1 == "email");

auto const singleValueField = Reflection::GetName<&SingleValueRecord::value>();
CHECK(singleValueField == "value");
}

TEST_CASE("single value record", "[reflection]")
{
static_assert(Reflection::CountMembers<SingleValueRecord> == 1);

auto const s = SingleValueRecord { 42 };
auto const t = Reflection::ToTuple(s);

CHECK(std::get<0>(t) == 42);
CHECK(Reflection::GetMemberAt<0>(s) == 42);

Reflection::CallOnMembers(s, [](auto&& name, auto&& value) {
CHECK(name == "value");
CHECK(value == 42);
});
}

TEST_CASE("core", "[reflection]")
{
auto s = SingleValueRecord { 42 };
CHECK(Reflection::Inspect(s) == "value=42");

auto p = Person { "John Doe", "[email protected]", 42 };
auto const result = Reflection::Inspect(p);
CHECK(result == R"(name="John Doe" email="[email protected]" age=42)");
Expand Down