BAN: Errors.h can be included from userspace

This commit is contained in:
Bananymous 2023-05-22 19:54:59 +03:00
parent 0ab3332ad3
commit bda4614783
1 changed files with 11 additions and 2 deletions

View File

@ -1,12 +1,13 @@
#pragma once #pragma once
#include <BAN/Formatter.h> #include <BAN/Formatter.h>
#include <BAN/StringView.h>
#include <BAN/Variant.h> #include <BAN/Variant.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#if defined(__is_kernel) #ifdef __is_kernel
#include <kernel/Panic.h> #include <kernel/Panic.h>
#include <kernel/Errors.h> #include <kernel/Errors.h>
#define MUST(expr) ({ auto&& e = expr; if (e.is_error()) Kernel::panic("{}", e.error()); e.release_value(); }) #define MUST(expr) ({ auto&& e = expr; if (e.is_error()) Kernel::panic("{}", e.error()); e.release_value(); })
@ -24,29 +25,37 @@ namespace BAN
class Error class Error
{ {
#ifdef __is_kernel
private: private:
static constexpr uint64_t kernel_error_mask = uint64_t(1) << 63; static constexpr uint64_t kernel_error_mask = uint64_t(1) << 63;
#endif
public: public:
#ifdef __is_kernel
static Error from_error_code(Kernel::ErrorCode error) static Error from_error_code(Kernel::ErrorCode error)
{ {
return Error((uint64_t)error | kernel_error_mask); return Error((uint64_t)error | kernel_error_mask);
} }
#endif
static Error from_errno(int error) static Error from_errno(int error)
{ {
return Error(error); return Error(error);
} }
#ifdef __is_kernel
Kernel::ErrorCode kernel_error() const Kernel::ErrorCode kernel_error() const
{ {
return (Kernel::ErrorCode)(m_error_code & ~kernel_error_mask); return (Kernel::ErrorCode)(m_error_code & ~kernel_error_mask);
} }
#endif
uint64_t get_error_code() const { return m_error_code; } uint64_t get_error_code() const { return m_error_code; }
BAN::StringView get_message() const BAN::StringView get_message() const
{ {
#ifdef __is_kernel
if (m_error_code & kernel_error_mask) if (m_error_code & kernel_error_mask)
return Kernel::error_string(kernel_error()); return Kernel::error_string(kernel_error());
#endif
return strerror(m_error_code); return strerror(m_error_code);
} }
@ -132,7 +141,7 @@ namespace BAN
void release_value() { } void release_value() { }
private: private:
Error m_data { Error::from_error_code(Kernel::ErrorCode::None) }; Error m_data { Error::from_errno(0) };
bool m_has_error { false }; bool m_has_error { false };
}; };