AK: Define some simple getters in a single line
This matches our coding style everywhere else. clang-format used to have trouble with these, but it doesn't any longer.
This commit is contained in:
parent
dceed08058
commit
95441dabd5
3 changed files with 41 additions and 63 deletions
|
|
@ -7,6 +7,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <AK/Error.h>
|
#include <AK/Error.h>
|
||||||
|
|
||||||
#ifdef AK_OS_WINDOWS
|
#ifdef AK_OS_WINDOWS
|
||||||
# include <AK/ByteString.h>
|
# include <AK/ByteString.h>
|
||||||
# include <AK/HashMap.h>
|
# include <AK/HashMap.h>
|
||||||
|
|
|
||||||
89
AK/Error.h
89
AK/Error.h
|
|
@ -15,9 +15,6 @@ namespace AK {
|
||||||
|
|
||||||
class [[nodiscard]] Error {
|
class [[nodiscard]] Error {
|
||||||
public:
|
public:
|
||||||
ALWAYS_INLINE Error(Error&&) = default;
|
|
||||||
ALWAYS_INLINE Error& operator=(Error&&) = default;
|
|
||||||
|
|
||||||
enum class Kind : u8 {
|
enum class Kind : u8 {
|
||||||
Errno,
|
Errno,
|
||||||
Syscall,
|
Syscall,
|
||||||
|
|
@ -31,42 +28,50 @@ public:
|
||||||
return Error(code);
|
return Error(code);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef AK_OS_WINDOWS
|
|
||||||
static Error from_windows_error(u32 windows_error);
|
|
||||||
static Error from_windows_error();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static Error from_syscall(StringView syscall_name, int code)
|
static Error from_syscall(StringView syscall_name, int code)
|
||||||
{
|
{
|
||||||
return Error(syscall_name, code);
|
return Error(syscall_name, code);
|
||||||
}
|
}
|
||||||
static Error from_string_view(StringView string_literal) { return Error(string_literal); }
|
|
||||||
|
// Prefer `from_string_literal` when directly typing out an error message:
|
||||||
|
//
|
||||||
|
// return Error::from_string_literal("Class: Some failure");
|
||||||
|
//
|
||||||
|
// If you need to return a static string based on a dynamic condition (like picking an error from an array), then
|
||||||
|
// prefer `from_string_view` instead.
|
||||||
|
template<size_t N>
|
||||||
|
ALWAYS_INLINE static Error from_string_literal(char const (&string_literal)[N])
|
||||||
|
{
|
||||||
|
return from_string_view(StringView { string_literal, N - 1 });
|
||||||
|
}
|
||||||
|
|
||||||
|
static Error from_string_view(StringView string_literal)
|
||||||
|
{
|
||||||
|
return Error(string_literal);
|
||||||
|
}
|
||||||
|
|
||||||
template<OneOf<ByteString, String, FlyString> T>
|
template<OneOf<ByteString, String, FlyString> T>
|
||||||
static Error from_string_view(T)
|
static Error from_string_view(T)
|
||||||
{
|
{
|
||||||
// `Error::from_string_view(ByteString::formatted(...))` is a somewhat common mistake, which leads to a UAF situation.
|
// `Error::from_string_view(ByteString::formatted(...))` is a somewhat common mistake, which leads to a UAF
|
||||||
// If your string outlives this error and _isn't_ a temporary being passed to this function, explicitly call .view() on it to resolve to the StringView overload.
|
// situation. If your string outlives this error and _isn't_ a temporary being passed to this function,
|
||||||
|
// explicitly call .view() on it to resolve to the StringView overload.
|
||||||
static_assert(DependentFalse<T>, "Error::from_string_view(String) is almost always a use-after-free");
|
static_assert(DependentFalse<T>, "Error::from_string_view(String) is almost always a use-after-free");
|
||||||
VERIFY_NOT_REACHED();
|
VERIFY_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef AK_OS_WINDOWS
|
||||||
|
static Error from_windows_error(u32 windows_error);
|
||||||
|
static Error from_windows_error();
|
||||||
|
#endif
|
||||||
|
|
||||||
static Error copy(Error const& error)
|
static Error copy(Error const& error)
|
||||||
{
|
{
|
||||||
return Error(error);
|
return Error(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: Prefer `from_string_literal` when directly typing out an error message:
|
ALWAYS_INLINE Error(Error&&) = default;
|
||||||
//
|
ALWAYS_INLINE Error& operator=(Error&&) = default;
|
||||||
// return Error::from_string_literal("Class: Some failure");
|
|
||||||
//
|
|
||||||
// If you need to return a static string based on a dynamic condition (like
|
|
||||||
// picking an error from an array), then prefer `from_string_view` instead.
|
|
||||||
template<size_t N>
|
|
||||||
ALWAYS_INLINE static Error from_string_literal(char const (&string_literal)[N])
|
|
||||||
{
|
|
||||||
return from_string_view(StringView { string_literal, N - 1 });
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator==(Error const& other) const
|
bool operator==(Error const& other) const
|
||||||
{
|
{
|
||||||
|
|
@ -74,30 +79,15 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
int code() const { return m_code; }
|
int code() const { return m_code; }
|
||||||
bool is_errno() const
|
bool is_errno() const { return m_kind == Kind::Errno || m_kind == Kind::Syscall; }
|
||||||
{
|
bool is_syscall() const { return m_kind == Kind::Syscall; }
|
||||||
return m_kind == Kind::Errno || m_kind == Kind::Syscall;
|
|
||||||
}
|
bool is_windows_error() const { return m_kind == Kind::Windows; }
|
||||||
bool is_syscall() const
|
|
||||||
{
|
bool is_string_literal() const { return m_kind == Kind::StringLiteral; }
|
||||||
return m_kind == Kind::Syscall;
|
StringView string_literal() const { return m_string_literal; }
|
||||||
}
|
|
||||||
bool is_windows_error() const
|
Kind kind() const { return m_kind; }
|
||||||
{
|
|
||||||
return m_kind == Kind::Windows;
|
|
||||||
}
|
|
||||||
bool is_string_literal() const
|
|
||||||
{
|
|
||||||
return m_kind == Kind::StringLiteral;
|
|
||||||
}
|
|
||||||
StringView string_literal() const
|
|
||||||
{
|
|
||||||
return m_string_literal;
|
|
||||||
}
|
|
||||||
Kind kind() const
|
|
||||||
{
|
|
||||||
return m_kind;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Error(int code, Kind kind = Kind::Errno)
|
Error(int code, Kind kind = Kind::Errno)
|
||||||
|
|
@ -124,9 +114,7 @@ private:
|
||||||
Error& operator=(Error const&) = default;
|
Error& operator=(Error const&) = default;
|
||||||
|
|
||||||
StringView m_string_literal;
|
StringView m_string_literal;
|
||||||
|
|
||||||
int m_code { 0 };
|
int m_code { 0 };
|
||||||
|
|
||||||
Kind m_kind {};
|
Kind m_kind {};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -166,10 +154,7 @@ public:
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
T& value()
|
T& value() { return m_value_or_error.template get<T>(); }
|
||||||
{
|
|
||||||
return m_value_or_error.template get<T>();
|
|
||||||
}
|
|
||||||
T const& value() const { return m_value_or_error.template get<T>(); }
|
T const& value() const { return m_value_or_error.template get<T>(); }
|
||||||
|
|
||||||
ErrorType& error() { return m_value_or_error.template get<ErrorType>(); }
|
ErrorType& error() { return m_value_or_error.template get<ErrorType>(); }
|
||||||
|
|
|
||||||
|
|
@ -57,10 +57,7 @@ public:
|
||||||
template<OneOf<String, FlyString, ByteString, ByteBuffer> StringType>
|
template<OneOf<String, FlyString, ByteString, ByteBuffer> StringType>
|
||||||
StringView& operator=(StringType&&) = delete;
|
StringView& operator=(StringType&&) = delete;
|
||||||
|
|
||||||
[[nodiscard]] constexpr bool is_null() const
|
[[nodiscard]] constexpr bool is_null() const { return m_characters == nullptr; }
|
||||||
{
|
|
||||||
return m_characters == nullptr;
|
|
||||||
}
|
|
||||||
[[nodiscard]] constexpr bool is_empty() const { return m_length == 0; }
|
[[nodiscard]] constexpr bool is_empty() const { return m_length == 0; }
|
||||||
|
|
||||||
[[nodiscard]] constexpr char const* characters_without_null_termination() const { return m_characters; }
|
[[nodiscard]] constexpr char const* characters_without_null_termination() const { return m_characters; }
|
||||||
|
|
@ -106,10 +103,7 @@ public:
|
||||||
[[nodiscard]] String to_ascii_uppercase_string() const;
|
[[nodiscard]] String to_ascii_uppercase_string() const;
|
||||||
[[nodiscard]] String to_ascii_titlecase_string() const;
|
[[nodiscard]] String to_ascii_titlecase_string() const;
|
||||||
|
|
||||||
[[nodiscard]] Optional<size_t> find(char needle, size_t start = 0) const
|
[[nodiscard]] Optional<size_t> find(char needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); }
|
||||||
{
|
|
||||||
return StringUtils::find(*this, needle, start);
|
|
||||||
}
|
|
||||||
[[nodiscard]] Optional<size_t> find(StringView needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); }
|
[[nodiscard]] Optional<size_t> find(StringView needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); }
|
||||||
[[nodiscard]] Optional<size_t> find_last(char needle) const { return StringUtils::find_last(*this, needle); }
|
[[nodiscard]] Optional<size_t> find_last(char needle) const { return StringUtils::find_last(*this, needle); }
|
||||||
[[nodiscard]] Optional<size_t> find_last(StringView needle) const { return StringUtils::find_last(*this, needle); }
|
[[nodiscard]] Optional<size_t> find_last(StringView needle) const { return StringUtils::find_last(*this, needle); }
|
||||||
|
|
@ -311,11 +305,8 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr bool operator<(StringView other) const { return compare(other) < 0; }
|
constexpr bool operator<(StringView other) const { return compare(other) < 0; }
|
||||||
|
|
||||||
constexpr bool operator<=(StringView other) const { return compare(other) <= 0; }
|
constexpr bool operator<=(StringView other) const { return compare(other) <= 0; }
|
||||||
|
|
||||||
constexpr bool operator>(StringView other) const { return compare(other) > 0; }
|
constexpr bool operator>(StringView other) const { return compare(other) > 0; }
|
||||||
|
|
||||||
constexpr bool operator>=(StringView other) const { return compare(other) >= 0; }
|
constexpr bool operator>=(StringView other) const { return compare(other) >= 0; }
|
||||||
|
|
||||||
[[nodiscard]] ByteString to_byte_string() const;
|
[[nodiscard]] ByteString to_byte_string() const;
|
||||||
|
|
@ -326,6 +317,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] ByteString replace(StringView needle, StringView replacement, ReplaceMode) const;
|
[[nodiscard]] ByteString replace(StringView needle, StringView replacement, ReplaceMode) const;
|
||||||
|
|
||||||
[[nodiscard]] size_t count(StringView needle) const
|
[[nodiscard]] size_t count(StringView needle) const
|
||||||
{
|
{
|
||||||
return StringUtils::count(*this, needle);
|
return StringUtils::count(*this, needle);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue