From 11894bef04095c37196af5ae1bfed885775d49eb Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Sun, 5 Jan 2025 16:23:04 +0100 Subject: Add formatting & factories for status --- asl/status.hpp | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'asl/status.hpp') diff --git a/asl/status.hpp b/asl/status.hpp index 4d54b61..0d1175e 100644 --- a/asl/status.hpp +++ b/asl/status.hpp @@ -2,11 +2,12 @@ #include "asl/integers.hpp" #include "asl/string_view.hpp" +#include "asl/format.hpp" namespace asl { -// @Todo Make status with formatting +class Formatter; enum class status_code : uint8_t { @@ -55,8 +56,6 @@ class status void unref(); public: - constexpr status() = default; - constexpr ~status() { if (!is_inline()) { unref(); } @@ -67,6 +66,7 @@ public: {} status(status_code code, string_view msg); + status(status_code code, string_view fmt, span args); constexpr status(const status& other) : m_payload{other.m_payload} @@ -100,7 +100,7 @@ public: constexpr bool ok() const { - return m_payload == nullptr || code() == status_code::ok; + return m_payload == nullptr; } // NOLINTNEXTLINE(*-explicit-conversions) @@ -119,6 +119,27 @@ public: } return {}; } + + friend void AslFormat(Formatter& f, const status&); }; +static constexpr status ok() { return status{status_code::ok}; } + +#define ASL_DEFINE_ERROR_(type) \ + static constexpr status type##_error() { return status{status_code::type}; } \ + static inline status type##_error(string_view sv) { return status{status_code::type, sv}; } \ + template \ + [[maybe_unused]] static status type##_error(string_view fmt, const Args&... args) \ + { \ + format_internals::type_erased_arg type_erased_args[] = { \ + format_internals::type_erased_arg(args)... \ + }; \ + return status{status_code::type, fmt, type_erased_args}; \ + } + +ASL_DEFINE_ERROR_(unknown) +ASL_DEFINE_ERROR_(internal) +ASL_DEFINE_ERROR_(runtime) +ASL_DEFINE_ERROR_(invalid_argument) + } // namespace asl -- cgit