Skip to content

Commit

Permalink
Fix TObject
Browse files Browse the repository at this point in the history
  • Loading branch information
AdventureT committed Nov 12, 2023
1 parent d82c52b commit 303f495
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
35 changes: 35 additions & 0 deletions Tools/UnitTests/Source/TKernel/TObject_Tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "TKernel/TObject.h"
#include <catch2/catch_test_macros.hpp>

TOSHI_NAMESPACE_USING

class Test : public TObject
{
DECLARE_DYNAMIC(Test);
};

IMPLEMENT_DYNCREATE(Test, TObject);

class Test2 : public Test
{
DECLARE_DYNAMIC(Test2);
};

IMPLEMENT_DYNCREATE(Test2, Test);

class Test3 : public TObject
{
DECLARE_DYNAMIC(Test3);
};

IMPLEMENT_DYNAMIC(Test3, TObject);

TEST_CASE("TObject Creation", "[TObject]")
{
Test test;
REQUIRE(test.IsA(TGetClass(TObject)));
Test2 test2;
REQUIRE(test.IsA(TGetClass(Test)));
REQUIRE(test.IsA(TGetClass(TObject)));
REQUIRE_FALSE(test.IsA(TGetClass(Test3)));
}
24 changes: 19 additions & 5 deletions Toshi/Include/TKernel/TObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include <new>
TOSHI_NAMESPACE_BEGIN

class TObject;

// TClassProps definitions
typedef class TObject* (__stdcall*t_CreateTObject)();
typedef class TObject* (__stdcall*t_CreateTObjectInPlace)(TPVOID);
Expand All @@ -16,7 +14,7 @@ typedef TBOOL (*t_RecurceTreeCheck)(class TClass*, TPCVOID);
typedef TBOOL (*t_RecurceTreeBaseBeginCb)(class TClass*, TPCVOID);
typedef TBOOL (*t_RecurceTreeBaseEndCb)(class TClass*, TPCVOID);

#define TGetClass(X) &X::m_sClass
#define TGetClass(X) X::m_sClass

class TOSHI_EXPORT TClass
{
Expand Down Expand Up @@ -113,7 +111,7 @@ private: \
void __stdcall class_name::InitialiseStatic() \
{ } \
TClass class_name::m_sClass = TClass( \
#class_name, base_class_name, pfnCreateObject, pfnCreateObjectInPlace, \
#class_name, &TGetClass(base_class_name), pfnCreateObject, pfnCreateObjectInPlace, \
class_name::InitialiseStatic, class_name::DeinitialiseStatic, version); \


Expand All @@ -133,7 +131,20 @@ private: \

class TOSHI_EXPORT TObject
{
DECLARE_DYNAMIC(TObject);
public: \
virtual TClass& GetClass() const { return m_sClass; }
static TClass m_sClass;
private:
static TObject* __stdcall CreateObject()
{
return new TObject;
}
static TObject* __stdcall CreateObjectInPlace(TPVOID a_pMem)
{
return new (a_pMem) TObject;
}
static void __stdcall DeinitialiseStatic() {}
static void __stdcall InitialiseStatic() {}

public:
virtual void Delete() { delete this; }
Expand All @@ -142,8 +153,11 @@ class TOSHI_EXPORT TObject
TBOOL IsExactly(const TClass& a_rClass) const { return GetClass().IsExactly(a_rClass); }

protected:
TObject() {}
virtual ~TObject() = default;

};

TClass TObject::m_sClass = TClass("TObject", TNULL, TObject::CreateObject, TObject::CreateObjectInPlace, TObject::InitialiseStatic, TObject::DeinitialiseStatic, 1);

TOSHI_NAMESPACE_END
7 changes: 2 additions & 5 deletions Toshi/Source/TKernel/TObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

TOSHI_NAMESPACE_USING

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)
{
m_pcName = a_pcName;
Expand Down Expand Up @@ -70,7 +67,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 = TGetClass(TObject);
if (a_pClass == TNULL) a_pClass = &TGetClass(TObject);
return FindRecurse(a_pcClassName, a_pClass, TFALSE);
}

Expand Down Expand Up @@ -106,7 +103,7 @@ void TClass::InitialiseStatic()
TBOOL TClass::IsA(const TClass& a_rClass) const
{
for (const TClass* pClass = this; pClass != TNULL; pClass = pClass->m_pParent) {
if (IsExactly(a_rClass)) return TTRUE;
if (pClass->IsExactly(a_rClass)) return TTRUE;
}
return TFALSE;
}
Expand Down

0 comments on commit 303f495

Please sign in to comment.