Ubpa Static Reflection
- the best C++ static reflection libliray in the whole world
- tiny, handy, elegant
- basic
- (non-static / static) member variable
- (non-static / static) member function
- attribute
- class
- member variable
- member function
- enum
- enumerator
- class
- template
- basic
- attribute
- inheritance
- inherit field (member variable, member function)
- iterate subclasses recursively
#include <USRefl.h>
#include <iostream>
using namespace Ubpa::USRefl;
using namespace std;
struct [[size(8)]] Point {
[[not_serialize]]
float x;
[[info("hello")]]
float y;
};
template<>
struct Type<Point> {
static constexpr std::string_view name = "Point";
using type = Point;
static constexpr FieldList fields = {
Field{"x", &Point::x, AttrList{ Attr{ "not_serialize" } }},
Field{"y", &Point::y, AttrList{ Attr{ "info", "hello" } }}
};
static constexpr AttrList attrs = {
Attr{ "size", 8 }
};
};
int main() {
Point p{ 1,2 };
Type<Point>::fields.ForEach([](auto field) {
cout << field.name << endl;
field.attrs.ForEach([](auto attr) {
cout << attr.name << endl;
if constexpr (attr.has_value)
cout << ": " << attr.value << endl;
});
});
static_assert(Type<Point>::fields.Contains("x"));
Type<Point>::attrs.ForEach([](auto attr) {
cout << "name : " << attr.name << endl;
if constexpr (!attr.has_value)
cout << "value : " << attr.value << endl;
});
ForEachVarOf(p, [](auto&& var) {
cout << var << endl;
});
}
other example
- template
- static
- func
- enum
- inheritance
- 99 line: USRefl_99.h,test USRefl_99.h