summaryrefslogtreecommitdiffhomepage
path: root/dev/Kernel/NewKit/Ref.hxx
diff options
context:
space:
mode:
authorAmlal EL Mahrouss <amlalelmahrouss@icloud.com>2024-08-15 18:35:34 +0200
committerAmlal EL Mahrouss <amlalelmahrouss@icloud.com>2024-08-15 18:35:34 +0200
commitf3d931aa7cfaf96baef8383b59a8938779541ee7 (patch)
treefdb9fc51badb3dbd03e46ab0766a49d9522e13e2 /dev/Kernel/NewKit/Ref.hxx
parent86640816e8b1d3595365f1fcc8a2a9e61fb40ff1 (diff)
[IMP] Moved source code into dev/ folder.
Signed-off-by: Amlal EL Mahrouss <amlalelmahrouss@icloud.com>
Diffstat (limited to 'dev/Kernel/NewKit/Ref.hxx')
-rw-r--r--dev/Kernel/NewKit/Ref.hxx106
1 files changed, 106 insertions, 0 deletions
diff --git a/dev/Kernel/NewKit/Ref.hxx b/dev/Kernel/NewKit/Ref.hxx
new file mode 100644
index 00000000..14d66f0a
--- /dev/null
+++ b/dev/Kernel/NewKit/Ref.hxx
@@ -0,0 +1,106 @@
+
+/* -------------------------------------------
+
+ Copyright ZKA Technologies.
+
+------------------------------------------- */
+
+#ifndef _NEWKIT_REF_HPP_
+#define _NEWKIT_REF_HPP_
+
+#include <NewKit/Defines.hxx>
+#include <NewKit/KernelCheck.hxx>
+
+namespace Kernel
+{
+ template <typename T>
+ class Ref final
+ {
+ public:
+ Ref() = default;
+
+ ~Ref()
+ {
+ if (fStrong)
+ {
+ fClass = nullptr;
+ }
+ }
+
+ 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() noexcept
+ {
+ return *fClass;
+ }
+
+ T& TryLeak() const noexcept
+ {
+ MUST_PASS(*fClass);
+ return *fClass;
+ }
+
+ T operator*()
+ {
+ return *fClass;
+ }
+
+ bool IsStrong() const
+ {
+ return fStrong;
+ }
+
+ operator bool() noexcept
+ {
+ 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)
+ {
+ MUST_PASS(ref != nullptr);
+ }
+
+ 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 Kernel
+
+#endif // ifndef _NEWKIT_REF_HPP_