Skip to content

Commit b6711fd

Browse files
code cleanups and compiler warning fixes
Signed-off-by: Christian Parpart <[email protected]>
1 parent 1ef843f commit b6711fd

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

include/reflection-cpp/reflection.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ namespace detail
7777
};
7878

7979
template <const std::string_view&... Strs>
80-
inline constexpr std::string_view join()
80+
constexpr std::string_view join()
8181
{
8282
constexpr auto joined_arr = []() {
8383
constexpr size_t len = (Strs.size() + ... + 0);
@@ -178,14 +178,14 @@ elisp functions to fill the ToTuple function
178178

179179
template <class T, size_t N = CountMembers<T>>
180180
requires(N <= MaxReflectionMemerCount)
181-
inline constexpr decltype(auto) ToTuple(T&& t) noexcept
181+
constexpr decltype(auto) ToTuple(T&& t) noexcept
182182
{
183183
if constexpr (N == 0)
184184
return std::tuple {};
185185
// clang-format off
186186
else if constexpr (N == 1)
187187
{
188-
auto& [p0] = t;
188+
auto& [p0] = std::forward<T>(t); // TODO: We need to fix the gen script and apply that to all the cases below
189189
return std::tie(p0);
190190
}
191191
else if constexpr (N == 2)
@@ -795,7 +795,7 @@ std::string Inspect(std::vector<Object> const& objects)
795795
}
796796

797797
template <typename Object, typename Callback>
798-
void CollectDifferences(const Object& lhs, const Object& rhs, Callback&& callback)
798+
void CollectDifferences(const Object& lhs, const Object& rhs, Callback const& callback)
799799
{
800800
template_for<0, CountMembers<Object>>([&]<auto I>() {
801801
if constexpr (std::equality_comparable<MemberTypeOf<I, Object>>)
@@ -814,7 +814,7 @@ void CollectDifferences(const Object& lhs, const Object& rhs, Callback&& callbac
814814

815815
template <typename Object, typename Callback>
816816
requires std::same_as<void, std::invoke_result_t<Callback, size_t, MemberTypeOf<0, Object>, MemberTypeOf<0, Object>>>
817-
void CollectDifferences(const Object& lhs, const Object& rhs, Callback&& callback)
817+
void CollectDifferences(const Object& lhs, const Object& rhs, Callback const& callback)
818818
{
819819
template_for<0, CountMembers<Object>>([&]<auto I>() {
820820
if constexpr (std::equality_comparable<MemberTypeOf<I, Object>>)

test-reflection-cpp.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#include <catch2/catch_test_macros.hpp>
55

6-
#include <iostream>
76
#include <string>
87
#include <string_view>
98

@@ -23,7 +22,7 @@ struct TestStruct
2322
Person e;
2423
};
2524

26-
enum Color
25+
enum Color : std::uint8_t
2726
{
2827
Red,
2928
Green,
@@ -71,7 +70,7 @@ TEST_CASE("core", "[reflection]")
7170
auto s = SingleValueRecord { 42 };
7271
CHECK(Reflection::Inspect(s) == "value=42");
7372

74-
auto p = Person { "John Doe", "[email protected]", 42 };
73+
auto p = Person { .name = "John Doe", .email = "[email protected]", .age = 42 };
7574
auto const result = Reflection::Inspect(p);
7675
CHECK(result == R"(name="John Doe" email="[email protected]" age=42)");
7776
}
@@ -89,14 +88,20 @@ name="John Doe" email="[email protected]" age=43
8988

9089
TEST_CASE("nested", "[reflection]")
9190
{
92-
auto ts = TestStruct { 1, 2.0f, 3.0, "hello", { "John Doe", "[email protected]", 42 } };
91+
auto ts = TestStruct {
92+
.a = 1,
93+
.b = 2.0f,
94+
.c = 3.0,
95+
.d = "hello",
96+
.e = { .name = "John Doe", .email = "[email protected]", .age = 42 },
97+
};
9398
auto const result = Reflection::Inspect(ts);
9499
CHECK(result == R"(a=1 b=2 c=3 d="hello" e={name="John Doe" email="[email protected]" age=42})");
95100
}
96101

97102
TEST_CASE("EnumerateMembers.index_and_value", "[reflection]")
98103
{
99-
auto ps = Person { "John Doe", "[email protected]", 42 };
104+
auto ps = Person { .name = "John Doe", .email = "[email protected]", .age = 42 };
100105
Reflection::EnumerateMembers(ps, []<size_t I>(auto&& value) {
101106
if constexpr (I == 0)
102107
{
@@ -118,22 +123,22 @@ TEST_CASE("EnumerateMembers.index_and_type", "[reflection]")
118123
Reflection::EnumerateMembers<Person>([]<auto I, typename T>() {
119124
if constexpr (I == 0)
120125
{
121-
static_assert(std::same_as<T,std::string_view>);
126+
static_assert(std::same_as<T, std::string_view>);
122127
}
123128
if constexpr (I == 1)
124129
{
125-
static_assert(std::same_as<T,std::string>);
130+
static_assert(std::same_as<T, std::string>);
126131
}
127132
if constexpr (I == 2)
128133
{
129-
static_assert(std::same_as<T,int>);
134+
static_assert(std::same_as<T, int>);
130135
}
131136
});
132137
}
133138

134139
TEST_CASE("CallOnMembers", "[reflection]")
135140
{
136-
auto ps = Person { "John Doe", "[email protected]", 42 };
141+
auto ps = Person { .name = "John Doe", .email = "[email protected]", .age = 42 };
137142
std::string result;
138143
Reflection::CallOnMembers(ps, [&result](auto&& name, auto&& value) {
139144
result += name;
@@ -163,7 +168,7 @@ struct S
163168

164169
TEST_CASE("FoldMembers.value", "[reflection]")
165170
{
166-
auto const s = S { 1, 2, 3 };
171+
auto const s = S { .a = 1, .b = 2, .c = 3 };
167172
auto const result = Reflection::FoldMembers(
168173
s, 0, [](auto&& /*name*/, auto&& memberValue, auto&& accum) { return accum + memberValue; });
169174

@@ -203,23 +208,20 @@ TEST_CASE("Compare.simple", "[reflection]")
203208
CHECK(diff == "id: 1 != 2\nname: John Doe != Jane Doe\nage: 42 != 43\n");
204209
}
205210

206-
207-
208211
TEST_CASE("Compare.simple_with_indexing", "[reflection]")
209212
{
210213
auto const r1 = Record { .id = 1, .name = "John Doe", .age = 42 };
211214
auto const r2 = Record { .id = 2, .name = "John Doe", .age = 42 };
212215

213-
size_t check = -1;
214-
auto differenceCallback = [&](size_t ind, auto const& lhs, auto const& rhs) {
215-
check = ind;
216+
auto check = static_cast<size_t>(-1);
217+
auto differenceCallback = [&](size_t index, auto const& /*lhs*/, auto const& /*rhs*/) {
218+
check = index;
216219
};
217220

218221
Reflection::CollectDifferences(r1, r2, differenceCallback);
219222
CHECK(check == 0);
220223
}
221224

222-
223225
struct Table
224226
{
225227
Record first;

0 commit comments

Comments
 (0)