Path: blob/master/thirdparty/jolt_physics/Jolt/Core/RTTI.cpp
9906 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#include <Jolt/Jolt.h>56#include <Jolt/Core/RTTI.h>7#include <Jolt/Core/StringTools.h>89JPH_NAMESPACE_BEGIN1011//////////////////////////////////////////////////////////////////////////////////////////12// RTTI13//////////////////////////////////////////////////////////////////////////////////////////1415RTTI::RTTI(const char *inName, int inSize, pCreateObjectFunction inCreateObject, pDestructObjectFunction inDestructObject) :16mName(inName),17mSize(inSize),18mCreate(inCreateObject),19mDestruct(inDestructObject)20{21JPH_ASSERT(inDestructObject != nullptr, "Object cannot be destructed");22}2324RTTI::RTTI(const char *inName, int inSize, pCreateObjectFunction inCreateObject, pDestructObjectFunction inDestructObject, pCreateRTTIFunction inCreateRTTI) :25mName(inName),26mSize(inSize),27mCreate(inCreateObject),28mDestruct(inDestructObject)29{30JPH_ASSERT(inDestructObject != nullptr, "Object cannot be destructed");3132inCreateRTTI(*this);33}3435int RTTI::GetBaseClassCount() const36{37return (int)mBaseClasses.size();38}3940const RTTI *RTTI::GetBaseClass(int inIdx) const41{42return mBaseClasses[inIdx].mRTTI;43}4445uint32 RTTI::GetHash() const46{47// 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)48uint64 hash = HashString(mName);49return (uint32)(hash ^ (hash >> 32));50}5152void *RTTI::CreateObject() const53{54return IsAbstract()? nullptr : mCreate();55}5657void RTTI::DestructObject(void *inObject) const58{59mDestruct(inObject);60}6162void RTTI::AddBaseClass(const RTTI *inRTTI, int inOffset)63{64JPH_ASSERT(inOffset >= 0 && inOffset < mSize, "Base class not contained in derived class");6566// Add base class67BaseClass base;68base.mRTTI = inRTTI;69base.mOffset = inOffset;70mBaseClasses.push_back(base);7172#ifdef JPH_OBJECT_STREAM73// Add attributes of base class74for (const SerializableAttribute &a : inRTTI->mAttributes)75mAttributes.push_back(SerializableAttribute(a, inOffset));76#endif // JPH_OBJECT_STREAM77}7879bool RTTI::operator == (const RTTI &inRHS) const80{81// Compare addresses82if (this == &inRHS)83return true;8485// Check that the names differ (if that is the case we probably have two instances86// of the same attribute info across the program, probably the second is in a DLL)87JPH_ASSERT(strcmp(mName, inRHS.mName) != 0);88return false;89}9091bool RTTI::IsKindOf(const RTTI *inRTTI) const92{93// Check if this is the same type94if (this == inRTTI)95return true;9697// Check all base classes98for (const BaseClass &b : mBaseClasses)99if (b.mRTTI->IsKindOf(inRTTI))100return true;101102return false;103}104105const void *RTTI::CastTo(const void *inObject, const RTTI *inRTTI) const106{107JPH_ASSERT(inObject != nullptr);108109// Check if this is the same type110if (this == inRTTI)111return inObject;112113// Check all base classes114for (const BaseClass &b : mBaseClasses)115{116// Cast the pointer to the base class117const void *casted = (const void *)(((const uint8 *)inObject) + b.mOffset);118119// Test base class120const void *rv = b.mRTTI->CastTo(casted, inRTTI);121if (rv != nullptr)122return rv;123}124125// Not possible to cast126return nullptr;127}128129#ifdef JPH_OBJECT_STREAM130131void RTTI::AddAttribute(const SerializableAttribute &inAttribute)132{133mAttributes.push_back(inAttribute);134}135136int RTTI::GetAttributeCount() const137{138return (int)mAttributes.size();139}140141const SerializableAttribute &RTTI::GetAttribute(int inIdx) const142{143return mAttributes[inIdx];144}145146#endif // JPH_OBJECT_STREAM147148JPH_NAMESPACE_END149150151