/* * ======================================================== * * CompilerKit * Copyright (C) 2024-2025 Amlal El Mahrouss, licensed under the Apache 2.0 license. * * ======================================================== */ #pragma once #include namespace CompilerKit { /// @author Amlal El Mahrouss /// @brief Reference holder class, refers to a pointer of data in static memory. template class StrongRef { public: StrongRef() = default; virtual ~StrongRef() { if (m_Strong) { MUST_PASS(m_Class); if (m_Class) delete m_Class; m_Class = nullptr; } } NECTI_COPY_DEFAULT(StrongRef) using Type = T; protected: StrongRef(Type* cls, const bool strong) : m_Class(cls), m_Strong(strong) {} public: StrongRef(Type* cls) : m_Class(cls), m_Strong(true) {} StrongRef& operator=(Type *ref) { m_Class = ref; return *this; } public: Type* operator->() const { return m_Class; } Type* Leak() { return m_Class; } Type* operator*() { return m_Class; } bool IsStrong() const { return m_Strong; } explicit operator bool() { return m_Class != nullptr; } private: Type* m_Class{nullptr}; bool m_Strong{false}; }; template class WeakRef final : public StrongRef { public: WeakRef() = delete; ~WeakRef() = default; NECTI_COPY_DEFAULT(WeakRef) public: using Type = T; 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; NonNullRef(Type* ref) : m_Ref(ref, true) {} StrongRef& operator->() { MUST_PASS(m_Ref); return m_Ref; } NonNullRef& operator=(const NonNullRef& ref) = delete; NonNullRef(const NonNullRef& ref) = default; private: StrongRef m_Ref{nullptr}; }; using StrongAny = StrongRef; using WeakAny = WeakRef; } // namespace CompilerKit