Nebula
Loading...
Searching...
No Matches
rttimacros.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
13
14//------------------------------------------------------------------------------
18#define __DeclareClass(type) \
19public: \
20 void* operator new(size_t size) \
21 { \
22 return RTTI.AllocInstanceMemory(); \
23 }; \
24 void* operator new[](size_t num) \
25 { \
26 return RTTI.AllocInstanceMemoryArray(num); \
27 }; \
28 void operator delete(void* p) \
29 { \
30 RTTI.FreeInstanceMemory(p); \
31 }; \
32 void operator delete(void* p, void*) \
33 { \
34 }; \
35 void operator delete[](void* p) \
36 { \
37 RTTI.FreeInstanceMemory(p); \
38 }; \
39 static Core::Rtti RTTI; \
40 static void* FactoryCreator(); \
41 static void* FactoryArrayCreator(SizeT num); \
42 static type* Create(); \
43 static type* CreateArray(SizeT num); \
44 static bool RegisterWithFactory(); \
45 virtual Core::Rtti* GetRtti() const; \
46private:
47
48#define __DeclareTemplateClass(type, temp) \
49public: \
50 void* operator new(size_t size) \
51 { \
52 return Memory::Alloc(Memory::ObjectHeap, size); \
53 }; \
54 void operator delete(void* p) \
55 { \
56 Memory::Free(Memory::ObjectHeap, p); \
57 }; \
58 static type<temp>* Create(); \
59 static type<temp>* CreateArray(SizeT num); \
60private:
61
62#define __DeclareAbstractClass(class_name) \
63public: \
64 static Core::Rtti RTTI; \
65 virtual Core::Rtti* GetRtti() const; \
66private:
67
68//------------------------------------------------------------------------------
73#define __RegisterClass(type) \
74 static const bool type##_registered = type::RegisterWithFactory(); \
75
76//------------------------------------------------------------------------------
83#if NEBULA_DEBUG
84#define __ImplementClass(type, fourcc, baseType) \
85 Core::Rtti type::RTTI(#type, fourcc, type::FactoryCreator, type::FactoryArrayCreator, &baseType::RTTI, sizeof(type)); \
86 Core::Rtti* type::GetRtti() const { return &this->RTTI; } \
87 void* type::FactoryCreator() { return type::Create(); } \
88 void* type::FactoryArrayCreator(SizeT num) { return type::CreateArray(num); } \
89 type* type::Create() \
90 { \
91 static_assert(std::is_base_of<Core::RefCounted, type>::value, "Class must inherit from Core::RefCounted"); \
92 RefCounted::isInCreate = true; \
93 type* newObject = new type; \
94 RefCounted::isInCreate = false; \
95 return newObject; \
96 }\
97 type* type::CreateArray(SizeT num) \
98 { \
99 static_assert(std::is_base_of<Core::RefCounted, type>::value, "Class must inherit from Core::RefCounted"); \
100 RefCounted::isInCreate = true; \
101 type* newObject = new type[num]; \
102 RefCounted::isInCreate = false; \
103 return newObject; \
104 }\
105 bool type::RegisterWithFactory() \
106 { \
107 Core::SysFunc::Setup(); \
108 if (!Core::Factory::Instance()->ClassExists(#type)) \
109 { \
110 Core::Factory::Instance()->Register(&type::RTTI, #type, fourcc); \
111 } \
112 return true; \
113 }
114
115//------------------------------------------------------------------------------
118#define __ImplementClassTemplate(type, baseType) \
119 template <class TEMP> \
120 inline type<TEMP>* type<TEMP>::Create() \
121 { \
122 RefCounted::isInCreate = true; \
123 type<TEMP>* newObject = new type<TEMP>; \
124 RefCounted::isInCreate = false; \
125 return newObject; \
126 } \
127 template <class TEMP> \
128 inline type<TEMP>* type<TEMP>::CreateArray(SizeT num) \
129 { \
130 RefCounted::isInCreate = true; \
131 type<TEMP>* newObject = new type<TEMP>[num]; \
132 RefCounted::isInCreate = false; \
133 return newObject; \
134 }
135
136//------------------------------------------------------------------------------
139#define __ImplementClassVariadicTemplate(type, baseType) \
140 template <class ... TEMP> \
141 inline type<TEMP...>* type<TEMP...>::Create() \
142 { \
143 RefCounted::isInCreate = true; \
144 type<TEMP...>* newObject = new type<TEMP...>; \
145 RefCounted::isInCreate = false; \
146 return newObject; \
147 } \
148 template <class ... TEMP> \
149 inline type<TEMP...>* type<TEMP...>::CreateArray(SizeT num) \
150 { \
151 RefCounted::isInCreate = true; \
152 type<TEMP...>* newObject = new type<TEMP...>[num]; \
153 RefCounted::isInCreate = false; \
154 return newObject; \
155 }
156#else
157//------------------------------------------------------------------------------
160#define __ImplementClass(type, fourcc, baseType) \
161 Core::Rtti type::RTTI(#type, fourcc, type::FactoryCreator, type::FactoryArrayCreator, &baseType::RTTI, sizeof(type)); \
162 Core::Rtti* type::GetRtti() const { return &this->RTTI; } \
163 void* type::FactoryCreator() { return type::Create(); } \
164 void* type::FactoryArrayCreator(SizeT num) { return type::CreateArray(num); } \
165 type* type::Create() \
166 { \
167 static_assert(std::is_base_of<Core::RefCounted, type>::value, "Class must inherit from Core::RefCounted"); \
168 return new type; \
169 }\
170 type* type::CreateArray(SizeT num) \
171 { \
172 static_assert(std::is_base_of<Core::RefCounted, type>::value, "Class must inherit from Core::RefCounted"); \
173 return new type[num]; \
174 }\
175 bool type::RegisterWithFactory() \
176 { \
177 Core::SysFunc::Setup(); \
178 if (!Core::Factory::Instance()->ClassExists(#type)) \
179 { \
180 Core::Factory::Instance()->Register(&type::RTTI, #type, fourcc); \
181 } \
182 return true; \
183 }
184
185//------------------------------------------------------------------------------
188#define __ImplementClassTemplate(type, baseType) \
189 template <class TEMP> \
190 inline type<TEMP>* type<TEMP>::Create() \
191 { \
192 type<TEMP>* newObject = new type<TEMP>; \
193 return newObject; \
194 } \
195 template <class TEMP> \
196 inline type<TEMP>* type<TEMP>::CreateArray(SizeT num) \
197 { \
198 type<TEMP>* newObject = new type<TEMP>[num]; \
199 return newObject; \
200 }
201
202//------------------------------------------------------------------------------
205#define __ImplementClassVariadicTemplate(type, baseType) \
206 template <class ... TEMP> \
207 inline type<TEMP...>* type<TEMP...>::Create() \
208 { \
209 type<TEMP...>* newObject = new type<TEMP...>; \
210 return newObject; \
211 } \
212 template <class ... TEMP> \
213 inline type<TEMP...>* type<TEMP...>::CreateArray(SizeT num) \
214 { \
215 type<TEMP...>* newObject = new type<TEMP...>[num]; \
216 return newObject; \
217 }
218#endif
219
220//------------------------------------------------------------------------------
223#define __ImplementWeakClass(type, fourcc, baseType) \
224 Core::Rtti type::RTTI(#type, fourcc, type::FactoryCreator, type::FactoryArrayCreator, &baseType::RTTI, sizeof(type)); \
225 Core::Rtti* type::GetRtti() const { return &this->RTTI; } \
226 void* type::FactoryCreator() { return type::Create(); } \
227 void* type::FactoryArrayCreator(SizeT num) { return type::CreateArray(num); } \
228 type* type::Create() \
229 { \
230 type* newObject = new type; \
231 return newObject; \
232 }\
233 type* type::CreateArray(SizeT num) \
234 { \
235 type* newObject = new type[num]; \
236 return newObject; \
237 }\
238 bool type::RegisterWithFactory() \
239 { \
240 Core::SysFunc::Setup(); \
241 if (!Core::Factory::Instance()->ClassExists(#type)) \
242 { \
243 Core::Factory::Instance()->Register(&type::RTTI, #type, fourcc); \
244 } \
245 return true; \
246 }
247
248//------------------------------------------------------------------------------
251#define __ImplementWeakRootClass(type, fourcc) \
252 Core::Rtti type::RTTI(#type, fourcc, type::FactoryCreator, type::FactoryArrayCreator, nullptr, sizeof(type)); \
253 Core::Rtti* type::GetRtti() const { return &this->RTTI; } \
254 void* type::FactoryCreator() { return type::Create(); } \
255 void* type::FactoryArrayCreator(SizeT num) { return type::CreateArray(num); } \
256 type* type::Create() \
257 { \
258 type* newObject = new type; \
259 return newObject; \
260 }\
261 type* type::CreateArray(SizeT num) \
262 { \
263 type* newObject = new type[num]; \
264 return newObject; \
265 }\
266 bool type::RegisterWithFactory() \
267 { \
268 Core::SysFunc::Setup(); \
269 if (!Core::Factory::Instance()->ClassExists(#type)) \
270 { \
271 Core::Factory::Instance()->Register(&type::RTTI, #type, fourcc); \
272 } \
273 return true; \
274 }
275
276//------------------------------------------------------------------------------
279#define __ImplementAbstractClass(type, fourcc, baseType) \
280 Core::Rtti type::RTTI(#type, fourcc, nullptr, nullptr, &baseType::RTTI, 0); \
281 Core::Rtti* type::GetRtti() const { return &this->RTTI; }
282
283//------------------------------------------------------------------------------
286#define __ImplementAbstractRootClass(type, fourcc) \
287 Core::Rtti type::RTTI(#type, fourcc, nullptr, nullptr, nullptr, 0); \
288 Core::Rtti* type::GetRtti() const { return &this->RTTI; }
289
290
291//------------------------------------------------------------------------------
295#if NEBULA_DEBUG
296#define __ImplementRootClass(type, fourcc) \
297 Core::Rtti type::RTTI(#type, fourcc, type::FactoryCreator, type::FactoryArrayCreator, nullptr, sizeof(type)); \
298 Core::Rtti* type::GetRtti() const { return &this->RTTI; } \
299 void* type::FactoryCreator() { return type::Create(); } \
300 void* type::FactoryArrayCreator(SizeT num) { return type::CreateArray(num); } \
301 type* type::Create() \
302 { \
303 static_assert(std::is_base_of<Core::RefCounted, type>::value, "Class must inherit from Core::RefCounted"); \
304 RefCounted::isInCreate = true; \
305 type* newObject = new type; \
306 RefCounted::isInCreate = false; \
307 return newObject; \
308 }\
309 type* type::CreateArray(SizeT num) \
310 { \
311 static_assert(std::is_base_of<Core::RefCounted, type>::value, "Class must inherit from Core::RefCounted"); \
312 RefCounted::isInCreate = true; \
313 type* newObject = new type[num]; \
314 RefCounted::isInCreate = false; \
315 return newObject; \
316 }\
317 bool type::RegisterWithFactory() \
318 { \
319 if (!Core::Factory::Instance()->ClassExists(#type)) \
320 { \
321 Core::Factory::Instance()->Register(&type::RTTI, #type, fourcc); \
322 } \
323 return true; \
324 }
325#else
326#define __ImplementRootClass(type, fourcc) \
327 Core::Rtti type::RTTI(#type, fourcc, type::FactoryCreator, type::FactoryArrayCreator, nullptr, sizeof(type)); \
328 Core::Rtti* type::GetRtti() const { return &this->RTTI; } \
329 void* type::FactoryCreator() { return type::Create(); } \
330 void* type::FactoryArrayCreator(SizeT num) { return type::CreateArray(num); } \
331 type* type::Create() \
332 { \
333 static_assert(std::is_base_of<Core::RefCounted, type>::value, "Class must inherit from Core::RefCounted"); \
334 return new type; \
335 }\
336 type* type::CreateArray(SizeT num) \
337 { \
338 static_assert(std::is_base_of<Core::RefCounted, type>::value, "Class must inherit from Core::RefCounted"); \
339 return new type[num]; \
340 }\
341 bool type::RegisterWithFactory() \
342 { \
343 if (!Core::Factory::Instance()->ClassExists(#type)) \
344 { \
345 Core::Factory::Instance()->Register(&type::RTTI, #type, fourcc); \
346 } \
347 return true; \
348 }
349#endif
350
351#define __SetupExternalAttributes() \
352 public: \
353 virtual void SetupExternalAttributes(); \
354 private:
355
356//------------------------------------------------------------------------------
360#define __ImplementEnumBitOperators(type) \
361 inline type operator|(type a, type b) { return static_cast<type>(static_cast<unsigned>(a) | static_cast<unsigned>(b)); }\
362 inline type operator&(type a, type b) { return static_cast<type>(static_cast<unsigned>(a) & static_cast<unsigned>(b)); }\
363 inline type& operator|=(type& a, type b) { a = static_cast<type>(static_cast<unsigned>(a) | static_cast<unsigned>(b)); return a; }\
364 inline type& operator&=(type& a, type b) { a = static_cast<type>(static_cast<unsigned>(a) & static_cast<unsigned>(b)); return a; }\
365 inline type operator|(type a, unsigned b) { return static_cast<type>(static_cast<unsigned>(a) | b); }\
366 inline type operator&(type a, unsigned b) { return static_cast<type>(static_cast<unsigned>(a) & b); }\
367 inline type& operator|=(type& a, unsigned b) { a = static_cast<type>(static_cast<unsigned>(a) | b); return a; }\
368 inline type& operator&=(type& a, unsigned b) { a = static_cast<type>(static_cast<unsigned>(a) & b); return a; }\
369 inline type operator~(type a) { return static_cast<type>(~static_cast<unsigned>(a)); }\
370 inline type operator!(type a) { return static_cast<type>(!static_cast<unsigned>(a)); }\
371 inline unsigned operator|(unsigned a, type b) { return a | static_cast<unsigned>(b); }\
372 inline unsigned operator&(unsigned a, type b) { return a & static_cast<unsigned>(b); }\
373 inline unsigned& operator|=(unsigned& a, type b) { a = a | static_cast<unsigned>(b); return a; }\
374 inline unsigned& operator&=(unsigned& a, type b) { a = a & static_cast<unsigned>(b); return a; }\
375 inline bool HasFlags(type a, type flags) { return (a & flags) == flags; }\
376 inline bool HasFlags(unsigned a, type flags) { return (a & static_cast<unsigned>(flags)) == static_cast<unsigned>(flags); }\
377 inline bool HasFlags(type a, unsigned flags) { return (static_cast<unsigned>(a) & flags) == flags; }
378
379#define __ImplementEnumComparisonOperators(type) \
380 inline bool operator>(type a, unsigned b) { return static_cast<unsigned>(a) > b; }\
381 inline bool operator>(unsigned a, type b) { return a > static_cast<unsigned>(b); }\
382 inline bool operator>(type a, int b) { return static_cast<int>(a) > b; }\
383 inline bool operator>(int a, type b) { return a > static_cast<int>(b); }\
384 inline bool operator<(type a, unsigned b) { return static_cast<unsigned>(a) < b; }\
385 inline bool operator<(unsigned a, type b) { return a < static_cast<unsigned>(b); }\
386 inline bool operator<(type a, int b) { return static_cast<unsigned>(a) < b; }\
387 inline bool operator<(int a, type b) { return a < static_cast<unsigned>(b); }\
388 inline bool operator>=(type a, unsigned b) { return static_cast<unsigned>(a) >= b; }\
389 inline bool operator>=(unsigned a, type b) { return a >= static_cast<unsigned>(b); }\
390 inline bool operator>=(type a, int b) { return static_cast<unsigned>(a) >= b; }\
391 inline bool operator>=(int a, type b) { return a >= static_cast<unsigned>(b); }\
392 inline bool operator<=(type a, unsigned b) { return static_cast<unsigned>(a) <= b; }\
393 inline bool operator<=(unsigned a, type b) { return a <= static_cast<unsigned>(b); }\
394 inline bool operator<=(type a, int b) { return static_cast<unsigned>(a) <= b; }\
395 inline bool operator<=(int a, type b) { return a <= static_cast<unsigned>(b); }\
396 inline bool operator==(type a, unsigned b) { return static_cast<unsigned>(a) == b; }\
397 inline bool operator==(unsigned a, type b) { return a == static_cast<unsigned>(b); }\
398 inline bool operator==(type a, int b) { return static_cast<unsigned>(a) == b; }\
399 inline bool operator==(int a, type b) { return a == static_cast<unsigned>(b); }\
400 inline bool operator!=(type a, unsigned b) { return static_cast<unsigned>(a) != b; }\
401 inline bool operator!=(unsigned a, type b) { return a != static_cast<unsigned>(b); }\
402 inline bool operator!=(type a, int b) { return static_cast<unsigned>(a) != b; }\
403 inline bool operator!=(int a, type b) { return a != static_cast<unsigned>(b); }
404
405#define __ImplementEnumMutableOperators(type) \
406 inline type operator++(type a) { return static_cast<type>(static_cast<unsigned>(a)++); }\
407 inline type operator--(type a) { return static_cast<type>(static_cast<unsigned>(a)--); }
408
409 /*
410namespace TYPE {\
411 inline bool HasFlags(const TYPE& a, TYPE flags) { return (a & flags) == flags; }\
412 constexpr typename std::underlying_type<TYPE>::type ToInteger(TYPE a) { return static_cast<typename std::underlying_type<TYPE>::type>(a); }\
413 constexpr TYPE FromInteger(typename std::underlying_type<TYPE>::type a) { return static_cast<TYPE>(a); }}
414 */