Files
Seele/src/Engine/MinimalEngine.h
T

367 lines
7.8 KiB
C++
Raw Normal View History

#pragma once
2020-04-01 02:17:49 +02:00
#include "Containers/Map.h"
#include "EngineTypes.h"
2020-03-05 11:43:38 +01:00
#include "Math/Math.h"
2021-10-23 00:22:35 +02:00
//#include "ThreadPool.h"
2020-05-05 01:51:13 +02:00
#define DEFINE_REF(x) \
2021-10-23 00:22:35 +02:00
typedef RefPtr<x> P##x; \
typedef UniquePtr<x> UP##x; \
typedef WeakPtr<x> W##x;
2020-05-05 01:51:13 +02:00
#define DECLARE_REF(x) \
2021-10-23 00:22:35 +02:00
class x; \
typedef RefPtr<x> P##x; \
typedef UniquePtr<x> UP##x; \
typedef WeakPtr<x> W##x;
2020-03-02 19:07:49 +01:00
2020-05-05 01:51:13 +02:00
#define DECLARE_NAME_REF(nmsp, x) \
2021-10-23 00:22:35 +02:00
namespace nmsp \
{ \
class x; \
typedef RefPtr<x> P##x; \
typedef UniquePtr<x> UP##x; \
typedef WeakPtr<x> W##x; \
}
2020-05-05 01:51:13 +02:00
extern Seele::Map<void *, void *> registeredObjects;
2020-06-08 01:44:47 +02:00
extern std::mutex registeredObjectsLock;
namespace Seele
{
2020-05-05 01:51:13 +02:00
template <typename T>
2021-04-13 23:09:16 +02:00
class RefPtr;
template <typename T>
2020-05-05 01:51:13 +02:00
class RefObject
{
public:
2021-10-23 00:22:35 +02:00
RefObject(T *ptr)
: handle(ptr), refCount(1)
{
registeredObjects[ptr] = this;
}
inline RefObject(const RefObject &rhs)
: handle(rhs.handle), refCount(rhs.refCount)
{
}
RefObject(RefObject &&rhs)
: handle(std::move(rhs.handle)), refCount(std::move(rhs.refCount))
{
}
~RefObject()
{
{
std::unique_lock lock(registeredObjectsLock);
registeredObjects.erase(handle);
}
2021-11-01 20:25:16 +01:00
// #pragma warning( disable: 4150)
2021-10-23 00:22:35 +02:00
delete handle;
2021-11-01 20:25:16 +01:00
// #pragma warning( default: 4150)
2021-10-23 00:22:35 +02:00
}
RefObject &operator=(const RefObject &rhs)
{
if (*this != rhs)
{
handle = rhs.handle;
refCount = rhs.refCount;
}
return *this;
}
RefObject &operator=(RefObject &&rhs)
{
if (*this != rhs)
{
handle = std::move(rhs.handle);
refCount = std::move(rhs.refCount);
rhs.handle = nullptr;
rhs.refCount = 0;
}
return *this;
}
inline bool operator==(const RefObject &other) const
{
return handle == other.handle;
}
inline bool operator!=(const RefObject &other) const
{
return handle != other.handle;
}
inline bool operator<(const RefObject &other) const
{
return handle < other.handle;
}
void addRef()
{
refCount++;
}
inline void removeRef()
{
refCount--;
if (refCount == 0)
{
delete this;
}
}
T *getHandle() const
{
return handle;
}
2020-05-05 01:51:13 +02:00
private:
2021-10-23 00:22:35 +02:00
T *handle;
std::atomic_uint64_t refCount;
friend class RefPtr<T>;
2020-05-05 01:51:13 +02:00
};
template <typename T>
class RefPtr
{
public:
2021-10-23 00:22:35 +02:00
RefPtr()
{
object = nullptr;
}
RefPtr(nullptr_t)
{
object = nullptr;
}
RefPtr(T *ptr)
{
std::unique_lock l(registeredObjectsLock);
auto registeredObj = registeredObjects.find(ptr);
// get here for thread safetly
auto registeredEnd = registeredObjects.end();
if (registeredObj == registeredEnd)
{
object = new RefObject<T>(ptr);
l.unlock();
}
else
{
l.unlock();
object = (RefObject<T> *)registeredObj->value;
object->addRef();
}
}
explicit RefPtr(RefObject<T> *other)
: object(other)
{
object->addRef();
}
inline RefPtr(const RefPtr &other)
: object(other.object)
{
if (object != nullptr)
{
object->addRef();
}
}
RefPtr(RefPtr &&rhs)
: object(std::move(rhs.object))
{
rhs.object = nullptr;
//Dont change references, they stay the same
}
template <typename F>
RefPtr(const RefPtr<F> &other)
{
F *f = other.getObject()->getHandle();
assert(static_cast<T *>(f));
object = (RefObject<T> *)other.getObject();
object->addRef();
}
2020-05-05 01:51:13 +02:00
2021-10-23 00:22:35 +02:00
template <typename F>
RefPtr<F> cast()
{
T *t = object->getHandle();
F *f = dynamic_cast<F *>(t);
if (f == nullptr)
{
return nullptr;
}
RefObject<F> *newObject = (RefObject<F> *)object;
return RefPtr<F>(newObject);
}
2020-05-05 01:51:13 +02:00
2021-10-23 00:22:35 +02:00
template <typename F>
const RefPtr<F> cast() const
{
T *t = object->getHandle();
F *f = dynamic_cast<F *>(t);
if (f == nullptr)
{
return nullptr;
}
RefObject<F> *newObject = (RefObject<F> *)object;
return RefPtr<F>(newObject);
}
2020-05-05 01:51:13 +02:00
2021-10-23 00:22:35 +02:00
RefPtr &operator=(const RefPtr &other)
{
if (this != &other)
{
if (object != nullptr)
{
object->removeRef();
}
object = other.object;
if (object != nullptr)
{
object->addRef();
}
}
return *this;
}
RefPtr &operator=(RefPtr &&rhs)
{
if (this != &rhs)
{
if (object != nullptr)
{
object->removeRef();
}
object = std::move(rhs.object);
rhs.object = nullptr;
}
return *this;
}
~RefPtr()
{
if (object != nullptr)
{
object->removeRef();
}
}
inline bool operator==(const RefPtr &other) const
{
return object == other.object;
}
inline bool operator!=(const RefPtr &other) const
{
return object != other.object;
}
bool operator<(const RefPtr &other) const
{
return object < other.object;
}
inline T *operator->()
{
assert(object != nullptr);
return object->handle;
}
inline const T *operator->() const
{
assert(object != nullptr);
return object->handle;
}
RefObject<T> *getObject() const
{
return object;
}
inline T *getHandle()
{
return object->getHandle();
}
inline const T *getHandle() const
{
return object->getHandle();
}
2020-05-05 01:51:13 +02:00
private:
2021-10-23 00:22:35 +02:00
RefObject<T> *object;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive& ar, const unsigned int)
{
ar & *object->getHandle();
}
2020-05-05 01:51:13 +02:00
};
template <typename T>
class UniquePtr
{
public:
2021-10-23 00:22:35 +02:00
UniquePtr()
: handle(nullptr)
{
}
UniquePtr(nullptr_t)
: handle(nullptr)
{
}
UniquePtr(T *ptr)
: handle(ptr)
{
}
UniquePtr(const UniquePtr &rhs) = delete;
UniquePtr(UniquePtr &&rhs) noexcept
: handle(rhs.handle)
{
rhs.handle = nullptr;
}
UniquePtr &operator=(const UniquePtr &rhs) = delete;
UniquePtr &operator=(UniquePtr &&rhs)
{
handle = rhs.handle;
rhs.handle = nullptr;
return *this;
}
~UniquePtr()
{
delete handle;
}
inline bool operator==(const UniquePtr &other) const
{
return handle == other.handle;
}
inline bool operator!=(const UniquePtr &other) const
{
return handle != other.handle;
}
inline T *operator->()
{
return handle;
}
bool isValid()
{
return handle != nullptr;
}
2020-05-05 01:51:13 +02:00
private:
2021-10-23 00:22:35 +02:00
T *handle;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar & *handle;
}
2020-05-05 01:51:13 +02:00
};
2021-09-29 22:12:56 +02:00
//A weak pointer has no ownership over an object and thus cant delete it
template <typename T>
class WeakPtr
{
public:
2021-10-23 00:22:35 +02:00
WeakPtr()
: pointer(nullptr)
{
}
WeakPtr(RefPtr<T> &sharedPtr)
: pointer(sharedPtr)
{
}
WeakPtr &operator=(WeakPtr<T> &weakPtr)
{
pointer = weakPtr.pointer;
return *this;
}
WeakPtr &operator=(RefPtr<T> &sharedPtr)
{
pointer = sharedPtr;
return *this;
}
2021-09-29 22:12:56 +02:00
private:
2021-10-23 00:22:35 +02:00
RefPtr<T> pointer;
2021-09-29 22:12:56 +02:00
};
2020-05-05 01:51:13 +02:00
} // namespace Seele
using namespace Seele;