Skip to content

Commit 6fdb93f

Browse files
authored
Merge pull request #16 from contour-terminal/fix/single-value-structs
Fixes problem of working on structs with only a single element
2 parents c7226db + 9186051 commit 6fdb93f

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

include/reflection-cpp/reflection.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ constexpr void EnumerateMembers(Callable&& callable)
715715
}
716716

717717
template <typename Object, typename Callable>
718-
requires std::same_as<void, std::invoke_result_t<Callable, std::string, MemberTypeOf<1, Object>>>
718+
requires std::same_as<void, std::invoke_result_t<Callable, std::string, MemberTypeOf<0, Object>>>
719719
void CallOnMembers(Object& object, Callable&& callable)
720720
{
721721
EnumerateMembers<Object>(object,
@@ -752,7 +752,7 @@ constexpr ResultType FoldMembers(ResultType initialValue, Callable const& callab
752752
///
753753
/// @return The result of the fold
754754
template <typename Object, typename Callable, typename ResultType>
755-
requires std::same_as<ResultType, std::invoke_result_t<Callable, std::string, MemberTypeOf<1, Object>, ResultType>>
755+
requires std::same_as<ResultType, std::invoke_result_t<Callable, std::string, MemberTypeOf<0, Object>, ResultType>>
756756
constexpr ResultType FoldMembers(Object& object, ResultType initialValue, Callable const& callable)
757757
{
758758
// clang-format off

test-reflection-cpp.cpp

+27-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ enum Color
3030
Blue
3131
};
3232

33+
struct SingleValueRecord
34+
{
35+
int value;
36+
};
37+
3338
TEST_CASE("GetName", "[reflection]")
3439
{
3540
auto const enumValue = Reflection::GetName<Color::Red>();
@@ -38,13 +43,34 @@ TEST_CASE("GetName", "[reflection]")
3843
auto const enumValue2 = Reflection::GetName<Color::Green>();
3944
CHECK(enumValue2 == "Green");
4045

41-
auto const person = Person { "John Doe", "[email protected]", 42 };
4246
auto const memberName1 = Reflection::GetName<&Person::email>();
4347
CHECK(memberName1 == "email");
48+
49+
auto const singleValueField = Reflection::GetName<&SingleValueRecord::value>();
50+
CHECK(singleValueField == "value");
51+
}
52+
53+
TEST_CASE("single value record", "[reflection]")
54+
{
55+
static_assert(Reflection::CountMembers<SingleValueRecord> == 1);
56+
57+
auto const s = SingleValueRecord { 42 };
58+
auto const t = Reflection::ToTuple(s);
59+
60+
CHECK(std::get<0>(t) == 42);
61+
CHECK(Reflection::GetMemberAt<0>(s) == 42);
62+
63+
Reflection::CallOnMembers(s, [](auto&& name, auto&& value) {
64+
CHECK(name == "value");
65+
CHECK(value == 42);
66+
});
4467
}
4568

4669
TEST_CASE("core", "[reflection]")
4770
{
71+
auto s = SingleValueRecord { 42 };
72+
CHECK(Reflection::Inspect(s) == "value=42");
73+
4874
auto p = Person { "John Doe", "[email protected]", 42 };
4975
auto const result = Reflection::Inspect(p);
5076
CHECK(result == R"(name="John Doe" email="[email protected]" age=42)");

0 commit comments

Comments
 (0)