From fc67c4af554189c941c811486a0b2b21aa3f54ea Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Fri, 23 May 2025 04:07:12 +0200 Subject: feat!(kernel): Rename NewKit to NeKit. Signed-off-by: Amlal El Mahrouss --- dev/kernel/NeKit/Ref.h | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 dev/kernel/NeKit/Ref.h (limited to 'dev/kernel/NeKit/Ref.h') 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 +#include +#include + +namespace Kernel { +template +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 +class NonNullRef final { + public: + NonNullRef() = delete; + NonNullRef(nullPtr) = delete; + + NonNullRef(T* ref) : fRef(ref) { MUST_PASS(ref); } + + Ref& operator->() { + MUST_PASS(fRef); + return fRef; + } + + NonNullRef& operator=(const NonNullRef& ref) = delete; + NonNullRef(const NonNullRef& ref) = default; + + private: + Ref fRef{nullptr}; +}; +} // namespace Kernel + +#endif // ifndef _NEWKIT_REF_H_ -- cgit v1.2.3