diff options
Diffstat (limited to 'include/CompilerKit/ErrorOr.h')
| -rw-r--r-- | include/CompilerKit/ErrorOr.h | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/include/CompilerKit/ErrorOr.h b/include/CompilerKit/ErrorOr.h new file mode 100644 index 0000000..100624e --- /dev/null +++ b/include/CompilerKit/ErrorOr.h @@ -0,0 +1,54 @@ +/* + * ======================================================== + * + * CompilerKit + * Copyright (C) 2024-2025 Amlal El Mahrouss, Licensed under the Apache 2.0 license. + * + * ======================================================== + */ + +#pragma once + +/// =========================================================== /// +/// @file ErrorOr.h +/// @author Amlal El Mahrouss +/// @brief ErrorOr for CompilerKit. +/// =========================================================== /// + +#include <CompilerKit/Detail/Config.h> +#include <CompilerKit/ErrorID.h> +#include <CompilerKit/Ref.h> + +namespace CompilerKit { +using ErrorT = Int32; + +template <typename T> +class ErrorOr final { + public: + ErrorOr() = default; + ~ErrorOr() = default; + + public: + explicit ErrorOr(ErrorT err) : mId(err) {} + explicit ErrorOr(std::nullptr_t null) {} + explicit ErrorOr(T klass) : mRef(klass) {} + + ErrorOr& operator=(const ErrorOr&) = default; + ErrorOr(const ErrorOr&) = default; + + Ref<T>& Leak() { return mRef; } + + ErrorT Error() { return mId; } + + bool HasError() { return mId != NECTI_SUCCESS; } + + explicit operator bool() { return mRef; } + + private: + Ref<T> mRef; + ErrorT mId{0}; +}; + +using ErrorOrAny = ErrorOr<VoidPtr>; +using ErrorOrString = ErrorOr<STLString>; +} // namespace CompilerKit |
