From d82c52b76aec35be26379c49675b13db5bc5dd8c Mon Sep 17 00:00:00 2001 From: AdventureT Date: Sun, 12 Nov 2023 12:31:28 +0100 Subject: [PATCH] Implement TObject macros --- Toshi/Include/TKernel/TObject.h | 72 +++++++++++++++++++++++-------- Toshi/Source/TKernel/TCString.cpp | 6 +-- Toshi/Source/TKernel/TObject.cpp | 11 ++--- 3 files changed, 63 insertions(+), 26 deletions(-) diff --git a/Toshi/Include/TKernel/TObject.h b/Toshi/Include/TKernel/TObject.h index e081695..7d5556b 100644 --- a/Toshi/Include/TKernel/TObject.h +++ b/Toshi/Include/TKernel/TObject.h @@ -1,21 +1,24 @@ #include "Defines.h" +#include "TDebug.h" #include TOSHI_NAMESPACE_BEGIN class TObject; // TClassProps definitions -typedef class TObject* (*t_CreateTObject)(); -typedef class TObject* (*t_CreateTObjectInPlace)(TPVOID); -typedef void (*t_InitializeStatic)(); -typedef void (*t_UninitializeStatic)(); +typedef class TObject* (__stdcall*t_CreateTObject)(); +typedef class TObject* (__stdcall*t_CreateTObjectInPlace)(TPVOID); +typedef void (__stdcall*t_InitializeStatic)(); +typedef void (__stdcall*t_UninitializeStatic)(); // RecurseTree definitions typedef TBOOL (*t_RecurceTreeCheck)(class TClass*, TPCVOID); typedef TBOOL (*t_RecurceTreeBaseBeginCb)(class TClass*, TPCVOID); typedef TBOOL (*t_RecurceTreeBaseEndCb)(class TClass*, TPCVOID); -TOSHI_EXPORT class TClass +#define TGetClass(X) &X::m_sClass + +class TOSHI_EXPORT TClass { public: TClass(TPCCHAR a_pcName, TClass* a_pParent, t_CreateTObject a_Create, t_CreateTObjectInPlace a_CreateInPlace, t_InitializeStatic a_Init, t_UninitializeStatic a_Uninit, TUINT a_uiVersion); @@ -91,21 +94,56 @@ TOSHI_EXPORT class TClass TBOOL m_bInitialised; // 0x24 }; + +#define DECLARE_DYNAMIC(class_name) \ +public: \ + virtual TClass& GetClass() const; \ + static TClass m_sClass; \ +private: \ + static TObject* __stdcall CreateObject(); \ + static TObject* __stdcall CreateObjectInPlace(TPVOID a_pMem); \ + static void __stdcall DeinitialiseStatic(); \ + static void __stdcall InitialiseStatic(); \ + +#define IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, pfnCreateObject, pfnCreateObjectInPlace, version) \ + TClass& class_name::GetClass() const \ + { return class_name::m_sClass; } \ + void __stdcall class_name::DeinitialiseStatic() \ + { } \ + void __stdcall class_name::InitialiseStatic() \ + { } \ + TClass class_name::m_sClass = TClass( \ + #class_name, base_class_name, pfnCreateObject, pfnCreateObjectInPlace, \ + class_name::InitialiseStatic, class_name::DeinitialiseStatic, version); \ + + +#define IMPLEMENT_DYNCREATE(class_name, base_class_name) \ + TObject* __stdcall class_name::CreateObject() \ + { return new class_name; } \ + TObject* __stdcall class_name::CreateObjectInPlace(TPVOID a_pMem) \ + { return new (a_pMem) class_name; } \ + IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, class_name::CreateObject, class_name::CreateObjectInPlace, 1) + +#define IMPLEMENT_DYNAMIC(class_name, base_class_name) \ + TObject* __stdcall class_name::CreateObject() \ + { TASSERT(!"This class does not support dynamic creation!"); return TNULL; } \ + TObject* __stdcall class_name::CreateObjectInPlace(TPVOID a_pMem) \ + { TASSERT(!"This class does not support dynamic creation!"); return TNULL; } \ + IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, class_name::CreateObject, class_name::CreateObjectInPlace, 1) + class TOSHI_EXPORT TObject { -private: - static TObject* __stdcall CreateObject() - { - TObject* pObject = new TObject(); - return pObject ? pObject : TNULL; - } - static TObject* __stdcall CreateObjectInPlace(TPVOID m_pMem) - { - TObject* pObject = new (m_pMem) TObject(); - return pObject ? pObject : TNULL; - } + DECLARE_DYNAMIC(TObject); + public: - //static TClass m_sClass; + virtual void Delete() { delete this; } + + TBOOL IsA(const TClass& a_rClass) const { return GetClass().IsA(a_rClass); } + TBOOL IsExactly(const TClass& a_rClass) const { return GetClass().IsExactly(a_rClass); } + +protected: + virtual ~TObject() = default; + }; TOSHI_NAMESPACE_END \ No newline at end of file diff --git a/Toshi/Source/TKernel/TCString.cpp b/Toshi/Source/TKernel/TCString.cpp index 9ff23f7..27b2ca2 100644 --- a/Toshi/Source/TKernel/TCString.cpp +++ b/Toshi/Source/TKernel/TCString.cpp @@ -5,7 +5,6 @@ TOSHI_NAMESPACE_USING - TBOOL TCString::AllocBuffer(TINT a_iLength, TBOOL a_bClear) { TASSERT(a_iLength >= 0); @@ -24,9 +23,8 @@ TBOOL TCString::AllocBuffer(TINT a_iLength, TBOOL a_bClear) TINT newExcessLen = (m_iStrLen - a_iLength) + m_iExcessLen; if (newExcessLen < 0 || newExcessLen > 0xFF) { - if (m_iStrLen != 0 && a_bClear) { - free(m_pBuffer); - } + if (m_iStrLen != 0 && a_bClear) free(m_pBuffer); + m_pBuffer = (TPCHAR)malloc(a_iLength + 1); m_iExcessLen = 0; TASSERT(m_pBuffer!=TNULL); diff --git a/Toshi/Source/TKernel/TObject.cpp b/Toshi/Source/TKernel/TObject.cpp index 2faa862..60c8409 100644 --- a/Toshi/Source/TKernel/TObject.cpp +++ b/Toshi/Source/TKernel/TObject.cpp @@ -4,7 +4,8 @@ TOSHI_NAMESPACE_USING -//TClass TObject::m_sClass = TClass("TObject", TNULL, TObject::CreateTObject, TObject::CreateTObjectInPlace, sizeof(TObject), alignof(TObject)); +IMPLEMENT_DYNCREATE(TObject, TNULL); +//TClass TObject::m_sClass = TClass("TObject", TNULL, TObject::CreateObject, TObject::CreateObjectInPlace, TObject::InitialiseStatic, TObject::DeinitialiseStatic, 1); TClass::TClass(TPCCHAR a_pcName, TClass* a_pParent, t_CreateTObject a_Create, t_CreateTObjectInPlace a_CreateInPlace, t_InitializeStatic a_Init, t_UninitializeStatic a_Uninit, TUINT a_uiVersion) { @@ -69,7 +70,7 @@ void __stdcall TClass::DumpObjectClassTree() const TClass* __stdcall TClass::Find(TPCCHAR a_pcClassName, const TClass* a_pClass) { TASSERT(a_pcClassName[1]!=0); - //if (a_pClass == TNULL) a_pClass = &TObject::m_sClass; + if (a_pClass == TNULL) a_pClass = TGetClass(TObject); return FindRecurse(a_pcClassName, a_pClass, TFALSE); } @@ -102,10 +103,10 @@ void TClass::InitialiseStatic() } } -TBOOL TClass::IsA(const TClass& m_rClass) const +TBOOL TClass::IsA(const TClass& a_rClass) const { for (const TClass* pClass = this; pClass != TNULL; pClass = pClass->m_pParent) { - if (IsExactly(m_rClass)) return TTRUE; + if (IsExactly(a_rClass)) return TTRUE; } return TFALSE; } @@ -146,7 +147,7 @@ TBOOL TClass::IsAttached() const { if (m_pParent) { for (TClass* i = m_pParent->m_pLastAttached; i!=TNULL; i = i->m_pPrevious) { - if (m_pParent->m_pLastAttached == this) return TTRUE; + if (i == this) return TTRUE; } } return TFALSE;