diff options
Diffstat (limited to 'dev/kernel/NeKit/Ref.h')
| -rw-r--r-- | dev/kernel/NeKit/Ref.h | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/dev/kernel/NeKit/Ref.h b/dev/kernel/NeKit/Ref.h new file mode 100644 index 00000000..489c51de --- /dev/null +++ b/dev/kernel/NeKit/Ref.h @@ -0,0 +1,79 @@ + +/* ------------------------------------------- + + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + +------------------------------------------- */ + +#ifndef _NEWKIT_REF_H_ +#define _NEWKIT_REF_H_ + +#include <KernelKit/MemoryMgr.h> +#include <NeKit/Defines.h> +#include <NeKit/KernelPanic.h> + +namespace Kernel { +template <typename T> +class Ref final { + public: + Ref() = default; + + ~Ref() { + if (mm_is_valid_ptr(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_ |
