summaryrefslogtreecommitdiffhomepage
path: root/dev/LibCompiler/Ref.h
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-04-19 17:33:26 +0200
committerAmlal El Mahrouss <amlal@nekernel.org>2025-04-19 17:33:26 +0200
commitbefde76cfa46c766e81f74eb5ac65d3dae2dde87 (patch)
tree45b2f9fd6b3f9605c2747485bd24483192f99e73 /dev/LibCompiler/Ref.h
parent3afc481dc64a07fe7fcaff9ce7a12a492c3ec8e7 (diff)
dev, LibCompiler, tooling: refactor and separate components into modules
(cppdrv, cxxdrv) Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'dev/LibCompiler/Ref.h')
-rw-r--r--dev/LibCompiler/Ref.h103
1 files changed, 103 insertions, 0 deletions
diff --git a/dev/LibCompiler/Ref.h b/dev/LibCompiler/Ref.h
new file mode 100644
index 0000000..117083c
--- /dev/null
+++ b/dev/LibCompiler/Ref.h
@@ -0,0 +1,103 @@
+
+/*
+ * ========================================================
+ *
+ * LibCompiler
+ * Copyright (C) 2024-2025 Amlal El Mahrouss, all rights reserved.
+ *
+ * ========================================================
+ */
+
+#pragma once
+
+#include <LibCompiler/Defines.h>
+
+namespace LibCompiler
+{
+ // @author EL Mahrouss Amlal
+ // @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)
+ {
+ (*m_Class).~T();
+ }
+ }
+
+ LIBCOMPILER_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;
+ }
+
+ operator bool()
+ {
+ return *m_Class;
+ }
+
+ private:
+ T* m_Class{nullptr};
+ Bool m_Strong{false};
+ };
+
+ // @author EL Mahrouss Amlal
+ // @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 LibCompiler