diff options
| author | Amlal <amlal@nekernel.org> | 2025-04-25 13:08:33 +0200 |
|---|---|---|
| committer | Amlal <amlal@nekernel.org> | 2025-04-25 13:08:33 +0200 |
| commit | fb790b07aeba8e22e4190cf3e1834d11ecde6c96 (patch) | |
| tree | 4cec7d1b321307b1d5935577631dae116a658a37 /dev/kernel/NewKit/Ref.h | |
| parent | 63a2d92c5dfe976175cda024ec01905d11b43738 (diff) | |
dev: better .clang-format, ran format command.
Signed-off-by: Amlal <amlal@nekernel.org>
Diffstat (limited to 'dev/kernel/NewKit/Ref.h')
| -rw-r--r-- | dev/kernel/NewKit/Ref.h | 164 |
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_ |
