summaryrefslogtreecommitdiffhomepage
path: root/NewKit/Ref.hpp
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-01-06 09:14:11 +0100
committerAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-01-06 09:14:11 +0100
commit5339d016c07bf717ee388f4feb73544087324af0 (patch)
tree94be6f67ed626091f24aee24ec3b3be03d01e4e7 /NewKit/Ref.hpp
git: port from mercurial repo.
Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'NewKit/Ref.hpp')
-rw-r--r--NewKit/Ref.hpp88
1 files changed, 88 insertions, 0 deletions
diff --git a/NewKit/Ref.hpp b/NewKit/Ref.hpp
new file mode 100644
index 00000000..610776a3
--- /dev/null
+++ b/NewKit/Ref.hpp
@@ -0,0 +1,88 @@
+
+/*
+ * ========================================================
+ *
+ * hCore
+ * Copyright 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