From 6fc52eabdcfb8c52ae577842b24b775f35997be5 Mon Sep 17 00:00:00 2001 From: Shauren Date: Sat, 13 Jul 2024 12:27:42 +0200 Subject: [PATCH] Minimize ElunaObject differences between cores using unique_weak_ptr and ones that dont --- ElunaTemplate.h | 40 +++++++++++++--------------------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/ElunaTemplate.h b/ElunaTemplate.h index d98d9e8106..fb395f488a 100644 --- a/ElunaTemplate.h +++ b/ElunaTemplate.h @@ -114,21 +114,15 @@ class ElunaObject { } -#ifdef TRINITY // Get wrapped object pointer virtual void* GetObjIfValid() const = 0; -#else - // Get wrapped object pointer - virtual void* GetObj() const = 0; - // Returns whether the object is valid or not - virtual bool IsValid() const = 0; + // Returns pointer to the wrapped object's type name + const char* GetTypeName() const { return type_name; } +#ifndef TRINITY // Invalidates the pointer if it should be invalidated virtual void Invalidate() = 0; #endif - // Returns pointer to the wrapped object's type name - const char* GetTypeName() const { return type_name; } - protected: Eluna* E; const char* type_name; @@ -179,12 +173,18 @@ class ElunaObjectImpl : public ElunaObject return nullptr; } #else - ElunaObjectImpl(Eluna* E, T* obj, char const* tname) : ElunaObject(E, tname), _obj(obj), callstackid(E->GetCallstackId()) + ElunaObjectImpl(Eluna* E, T const* obj, char const* tname) : ElunaObject(E, tname), _obj(obj), callstackid(E->GetCallstackId()) + { + } + + void* GetObjIfValid() const override { + if (callstackid == E->GetCallstackId()) + return _obj; + + return nullptr; } - void* GetObj() const override { return _obj; } - bool IsValid() const override { return callstackid == E->GetCallstackId(); } void Invalidate() override { callstackid = 1; } #endif @@ -201,19 +201,13 @@ template class ElunaObjectValueImpl : public ElunaObject { public: -#ifdef TRINITY ElunaObjectValueImpl(Eluna* E, T const* obj, char const* tname) : ElunaObject(E, tname), _obj(*obj /*always a copy, what gets passed here might be pointing to something not owned by us*/) { } void* GetObjIfValid() const override { return const_cast(&_obj); } -#else - ElunaObjectValueImpl(Eluna* E, T* obj, char const* tname) : ElunaObject(E, tname), _obj(*obj /*always a copy, what gets passed here might be pointing to something not owned by us*/) - { - } - void* GetObj() const override { return const_cast(&_obj); } - bool IsValid() const override { return true; } +#ifndef TRINITY void Invalidate() override { } #endif @@ -442,12 +436,8 @@ class ElunaTemplate if (!elunaObj) return NULL; -#ifdef TRINITY void* obj = elunaObj->GetObjIfValid(); if (!obj) -#else - if (!elunaObj->IsValid()) -#endif { char buff[256]; snprintf(buff, 256, "%s expected, got pointer to nonexisting (invalidated) object (%s). Check your code.", tname, luaL_typename(L, narg)); @@ -461,11 +451,7 @@ class ElunaTemplate } return NULL; } -#ifdef TRINITY return static_cast(obj); -#else - return static_cast(elunaObj->GetObj()); -#endif } static int GetType(lua_State* L)