/* ======================================== Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. ======================================== */ #ifndef _NEKIT_REF_H_ #define _NEKIT_REF_H_ #include #include #include #include #include #include namespace Kernel { /// =========================================================== /// /// @brief Reference wrapper class. /// /// =========================================================== /// template class Ref final { public: explicit Ref() = default; ~Ref() = default; public: using Type = T; Ref(Type* cls) : fClass(*cls) {} Ref(Type cls) : fClass(cls) {} Ref& operator=(nullPtr) { return *this; } Ref& operator=(Type* ref) { fClass = *ref; return *this; } Ref& operator=(Type ref) { fClass = ref; return *this; } NE_COPY_DEFAULT(Ref) public: Type operator->() const { return fClass; } Type& Leak() { return fClass; } Type& TryLeak() { return fClass; } Type operator*() { return fClass; } explicit operator bool() { return Vettable::kValue; } bool operator!() { return !Vettable::kValue; } private: Type fClass; }; template class NonNullRef final { public: using RefType = Ref; using Type = T; NonNullRef() = delete; NonNullRef(Type* ref) : fRef(ref) {} NonNullRef(nullPtr ref) = delete; NonNullRef(RefType ref) : fRef(ref) {} Ref& operator->() { MUST_PASS(fRef); return fRef; } NonNullRef& operator=(const NonNullRef& ref) = delete; NonNullRef(const NonNullRef& ref) = delete; private: Ref fRef{}; }; using RefAny = Ref; using NonNullRefAny = NonNullRef; } // namespace Kernel #endif // ifndef _NEKIT_REF_H_