From 44629ba5dd6a891ec6e43bba823c27775e920ed0 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 10 Oct 2024 21:54:52 +0300 Subject: [PATCH] BAN: Allow userspace to use string literals with BAN::Error --- BAN/include/BAN/Errors.h | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/BAN/include/BAN/Errors.h b/BAN/include/BAN/Errors.h index d377d259..e0be8fa2 100644 --- a/BAN/include/BAN/Errors.h +++ b/BAN/include/BAN/Errors.h @@ -36,7 +36,14 @@ namespace BAN { return Error((uint64_t)error | kernel_error_mask); } +#else + template + consteval static Error from_literal(const char (&message)[N]) + { + return Error(message); + } #endif + static Error from_errno(int error) { return Error(error); @@ -54,12 +61,15 @@ namespace BAN } #endif - uint64_t get_error_code() const { return m_error_code; } + constexpr uint64_t get_error_code() const { return m_error_code; } const char* get_message() const { #ifdef __is_kernel if (m_error_code & kernel_error_mask) return Kernel::error_string(kernel_error()); +#else + if (m_message) + return m_message; #endif if (auto* desc = strerrordesc_np(m_error_code)) return desc; @@ -67,11 +77,21 @@ namespace BAN } private: - Error(uint64_t error) + constexpr Error(uint64_t error) : m_error_code(error) {} - uint64_t m_error_code; +#ifndef __is_kernel + constexpr Error(const char* message) + : m_message(message) + {} +#endif + + uint64_t m_error_code { 0 }; + +#ifndef __is_kernel + const char* m_message { nullptr }; +#endif }; template