Skip to content

Commit

Permalink
Implement TObject macros
Browse files Browse the repository at this point in the history
  • Loading branch information
AdventureT committed Nov 12, 2023
1 parent 7fdfdf0 commit d82c52b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 26 deletions.
72 changes: 55 additions & 17 deletions Toshi/Include/TKernel/TObject.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
#include "Defines.h"
#include "TDebug.h"
#include <new>
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);
Expand Down Expand Up @@ -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
6 changes: 2 additions & 4 deletions Toshi/Source/TKernel/TCString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

TOSHI_NAMESPACE_USING


TBOOL TCString::AllocBuffer(TINT a_iLength, TBOOL a_bClear)
{
TASSERT(a_iLength >= 0);
Expand All @@ -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);
Expand Down
11 changes: 6 additions & 5 deletions Toshi/Source/TKernel/TObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit d82c52b

Please sign in to comment.