/src/serenity/AK/Result.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/Assertions.h> |
10 | | #include <AK/Optional.h> |
11 | | |
12 | | namespace AK { |
13 | | |
14 | | template<typename ValueT, typename ErrorT> |
15 | | class [[nodiscard]] Result { |
16 | | public: |
17 | | using ValueType = ValueT; |
18 | | using ErrorType = ErrorT; |
19 | | |
20 | | Result(ValueType const& res) |
21 | 0 | : m_result(res) |
22 | 0 | { |
23 | 0 | } |
24 | | |
25 | | Result(ValueType&& res) |
26 | 2.21M | : m_result(move(res)) |
27 | 2.21M | { |
28 | 2.21M | } AK::Result<unsigned int, AK::GenericLexer::UnicodeEscapeError>::Result(unsigned int&&) Line | Count | Source | 26 | 2.21M | : m_result(move(res)) | 27 | 2.21M | { | 28 | 2.21M | } |
AK::Result<regex::RegexOptions<regex::ECMAScriptFlags>, AK::ByteString>::Result(regex::RegexOptions<regex::ECMAScriptFlags>&&) Line | Count | Source | 26 | 38 | : m_result(move(res)) | 27 | 38 | { | 28 | 38 | } |
AK::Result<JS::NonnullGCPtr<JS::Script>, AK::Vector<JS::ParserError, 0ul> >::Result(JS::NonnullGCPtr<JS::Script>&&) Line | Count | Source | 26 | 16 | : m_result(move(res)) | 27 | 16 | { | 28 | 16 | } |
Unexecuted instantiation: AK::Result<JS::NonnullGCPtr<JS::SourceTextModule>, AK::Vector<JS::ParserError, 0ul> >::Result(JS::NonnullGCPtr<JS::SourceTextModule>&&) Unexecuted instantiation: AK::Result<AK::ByteString, Line::Editor::Error>::Result(AK::ByteString&&) Unexecuted instantiation: AK::Result<AK::Vector<unsigned long, 2ul>, Line::Editor::Error>::Result(AK::Vector<unsigned long, 2ul>&&) |
29 | | |
30 | | Result(ErrorType const& error) |
31 | 46 | : m_error(error) |
32 | 46 | { |
33 | 46 | } AK::Result<JS::NonnullGCPtr<JS::Script>, AK::Vector<JS::ParserError, 0ul> >::Result(AK::Vector<JS::ParserError, 0ul> const&) Line | Count | Source | 31 | 46 | : m_error(error) | 32 | 46 | { | 33 | 46 | } |
Unexecuted instantiation: AK::Result<JS::NonnullGCPtr<JS::SourceTextModule>, AK::Vector<JS::ParserError, 0ul> >::Result(AK::Vector<JS::ParserError, 0ul> const&) Unexecuted instantiation: AK::Result<AK::ByteString, Line::Editor::Error>::Result(Line::Editor::Error const&) Unexecuted instantiation: AK::Result<AK::Vector<unsigned long, 2ul>, Line::Editor::Error>::Result(Line::Editor::Error const&) |
34 | | |
35 | | Result(ErrorType&& error) |
36 | 34.8k | : m_error(move(error)) |
37 | 34.8k | { |
38 | 34.8k | } AK::Result<unsigned int, AK::GenericLexer::UnicodeEscapeError>::Result(AK::GenericLexer::UnicodeEscapeError&&) Line | Count | Source | 36 | 34.6k | : m_error(move(error)) | 37 | 34.6k | { | 38 | 34.6k | } |
AK::Result<regex::RegexOptions<regex::ECMAScriptFlags>, AK::ByteString>::Result(AK::ByteString&&) Line | Count | Source | 36 | 140 | : m_error(move(error)) | 37 | 140 | { | 38 | 140 | } |
Unexecuted instantiation: AK::Result<AK::ByteString, Line::Editor::Error>::Result(Line::Editor::Error&&) Unexecuted instantiation: AK::Result<AK::Vector<unsigned long, 2ul>, Line::Editor::Error>::Result(Line::Editor::Error&&) |
39 | | |
40 | | Result(Result&& other) = default; |
41 | | Result(Result const& other) = default; |
42 | 240 | ~Result() = default; AK::Result<JS::NonnullGCPtr<JS::Script>, AK::Vector<JS::ParserError, 0ul> >::~Result() Line | Count | Source | 42 | 62 | ~Result() = default; |
AK::Result<regex::RegexOptions<regex::ECMAScriptFlags>, AK::ByteString>::~Result() Line | Count | Source | 42 | 178 | ~Result() = default; |
Unexecuted instantiation: AK::Result<JS::NonnullGCPtr<JS::SourceTextModule>, AK::Vector<JS::ParserError, 0ul> >::~Result() Unexecuted instantiation: AK::Result<AK::ByteString, Line::Editor::Error>::~Result() Unexecuted instantiation: AK::Result<AK::Vector<unsigned long, 2ul>, Line::Editor::Error>::~Result() |
43 | | |
44 | | ValueType& value() |
45 | 2.21M | { |
46 | 2.21M | return m_result.value(); |
47 | 2.21M | } AK::Result<unsigned int, AK::GenericLexer::UnicodeEscapeError>::value() Line | Count | Source | 45 | 2.21M | { | 46 | 2.21M | return m_result.value(); | 47 | 2.21M | } |
AK::Result<JS::NonnullGCPtr<JS::Script>, AK::Vector<JS::ParserError, 0ul> >::value() Line | Count | Source | 45 | 16 | { | 46 | 16 | return m_result.value(); | 47 | 16 | } |
Unexecuted instantiation: AK::Result<AK::Vector<unsigned long, 2ul>, Line::Editor::Error>::value() Unexecuted instantiation: AK::Result<AK::ByteString, Line::Editor::Error>::value() Unexecuted instantiation: AK::Result<JS::NonnullGCPtr<JS::SourceTextModule>, AK::Vector<JS::ParserError, 0ul> >::value() |
48 | | |
49 | | ErrorType& error() |
50 | 0 | { |
51 | 0 | return m_error.value(); |
52 | 0 | } Unexecuted instantiation: AK::Result<JS::NonnullGCPtr<JS::SourceTextModule>, AK::Vector<JS::ParserError, 0ul> >::error() Unexecuted instantiation: AK::Result<unsigned int, AK::GenericLexer::UnicodeEscapeError>::error() Unexecuted instantiation: AK::Result<AK::Vector<unsigned long, 2ul>, Line::Editor::Error>::error() Unexecuted instantiation: AK::Result<AK::ByteString, Line::Editor::Error>::error() Unexecuted instantiation: AK::Result<JS::NonnullGCPtr<JS::Script>, AK::Vector<JS::ParserError, 0ul> >::error() |
53 | | |
54 | | bool is_error() const |
55 | 2.24M | { |
56 | 2.24M | return m_error.has_value(); |
57 | 2.24M | } AK::Result<unsigned int, AK::GenericLexer::UnicodeEscapeError>::is_error() const Line | Count | Source | 55 | 2.24M | { | 56 | 2.24M | return m_error.has_value(); | 57 | 2.24M | } |
AK::Result<JS::NonnullGCPtr<JS::Script>, AK::Vector<JS::ParserError, 0ul> >::is_error() const Line | Count | Source | 55 | 62 | { | 56 | 62 | return m_error.has_value(); | 57 | 62 | } |
AK::Result<regex::RegexOptions<regex::ECMAScriptFlags>, AK::ByteString>::is_error() const Line | Count | Source | 55 | 178 | { | 56 | 178 | return m_error.has_value(); | 57 | 178 | } |
Unexecuted instantiation: AK::Result<JS::NonnullGCPtr<JS::SourceTextModule>, AK::Vector<JS::ParserError, 0ul> >::is_error() const Unexecuted instantiation: AK::Result<AK::Vector<unsigned long, 2ul>, Line::Editor::Error>::is_error() const Unexecuted instantiation: AK::Result<AK::ByteString, Line::Editor::Error>::is_error() const |
58 | | |
59 | | ValueType release_value() |
60 | 38 | { |
61 | 38 | return m_result.release_value(); |
62 | 38 | } AK::Result<regex::RegexOptions<regex::ECMAScriptFlags>, AK::ByteString>::release_value() Line | Count | Source | 60 | 38 | { | 61 | 38 | return m_result.release_value(); | 62 | 38 | } |
Unexecuted instantiation: AK::Result<JS::NonnullGCPtr<JS::SourceTextModule>, AK::Vector<JS::ParserError, 0ul> >::release_value() Unexecuted instantiation: AK::Result<JS::NonnullGCPtr<JS::Script>, AK::Vector<JS::ParserError, 0ul> >::release_value() |
63 | | |
64 | | ErrorType release_error() |
65 | 140 | { |
66 | 140 | return m_error.release_value(); |
67 | 140 | } |
68 | | |
69 | | private: |
70 | | Optional<ValueType> m_result; |
71 | | Optional<ErrorType> m_error; |
72 | | }; |
73 | | |
74 | | // Partial specialization for void value type |
75 | | template<typename ErrorT> |
76 | | class [[nodiscard]] Result<void, ErrorT> { |
77 | | public: |
78 | | using ValueType = void; |
79 | | using ErrorType = ErrorT; |
80 | | |
81 | | Result(ErrorType const& error) |
82 | | : m_error(error) |
83 | | { |
84 | | } |
85 | | |
86 | | Result(ErrorType&& error) |
87 | 0 | : m_error(move(error)) |
88 | 0 | { |
89 | 0 | } |
90 | | |
91 | 45 | Result() = default; |
92 | | Result(Result&& other) = default; |
93 | | Result(Result const& other) = default; |
94 | | ~Result() = default; |
95 | | |
96 | | // For compatibility with TRY(). |
97 | | void value() {}; |
98 | | void release_value() {}; |
99 | | |
100 | | ErrorType& error() |
101 | | { |
102 | | return m_error.value(); |
103 | | } |
104 | | |
105 | | bool is_error() const |
106 | | { |
107 | | return m_error.has_value(); |
108 | | } |
109 | | |
110 | | ErrorType release_error() |
111 | | { |
112 | | return m_error.release_value(); |
113 | | } |
114 | | |
115 | | private: |
116 | | Optional<ErrorType> m_error; |
117 | | }; |
118 | | |
119 | | } |
120 | | |
121 | | #if USING_AK_GLOBALLY |
122 | | using AK::Result; |
123 | | #endif |