summaryrefslogtreecommitdiffhomepage
path: root/include/CompilerKit/Ref.h
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-12-05 11:49:28 -0500
committerAmlal El Mahrouss <amlal@nekernel.org>2025-12-05 11:50:39 -0500
commit037ac38824623c13070384e8fc0e70c4770dcdbd (patch)
tree19d7286c5d226b33f10743c76436dace0cf42112 /include/CompilerKit/Ref.h
parent5535f22998bf991eeb75a56c9e147f0fd4bd23b2 (diff)
chore! new project filesystem structure.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'include/CompilerKit/Ref.h')
-rw-r--r--include/CompilerKit/Ref.h77
1 files changed, 77 insertions, 0 deletions
diff --git a/include/CompilerKit/Ref.h b/include/CompilerKit/Ref.h
new file mode 100644
index 0000000..863a100
--- /dev/null
+++ b/include/CompilerKit/Ref.h
@@ -0,0 +1,77 @@
+
+/*
+ * ========================================================
+ *
+ * CompilerKit
+ * Copyright (C) 2024-2025 Amlal El Mahrouss, Licensed under the Apache 2.0 license.
+ *
+ * ========================================================
+ */
+
+#pragma once
+
+#include <CompilerKit/Detail/Config.h>
+
+namespace CompilerKit {
+/// @author Amlal El Mahrouss
+/// @brief Reference holder class, refers to a pointer of data in static memory.
+template <typename T>
+class Ref final {
+ public:
+ explicit Ref() = default;
+
+ ~Ref() {
+ if (m_Strong) {
+ MUST_PASS(m_Class);
+ if (m_Class) delete m_Class;
+ m_Class = nullptr;
+ }
+ }
+
+ NECTI_COPY_DEFAULT(Ref);
+
+ public:
+ explicit 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; }
+
+ explicit operator bool() { return *m_Class; }
+
+ private:
+ T* m_Class{nullptr};
+ bool m_Strong{false};
+};
+
+// @author Amlal El Mahrouss
+// @brief Non null Reference holder class, refers to a pointer of data in static memory.
+template <typename T>
+class NonNullRef final {
+ public:
+ explicit NonNullRef() = delete;
+
+ explicit 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 CompilerKit