| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <Jolt/Core/Reference.h> |
| 8 |
#include <Jolt/Core/StaticArray.h> |
| 9 |
#include <Jolt/ObjectStream/SerializableAttribute.h> |
| 10 |
|
| 11 |
JPH_NAMESPACE_BEGIN |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
class JPH_EXPORT RTTI |
| 122 |
{ |
| 123 |
public: |
| 124 |
|
| 125 |
using pCreateObjectFunction = void *(*)(); |
| 126 |
|
| 127 |
|
| 128 |
using pDestructObjectFunction = void (*)(void *inObject); |
| 129 |
|
| 130 |
|
| 131 |
using pCreateRTTIFunction = void (*)(RTTI &inRTTI); |
| 132 |
|
| 133 |
|
| 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 |
|
| 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 |
|
| 146 |
|
| 147 |
void * CreateObject() const; |
| 148 |
|
| 149 |
|
| 150 |
void DestructObject(void *inObject) const; |
| 151 |
|
| 152 |
|
| 153 |
void AddBaseClass(const RTTI *inRTTI, int inOffset); |
| 154 |
|
| 155 |
|
| 156 |
bool operator == (const RTTI &inRHS) const; |
| 157 |
bool operator != (const RTTI &inRHS) const { return !(*this == inRHS); } |
| 158 |
|
| 159 |
|
| 160 |
bool IsKindOf(const RTTI *inRTTI) const; |
| 161 |
|
| 162 |
|
| 163 |
const void * CastTo(const void *inObject, const RTTI *inRTTI) const; |
| 164 |
|
| 165 |
#ifdef JPH_OBJECT_STREAM |
| 166 |
|
| 167 |
void AddAttribute(const SerializableAttribute &inAttribute); |
| 168 |
int GetAttributeCount() const; |
| 169 |
const SerializableAttribute & GetAttribute(int inIdx) const; |
| 170 |
#endif |
| 171 |
|
| 172 |
protected: |
| 173 |
|
| 174 |
struct BaseClass |
| 175 |
{ |
| 176 |
const RTTI * mRTTI; |
| 177 |
int mOffset; |
| 178 |
}; |
| 179 |
|
| 180 |
const char * mName; |
| 181 |
int mSize; |
| 182 |
StaticArray<BaseClass, 4> mBaseClasses; |
| 183 |
pCreateObjectFunction mCreate; |
| 184 |
pDestructObjectFunction mDestruct; |
| 185 |
#ifdef JPH_OBJECT_STREAM |
| 186 |
StaticArray<SerializableAttribute, 32> mAttributes; |
| 187 |
#endif |
| 188 |
}; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
#define JPH_DECLARE_RTTI_NON_VIRTUAL(linkage, class_name) \ |
| 196 |
public: \ |
| 197 |
JPH_OVERRIDE_NEW_DELETE \ |
| 198 |
friend linkage RTTI * GetRTTIOfType(class_name *); \ |
| 199 |
friend inline const RTTI * GetRTTI([[maybe_unused]] const class_name *inObject) { return GetRTTIOfType(static_cast<class_name *>(nullptr)); }\ |
| 200 |
static void sCreateRTTI(RTTI &inRTTI); \ |
| 201 |
|
| 202 |
|
| 203 |
#define JPH_IMPLEMENT_RTTI_NON_VIRTUAL(class_name) \ |
| 204 |
RTTI * GetRTTIOfType(class_name *) \ |
| 205 |
{ \ |
| 206 |
static RTTI rtti(#class_name, sizeof(class_name), []() -> void * { return new class_name; }, [](void *inObject) { delete (class_name *)inObject; }, &class_name::sCreateRTTI); \ |
| 207 |
return &rtti; \ |
| 208 |
} \ |
| 209 |
void class_name::sCreateRTTI(RTTI &inRTTI) \ |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
#define JPH_DECLARE_RTTI_OUTSIDE_CLASS(linkage, class_name) \ |
| 218 |
linkage RTTI * GetRTTIOfType(class_name *); \ |
| 219 |
inline const RTTI * GetRTTI(const class_name *inObject) { return GetRTTIOfType((class_name *)nullptr); }\ |
| 220 |
void CreateRTTI##class_name(RTTI &inRTTI); \ |
| 221 |
|
| 222 |
|
| 223 |
#define JPH_IMPLEMENT_RTTI_OUTSIDE_CLASS(class_name) \ |
| 224 |
RTTI * GetRTTIOfType(class_name *) \ |
| 225 |
{ \ |
| 226 |
static RTTI rtti((const char *)#class_name, sizeof(class_name), []() -> void * { return new class_name; }, [](void *inObject) { delete (class_name *)inObject; }, &CreateRTTI##class_name); \ |
| 227 |
return &rtti; \ |
| 228 |
} \ |
| 229 |
void CreateRTTI##class_name(RTTI &inRTTI) |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
#define JPH_DECLARE_RTTI_HELPER(linkage, class_name, modifier) \ |
| 236 |
public: \ |
| 237 |
JPH_OVERRIDE_NEW_DELETE \ |
| 238 |
friend linkage RTTI * GetRTTIOfType(class_name *); \ |
| 239 |
friend inline const RTTI * GetRTTI(const class_name *inObject) { return inObject->GetRTTI(); } \ |
| 240 |
virtual const RTTI * GetRTTI() const modifier; \ |
| 241 |
virtual const void * CastTo(const RTTI *inRTTI) const modifier; \ |
| 242 |
static void sCreateRTTI(RTTI &inRTTI); \ |
| 243 |
|
| 244 |
|
| 245 |
#define JPH_DECLARE_RTTI_VIRTUAL(linkage, class_name) \ |
| 246 |
JPH_DECLARE_RTTI_HELPER(linkage, class_name, override) |
| 247 |
|
| 248 |
|
| 249 |
#define JPH_IMPLEMENT_RTTI_VIRTUAL(class_name) \ |
| 250 |
RTTI * GetRTTIOfType(class_name *) \ |
| 251 |
{ \ |
| 252 |
static RTTI rtti(#class_name, sizeof(class_name), []() -> void * { return new class_name; }, [](void *inObject) { delete (class_name *)inObject; }, &class_name::sCreateRTTI); \ |
| 253 |
return &rtti; \ |
| 254 |
} \ |
| 255 |
const RTTI * class_name::GetRTTI() const \ |
| 256 |
{ \ |
| 257 |
return JPH_RTTI(class_name); \ |
| 258 |
} \ |
| 259 |
const void * class_name::CastTo(const RTTI *inRTTI) const \ |
| 260 |
{ \ |
| 261 |
return JPH_RTTI(class_name)->CastTo((const void *)this, inRTTI); \ |
| 262 |
} \ |
| 263 |
void class_name::sCreateRTTI(RTTI &inRTTI) \ |
| 264 |
|
| 265 |
|
| 266 |
#define JPH_DECLARE_RTTI_VIRTUAL_BASE(linkage, class_name) \ |
| 267 |
JPH_DECLARE_RTTI_HELPER(linkage, class_name, ) |
| 268 |
|
| 269 |
|
| 270 |
#define JPH_IMPLEMENT_RTTI_VIRTUAL_BASE(class_name) \ |
| 271 |
JPH_IMPLEMENT_RTTI_VIRTUAL(class_name) |
| 272 |
|
| 273 |
|
| 274 |
#define JPH_DECLARE_RTTI_ABSTRACT(linkage, class_name) \ |
| 275 |
JPH_DECLARE_RTTI_HELPER(linkage, class_name, override) |
| 276 |
|
| 277 |
|
| 278 |
#define JPH_IMPLEMENT_RTTI_ABSTRACT(class_name) \ |
| 279 |
RTTI * GetRTTIOfType(class_name *) \ |
| 280 |
{ \ |
| 281 |
static RTTI rtti(#class_name, sizeof(class_name), nullptr, [](void *inObject) { delete (class_name *)inObject; }, &class_name::sCreateRTTI); \ |
| 282 |
return &rtti; \ |
| 283 |
} \ |
| 284 |
const RTTI * class_name::GetRTTI() const \ |
| 285 |
{ \ |
| 286 |
return JPH_RTTI(class_name); \ |
| 287 |
} \ |
| 288 |
const void * class_name::CastTo(const RTTI *inRTTI) const \ |
| 289 |
{ \ |
| 290 |
return JPH_RTTI(class_name)->CastTo((const void *)this, inRTTI); \ |
| 291 |
} \ |
| 292 |
void class_name::sCreateRTTI(RTTI &inRTTI) \ |
| 293 |
|
| 294 |
|
| 295 |
#define JPH_DECLARE_RTTI_ABSTRACT_BASE(linkage, class_name) \ |
| 296 |
JPH_DECLARE_RTTI_HELPER(linkage, class_name, ) |
| 297 |
|
| 298 |
|
| 299 |
#define JPH_IMPLEMENT_RTTI_ABSTRACT_BASE(class_name) \ |
| 300 |
JPH_IMPLEMENT_RTTI_ABSTRACT(class_name) |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
#define JPH_DECLARE_RTTI_FOR_FACTORY(linkage, class_name) \ |
| 307 |
linkage RTTI * GetRTTIOfType(class class_name *); |
| 308 |
|
| 309 |
#define JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(linkage, name_space, class_name) \ |
| 310 |
namespace name_space { \ |
| 311 |
class class_name; \ |
| 312 |
linkage RTTI * GetRTTIOfType(class class_name *); \ |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
#define JPH_RTTI(class_name) GetRTTIOfType(static_cast<class_name *>(nullptr)) |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
#define JPH_RENAME_CLASS(class_name, new_name) \ |
| 331 |
inRTTI.SetName(#new_name); |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
#define JPH_BASE_CLASS_OFFSET(inClass, inBaseClass) ((int(uint64((inBaseClass *)((inClass *)0x10000))))-0x10000) |
| 339 |
|
| 340 |
|
| 341 |
#define JPH_ADD_BASE_CLASS(class_name, base_class_name) \ |
| 342 |
inRTTI.AddBaseClass(JPH_RTTI(base_class_name), JPH_BASE_CLASS_OFFSET(class_name, base_class_name)); |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
template <class Type> |
| 350 |
inline bool IsType(const Type *inObject, const RTTI *inRTTI) |
| 351 |
{ |
| 352 |
return inObject == nullptr || *inObject->GetRTTI() == *inRTTI; |
| 353 |
} |
| 354 |
|
| 355 |
template <class Type> |
| 356 |
inline bool IsType(const RefConst<Type> &inObject, const RTTI *inRTTI) |
| 357 |
{ |
| 358 |
return inObject == nullptr || *inObject->GetRTTI() == *inRTTI; |
| 359 |
} |
| 360 |
|
| 361 |
template <class Type> |
| 362 |
inline bool IsType(const Ref<Type> &inObject, const RTTI *inRTTI) |
| 363 |
{ |
| 364 |
return inObject == nullptr || *inObject->GetRTTI() == *inRTTI; |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
template <class Type> |
| 369 |
inline bool IsKindOf(const Type *inObject, const RTTI *inRTTI) |
| 370 |
{ |
| 371 |
return inObject == nullptr || inObject->GetRTTI()->IsKindOf(inRTTI); |
| 372 |
} |
| 373 |
|
| 374 |
template <class Type> |
| 375 |
inline bool IsKindOf(const RefConst<Type> &inObject, const RTTI *inRTTI) |
| 376 |
{ |
| 377 |
return inObject == nullptr || inObject->GetRTTI()->IsKindOf(inRTTI); |
| 378 |
} |
| 379 |
|
| 380 |
template <class Type> |
| 381 |
inline bool IsKindOf(const Ref<Type> &inObject, const RTTI *inRTTI) |
| 382 |
{ |
| 383 |
return inObject == nullptr || inObject->GetRTTI()->IsKindOf(inRTTI); |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
template <class DstType, class SrcType, std::enable_if_t<std::is_base_of_v<DstType, SrcType> || std::is_base_of_v<SrcType, DstType>, bool> = true> |
| 388 |
inline const DstType *StaticCast(const SrcType *inObject) |
| 389 |
{ |
| 390 |
return static_cast<const DstType *>(inObject); |
| 391 |
} |
| 392 |
|
| 393 |
template <class DstType, class SrcType, std::enable_if_t<std::is_base_of_v<DstType, SrcType> || std::is_base_of_v<SrcType, DstType>, bool> = true> |
| 394 |
inline DstType *StaticCast(SrcType *inObject) |
| 395 |
{ |
| 396 |
return static_cast<DstType *>(inObject); |
| 397 |
} |
| 398 |
|
| 399 |
template <class DstType, class SrcType, std::enable_if_t<std::is_base_of_v<DstType, SrcType> || std::is_base_of_v<SrcType, DstType>, bool> = true> |
| 400 |
inline const DstType *StaticCast(const RefConst<SrcType> &inObject) |
| 401 |
{ |
| 402 |
return static_cast<const DstType *>(inObject.GetPtr()); |
| 403 |
} |
| 404 |
|
| 405 |
template <class DstType, class SrcType, std::enable_if_t<std::is_base_of_v<DstType, SrcType> || std::is_base_of_v<SrcType, DstType>, bool> = true> |
| 406 |
inline DstType *StaticCast(const Ref<SrcType> &inObject) |
| 407 |
{ |
| 408 |
return static_cast<DstType *>(inObject.GetPtr()); |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
template <class DstType, class SrcType> |
| 413 |
inline const DstType *DynamicCast(const SrcType *inObject) |
| 414 |
{ |
| 415 |
return inObject != nullptr? reinterpret_cast<const DstType *>(inObject->CastTo(JPH_RTTI(DstType))) : nullptr; |
| 416 |
} |
| 417 |
|
| 418 |
template <class DstType, class SrcType> |
| 419 |
inline DstType *DynamicCast(SrcType *inObject) |
| 420 |
{ |
| 421 |
return inObject != nullptr? const_cast<DstType *>(reinterpret_cast<const DstType *>(inObject->CastTo(JPH_RTTI(DstType)))) : nullptr; |
| 422 |
} |
| 423 |
|
| 424 |
template <class DstType, class SrcType> |
| 425 |
inline const DstType *DynamicCast(const RefConst<SrcType> &inObject) |
| 426 |
{ |
| 427 |
return inObject != nullptr? reinterpret_cast<const DstType *>(inObject->CastTo(JPH_RTTI(DstType))) : nullptr; |
| 428 |
} |
| 429 |
|
| 430 |
template <class DstType, class SrcType> |
| 431 |
inline DstType *DynamicCast(const Ref<SrcType> &inObject) |
| 432 |
{ |
| 433 |
return inObject != nullptr? const_cast<DstType *>(reinterpret_cast<const DstType *>(inObject->CastTo(JPH_RTTI(DstType)))) : nullptr; |
| 434 |
} |
| 435 |
|
| 436 |
JPH_NAMESPACE_END |