summaryrefslogtreecommitdiffhomepage
path: root/Private/NewKit/Ref.hpp
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-01-26 22:26:48 +0100
committerAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-01-26 22:27:09 +0100
commiteba8b7ddd0a455d9e49f32dcae712c5612c0093c (patch)
tree749a3d34546d055507a920bce4ab10e8a9945719 /Private/NewKit/Ref.hpp
parentdd192787a70a973f2474720aea49af3f6ddabb7a (diff)
Kernel: Major repository refactor.
Rework the repo into Private and Public modules. Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'Private/NewKit/Ref.hpp')
-rw-r--r--Private/NewKit/Ref.hpp88
1 files changed, 88 insertions, 0 deletions
diff --git a/Private/NewKit/Ref.hpp b/Private/NewKit/Ref.hpp
new file mode 100644
index 00000000..3d20c506
--- /dev/null
+++ b/Private/NewKit/Ref.hpp
@@ -0,0 +1,88 @@
+
+/*
+ * ========================================================
+ *
+ * hCore
+ * Copyright 2024 Mahrouss Logic, all rights reserved.
+ *
+ * ========================================================
+ */
+
+#pragma once
+
+#include <NewKit/Defines.hpp>
+#include <NewKit/Panic.hpp>
+
+namespace hCore
+{
+ template <typename T>
+ class Ref final
+ {
+ public:
+ Ref() = default;
+ ~Ref() = default;
+
+ public:
+ Ref(T cls, const bool &strong = false) : m_Class(cls), m_Strong(strong) {}
+
+ Ref &operator=(T ref)
+ {
+ m_Class = ref;
+ return *this;
+ }
+
+ public:
+ T operator->() const
+ {
+ return m_Class;
+ }
+
+ T &Leak()
+ {
+ return m_Class;
+ }
+
+ T operator*()
+ {
+ return m_Class;
+ }
+
+ bool IsStrong() const
+ {
+ return m_Strong;
+ }
+
+ operator bool()
+ {
+ return m_Class;
+ }
+
+ private:
+ T m_Class;
+ bool m_Strong{ false };
+
+ };
+
+ template <typename T>
+ class NonNullRef final
+ {
+ public:
+ NonNullRef() = delete;
+ NonNullRef(nullPtr) = delete;
+
+ NonNullRef(T *ref) : m_Ref(ref, true) {}
+
+ Ref<T> &operator->()
+ {
+ MUST_PASS(m_Ref);
+ return m_Ref;
+ }
+
+ NonNullRef &operator=(const NonNullRef<T> &ref) = delete;
+ NonNullRef(const NonNullRef<T> &ref) = default;
+
+ private:
+ Ref<T> m_Ref{nullptr};
+
+ };
+} // namespace hCore