Skip to content

Commit

Permalink
Minimize ElunaObject differences between cores using unique_weak_ptr …
Browse files Browse the repository at this point in the history
…and ones that dont
  • Loading branch information
Shauren committed Jul 13, 2024
1 parent 7ba7506 commit 6fc52ea
Showing 1 changed file with 13 additions and 27 deletions.
40 changes: 13 additions & 27 deletions ElunaTemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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

Expand All @@ -201,19 +201,13 @@ template <typename T>
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<T*>(&_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<T*>(&_obj); }
bool IsValid() const override { return true; }
#ifndef TRINITY
void Invalidate() override { }
#endif

Expand Down Expand Up @@ -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));
Expand All @@ -461,11 +451,7 @@ class ElunaTemplate
}
return NULL;
}
#ifdef TRINITY
return static_cast<T*>(obj);
#else
return static_cast<T*>(elunaObj->GetObj());
#endif
}

static int GetType(lua_State* L)
Expand Down

0 comments on commit 6fc52ea

Please sign in to comment.