Debian Codegraph @

thirdparty/jolt_physics/Jolt/Core/RTTI.cpp

raw symbols SCIP
1 // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2 // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3 // SPDX-License-Identifier: MIT
4
5 #include <Jolt/Jolt.h>
6
7 #include <Jolt/Core/RTTI.h>
8 #include <Jolt/Core/StringTools.h>
9
10 JPH_NAMESPACE_BEGIN
11
12 //////////////////////////////////////////////////////////////////////////////////////////
13 // RTTI
14 //////////////////////////////////////////////////////////////////////////////////////////
15
16 RTTI::RTTI(const char *inName, int inSize, pCreateObjectFunction inCreateObject, pDestructObjectFunction inDestructObject) :
17 mName(inName),
18 mSize(inSize),
19 mCreate(inCreateObject),
20 mDestruct(inDestructObject)
21 {
22 JPH_ASSERT(inDestructObject != nullptr, "Object cannot be destructed");
23 }
24
25 RTTI::RTTI(const char *inName, int inSize, pCreateObjectFunction inCreateObject, pDestructObjectFunction inDestructObject, pCreateRTTIFunction inCreateRTTI) :
26 mName(inName),
27 mSize(inSize),
28 mCreate(inCreateObject),
29 mDestruct(inDestructObject)
30 {
31 JPH_ASSERT(inDestructObject != nullptr, "Object cannot be destructed");
32
33 inCreateRTTI(*this);
34 }
35
36 int RTTI::GetBaseClassCount() const
37 {
38 return (int)mBaseClasses.size();
39 }
40
41 const RTTI *RTTI::GetBaseClass(int inIdx) const
42 {
43 return mBaseClasses[inIdx].mRTTI;
44 }
45
46 uint32 RTTI::GetHash() const
47 {
48 // Perform diffusion step to get from 64 to 32 bits (see https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function)
49 uint64 hash = HashString(mName);
50 return (uint32)(hash ^ (hash >> 32));
51 }
52
53 void *RTTI::CreateObject() const
54 {
55 return IsAbstract()? nullptr : mCreate();
56 }
57
58 void RTTI::DestructObject(void *inObject) const
59 {
60 mDestruct(inObject);
61 }
62
63 void RTTI::AddBaseClass(const RTTI *inRTTI, int inOffset)
64 {
65 JPH_ASSERT(inOffset >= 0 && inOffset < mSize, "Base class not contained in derived class");
66
67 // Add base class
68 BaseClass base;
69 base.mRTTI = inRTTI;
70 base.mOffset = inOffset;
71 mBaseClasses.push_back(base);
72
73 #ifdef JPH_OBJECT_STREAM
74 // Add attributes of base class
75 for (const SerializableAttribute &a : inRTTI->mAttributes)
76 mAttributes.push_back(SerializableAttribute(a, inOffset));
77 #endif // JPH_OBJECT_STREAM
78 }
79
80 bool RTTI::operator == (const RTTI &inRHS) const
81 {
82 // Compare addresses
83 if (this == &inRHS)
84 return true;
85
86 // Check that the names differ (if that is the case we probably have two instances
87 // of the same attribute info across the program, probably the second is in a DLL)
88 JPH_ASSERT(strcmp(mName, inRHS.mName) != 0);
89 return false;
90 }
91
92 bool RTTI::IsKindOf(const RTTI *inRTTI) const
93 {
94 // Check if this is the same type
95 if (this == inRTTI)
96 return true;
97
98 // Check all base classes
99 for (const BaseClass &b : mBaseClasses)
100 if (b.mRTTI->IsKindOf(inRTTI))
101 return true;
102
103 return false;
104 }
105
106 const void *RTTI::CastTo(const void *inObject, const RTTI *inRTTI) const
107 {
108 JPH_ASSERT(inObject != nullptr);
109
110 // Check if this is the same type
111 if (this == inRTTI)
112 return inObject;
113
114 // Check all base classes
115 for (const BaseClass &b : mBaseClasses)
116 {
117 // Cast the pointer to the base class
118 const void *casted = (const void *)(((const uint8 *)inObject) + b.mOffset);
119
120 // Test base class
121 const void *rv = b.mRTTI->CastTo(casted, inRTTI);
122 if (rv != nullptr)
123 return rv;
124 }
125
126 // Not possible to cast
127 return nullptr;
128 }
129
130 #ifdef JPH_OBJECT_STREAM
131
132 void RTTI::AddAttribute(const SerializableAttribute &inAttribute)
133 {
134 mAttributes.push_back(inAttribute);
135 }
136
137 int RTTI::GetAttributeCount() const
138 {
139 return (int)mAttributes.size();
140 }
141
142 const SerializableAttribute &RTTI::GetAttribute(int inIdx) const
143 {
144 return mAttributes[inIdx];
145 }
146
147 #endif // JPH_OBJECT_STREAM
148
149 JPH_NAMESPACE_END