diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-07-30 08:50:15 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-07-30 08:50:15 +0100 |
| commit | 1c8c5cff67b20d86c442b0917d6c1fc6407140df (patch) | |
| tree | 53ebea660bef14cdc2ff5b7ebefb4049f705f997 /dev/CompilerKit/Ref.h | |
| parent | 073811d89c98d6e1c078a032ca2eedefebf80384 (diff) | |
feat! Breaking API changes of NeCTI's LibCompiler and LibDebugger.
what:
- They've now become CompilerKit and DebuggerKit.
- Expanding XCoff for NeBoot PowerPC backend.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'dev/CompilerKit/Ref.h')
| -rw-r--r-- | dev/CompilerKit/Ref.h | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/dev/CompilerKit/Ref.h b/dev/CompilerKit/Ref.h new file mode 100644 index 0000000..a3640ac --- /dev/null +++ b/dev/CompilerKit/Ref.h @@ -0,0 +1,76 @@ + +/* + * ======================================================== + * + * CompilerKit + * Copyright (C) 2024-2025 Amlal El Mahrouss, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <CompilerKit/Defines.h> + +namespace CompilerKit { +// @author EL Mahrouss Amlal +// @brief Reference holder class, refers to a pointer of data in static memory. +template <typename T> +class Ref final { + public: + explicit Ref() = default; + + ~Ref() { + if (m_Strong) { + if (m_Class) delete m_Class; + m_Class = nullptr; + } + } + + LIBCOMPILER_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; } + + operator bool() { return *m_Class; } + + private: + T* m_Class{nullptr}; + Bool m_Strong{false}; +}; + +// @author EL Mahrouss Amlal +// @brief Non null Reference holder class, refers to a pointer of data in static memory. +template <typename T> +class NonNullRef final { + public: + explicit NonNullRef() = delete; + + explicit NonNullRef(T* ref) : m_Ref(ref, true) {} + + Ref<T>& operator->() { + MUST_PASS(m_Ref); + return m_Ref; + } + + NonNullRef& operator=(const NonNullRef<T>& ref) = delete; + NonNullRef(const NonNullRef<T>& ref) = default; + + private: + Ref<T> m_Ref{nullptr}; +}; +} // namespace CompilerKit |
