Files
Seele/src/Engine/MinimalEngine.h
T

261 lines
5.3 KiB
C++
Raw Normal View History

#pragma once
2020-04-01 02:17:49 +02:00
#include "EngineTypes.h"
2021-12-02 13:00:03 +01:00
#include <map>
2020-05-05 01:51:13 +02:00
#define DEFINE_REF(x) \
2022-11-29 16:34:42 +01:00
typedef ::Seele::RefPtr<x> P##x; \
typedef ::Seele::UniquePtr<x> UP##x; \
2023-11-01 23:12:30 +01:00
typedef ::Seele::OwningPtr<x> O##x;
2020-05-05 01:51:13 +02:00
#define DECLARE_REF(x) \
2021-10-23 00:22:35 +02:00
class x; \
2022-11-29 16:34:42 +01:00
typedef ::Seele::RefPtr<x> P##x; \
typedef ::Seele::UniquePtr<x> UP##x; \
2023-11-01 23:12:30 +01:00
typedef ::Seele::OwningPtr<x> O##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; \
2023-11-01 23:12:30 +01:00
typedef OwningPtr<x> O##x; \
2021-10-23 00:22:35 +02:00
}
2020-05-05 01:51:13 +02:00
2023-08-26 15:19:12 +02:00
namespace Seele
{
2023-11-01 23:12:30 +01:00
template <typename T>
2020-05-05 01:51:13 +02:00
class RefPtr
{
public:
2022-04-13 13:01:35 +02:00
constexpr RefPtr() noexcept
2023-10-29 09:20:23 +01:00
: object(nullptr)
2021-10-23 00:22:35 +02:00
{
}
2023-10-29 09:20:23 +01:00
constexpr RefPtr(std::nullptr_t) noexcept
: object(nullptr)
2021-10-23 00:22:35 +02:00
{
}
2023-11-05 10:36:01 +01:00
RefPtr(T* ptr)
2023-11-01 23:12:30 +01:00
: object(ptr)
2021-10-23 00:22:35 +02:00
{
}
2023-11-05 10:36:01 +01:00
constexpr RefPtr(const RefPtr& other) noexcept
2021-10-23 00:22:35 +02:00
: object(other.object)
{
}
2023-11-05 10:36:01 +01:00
constexpr RefPtr(RefPtr&& rhs) noexcept
2021-10-23 00:22:35 +02:00
: object(std::move(rhs.object))
{
rhs.object = nullptr;
}
template <typename F>
2023-11-05 10:36:01 +01:00
constexpr RefPtr<F> cast()
2021-10-23 00:22:35 +02:00
{
2023-11-05 10:36:01 +01:00
T* t = object;
F* f = dynamic_cast<F*>(t);
2021-10-23 00:22:35 +02:00
if (f == nullptr)
{
return nullptr;
}
2023-11-01 23:12:30 +01:00
return RefPtr<F>(f);
2021-10-23 00:22:35 +02:00
}
2023-11-05 10:36:01 +01:00
template <typename F>
constexpr const RefPtr<F> cast() const
{
T* t = object;
F* f = dynamic_cast<F*>(t);
if (f == nullptr)
{
return nullptr;
}
return RefPtr<F>(f);
}
constexpr RefPtr& operator=(const RefPtr& other)
2021-10-23 00:22:35 +02:00
{
if (this != &other)
{
object = other.object;
}
return *this;
}
2023-11-05 10:36:01 +01:00
constexpr RefPtr& operator=(RefPtr&& rhs) noexcept
2021-10-23 00:22:35 +02:00
{
if (this != &rhs)
{
object = std::move(rhs.object);
rhs.object = nullptr;
}
return *this;
}
2022-04-13 13:01:35 +02:00
constexpr ~RefPtr()
2021-10-23 00:22:35 +02:00
{
}
2022-04-13 13:01:35 +02:00
constexpr bool operator==(const RefPtr& rhs) const noexcept
2021-10-23 00:22:35 +02:00
{
2021-11-11 20:12:50 +01:00
return object == rhs.object;
2021-10-23 00:22:35 +02:00
}
2023-11-05 10:36:01 +01:00
constexpr auto operator<=>(const RefPtr& rhs) const noexcept
2021-10-23 00:22:35 +02:00
{
2021-11-11 20:12:50 +01:00
return object <=> rhs.object;
2021-10-23 00:22:35 +02:00
}
2023-11-05 10:36:01 +01:00
template<typename F>
constexpr operator RefPtr<F>()
{
return RefPtr<F>(static_cast<F*>(object));
}
template<typename F>
constexpr operator RefPtr<F>() const
{
return RefPtr<F>(static_cast<F*>(object));
}
constexpr T* operator->()
2021-10-23 00:22:35 +02:00
{
2023-11-01 23:12:30 +01:00
return object;
2021-10-23 00:22:35 +02:00
}
2023-11-05 10:36:01 +01:00
constexpr const T* operator->() const
2021-10-23 00:22:35 +02:00
{
2023-11-01 23:12:30 +01:00
return object;
2021-10-23 00:22:35 +02:00
}
2023-11-01 23:12:30 +01:00
constexpr T* getHandle()
2021-10-23 00:22:35 +02:00
{
return object;
}
2023-11-01 23:12:30 +01:00
constexpr const T* getHandle() const
2021-10-23 00:22:35 +02:00
{
2023-11-01 23:12:30 +01:00
return object;
2022-01-12 14:40:26 +01:00
}
2020-05-05 01:51:13 +02:00
private:
2023-11-01 23:12:30 +01:00
T* object;
2020-05-05 01:51:13 +02:00
};
2023-11-05 10:36:01 +01:00
template <typename T, typename Deleter = std::default_delete<T>>
class OwningPtr
{
public:
OwningPtr()
: pointer(nullptr)
{
}
OwningPtr(T* ptr)
: pointer(ptr)
{}
template<typename F>
OwningPtr(OwningPtr<F>&& other)
{
pointer = dynamic_cast<T*>(*other);
}
OwningPtr(const OwningPtr& other) = delete;
OwningPtr(OwningPtr&& other) noexcept
{
pointer = other.pointer;
other.pointer = nullptr;
}
OwningPtr& operator=(const OwningPtr& other) = delete;
OwningPtr& operator=(OwningPtr&& other) noexcept
{
if (this != &other)
{
pointer = other.pointer;
}
return *this;
}
operator RefPtr<T>()
{
return RefPtr<T>(pointer);
}
operator RefPtr<T>() const
{
return RefPtr<T>(pointer);
}
constexpr T* operator->()
{
return pointer;
}
constexpr const T* operator->() const
{
return pointer;
}
constexpr T* operator*()
{
return pointer;
}
constexpr const T* operator*() const
{
return pointer;
}
constexpr bool operator==(const OwningPtr& rhs) const noexcept
{
return pointer == rhs.pointer;
}
constexpr auto operator<=>(const OwningPtr& rhs) const noexcept
{
return pointer <=> rhs.pointer;
}
private:
friend class RefPtr<T>;
T* pointer;
};
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)
{
}
2023-10-29 09:20:23 +01:00
UniquePtr(std::nullptr_t)
2021-10-23 00:22:35 +02:00
: 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)
{
2023-11-05 10:36:01 +01:00
if (this != &rhs)
{
handle = rhs.handle;
rhs.handle = nullptr;
}
2021-10-23 00:22:35 +02:00
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;
}
2021-12-02 13:00:03 +01:00
inline T* getHandle()
{
return handle;
}
2021-10-23 00:22:35 +02:00
bool isValid()
{
return handle != nullptr;
}
2020-05-05 01:51:13 +02:00
private:
2021-10-23 00:22:35 +02:00
T *handle;
2020-05-05 01:51:13 +02:00
};
} // namespace Seele