CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
reflection.h
1
3#ifndef _Reflection_h_
4#define _Reflection_h_
5
6#include <memory>
7#include <typeinfo>
8#include <mutex>
9#include <type_traits>
10
11#include <system/details/demangle.h>
12#include "system/reflection_seprt.h"
13
14namespace System
15{
16
18
22template <typename T>
23T& static_holder()
24{
25 static T value;
26 return value;
27}
28
29class TypeInfo;
30
31template <typename ...BaseTypes> class BaseTypesInfo;
32
33
36template <typename ...AllTypes> struct AnyTypesMatch;
37
42template <typename FirstType, typename SecondType, typename ...OtherTypes>
43struct AnyTypesMatch<FirstType, SecondType, OtherTypes...>
44{
46 static constexpr bool value =
47 std::is_same<FirstType, SecondType>::value ||
48 AnyTypesMatch<FirstType, OtherTypes...>::value ||
49 AnyTypesMatch<SecondType, OtherTypes...>::value;
50};
51
54template <typename LastType>
55struct AnyTypesMatch<LastType>
56{
58 static constexpr bool value = false;
59};
60
61
65template <typename FirstType, typename ...BaseTypes>
66class BaseTypesInfo<FirstType, BaseTypes...>
67{
68 static_assert(!AnyTypesMatch<FirstType, BaseTypes...>::value, "Two or more of the direct basic types match");
69public:
70 typedef FirstType Type;
76 template <typename T>
77 inline static bool CallIsForAll(T *object, const TypeInfo &typeInfo)
78 {
79 return object->FirstType::Is(typeInfo) || BaseTypesInfo<BaseTypes...>::CallIsForAll(object, typeInfo);
80 }
81};
82
84template <>
85class BaseTypesInfo<>
86{
87public:
89 template <typename T>
90 inline static bool CallIsForAll(T*, const TypeInfo&)
91 {
92 return false;
93 }
94};
95
97
98}
99
100
101//-------------------------------------------------------
102// definition of RTTI methonds inside a class declaration
103
106#define __ThisTypeInfo_Compiletime(thisType, hash) \
107 struct ThisTypeInfo : System::TypeInfoPtr { \
108 ThisTypeInfo() : System::TypeInfoPtr(ASPOSE_T(#thisType), hash) {
109
112#define __ThisTypeInfo_Compiletime_Named(thisType, name, hash) \
113 struct ThisTypeInfo : System::TypeInfoPtr { \
114 ThisTypeInfo() : System::TypeInfoPtr(ASPOSE_T(name), hash) {
115
119#define __ThisTypeInfo_Compiletime_Named_Declare_Exported(method_api, thisType, name, hash) \
120 struct ThisTypeInfo : System::TypeInfoPtr { \
121 method_api ThisTypeInfo();
122
125#define __ThisTypeInfo_Runtime(thisType) \
126 struct ThisTypeInfo : System::TypeInfoPtr { \
127 ThisTypeInfo() : System::TypeInfoPtr(typeid(thisType)) {
128
130#define __RTTI_INFO_METHODS(thisType, baseTypes) \
131 public: \
132 \
133 \
134 static const System::TypeInfo& Type() { \
135 return *System::static_holder<ThisTypeInfo>(); \
136 } \
137 \
138 const System::TypeInfo& GetType() const override { \
139 return thisType::Type(); \
140 } \
141 \
142 \
143 \
144 bool Is(const System::TypeInfo &target) const override { \
145 const System::TypeInfo& thisis = thisType::Type(); \
146 return (thisis == target) ? true : baseTypes::CallIsForAll(this, target); \
147 } \
148 private:
149
151#define __RTTI_INFO_METHODS_EXPORTED(method_api, thisType, baseTypes) \
152 public: \
153 \
154 \
155 static method_api const System::TypeInfo& Type(); \
156 \
157 method_api const System::TypeInfo& GetType() const override; \
158 \
159 \
160 \
161 method_api bool Is(const System::TypeInfo &target) const override; \
162 private:
163
164#define __RTTI_INFO_METHODS_BOXED(thisType, baseTypes) \
165 public: \
166 static const System::TypeInfo& Type() { \
167 return *System::static_holder<ThisTypeInfo>(); \
168 } \
169 bool Is(const System::TypeInfo &target) const override { \
170 const System::TypeInfo& thisis = thisType::GetType(); \
171 return (thisis == target) ? true : baseTypes::CallIsForAll(this, target); \
172 } \
173 private:
174
175
177#define RTTI_INFO_END() } };
178
179
180// for class without attributes
181
182// baseTypes: (TypeA)(TypeB)(TypeC)
183
187#define RTTI_INFO(thisType, baseTypes) \
188 __RTTI_INFO_METHODS(thisType, baseTypes) \
189 __ThisTypeInfo_Compiletime(thisType, 0) \
190 RTTI_INFO_END()
191
195#define RTTI_INFO_NAMED(thisType, name, baseTypes) \
196 __RTTI_INFO_METHODS(thisType, baseTypes) \
197 __ThisTypeInfo_Compiletime_Named(thisType, name, 0) \
198 RTTI_INFO_END()
199
203#define RTTI_INFO_NAMED_DECLARE_EXPORTED(method_api, thisType, name, baseTypes) \
204 __RTTI_INFO_METHODS_EXPORTED(method_api, thisType, baseTypes) \
205 __ThisTypeInfo_Compiletime_Named_Declare_Exported(method_api, thisType, name, 0) \
206 };
207
208// baseTypes: (TypeA)(TypeB)(TypeC)
213#define RTTI_INFO_HASH(hash, thisType, baseTypes) \
214 __RTTI_INFO_METHODS(thisType, baseTypes) \
215 __ThisTypeInfo_Compiletime(thisType, hash) \
216 RTTI_INFO_END()
217
218#define __ThisTypeInfo_SetBaseType(baseTypes) \
219 ptr->SetBaseType(&baseTypes::Type::Type);
220
221#define __ThisTypeInfo_SetTemplParamType(templParamType) \
222 ptr->SetTemplParamType(System::ObjectExt::GetType<templParamType>());
223
224#define __ThisTypeInfo_SetDefaultConstructor(elementType) \
225 ptr->AddDefaultConstructor<elementType>();
226
227
228#define RTTI_INFO_TEMPLATE_CLASS_REFL(thisType, baseTypes, templParamType) \
229 __RTTI_INFO_METHODS(thisType, baseTypes) \
230 __ThisTypeInfo_Runtime(thisType) \
231 __ThisTypeInfo_SetBaseType(baseTypes) \
232 __ThisTypeInfo_SetDefaultConstructor(thisType) \
233 __ThisTypeInfo_SetTemplParamType(templParamType) \
234 RTTI_INFO_END()
235
236
237// baseTypes: (TypeA)(TypeB)(TypeC)
241#define RTTI_INFO_TEMPLATE_CLASS(thisType, baseTypes) \
242 __RTTI_INFO_METHODS(thisType, baseTypes) \
243 __ThisTypeInfo_Runtime(thisType) \
244 __ThisTypeInfo_SetBaseType(baseTypes) \
245 RTTI_INFO_END()
246
247// baseTypes: (TypeA)(TypeB)(TypeC)
250#define RTTI_INFO_CUSTOM(thisType, baseTypes) \
251 __RTTI_INFO_METHODS(thisType, baseTypes)
252
253
254// baseTypes: (TypeA)(TypeB)(TypeC)
257#define RTTI_INFO_BOXED_CUSTOM(thisType, baseTypes) \
258 __RTTI_INFO_METHODS_BOXED(thisType, baseTypes)
259
260#define RTTI_INFO_BOXED(thisType, baseTypes) \
261 __RTTI_INFO_METHODS_BOXED(thisType, baseTypes) \
262 __ThisTypeInfo_Runtime(thisType) \
263 __ThisTypeInfo_SetBaseType(baseTypes) \
264 RTTI_INFO_END()
265
266
267// for class with attributes
268
269// baseTypes: (TypeA)(TypeB)(TypeC)
273#define RTTI_INFO_BEGIN(thisType, baseTypes) \
274 __RTTI_INFO_METHODS(thisType, baseTypes) \
275 __ThisTypeInfo_Compiletime(thisType, 0)
276
277// baseTypes: (TypeA)(TypeB)(TypeC)
280#define RTTI_INFO_HASH_BEGIN(hash, thisType, baseTypes) \
281 __RTTI_INFO_METHODS(hash, thisType, baseTypes)
282
284#define RTTI_ATTRIBUTE(type, params) \
285 ptr->AddAttribute( System::static_pointer_cast<System::Object>( System::MakeObject<type> (params) ) );
286
287// for value types
291#define RTTI_INFO_VALUE_TYPE(thisType) \
292 public: \
293 static const System::TypeInfo& Type() { \
294 return *System::static_holder<ThisTypeInfo>(); \
295 } \
296 const System::TypeInfo& GetType() const { \
297 return thisType::Type(); \
298 } \
299 private:\
300 __ThisTypeInfo_Compiletime(thisType, 0) \
301 ptr->SetBaseType(&System::Object::Type); \
302 RTTI_INFO_END()
303
304#endif // _Reflection_h_
@ TypeInfo
Specifies that the member is a type.
Definition: db_command.h:9