From bbe2c77243c541ca7e0075149f5be3262eb89523 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Mon, 24 Nov 2025 03:05:29 +0100 Subject: feat! breaking changes on necti sources. Signed-off-by: Amlal El Mahrouss --- src/CompilerKit/Ref.h | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/CompilerKit/Ref.h (limited to 'src/CompilerKit/Ref.h') diff --git a/src/CompilerKit/Ref.h b/src/CompilerKit/Ref.h new file mode 100644 index 0000000..66ed60f --- /dev/null +++ b/src/CompilerKit/Ref.h @@ -0,0 +1,77 @@ + +/* + * ======================================================== + * + * 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 Ref final { + public: + explicit Ref() = default; + + ~Ref() { + if (m_Strong) { + MUST_PASS(m_Class); + if (m_Class) delete m_Class; + m_Class = nullptr; + } + } + + NECTI_COPY_DEFAULT(Ref); + + public: + explicit Ref(T* cls, const Bool& strong = false) : m_Class(cls), m_Strong(strong) {} + + Ref& operator=(T ref) { + *m_Class = ref; + return *this; + } + + public: + T* operator->() const { return m_Class; } + + T& Leak() { return *m_Class; } + + T 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}; +}; + +// @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) {} + + Ref& operator->() { + MUST_PASS(m_Ref); + return m_Ref; + } + + NonNullRef& operator=(const NonNullRef& ref) = delete; + NonNullRef(const NonNullRef& ref) = default; + + private: + Ref m_Ref{nullptr}; +}; +} // namespace CompilerKit -- cgit v1.2.3