-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdb.cpp.in
137 lines (107 loc) · 3.38 KB
/
db.cpp.in
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "db.h"
#include "MemberAdapter.h"
#include <map>
<<includes>>
using namespace std;
using namespace X3D;
namespace {
map<const string, reflection::TypeInfoCommon*> _typeDB;
map<const std::type_info*, reflection::TypeInfoCommon*> _rttiDB;
template <typename T, class Enable = void>
struct TypeInfo : public reflection::TypeInfoCommon {
TypeInfo(const string& typeName, const string& parentType) : reflection::TypeInfoCommon(parentType) {
_typeDB.emplace(typeName, this);
_rttiDB.emplace(&typeid(T), this);
}
template <typename M, void (T::*member)(M*)>
TypeInfo* addSetter(const string& name) {
_setters.emplace(name, new reflection::SetterAdapter<T, M, member>());
return this;
}
template <typename M, void (T::*member)(const M&)>
TypeInfo* addSetter(const string& name) {
_setters.emplace(name, new reflection::SetterAdapter<T, M, member>());
return this;
}
template <typename M, const M& (T::*member)() const>
TypeInfo* addGetter(const string& name) {
_getters.emplace(name, new reflection::GetterAdapter<T, M, member>());
return this;
}
reflection::Object* create() {
return new T;
}
};
template <typename T>
struct TypeInfo<T, typename std::enable_if<std::is_abstract<T>::value >::type> : public reflection::TypeInfoCommon {
TypeInfo(const string& typeName, const string& parentType) : reflection::TypeInfoCommon(parentType) {
_typeDB.emplace(typeName, this);
_rttiDB.emplace(&typeid(T), this);
}
template <typename M, void (T::*member)(M*)>
TypeInfo* addSetter(const string& name) {
_setters.emplace(name, new reflection::SetterAdapter<T, M, member>());
return this;
}
template <typename M, void (T::*member)(const M&)>
TypeInfo* addSetter(const string& name) {
_setters.emplace(name, new reflection::SetterAdapter<T, M, member>());
return this;
}
template <typename M, const M& (T::*member)() const>
TypeInfo* addGetter(const string& name) {
_getters.emplace(name, new reflection::GetterAdapter<T, M, member>());
return this;
}
reflection::Object* create() {
return nullptr;
}
};
void initMap() {
<<constructors>>
auto i = _typeDB.begin();
for(;i != _typeDB.end(); ++i) {
i->second->_resolveParentInfo();
}
}
}
namespace reflection {
void alias(const std::string& type, const std::string& alias) {
if(_typeDB.empty()) {
initMap();
}
auto i = _typeDB.find(type);
if(i == _typeDB.end()) {
throw runtime_error("type '"+type+"' is not defined");
}
_typeDB.emplace(alias, i->second);
}
TypeInfoCommon* getTypeInfo(const string& type) {
if(_typeDB.empty()) {
initMap();
}
auto i = _typeDB.find(type);
return i == _typeDB.end() ? nullptr : i->second;
}
TypeInfoCommon* getTypeInfo(const reflection::Object& obj) {
if(_typeDB.empty()) {
initMap();
}
auto i = _rttiDB.find(&typeid(obj));
return i == _rttiDB.end() ? nullptr : i->second;
}
std::string getTypeName(const Object& obj) {
if(_typeDB.empty()) {
initMap();
}
auto i = _rttiDB.find(&typeid(obj));
if(i == _rttiDB.end())
return "";
for(const auto& d : _typeDB) {
if(i->second == d.second) {
return d.first;
}
}
return "";
}
}