From f3d931aa7cfaf96baef8383b59a8938779541ee7 Mon Sep 17 00:00:00 2001 From: Amlal EL Mahrouss Date: Thu, 15 Aug 2024 18:35:34 +0200 Subject: [IMP] Moved source code into dev/ folder. Signed-off-by: Amlal EL Mahrouss --- dev/Kernel/NewKit/Ref.hxx | 106 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 dev/Kernel/NewKit/Ref.hxx (limited to 'dev/Kernel/NewKit/Ref.hxx') 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 +#include + +namespace Kernel +{ + template + 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 + class NonNullRef final + { + public: + NonNullRef() = delete; + NonNullRef(nullPtr) = delete; + + NonNullRef(T* ref) + : fRef(ref, true) + { + MUST_PASS(ref != nullptr); + } + + 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_HPP_ -- cgit v1.2.3