summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-11-07 01:30:35 +0100
committerAndreas Kling <kling@serenityos.org>2021-11-08 00:35:27 +0100
commite253cf694e5c710303bb6560315e80e79d5664bd (patch)
treee6198b855e8547a64d0455b4abc596d773ea1c69 /AK
parentb2170c11a46cbe8e45c5265a20927dad63dc52b5 (diff)
downloadserenity-e253cf694e5c710303bb6560315e80e79d5664bd.zip
AK: Allow subclassing Error
Diffstat (limited to 'AK')
-rw-r--r--AK/Error.h24
1 files changed, 13 insertions, 11 deletions
diff --git a/AK/Error.h b/AK/Error.h
index bdc495f889..f79e2ff151 100644
--- a/AK/Error.h
+++ b/AK/Error.h
@@ -28,12 +28,13 @@ public:
int code() const { return m_code; }
StringView string_literal() const { return m_string_literal; }
-private:
+protected:
Error(int code)
: m_code(code)
{
}
+private:
Error(StringView string_literal)
: m_string_literal(string_literal)
{
@@ -43,7 +44,7 @@ private:
StringView m_string_literal;
};
-template<typename T>
+template<typename T, typename ErrorType = Error>
class [[nodiscard]] ErrorOr {
public:
ErrorOr(T const& value)
@@ -63,7 +64,7 @@ public:
}
#endif
- ErrorOr(Error&& error)
+ ErrorOr(ErrorType&& error)
: m_error(move(error))
{
}
@@ -78,20 +79,20 @@ public:
bool is_error() const { return m_error.has_value(); }
T release_value() { return m_value.release_value(); }
- Error release_error() { return m_error.release_value(); }
+ ErrorType release_error() { return m_error.release_value(); }
T release_value_but_fixme_should_propagate_errors() { return release_value(); }
private:
Optional<T> m_value;
- Optional<Error> m_error;
+ Optional<ErrorType> m_error;
};
// Partial specialization for void value type
-template<>
-class [[nodiscard]] ErrorOr<void> {
+template<typename ErrorType>
+class [[nodiscard]] ErrorOr<void, ErrorType> {
public:
- ErrorOr(Error error)
+ ErrorOr(ErrorType error)
: m_error(move(error))
{
}
@@ -101,12 +102,13 @@ public:
ErrorOr(const ErrorOr& other) = default;
~ErrorOr() = default;
- Error& error() { return m_error.value(); }
+ ErrorType& error() { return m_error.value(); }
bool is_error() const { return m_error.has_value(); }
- Error release_error() { return m_error.release_value(); }
+ ErrorType release_error() { return m_error.release_value(); }
+ void release_value() { }
private:
- Optional<Error> m_error;
+ Optional<ErrorType> m_error;
};
template<>