diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-12-11 09:10:04 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-12-11 09:10:04 +0100 |
| commit | 12107439786af03c0e0580250df4c4ec58d884eb (patch) | |
| tree | c4ba59fb92303f80b11304261484a93de10925f4 /include | |
| parent | 5ae6edf21e036144fe0453170c5642735f56177c (diff) | |
chore: bump boost version.
feat! StrongRef and WeakRef replaces Ref.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'include')
| -rw-r--r-- | include/CompilerKit/ErrorOr.h | 4 | ||||
| -rw-r--r-- | include/CompilerKit/Ref.h | 59 |
2 files changed, 43 insertions, 20 deletions
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 <CompilerKit/Detail/Config.h> #include <CompilerKit/ErrorID.h> -#include <CompilerKit/Ref.h> +#include <CompilerKit/StrongRef.h> namespace CompilerKit { using ErrorT = Int32; @@ -29,7 +29,7 @@ class ErrorOr final { ~ErrorOr() = default; public: - using RefType = Ref<T>; + using RefType = StrongRef<T>; 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 <typename T> -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 <typename T> +class WeakRef final : StrongRef<T> { + public: + WeakRef() = default; + + ~WeakRef() = default; + + NECTI_COPY_DELETE(WeakRef) + + public: + using Type = T; + + explicit WeakRef(Type* cls) : StrongRef<Type>(cls, false) {} +}; + +/// @author Amlal El Mahrouss +/// @brief Non null reference holder class, refers to a pointer of data in static memory. +template <typename Type> class NonNullRef final { public: explicit NonNullRef() = delete; - explicit NonNullRef(T* ref) : m_Ref(ref, true) {} + explicit NonNullRef(Type* ref) : m_Ref(ref, true) {} - Ref<T>& operator->() { + StrongRef<Type>& operator->() { MUST_PASS(m_Ref); return m_Ref; } - NonNullRef& operator=(const NonNullRef<T>& ref) = delete; - NonNullRef(const NonNullRef<T>& ref) = default; + NonNullRef& operator=(const NonNullRef<Type>& ref) = delete; + NonNullRef(const NonNullRef<Type>& ref) = default; private: - Ref<T> m_Ref{nullptr}; + StrongRef<Type> m_Ref{nullptr}; }; + +using StrongAny = StrongRef<VoidPtr>; +using WeakAny = WeakRef<VoidPtr>; } // namespace CompilerKit |
