summaryrefslogtreecommitdiffhomepage
path: root/Kernel/NewKit/Ref.hpp
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-05-09 00:42:44 +0200
committerAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-05-09 00:42:44 +0200
commitaf8a516fc22865abd80d6e26f1541fa3d6bebfdc (patch)
tree96d42a10945fc03df022389aef54708383c1d616 /Kernel/NewKit/Ref.hpp
parenta874e9cc98df994178d55996943fe81799c61d2f (diff)
MHR-23: :boom:, refactors.
- Move NewBoot to /Boot, thus making Kernel directory only containing the kernel. Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'Kernel/NewKit/Ref.hpp')
-rw-r--r--Kernel/NewKit/Ref.hpp89
1 files changed, 89 insertions, 0 deletions
diff --git a/Kernel/NewKit/Ref.hpp b/Kernel/NewKit/Ref.hpp
new file mode 100644
index 00000000..356fcad0
--- /dev/null
+++ b/Kernel/NewKit/Ref.hpp
@@ -0,0 +1,89 @@
+
+/* -------------------------------------------
+
+ Copyright SoftwareLabs
+
+------------------------------------------- */
+
+#pragma once
+
+#include <NewKit/Defines.hpp>
+#include <NewKit/KernelCheck.hpp>
+
+namespace NewOS
+{
+ template <typename T>
+ class Ref final
+ {
+ public:
+ Ref() = default;
+ ~Ref() = default;
+
+ public:
+ Ref(T cls, const bool& strong = false)
+ : fClass(cls), fStrong(strong)
+ {
+ }
+
+ Ref& operator=(T ref)
+ {
+ fClass = ref;
+ return *this;
+ }
+
+ public:
+ T operator->() const
+ {
+ return fClass;
+ }
+
+ T& Leak()
+ {
+ return fClass;
+ }
+
+ T operator*()
+ {
+ return fClass;
+ }
+
+ bool IsStrong() const
+ {
+ return fStrong;
+ }
+
+ operator bool()
+ {
+ return fStrong;
+ }
+
+ private:
+ T fClass;
+ bool fStrong{false};
+ };
+
+ template <typename T>
+ class NonNullRef final
+ {
+ public:
+ NonNullRef() = delete;
+ NonNullRef(nullPtr) = delete;
+
+ NonNullRef(T* ref)
+ : fRef(ref, true)
+ {
+ }
+
+ Ref<T>& operator->()
+ {
+ MUST_PASS(fRef);
+ return fRef;
+ }
+
+ NonNullRef& operator=(const NonNullRef<T>& ref) = delete;
+ NonNullRef(const NonNullRef<T>& ref) = default;
+
+ private:
+ Ref<T> fRef{nullptr};
+ };
+} // namespace NewOS