summaryrefslogtreecommitdiffhomepage
path: root/dev/kernel/NewKit/Ref.h
diff options
context:
space:
mode:
Diffstat (limited to 'dev/kernel/NewKit/Ref.h')
-rw-r--r--dev/kernel/NewKit/Ref.h164
1 files changed, 67 insertions, 97 deletions
diff --git a/dev/kernel/NewKit/Ref.h b/dev/kernel/NewKit/Ref.h
index 7ec0825a..6737ce09 100644
--- a/dev/kernel/NewKit/Ref.h
+++ b/dev/kernel/NewKit/Ref.h
@@ -1,109 +1,79 @@
/* -------------------------------------------
- Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved.
+ Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved.
------------------------------------------- */
#ifndef _NEWKIT_REF_H_
#define _NEWKIT_REF_H_
+#include <KernelKit/MemoryMgr.h>
#include <NewKit/Defines.h>
#include <NewKit/KernelPanic.h>
-#include <KernelKit/MemoryMgr.h>
-namespace Kernel
-{
- template <typename T>
- class Ref final
- {
- public:
- Ref() = default;
-
- ~Ref()
- {
- if (mm_is_valid_heap(fClass))
- delete fClass;
- }
-
- public:
- Ref(T* cls)
- : fClass(cls)
- {
- }
-
- Ref(T cls)
- : fClass(nullptr)
- {
- fClass = new T(cls);
- }
-
- Ref& operator=(T ref)
- {
- if (!fClass)
- return *this;
-
- fClass = &ref;
- return *this;
- }
-
- public:
- T operator->() const
- {
- MUST_PASS(*fClass);
- return *fClass;
- }
-
- T& Leak() noexcept
- {
- return *fClass;
- }
-
- T& TryLeak() const noexcept
- {
- MUST_PASS(*fClass);
- return *fClass;
- }
-
- T operator*()
- {
- return *fClass;
- }
-
- operator bool() noexcept
- {
- return fClass;
- }
-
- private:
- T* fClass{nullptr};
- };
-
- template <typename T>
- class NonNullRef final
- {
- public:
- NonNullRef() = delete;
- NonNullRef(nullPtr) = delete;
-
- NonNullRef(T* ref)
- : fRef(ref)
- {
- MUST_PASS(ref);
- }
-
- 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_H_
+namespace Kernel {
+template <typename T>
+class Ref final {
+ public:
+ Ref() = default;
+
+ ~Ref() {
+ if (mm_is_valid_heap(fClass)) delete fClass;
+ }
+
+ public:
+ Ref(T* cls) : fClass(cls) {}
+
+ Ref(T cls) : fClass(nullptr) { fClass = new T(cls); }
+
+ Ref& operator=(T ref) {
+ if (!fClass) return *this;
+
+ fClass = &ref;
+ return *this;
+ }
+
+ public:
+ T operator->() const {
+ MUST_PASS(*fClass);
+ return *fClass;
+ }
+
+ T& Leak() noexcept { return *fClass; }
+
+ T& TryLeak() const noexcept {
+ MUST_PASS(*fClass);
+ return *fClass;
+ }
+
+ T operator*() { return *fClass; }
+
+ operator bool() noexcept { return fClass; }
+
+ private:
+ T* fClass{nullptr};
+};
+
+template <typename T>
+class NonNullRef final {
+ public:
+ NonNullRef() = delete;
+ NonNullRef(nullPtr) = delete;
+
+ NonNullRef(T* ref) : fRef(ref) { MUST_PASS(ref); }
+
+ 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_H_