/* * ======================================================== * * NeKernel * Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. * * ======================================================== */ #pragma once #include #include namespace Kernel { /// ================================================================================ /// @brief ErrorOr class for error handling. /// ================================================================================ template class ErrorOr final { public: explicit ErrorOr() = default; ~ErrorOr() = default; public: using RefType = Ref; using Type = T; explicit ErrorOr(ErrorT err) : mRef((T*) RTL_ALLOCA(sizeof(T))), mId(err) {} explicit ErrorOr(nullPtr) {} explicit ErrorOr(Type* klass) : mRef(klass) {} explicit ErrorOr(Type klass) : mRef(klass) {} ErrorOr& operator=(const ErrorOr&) = default; ErrorOr(const ErrorOr&) = default; ErrorOr& operator=(const RefType& ref) { mRef = ref; return *this; } const Type& Value() { return mRef.TryLeak(); } RefType& Leak() { return mRef; } ErrorT Error() { return mId; } /// @note DO NOT MAKE THIS EXPLICIT! IT WILL BREAK THE COMPILATION. explicit operator bool() { return mRef.Leak(); } BOOL HasError() { return this->mId < kErrorSuccess; } private: RefType mRef; ErrorT mId{0}; }; using ErrorOrAny = ErrorOr; } // namespace Kernel