From 12107439786af03c0e0580250df4c4ec58d884eb Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 11 Dec 2025 09:10:04 +0100 Subject: chore: bump boost version. feat! StrongRef and WeakRef replaces Ref. Signed-off-by: Amlal El Mahrouss --- include/CompilerKit/ErrorOr.h | 4 +-- include/CompilerKit/Ref.h | 59 ++++++++++++++++++++++++++++++------------- 2 files changed, 43 insertions(+), 20 deletions(-) (limited to 'include/CompilerKit') diff --git a/include/CompilerKit/ErrorOr.h b/include/CompilerKit/ErrorOr.h index db8339a..7cc418a 100644 --- a/include/CompilerKit/ErrorOr.h +++ b/include/CompilerKit/ErrorOr.h @@ -17,7 +17,7 @@ #include #include -#include +#include namespace CompilerKit { using ErrorT = Int32; @@ -29,7 +29,7 @@ class ErrorOr final { ~ErrorOr() = default; public: - using RefType = Ref; + using RefType = StrongRef; explicit ErrorOr(ErrorT err) : mId(err) {} explicit ErrorOr(std::nullptr_t null) {} diff --git a/include/CompilerKit/Ref.h b/include/CompilerKit/Ref.h index 859b3fe..618a90a 100644 --- a/include/CompilerKit/Ref.h +++ b/include/CompilerKit/Ref.h @@ -16,11 +16,11 @@ namespace CompilerKit { /// @author Amlal El Mahrouss /// @brief Reference holder class, refers to a pointer of data in static memory. template -class Ref final { +class StrongRef { public: - Ref() = default; + StrongRef() = default; - ~Ref() { + virtual ~StrongRef() { if (m_Strong) { MUST_PASS(m_Class); if (m_Class) delete m_Class; @@ -28,49 +28,72 @@ class Ref final { } } - NECTI_COPY_DEFAULT(Ref); + NECTI_COPY_DEFAULT(StrongRef) + + using Type = T; + + protected: + explicit StrongRef(Type* cls, const bool strong) : m_Class(cls), m_Strong(strong) {} public: - explicit Ref(T* cls, const bool& strong = false) : m_Class(cls), m_Strong(strong) {} + explicit StrongRef(Type* cls) : m_Class(cls), m_Strong(true) {} - Ref& operator=(T ref) { + StrongRef& operator=(Type ref) { *m_Class = ref; return *this; } public: - T* operator->() const { return m_Class; } + Type* operator->() const { return m_Class; } - T& Leak() { return *m_Class; } + Type& Leak() { return *m_Class; } - T operator*() { return *m_Class; } + Type operator*() { return *m_Class; } bool IsStrong() const { return m_Strong; } explicit operator bool() { return *m_Class; } private: - T* m_Class{nullptr}; - bool m_Strong{false}; + Type* m_Class{nullptr}; + bool m_Strong{false}; }; -// @author Amlal El Mahrouss -// @brief Non null Reference holder class, refers to a pointer of data in static memory. template +class WeakRef final : StrongRef { + public: + WeakRef() = default; + + ~WeakRef() = default; + + NECTI_COPY_DELETE(WeakRef) + + public: + using Type = T; + + explicit WeakRef(Type* cls) : StrongRef(cls, false) {} +}; + +/// @author Amlal El Mahrouss +/// @brief Non null reference holder class, refers to a pointer of data in static memory. +template class NonNullRef final { public: explicit NonNullRef() = delete; - explicit NonNullRef(T* ref) : m_Ref(ref, true) {} + explicit NonNullRef(Type* ref) : m_Ref(ref, true) {} - Ref& operator->() { + StrongRef& operator->() { MUST_PASS(m_Ref); return m_Ref; } - NonNullRef& operator=(const NonNullRef& ref) = delete; - NonNullRef(const NonNullRef& ref) = default; + NonNullRef& operator=(const NonNullRef& ref) = delete; + NonNullRef(const NonNullRef& ref) = default; private: - Ref m_Ref{nullptr}; + StrongRef m_Ref{nullptr}; }; + +using StrongAny = StrongRef; +using WeakAny = WeakRef; } // namespace CompilerKit -- cgit v1.2.3