From ea937555b04ae7f39785341e45955b48515e5bf1 Mon Sep 17 00:00:00 2001 From: Amlal Date: Sun, 21 Jul 2024 09:48:52 +0200 Subject: [IMP] MHR-36: Much better ref class Signed-off-by: Amlal --- Kernel/NewKit/ErrorOr.hpp | 2 +- Kernel/NewKit/NewKit.hpp | 2 +- Kernel/NewKit/OwnPtr.hpp | 2 +- Kernel/NewKit/PageManager.hpp | 2 +- Kernel/NewKit/Pmm.hpp | 2 +- Kernel/NewKit/Ref.hpp | 98 -------------------------------------- Kernel/NewKit/Ref.hxx | 106 ++++++++++++++++++++++++++++++++++++++++++ Kernel/NewKit/Stream.hpp | 2 +- 8 files changed, 112 insertions(+), 104 deletions(-) delete mode 100644 Kernel/NewKit/Ref.hpp create mode 100644 Kernel/NewKit/Ref.hxx (limited to 'Kernel/NewKit') diff --git a/Kernel/NewKit/ErrorOr.hpp b/Kernel/NewKit/ErrorOr.hpp index a528de57..7e261abf 100644 --- a/Kernel/NewKit/ErrorOr.hpp +++ b/Kernel/NewKit/ErrorOr.hpp @@ -10,7 +10,7 @@ #pragma once #include -#include +#include namespace Kernel { diff --git a/Kernel/NewKit/NewKit.hpp b/Kernel/NewKit/NewKit.hpp index 761a0411..144a0ea9 100644 --- a/Kernel/NewKit/NewKit.hpp +++ b/Kernel/NewKit/NewKit.hpp @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/Kernel/NewKit/OwnPtr.hpp b/Kernel/NewKit/OwnPtr.hpp index 7aa2a247..6157a1b6 100644 --- a/Kernel/NewKit/OwnPtr.hpp +++ b/Kernel/NewKit/OwnPtr.hpp @@ -9,7 +9,7 @@ #include #include -#include +#include namespace Kernel { diff --git a/Kernel/NewKit/PageManager.hpp b/Kernel/NewKit/PageManager.hpp index 89c449f7..57b842ba 100644 --- a/Kernel/NewKit/PageManager.hpp +++ b/Kernel/NewKit/PageManager.hpp @@ -11,7 +11,7 @@ #include #include -#include +#include #ifndef kBadAddress #define kBadAddress (0) diff --git a/Kernel/NewKit/Pmm.hpp b/Kernel/NewKit/Pmm.hpp index 8c918de4..46db1879 100644 --- a/Kernel/NewKit/Pmm.hpp +++ b/Kernel/NewKit/Pmm.hpp @@ -8,7 +8,7 @@ #pragma once #include -#include +#include namespace Kernel { diff --git a/Kernel/NewKit/Ref.hpp b/Kernel/NewKit/Ref.hpp deleted file mode 100644 index 0b0f89cc..00000000 --- a/Kernel/NewKit/Ref.hpp +++ /dev/null @@ -1,98 +0,0 @@ - -/* ------------------------------------------- - - Copyright ZKA Technologies - -------------------------------------------- */ - -#ifndef _NEWKIT_REF_HPP_ -#define _NEWKIT_REF_HPP_ - -#include -#include - -namespace Kernel -{ - template - class Ref final - { - public: - Ref() = default; - ~Ref() = default; - - 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& Fetch() const noexcept - { - 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_ diff --git a/Kernel/NewKit/Ref.hxx b/Kernel/NewKit/Ref.hxx new file mode 100644 index 00000000..69150054 --- /dev/null +++ b/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_ diff --git a/Kernel/NewKit/Stream.hpp b/Kernel/NewKit/Stream.hpp index b615dc77..eefb0a1c 100644 --- a/Kernel/NewKit/Stream.hpp +++ b/Kernel/NewKit/Stream.hpp @@ -8,7 +8,7 @@ #pragma once #include -#include +#include namespace Kernel { -- cgit v1.2.3