-
Notifications
You must be signed in to change notification settings - Fork 1
/
reflection.hpp
64 lines (53 loc) · 1.95 KB
/
reflection.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* >= C++11 Reflection
*
* - Daniel Bloemendal
*/
#pragma once
struct for_each_impl {
template<std::size_t I = 0, typename Function, typename... Items>
static typename std::enable_if<I == sizeof...(Items), void>::type
for_each(std::tuple<Items...>& tuple, Function& fn) {}
template<std::size_t I = 0, typename Function, typename... Items>
static typename std::enable_if<I < sizeof...(Items), void>::type
for_each(std::tuple<Items...>& tuple, Function& fn) {
fn(std::get<I>(tuple));
for_each<I + 1, Function, Items...>(tuple, fn);
}
};
template<typename Function, typename... Items>
inline void for_each(std::tuple<Items...>& tuple, Function& fn) {
for_each_impl::for_each(tuple, fn);
}
template<typename Function, typename... Items>
inline void for_each(std::tuple<Items...>&& tuple, Function& fn) {
for_each_impl::for_each(tuple, fn);
}
template<typename Klass, typename Type>
struct field {
// Type definitions
enum { flags_constant = 1, flags_private = 2 };
typedef Type (Klass::*pointer_type);
// Field information
std::string name;
pointer_type pointer;
int flags;
std::string caption;
std::string doc;
};
template<typename Klass, typename Type>
inline field<Klass, Type> make_field(std::string name, Type (Klass::*pointer), int flags = 0, std::string caption = "", std::string doc = "") {
return field<Klass, Type>{std::move(name), pointer, flags, std::move(caption), std::move(doc)};
}
template<typename Klass>
struct meta {
static std::tuple<> fields() { return std::tuple<>(); }
};
#define REFLECT_REGISTER(cls, ...) \
template<> \
struct meta<cls> { \
typedef cls klass; \
static auto fields() -> decltype(std::make_tuple(__VA_ARGS__)) { return std::make_tuple(__VA_ARGS__); } \
};
#define REFLECT_FIELD(name, caption) \
make_field(#name, &klass::name, 0, caption, "")