🎉 begin project
This commit is contained in:
commit
8da6ddc689
29 changed files with 1261 additions and 0 deletions
4
Source/Types/CMakeLists.txt
Normal file
4
Source/Types/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
add_module_sources(
|
||||
Geometry.cppm
|
||||
Ref.cppm
|
||||
)
|
||||
45
Source/Types/Geometry.cppm
Normal file
45
Source/Types/Geometry.cppm
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
module;
|
||||
#include <cstdint>
|
||||
|
||||
export module VUI:Geometry;
|
||||
|
||||
namespace VUI {
|
||||
export struct Point {
|
||||
float x{0};
|
||||
float y{0};
|
||||
};
|
||||
|
||||
export struct UPoint {
|
||||
uint32_t x{0};
|
||||
uint32_t y{0};
|
||||
};
|
||||
|
||||
export struct URect {
|
||||
uint32_t x{0};
|
||||
uint32_t y{0};
|
||||
uint32_t width{0};
|
||||
uint32_t height{0};
|
||||
};
|
||||
|
||||
export struct Rect {
|
||||
float x{0};
|
||||
float y{0};
|
||||
float width{0};
|
||||
float height{0};
|
||||
};
|
||||
|
||||
// replace with glm
|
||||
export struct Vec3 {
|
||||
float x{0};
|
||||
float y{0};
|
||||
float z{0};
|
||||
};
|
||||
|
||||
export struct Vec4 {
|
||||
float x{0};
|
||||
float y{0};
|
||||
float z{0};
|
||||
float w{0};
|
||||
};
|
||||
|
||||
} // namespace VUI
|
||||
137
Source/Types/Ref.cppm
Normal file
137
Source/Types/Ref.cppm
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
module;
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
|
||||
export module VUI:Ref;
|
||||
|
||||
namespace VUI {
|
||||
|
||||
export struct RefCounted {
|
||||
virtual ~RefCounted() = default;
|
||||
|
||||
void incrementRefCount() const { ++m_RefCount; }
|
||||
void decrementRefCount() const { --m_RefCount; }
|
||||
|
||||
[[nodiscard]] std::uint32_t getRefCount() const { return m_RefCount.load(); }
|
||||
|
||||
private:
|
||||
mutable std::atomic<uint32_t> m_RefCount = 0;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
concept IsRefCounted = std::is_base_of_v<RefCounted, T>;
|
||||
|
||||
export template <typename T> class Ref {
|
||||
public:
|
||||
static_assert(IsRefCounted<T>, "Should be RefCounted");
|
||||
Ref() : m_Instance(nullptr) {}
|
||||
|
||||
explicit Ref(std::nullptr_t) : m_Instance(nullptr) {}
|
||||
|
||||
explicit Ref(T *instance) : m_Instance(instance) { incRef(); }
|
||||
|
||||
template <typename T2> explicit Ref(const Ref<T2> &other) {
|
||||
m_Instance = static_cast<T *>(other.m_Instance);
|
||||
incRef();
|
||||
}
|
||||
|
||||
template <typename T2> explicit Ref(Ref<T2> &&other) {
|
||||
m_Instance = static_cast<T *>(other.m_Instance);
|
||||
other.m_Instance = nullptr;
|
||||
}
|
||||
|
||||
~Ref() { decRef(); }
|
||||
|
||||
Ref(const Ref &other) : m_Instance(other.m_Instance) { incRef(); }
|
||||
|
||||
Ref &operator=(std::nullptr_t) {
|
||||
decRef();
|
||||
m_Instance = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Ref &operator=(const Ref &other) {
|
||||
if (this == &other)
|
||||
return *this;
|
||||
|
||||
other.incRef();
|
||||
decRef();
|
||||
|
||||
m_Instance = other.m_Instance;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T2> Ref &operator=(const Ref<T2> &other) {
|
||||
other.incRef();
|
||||
decRef();
|
||||
|
||||
m_Instance = other.m_Instance;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T2> Ref &operator=(Ref<T2> &&other) {
|
||||
decRef();
|
||||
|
||||
m_Instance = other.m_Instance;
|
||||
other.m_Instance = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator bool() { return m_Instance != nullptr; }
|
||||
operator bool() const { return m_Instance != nullptr; }
|
||||
|
||||
T *operator->() { return m_Instance; }
|
||||
const T *operator->() const { return m_Instance; }
|
||||
|
||||
T &operator*() { return *m_Instance; }
|
||||
const T &operator*() const { return *m_Instance; }
|
||||
|
||||
T *value() { return m_Instance; }
|
||||
const T *value() const { return m_Instance; }
|
||||
|
||||
void reset(T *instance = nullptr) {
|
||||
decRef();
|
||||
m_Instance = instance;
|
||||
}
|
||||
|
||||
template <typename T2> Ref<T2> as() const { return Ref<T2>(*this); }
|
||||
|
||||
template <typename... Args> static Ref Create(Args &&...args) {
|
||||
return Ref(new T(std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
bool operator==(const Ref &other) const {
|
||||
return m_Instance == other.m_Instance;
|
||||
}
|
||||
|
||||
bool operator!=(const Ref &other) const { return !(*this == other); }
|
||||
|
||||
bool equals(const Ref &other) {
|
||||
if (!m_Instance || !other.m_Instance)
|
||||
return false;
|
||||
|
||||
return m_Instance == other.m_Instance;
|
||||
}
|
||||
|
||||
private:
|
||||
void incRef() const {
|
||||
if (m_Instance) {
|
||||
m_Instance->incrementRefCount();
|
||||
}
|
||||
}
|
||||
|
||||
void decRef() const {
|
||||
if (m_Instance) {
|
||||
m_Instance->decrementRefCount();
|
||||
|
||||
if (m_Instance->getRefCount() == 0) {
|
||||
delete m_Instance;
|
||||
m_Instance = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T2> friend class Ref;
|
||||
mutable T *m_Instance;
|
||||
};
|
||||
} // namespace VUI
|
||||
Loading…
Add table
Add a link
Reference in a new issue