Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
RTTI.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
10
12
14// RTTI
16
122{
123public:
125 using pCreateObjectFunction = void *(*)();
126
128 using pDestructObjectFunction = void (*)(void *inObject);
129
131 using pCreateRTTIFunction = void (*)(RTTI &inRTTI);
132
134 RTTI(const char *inName, int inSize, pCreateObjectFunction inCreateObject, pDestructObjectFunction inDestructObject);
135 RTTI(const char *inName, int inSize, pCreateObjectFunction inCreateObject, pDestructObjectFunction inDestructObject, pCreateRTTIFunction inCreateRTTI);
136
137 // Properties
138 inline const char * GetName() const { return mName; }
139 void SetName(const char *inName) { mName = inName; }
140 inline int GetSize() const { return mSize; }
141 bool IsAbstract() const { return mCreate == nullptr || mDestruct == nullptr; }
142 int GetBaseClassCount() const;
143 const RTTI * GetBaseClass(int inIdx) const;
144 uint32 GetHash() const;
145
147 void * CreateObject() const;
148
150 void DestructObject(void *inObject) const;
151
153 void AddBaseClass(const RTTI *inRTTI, int inOffset);
154
156 bool operator == (const RTTI &inRHS) const;
157 bool operator != (const RTTI &inRHS) const { return !(*this == inRHS); }
158
160 bool IsKindOf(const RTTI *inRTTI) const;
161
163 const void * CastTo(const void *inObject, const RTTI *inRTTI) const;
164
165#ifdef JPH_OBJECT_STREAM
167 void AddAttribute(const SerializableAttribute &inAttribute);
168 int GetAttributeCount() const;
169 const SerializableAttribute & GetAttribute(int inIdx) const;
170#endif // JPH_OBJECT_STREAM
171
173 template <class T>
174 static void * sCreateObjectInternal()
175 {
176 #ifdef JPH_DISABLE_CUSTOM_ALLOCATOR
177 return new T;
178 #else
179 void *object;
180 if constexpr (alignof(T) > JPH_DEFAULT_ALLOCATE_ALIGNMENT)
181 object = AlignedAllocate(sizeof(T), alignof(T));
182 else
183 object = JPH_INTERNAL_DEFAULT_ALLOCATE(sizeof(T));
184 return ::new (object) T;
185 #endif
186 }
187
189 template <class T>
190 static void sDestructObjectInternal(void *inObject)
191 {
192 #ifdef JPH_DISABLE_CUSTOM_ALLOCATOR
193 delete static_cast<T *>(inObject);
194 #else
195 static_cast<T *>(inObject)->~T();
196 if constexpr (alignof(T) > JPH_DEFAULT_ALLOCATE_ALIGNMENT)
197 AlignedFree(inObject);
198 else
200 #endif
201 }
202
203protected:
206 {
207 const RTTI * mRTTI;
209 };
210
211 const char * mName;
212 int mSize;
216#ifdef JPH_OBJECT_STREAM
218#endif // JPH_OBJECT_STREAM
219};
220
222// Add run time type info to types that don't have virtual functions
224
225// JPH_DECLARE_RTTI_NON_VIRTUAL
226#define JPH_DECLARE_RTTI_NON_VIRTUAL(linkage, class_name) \
227public: \
228 JPH_OVERRIDE_NEW_DELETE \
229 friend linkage JPH::RTTI * GetRTTIOfType(class_name *); \
230 friend inline const JPH::RTTI *GetRTTI([[maybe_unused]] const class_name *inObject) { return GetRTTIOfType(static_cast<class_name *>(nullptr)); } \
231 static void sCreateRTTI(JPH::RTTI &inRTTI); \
232
233// JPH_IMPLEMENT_RTTI_NON_VIRTUAL
234#define JPH_IMPLEMENT_RTTI_NON_VIRTUAL(class_name) \
235 JPH::RTTI * GetRTTIOfType(class_name *) \
236 { \
237 static JPH::RTTI rtti(#class_name, sizeof(class_name), []() -> void * { return new class_name; }, [](void *inObject) { delete (class_name *)inObject; }, &class_name::sCreateRTTI); \
238 return &rtti; \
239 } \
240 void class_name::sCreateRTTI(JPH::RTTI &inRTTI) \
241
242
243// Same as above, but when you cannot insert the declaration in the class
244// itself, for example for templates and third party classes
246
247// JPH_DECLARE_RTTI_OUTSIDE_CLASS
248#define JPH_DECLARE_RTTI_OUTSIDE_CLASS(linkage, class_name) \
249 linkage JPH::RTTI * GetRTTIOfType(class_name *); \
250 inline const JPH::RTTI * GetRTTI(const class_name *inObject) { return GetRTTIOfType((class_name *)nullptr); }\
251 void CreateRTTI##class_name(JPH::RTTI &inRTTI); \
252
253// JPH_IMPLEMENT_RTTI_OUTSIDE_CLASS
254#define JPH_IMPLEMENT_RTTI_OUTSIDE_CLASS(class_name) \
255 JPH::RTTI * GetRTTIOfType(class_name *) \
256 { \
257 static JPH::RTTI rtti((const char *)#class_name, sizeof(class_name), &JPH::RTTI::sCreateObjectInternal<class_name>, &JPH::RTTI::sDestructObjectInternal<class_name>, &CreateRTTI##class_name); \
258 return &rtti; \
259 } \
260 void CreateRTTI##class_name(JPH::RTTI &inRTTI)
261
263// Same as above, but for classes that have virtual functions
265
266#define JPH_DECLARE_RTTI_HELPER(linkage, class_name, modifier) \
267public: \
268 JPH_OVERRIDE_NEW_DELETE \
269 friend linkage JPH::RTTI * GetRTTIOfType(class_name *); \
270 friend inline const JPH::RTTI *GetRTTI(const class_name *inObject) { return inObject->GetRTTI(); } \
271 virtual const JPH::RTTI * GetRTTI() const modifier; \
272 virtual const void * CastTo(const JPH::RTTI *inRTTI) const modifier; \
273 static void sCreateRTTI(JPH::RTTI &inRTTI); \
274
275// JPH_DECLARE_RTTI_VIRTUAL - for derived classes with RTTI
276#define JPH_DECLARE_RTTI_VIRTUAL(linkage, class_name) \
277 JPH_DECLARE_RTTI_HELPER(linkage, class_name, override)
278
279// JPH_IMPLEMENT_RTTI_VIRTUAL
280#define JPH_IMPLEMENT_RTTI_VIRTUAL(class_name) \
281 JPH::RTTI * GetRTTIOfType(class_name *) \
282 { \
283 static JPH::RTTI rtti(#class_name, sizeof(class_name), []() -> void * { return new class_name; }, [](void *inObject) { delete (class_name *)inObject; }, &class_name::sCreateRTTI); \
284 return &rtti; \
285 } \
286 const JPH::RTTI * class_name::GetRTTI() const \
287 { \
288 return JPH_RTTI(class_name); \
289 } \
290 const void * class_name::CastTo(const JPH::RTTI *inRTTI) const \
291 { \
292 return JPH_RTTI(class_name)->CastTo((const void *)this, inRTTI); \
293 } \
294 void class_name::sCreateRTTI(JPH::RTTI &inRTTI) \
295
296// JPH_DECLARE_RTTI_VIRTUAL_BASE - for concrete base class that has RTTI
297#define JPH_DECLARE_RTTI_VIRTUAL_BASE(linkage, class_name) \
298 JPH_DECLARE_RTTI_HELPER(linkage, class_name, )
299
300// JPH_IMPLEMENT_RTTI_VIRTUAL_BASE
301#define JPH_IMPLEMENT_RTTI_VIRTUAL_BASE(class_name) \
302 JPH_IMPLEMENT_RTTI_VIRTUAL(class_name)
303
304// JPH_DECLARE_RTTI_ABSTRACT - for derived abstract class that have RTTI
305#define JPH_DECLARE_RTTI_ABSTRACT(linkage, class_name) \
306 JPH_DECLARE_RTTI_HELPER(linkage, class_name, override)
307
308// JPH_IMPLEMENT_RTTI_ABSTRACT
309#define JPH_IMPLEMENT_RTTI_ABSTRACT(class_name) \
310 JPH::RTTI * GetRTTIOfType(class_name *) \
311 { \
312 static JPH::RTTI rtti(#class_name, sizeof(class_name), nullptr, [](void *inObject) { delete (class_name *)inObject; }, &class_name::sCreateRTTI); \
313 return &rtti; \
314 } \
315 const JPH::RTTI * class_name::GetRTTI() const \
316 { \
317 return JPH_RTTI(class_name); \
318 } \
319 const void * class_name::CastTo(const JPH::RTTI *inRTTI) const \
320 { \
321 return JPH_RTTI(class_name)->CastTo((const void *)this, inRTTI); \
322 } \
323 void class_name::sCreateRTTI(JPH::RTTI &inRTTI) \
324
325// JPH_DECLARE_RTTI_ABSTRACT_BASE - for abstract base class that has RTTI
326#define JPH_DECLARE_RTTI_ABSTRACT_BASE(linkage, class_name) \
327 JPH_DECLARE_RTTI_HELPER(linkage, class_name, )
328
329// JPH_IMPLEMENT_RTTI_ABSTRACT_BASE
330#define JPH_IMPLEMENT_RTTI_ABSTRACT_BASE(class_name) \
331 JPH_IMPLEMENT_RTTI_ABSTRACT(class_name)
332
334// Declare an RTTI class for registering with the factory
336
337#define JPH_DECLARE_RTTI_FOR_FACTORY(linkage, class_name) \
338 linkage JPH::RTTI * GetRTTIOfType(class class_name *);
339
340#define JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(linkage, name_space, class_name) \
341 namespace name_space { \
342 class class_name; \
343 linkage JPH::RTTI * GetRTTIOfType(class class_name *); \
344 }
345
347// Find the RTTI of a class
349
350#define JPH_RTTI(class_name) GetRTTIOfType(static_cast<class_name *>(nullptr))
351
353// Macro to rename a class, useful for embedded classes:
354//
355// class A { class B { }; }
356//
357// Now use JPH_RENAME_CLASS(B, A::B) to avoid conflicts with other classes named B
359
360// JPH_RENAME_CLASS
361#define JPH_RENAME_CLASS(class_name, new_name) \
362 inRTTI.SetName(#new_name);
363
365// Macro to add base classes
367
369#define JPH_BASE_CLASS_OFFSET(inClass, inBaseClass) ((int(JPH::uint64((inBaseClass *)((inClass *)0x10000))))-0x10000)
370
371// JPH_ADD_BASE_CLASS
372#define JPH_ADD_BASE_CLASS(class_name, base_class_name) \
373 inRTTI.AddBaseClass(JPH_RTTI(base_class_name), JPH_BASE_CLASS_OFFSET(class_name, base_class_name));
374
376// Macros and templates to identify a class
378
380template <class Type>
381inline bool IsType(const Type *inObject, const RTTI *inRTTI)
382{
383 return inObject == nullptr || *inObject->GetRTTI() == *inRTTI;
384}
385
386template <class Type>
387inline bool IsType(const RefConst<Type> &inObject, const RTTI *inRTTI)
388{
389 return inObject == nullptr || *inObject->GetRTTI() == *inRTTI;
390}
391
392template <class Type>
393inline bool IsType(const Ref<Type> &inObject, const RTTI *inRTTI)
394{
395 return inObject == nullptr || *inObject->GetRTTI() == *inRTTI;
396}
397
399template <class Type>
400inline bool IsKindOf(const Type *inObject, const RTTI *inRTTI)
401{
402 return inObject == nullptr || inObject->GetRTTI()->IsKindOf(inRTTI);
403}
404
405template <class Type>
406inline bool IsKindOf(const RefConst<Type> &inObject, const RTTI *inRTTI)
407{
408 return inObject == nullptr || inObject->GetRTTI()->IsKindOf(inRTTI);
409}
410
411template <class Type>
412inline bool IsKindOf(const Ref<Type> &inObject, const RTTI *inRTTI)
413{
414 return inObject == nullptr || inObject->GetRTTI()->IsKindOf(inRTTI);
415}
416
418template <class DstType, class SrcType, std::enable_if_t<std::is_base_of_v<DstType, SrcType> || std::is_base_of_v<SrcType, DstType>, bool> = true>
419inline const DstType *StaticCast(const SrcType *inObject)
420{
421 return static_cast<const DstType *>(inObject);
422}
423
424template <class DstType, class SrcType, std::enable_if_t<std::is_base_of_v<DstType, SrcType> || std::is_base_of_v<SrcType, DstType>, bool> = true>
425inline DstType *StaticCast(SrcType *inObject)
426{
427 return static_cast<DstType *>(inObject);
428}
429
430template <class DstType, class SrcType, std::enable_if_t<std::is_base_of_v<DstType, SrcType> || std::is_base_of_v<SrcType, DstType>, bool> = true>
431inline const DstType *StaticCast(const RefConst<SrcType> &inObject)
432{
433 return static_cast<const DstType *>(inObject.GetPtr());
434}
435
436template <class DstType, class SrcType, std::enable_if_t<std::is_base_of_v<DstType, SrcType> || std::is_base_of_v<SrcType, DstType>, bool> = true>
437inline DstType *StaticCast(const Ref<SrcType> &inObject)
438{
439 return static_cast<DstType *>(inObject.GetPtr());
440}
441
443template <class DstType, class SrcType>
444inline const DstType *DynamicCast(const SrcType *inObject)
445{
446 return inObject != nullptr? reinterpret_cast<const DstType *>(inObject->CastTo(JPH_RTTI(DstType))) : nullptr;
447}
448
449template <class DstType, class SrcType>
450inline DstType *DynamicCast(SrcType *inObject)
451{
452 return inObject != nullptr? const_cast<DstType *>(reinterpret_cast<const DstType *>(inObject->CastTo(JPH_RTTI(DstType)))) : nullptr;
453}
454
455template <class DstType, class SrcType>
456inline const DstType *DynamicCast(const RefConst<SrcType> &inObject)
457{
458 return inObject != nullptr? reinterpret_cast<const DstType *>(inObject->CastTo(JPH_RTTI(DstType))) : nullptr;
459}
460
461template <class DstType, class SrcType>
462inline DstType *DynamicCast(const Ref<SrcType> &inObject)
463{
464 return inObject != nullptr? const_cast<DstType *>(reinterpret_cast<const DstType *>(inObject->CastTo(JPH_RTTI(DstType)))) : nullptr;
465}
466
#define JPH_DEFAULT_ALLOCATE_ALIGNMENT
Definition Core.h:607
#define JPH_EXPORT
Definition Core.h:303
#define JPH_NAMESPACE_END
Definition Core.h:487
std::uint32_t uint32
Definition Core.h:567
#define JPH_NAMESPACE_BEGIN
Definition Core.h:481
AlignedFreeFunction AlignedFree
Definition Memory.cpp:72
AlignedAllocateFunction AlignedAllocate
Definition Memory.cpp:71
#define JPH_INTERNAL_DEFAULT_FREE(pointer)
Definition Memory.h:47
#define JPH_INTERNAL_DEFAULT_ALLOCATE(size)
Definition Memory.h:46
const DstType * StaticCast(const SrcType *inObject)
Cast inObject to DstType, asserts on failure.
Definition RTTI.h:419
bool IsType(const Type *inObject, const RTTI *inRTTI)
Check if inObject is of DstType.
Definition RTTI.h:381
bool IsKindOf(const Type *inObject, const RTTI *inRTTI)
Check if inObject is or is derived from DstType.
Definition RTTI.h:400
#define JPH_RTTI(class_name)
Definition RTTI.h:350
const DstType * DynamicCast(const SrcType *inObject)
Cast inObject to DstType, returns nullptr on failure.
Definition RTTI.h:444
Definition RTTI.h:122
pDestructObjectFunction mDestruct
Pointer to a function that will destruct an object of this class.
Definition RTTI.h:215
pCreateObjectFunction mCreate
Pointer to a function that will create a new instance of this class.
Definition RTTI.h:214
const char * GetName() const
Definition RTTI.h:138
StaticArray< SerializableAttribute, 32 > mAttributes
All attributes of this class.
Definition RTTI.h:217
void *(*)() pCreateObjectFunction
Function to create an object.
Definition RTTI.h:125
StaticArray< BaseClass, 4 > mBaseClasses
Names of base classes.
Definition RTTI.h:213
int GetSize() const
Definition RTTI.h:140
bool IsAbstract() const
Definition RTTI.h:141
bool IsKindOf(const RTTI *inRTTI) const
Test if this class is derived from class of type inRTTI.
Definition RTTI.cpp:92
void(*)(RTTI &inRTTI) pCreateRTTIFunction
Function to initialize the runtime type info structure.
Definition RTTI.h:131
void(*)(void *inObject) pDestructObjectFunction
Function to destroy an object.
Definition RTTI.h:128
void SetName(const char *inName)
Definition RTTI.h:139
static void * sCreateObjectInternal()
Create an object of type T for types that cannot use JPH_OVERRIDE_NEW_DELETE (e.g....
Definition RTTI.h:174
const char * mName
Class name.
Definition RTTI.h:211
int mSize
Class size.
Definition RTTI.h:212
RTTI(const char *inName, int inSize, pCreateObjectFunction inCreateObject, pDestructObjectFunction inDestructObject)
Constructor.
Definition RTTI.cpp:16
static void sDestructObjectInternal(void *inObject)
Destruct an object created with sCreateObjectInternal.
Definition RTTI.h:190
Definition Reference.h:166
const T * GetPtr() const
Get pointer.
Definition Reference.h:200
Definition Reference.h:110
T * GetPtr() const
Get pointer.
Definition Reference.h:138
Attributes are members of classes that need to be serialized.
Definition SerializableAttribute.h:37
Simple variable length array backed by a fixed size buffer.
Definition StaticArray.h:14
Base class information.
Definition RTTI.h:206
const RTTI * mRTTI
Definition RTTI.h:207
int mOffset
Definition RTTI.h:208