Nebula
Loading...
Searching...
No Matches
rttimacros.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
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::criticalSection.Enter(); \
93 RefCounted::isInCreate = true; \
94 type* newObject = new type; \
95 RefCounted::isInCreate = false; \
96 RefCounted::criticalSection.Leave(); \
97 return newObject; \
98 }\
99 type* type::CreateArray(SizeT num) \
100 { \
101 static_assert(std::is_base_of<Core::RefCounted, type>::value, "Class must inherit from Core::RefCounted"); \
102 RefCounted::criticalSection.Enter(); \
103 RefCounted::isInCreate = true; \
104 type* newObject = new type[num]; \
105 RefCounted::isInCreate = false; \
106 RefCounted::criticalSection.Leave(); \
107 return newObject; \
108 }\
109 bool type::RegisterWithFactory() \
110 { \
111 Core::SysFunc::Setup(); \
112 if (!Core::Factory::Instance()->ClassExists(#type)) \
113 { \
114 Core::Factory::Instance()->Register(&type::RTTI, #type, fourcc); \
115 } \
116 return true; \
117 }
118
119//------------------------------------------------------------------------------
122#define __ImplementClassTemplate(type, baseType) \
123 template <class TEMP> \
124 inline type<TEMP>* type<TEMP>::Create() \
125 { \
126 RefCounted::criticalSection.Enter(); \
127 RefCounted::isInCreate = true; \
128 type<TEMP>* newObject = new type<TEMP>; \
129 RefCounted::isInCreate = false; \
130 RefCounted::criticalSection.Leave(); \
131 return newObject; \
132 } \
133 template <class TEMP> \
134 inline type<TEMP>* type<TEMP>::CreateArray(SizeT num) \
135 { \
136 RefCounted::criticalSection.Enter(); \
137 RefCounted::isInCreate = true; \
138 type<TEMP>* newObject = new type<TEMP>[num]; \
139 RefCounted::isInCreate = false; \
140 RefCounted::criticalSection.Leave(); \
141 return newObject; \
142 }
143
144//------------------------------------------------------------------------------
147#define __ImplementClassVariadicTemplate(type, baseType) \
148 template <class ... TEMP> \
149 inline type<TEMP...>* type<TEMP...>::Create() \
150 { \
151 RefCounted::criticalSection.Enter(); \
152 RefCounted::isInCreate = true; \
153 type<TEMP...>* newObject = new type<TEMP...>; \
154 RefCounted::isInCreate = false; \
155 RefCounted::criticalSection.Leave(); \
156 return newObject; \
157 } \
158 template <class ... TEMP> \
159 inline type<TEMP...>* type<TEMP...>::CreateArray(SizeT num) \
160 { \
161 RefCounted::criticalSection.Enter(); \
162 RefCounted::isInCreate = true; \
163 type<TEMP...>* newObject = new type<TEMP...>[num]; \
164 RefCounted::isInCreate = false; \
165 RefCounted::criticalSection.Leave(); \
166 return newObject; \
167 }
168#else
169//------------------------------------------------------------------------------
172#define __ImplementClass(type, fourcc, baseType) \
173 Core::Rtti type::RTTI(#type, fourcc, type::FactoryCreator, type::FactoryArrayCreator, &baseType::RTTI, sizeof(type)); \
174 Core::Rtti* type::GetRtti() const { return &this->RTTI; } \
175 void* type::FactoryCreator() { return type::Create(); } \
176 void* type::FactoryArrayCreator(SizeT num) { return type::CreateArray(num); } \
177 type* type::Create() \
178 { \
179 static_assert(std::is_base_of<Core::RefCounted, type>::value, "Class must inherit from Core::RefCounted"); \
180 return new type; \
181 }\
182 type* type::CreateArray(SizeT num) \
183 { \
184 static_assert(std::is_base_of<Core::RefCounted, type>::value, "Class must inherit from Core::RefCounted"); \
185 return new type[num]; \
186 }\
187 bool type::RegisterWithFactory() \
188 { \
189 Core::SysFunc::Setup(); \
190 if (!Core::Factory::Instance()->ClassExists(#type)) \
191 { \
192 Core::Factory::Instance()->Register(&type::RTTI, #type, fourcc); \
193 } \
194 return true; \
195 }
196
197//------------------------------------------------------------------------------
200#define __ImplementClassTemplate(type, baseType) \
201 template <class TEMP> \
202 inline type<TEMP>* type<TEMP>::Create() \
203 { \
204 type<TEMP>* newObject = new type<TEMP>; \
205 return newObject; \
206 } \
207 template <class TEMP> \
208 inline type<TEMP>* type<TEMP>::CreateArray(SizeT num) \
209 { \
210 type<TEMP>* newObject = new type<TEMP>[num]; \
211 return newObject; \
212 }
213
214//------------------------------------------------------------------------------
217#define __ImplementClassVariadicTemplate(type, baseType) \
218 template <class ... TEMP> \
219 inline type<TEMP...>* type<TEMP...>::Create() \
220 { \
221 type<TEMP...>* newObject = new type<TEMP...>; \
222 return newObject; \
223 } \
224 template <class ... TEMP> \
225 inline type<TEMP...>* type<TEMP...>::CreateArray(SizeT num) \
226 { \
227 type<TEMP...>* newObject = new type<TEMP...>[num]; \
228 return newObject; \
229 }
230#endif
231
232//------------------------------------------------------------------------------
235#define __ImplementWeakClass(type, fourcc, baseType) \
236 Core::Rtti type::RTTI(#type, fourcc, type::FactoryCreator, type::FactoryArrayCreator, &baseType::RTTI, sizeof(type)); \
237 Core::Rtti* type::GetRtti() const { return &this->RTTI; } \
238 void* type::FactoryCreator() { return type::Create(); } \
239 void* type::FactoryArrayCreator(SizeT num) { return type::CreateArray(num); } \
240 type* type::Create() \
241 { \
242 type* newObject = new type; \
243 return newObject; \
244 }\
245 type* type::CreateArray(SizeT num) \
246 { \
247 type* newObject = new type[num]; \
248 return newObject; \
249 }\
250 bool type::RegisterWithFactory() \
251 { \
252 Core::SysFunc::Setup(); \
253 if (!Core::Factory::Instance()->ClassExists(#type)) \
254 { \
255 Core::Factory::Instance()->Register(&type::RTTI, #type, fourcc); \
256 } \
257 return true; \
258 }
259
260//------------------------------------------------------------------------------
263#define __ImplementWeakRootClass(type, fourcc) \
264 Core::Rtti type::RTTI(#type, fourcc, type::FactoryCreator, type::FactoryArrayCreator, nullptr, sizeof(type)); \
265 Core::Rtti* type::GetRtti() const { return &this->RTTI; } \
266 void* type::FactoryCreator() { return type::Create(); } \
267 void* type::FactoryArrayCreator(SizeT num) { return type::CreateArray(num); } \
268 type* type::Create() \
269 { \
270 type* newObject = new type; \
271 return newObject; \
272 }\
273 type* type::CreateArray(SizeT num) \
274 { \
275 type* newObject = new type[num]; \
276 return newObject; \
277 }\
278 bool type::RegisterWithFactory() \
279 { \
280 Core::SysFunc::Setup(); \
281 if (!Core::Factory::Instance()->ClassExists(#type)) \
282 { \
283 Core::Factory::Instance()->Register(&type::RTTI, #type, fourcc); \
284 } \
285 return true; \
286 }
287
288//------------------------------------------------------------------------------
291#define __ImplementAbstractClass(type, fourcc, baseType) \
292 Core::Rtti type::RTTI(#type, fourcc, nullptr, nullptr, &baseType::RTTI, 0); \
293 Core::Rtti* type::GetRtti() const { return &this->RTTI; }
294
295//------------------------------------------------------------------------------
298#define __ImplementAbstractRootClass(type, fourcc) \
299 Core::Rtti type::RTTI(#type, fourcc, nullptr, nullptr, nullptr, 0); \
300 Core::Rtti* type::GetRtti() const { return &this->RTTI; }
301
302
303//------------------------------------------------------------------------------
307#if NEBULA_DEBUG
308#define __ImplementRootClass(type, fourcc) \
309 Core::Rtti type::RTTI(#type, fourcc, type::FactoryCreator, type::FactoryArrayCreator, nullptr, sizeof(type)); \
310 Core::Rtti* type::GetRtti() const { return &this->RTTI; } \
311 void* type::FactoryCreator() { return type::Create(); } \
312 void* type::FactoryArrayCreator(SizeT num) { return type::CreateArray(num); } \
313 type* type::Create() \
314 { \
315 static_assert(std::is_base_of<Core::RefCounted, type>::value, "Class must inherit from Core::RefCounted"); \
316 RefCounted::criticalSection.Enter(); \
317 RefCounted::isInCreate = true; \
318 type* newObject = new type; \
319 RefCounted::isInCreate = false; \
320 RefCounted::criticalSection.Leave(); \
321 return newObject; \
322 }\
323 type* type::CreateArray(SizeT num) \
324 { \
325 static_assert(std::is_base_of<Core::RefCounted, type>::value, "Class must inherit from Core::RefCounted"); \
326 RefCounted::criticalSection.Enter(); \
327 RefCounted::isInCreate = true; \
328 type* newObject = new type[num]; \
329 RefCounted::isInCreate = false; \
330 RefCounted::criticalSection.Leave(); \
331 return newObject; \
332 }\
333 bool type::RegisterWithFactory() \
334 { \
335 if (!Core::Factory::Instance()->ClassExists(#type)) \
336 { \
337 Core::Factory::Instance()->Register(&type::RTTI, #type, fourcc); \
338 } \
339 return true; \
340 }
341#else
342#define __ImplementRootClass(type, fourcc) \
343 Core::Rtti type::RTTI(#type, fourcc, type::FactoryCreator, type::FactoryArrayCreator, nullptr, sizeof(type)); \
344 Core::Rtti* type::GetRtti() const { return &this->RTTI; } \
345 void* type::FactoryCreator() { return type::Create(); } \
346 void* type::FactoryArrayCreator(SizeT num) { return type::CreateArray(num); } \
347 type* type::Create() \
348 { \
349 static_assert(std::is_base_of<Core::RefCounted, type>::value, "Class must inherit from Core::RefCounted"); \
350 return new type; \
351 }\
352 type* type::CreateArray(SizeT num) \
353 { \
354 static_assert(std::is_base_of<Core::RefCounted, type>::value, "Class must inherit from Core::RefCounted"); \
355 return new type[num]; \
356 }\
357 bool type::RegisterWithFactory() \
358 { \
359 if (!Core::Factory::Instance()->ClassExists(#type)) \
360 { \
361 Core::Factory::Instance()->Register(&type::RTTI, #type, fourcc); \
362 } \
363 return true; \
364 }
365#endif
366
367#define __SetupExternalAttributes() \
368 public: \
369 virtual void SetupExternalAttributes(); \
370 private:
371
372//------------------------------------------------------------------------------
376#define __ImplementEnumBitOperators(type) \
377 inline type operator|(type a, type b) { return static_cast<type>(static_cast<unsigned>(a) | static_cast<unsigned>(b)); }\
378 inline type operator&(type a, type b) { return static_cast<type>(static_cast<unsigned>(a) & static_cast<unsigned>(b)); }\
379 inline type& operator|=(type& a, type b) { a = static_cast<type>(static_cast<unsigned>(a) | static_cast<unsigned>(b)); return a; }\
380 inline type& operator&=(type& a, type b) { a = static_cast<type>(static_cast<unsigned>(a) & static_cast<unsigned>(b)); return a; }\
381 inline type operator|(type a, unsigned b) { return static_cast<type>(static_cast<unsigned>(a) | b); }\
382 inline type operator&(type a, unsigned b) { return static_cast<type>(static_cast<unsigned>(a) & b); }\
383 inline type& operator|=(type& a, unsigned b) { a = static_cast<type>(static_cast<unsigned>(a) | b); return a; }\
384 inline type& operator&=(type& a, unsigned b) { a = static_cast<type>(static_cast<unsigned>(a) & b); return a; }\
385 inline type operator~(type a) { return static_cast<type>(~static_cast<unsigned>(a)); }\
386 inline type operator!(type a) { return static_cast<type>(!static_cast<unsigned>(a)); }\
387 inline unsigned operator|(unsigned a, type b) { return a | static_cast<unsigned>(b); }\
388 inline unsigned operator&(unsigned a, type b) { return a & static_cast<unsigned>(b); }\
389 inline unsigned& operator|=(unsigned& a, type b) { a = a | static_cast<unsigned>(b); return a; }\
390 inline unsigned& operator&=(unsigned& a, type b) { a = a & static_cast<unsigned>(b); return a; }
391
392#define __ImplementEnumComparisonOperators(type) \
393 inline bool operator>(type a, unsigned b) { return static_cast<unsigned>(a) > b; }\
394 inline bool operator>(unsigned a, type b) { return a > static_cast<unsigned>(b); }\
395 inline bool operator>(type a, int b) { return static_cast<int>(a) > b; }\
396 inline bool operator>(int a, type b) { return a > static_cast<int>(b); }\
397 inline bool operator<(type a, unsigned b) { return static_cast<unsigned>(a) < b; }\
398 inline bool operator<(unsigned a, type b) { return a < static_cast<unsigned>(b); }\
399 inline bool operator<(type a, int b) { return static_cast<unsigned>(a) < b; }\
400 inline bool operator<(int a, type b) { return a < static_cast<unsigned>(b); }\
401 inline bool operator>=(type a, unsigned b) { return static_cast<unsigned>(a) >= b; }\
402 inline bool operator>=(unsigned a, type b) { return a >= static_cast<unsigned>(b); }\
403 inline bool operator>=(type a, int b) { return static_cast<unsigned>(a) >= b; }\
404 inline bool operator>=(int a, type b) { return a >= static_cast<unsigned>(b); }\
405 inline bool operator<=(type a, unsigned b) { return static_cast<unsigned>(a) <= b; }\
406 inline bool operator<=(unsigned a, type b) { return a <= static_cast<unsigned>(b); }\
407 inline bool operator<=(type a, int b) { return static_cast<unsigned>(a) <= b; }\
408 inline bool operator<=(int a, type b) { return a <= static_cast<unsigned>(b); }\
409 inline bool operator==(type a, unsigned b) { return static_cast<unsigned>(a) == b; }\
410 inline bool operator==(unsigned a, type b) { return a == static_cast<unsigned>(b); }\
411 inline bool operator==(type a, int b) { return static_cast<unsigned>(a) == b; }\
412 inline bool operator==(int a, type b) { return a == static_cast<unsigned>(b); }\
413 inline bool operator!=(type a, unsigned b) { return static_cast<unsigned>(a) != b; }\
414 inline bool operator!=(unsigned a, type b) { return a != static_cast<unsigned>(b); }\
415 inline bool operator!=(type a, int b) { return static_cast<unsigned>(a) != b; }\
416 inline bool operator!=(int a, type b) { return a != static_cast<unsigned>(b); }
417
418#define __ImplementEnumMutableOperators(type) \
419 inline type operator++(type a) { return static_cast<type>(static_cast<unsigned>(a)++); }\
420 inline type operator--(type a) { return static_cast<type>(static_cast<unsigned>(a)--); }
421
422 /*
423namespace TYPE {\
424 inline bool HasFlags(const TYPE& a, TYPE flags) { return (a & flags) == flags; }\
425 constexpr typename std::underlying_type<TYPE>::type ToInteger(TYPE a) { return static_cast<typename std::underlying_type<TYPE>::type>(a); }\
426 constexpr TYPE FromInteger(typename std::underlying_type<TYPE>::type a) { return static_cast<TYPE>(a); }}
427 */