Skip to content

Commit 36fba5c

Browse files
Started working on inheritance
1 parent 02037cf commit 36fba5c

2 files changed

Lines changed: 55 additions & 4 deletions

File tree

include/rfl/internal/cpp26/get_field_names.hpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,31 @@ consteval auto get_field_names_impl() {
3535

3636
template <auto _i, auto _j>
3737
consteval bool is_same() {
38-
static_assert(_i == _j, "Mismatched types");
38+
static_assert(_i == _j, "Mismatched lengths");
3939
return _i == _j;
4040
}
4141

42+
template <class Type>
43+
consteval std::vector<std::meta::info> get_members() {
44+
constexpr auto ctx = std::meta::access_context::current();
45+
46+
std::vector<std::meta::info> members;
47+
48+
constexpr auto nonstatic_members = std::define_static_array(
49+
std::meta::nonstatic_data_members_of(^^Type, ctx));
50+
51+
template for (auto member : nonstatic_members) { members.push_back(member); }
52+
53+
return members;
54+
}
55+
4256
template <class T>
4357
consteval auto get_field_names() {
4458
using Type = std::remove_cvref_t<T>;
4559
if constexpr (std::is_pointer_v<Type>) {
4660
return get_field_names<std::remove_pointer_t<Type>>();
4761
} else {
48-
constexpr auto members =
49-
std::define_static_array(std::meta::nonstatic_data_members_of(
50-
^^Type, std::meta::access_context::current()));
62+
constexpr auto members = std::define_static_array(get_members<Type>());
5163

5264
using TupleT = decltype(bind_to_tuple(std::declval<Type&>()));
5365

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#ifdef REFLECTCPP_USE_CPP_REFLECTION
2+
3+
// Only supported with C++26 reflection
4+
5+
#include <gtest/gtest.h>
6+
7+
#include <cassert>
8+
#include <rfl.hpp>
9+
10+
#include "rfl/num_fields.hpp"
11+
12+
namespace test_inheritance3 {
13+
14+
struct Base {
15+
int x;
16+
};
17+
18+
struct Derived : public Base {
19+
int y;
20+
};
21+
22+
TEST(json, test_inheritance3) {
23+
Derived derived;
24+
const auto derived_view = rfl::to_view(derived);
25+
26+
constexpr auto name_x =
27+
rfl::tuple_element_t<0, typename rfl::named_tuple_t<T>::Fields>::name();
28+
constexpr auto name_y =
29+
rfl::tuple_element_t<1, typename rfl::named_tuple_t<T>::Fields>::name();
30+
31+
static_assert(name_x == "x");
32+
static_assert(name_y == "y");
33+
34+
EXPECT_TRUE(true);
35+
}
36+
37+
} // namespace test_inheritance3
38+
39+
#endif

0 commit comments

Comments
 (0)