Add ecs ComponentListener

- Add ecs ComponentListener which listens to Component addition and
  removal
- Add RefCounter class used to keep track of moves and copies
This commit is contained in:
Thraix
2023-05-29 17:49:37 +02:00
parent 5a615ecc4e
commit 3ec9bcd152
24 changed files with 351 additions and 70 deletions
@@ -0,0 +1,72 @@
#include "copium/util/RefCounter.h"
#include "copium/util/Common.h"
namespace Copium
{
RefCounter::RefCounter()
: refCounter{new int{1}}
{}
RefCounter::~RefCounter()
{
if (Valid())
{
(*refCounter)--;
if (*refCounter == 0)
{
delete refCounter;
refCounter = nullptr;
}
}
}
RefCounter::RefCounter(RefCounter&& rhs)
: refCounter(rhs.refCounter)
{
CP_ASSERT(Valid(), "RefCounter : Moving a deleted RefCounter");
rhs.refCounter = nullptr;
}
RefCounter::RefCounter(const RefCounter& rhs)
: refCounter{rhs.refCounter}
{
CP_ASSERT(Valid(), "RefCounter : Copying a deleted RefCounter");
(*refCounter)++;
}
RefCounter& RefCounter::operator=(RefCounter&& rhs)
{
CP_ASSERT(Valid(), "operator= : Moving a deleted RefCounter");
refCounter = rhs.refCounter;
rhs.refCounter = nullptr;
return *this;
}
RefCounter& RefCounter::operator=(const RefCounter& rhs)
{
CP_ASSERT(Valid(), "operator= : Copying a deleted RefCounter");
refCounter = rhs.refCounter;
(*refCounter)++;
return *this;
}
bool RefCounter::Valid() const
{
return refCounter != nullptr;
}
bool RefCounter::LastRef() const
{
return Valid() && *refCounter == 1;
}
int RefCounter::Counter() const
{
CP_ASSERT(Valid(), "Counter : referencing a deleted RefCounter");
return *refCounter;
}
}
+24
View File
@@ -0,0 +1,24 @@
#pragma once
namespace Copium
{
class RefCounter final
{
private:
int* refCounter;
public:
RefCounter();
~RefCounter();
RefCounter(RefCounter&& rhs);
RefCounter(const RefCounter& rhs);
RefCounter& operator=(RefCounter&& rhs);
RefCounter& operator=(const RefCounter& rhs);
bool Valid() const;
bool LastRef() const;
int Counter() const;
};
}