diff options
Diffstat (limited to 'framework/src/errors.h')
-rw-r--r-- | framework/src/errors.h | 46 |
1 files changed, 31 insertions, 15 deletions
diff --git a/framework/src/errors.h b/framework/src/errors.h index f6c6cfab..9c48f32a 100644 --- a/framework/src/errors.h +++ b/framework/src/errors.h | |||
@@ -20,9 +20,13 @@ public: | |||
20 | 20 | ||
21 | // For implicit conversions when doing makeUnexpected(other) | 21 | // For implicit conversions when doing makeUnexpected(other) |
22 | template <typename Other> | 22 | template <typename Other> |
23 | constexpr explicit Unexpected(const Unexpected<Other> &error) : mValue(error.value()) {} | 23 | constexpr explicit Unexpected(const Unexpected<Other> &error) : mValue(error.value()) |
24 | { | ||
25 | } | ||
24 | template <typename Other> | 26 | template <typename Other> |
25 | constexpr explicit Unexpected(Unexpected<Other> &&error) : mValue(std::move(error.value())) {} | 27 | constexpr explicit Unexpected(Unexpected<Other> &&error) : mValue(std::move(error.value())) |
28 | { | ||
29 | } | ||
26 | 30 | ||
27 | constexpr const Error &value() const & | 31 | constexpr const Error &value() const & |
28 | { | 32 | { |
@@ -82,7 +86,7 @@ protected: | |||
82 | 86 | ||
83 | constexpr void copyPartFromOther(const StorageBase &other) | 87 | constexpr void copyPartFromOther(const StorageBase &other) |
84 | { | 88 | { |
85 | if (isValue) { | 89 | if (mIsValue) { |
86 | mValue = other.mValue; | 90 | mValue = other.mValue; |
87 | } else { | 91 | } else { |
88 | mError = other.mError; | 92 | mError = other.mError; |
@@ -91,40 +95,40 @@ protected: | |||
91 | 95 | ||
92 | void movePartFromOther(StorageBase &&other) | 96 | void movePartFromOther(StorageBase &&other) |
93 | { | 97 | { |
94 | if (isValue) { | 98 | if (mIsValue) { |
95 | mValue = std::move(other.mValue); | 99 | mValue = std::move(other.mValue); |
96 | } else { | 100 | } else { |
97 | mError = std::move(other.mError); | 101 | mError = std::move(other.mError); |
98 | } | 102 | } |
99 | } | 103 | } |
100 | 104 | ||
101 | StorageBase(const StorageBase &other) : isValue(other.isValue) | 105 | StorageBase(const StorageBase &other) : mIsValue(other.mIsValue) |
102 | { | 106 | { |
103 | copyPartFromOther(other); | 107 | copyPartFromOther(other); |
104 | } | 108 | } |
105 | 109 | ||
106 | StorageBase(StorageBase &&other) : isValue(other.isValue) | 110 | StorageBase(StorageBase &&other) : mIsValue(other.mIsValue) |
107 | { | 111 | { |
108 | movePartFromOther(std::move(other)); | 112 | movePartFromOther(std::move(other)); |
109 | } | 113 | } |
110 | 114 | ||
111 | constexpr StorageBase &operator=(const StorageBase &other) | 115 | constexpr StorageBase &operator=(const StorageBase &other) |
112 | { | 116 | { |
113 | isValue = other.isValue; | 117 | mIsValue = other.mIsValue; |
114 | copyPartFromOther(other); | 118 | copyPartFromOther(other); |
115 | return *this; | 119 | return *this; |
116 | } | 120 | } |
117 | 121 | ||
118 | constexpr StorageBase &operator=(StorageBase &&other) | 122 | constexpr StorageBase &operator=(StorageBase &&other) |
119 | { | 123 | { |
120 | isValue = other.isValue; | 124 | mIsValue = other.mIsValue; |
121 | movePartFromOther(other); | 125 | movePartFromOther(other); |
122 | return *this; | 126 | return *this; |
123 | } | 127 | } |
124 | 128 | ||
125 | ~StorageBase() | 129 | ~StorageBase() |
126 | { | 130 | { |
127 | if (isValue) { | 131 | if (mIsValue) { |
128 | mValue.~Type(); | 132 | mValue.~Type(); |
129 | } else { | 133 | } else { |
130 | mError.~Unexpected<Error>(); | 134 | mError.~Unexpected<Error>(); |
@@ -135,13 +139,13 @@ protected: | |||
135 | 139 | ||
136 | template <typename... Args> | 140 | template <typename... Args> |
137 | constexpr StorageBase(tags::Expected, Args &&... args) | 141 | constexpr StorageBase(tags::Expected, Args &&... args) |
138 | : mValue(std::forward<Args>(args)...), isValue(true) | 142 | : mValue(std::forward<Args>(args)...), mIsValue(true) |
139 | { | 143 | { |
140 | } | 144 | } |
141 | 145 | ||
142 | template <typename... Args> | 146 | template <typename... Args> |
143 | constexpr StorageBase(tags::Unexpected, Args &&... args) | 147 | constexpr StorageBase(tags::Unexpected, Args &&... args) |
144 | : mError(std::forward<Args>(args)...), isValue(false) | 148 | : mError(std::forward<Args>(args)...), mIsValue(false) |
145 | { | 149 | { |
146 | } | 150 | } |
147 | 151 | ||
@@ -150,7 +154,7 @@ protected: | |||
150 | Unexpected<Error> mError; | 154 | Unexpected<Error> mError; |
151 | Type mValue; | 155 | Type mValue; |
152 | }; | 156 | }; |
153 | bool isValue; | 157 | bool mIsValue; |
154 | }; | 158 | }; |
155 | 159 | ||
156 | // Write functions here when storage related and when Type == void | 160 | // Write functions here when storage related and when Type == void |
@@ -158,16 +162,16 @@ template <typename Error> | |||
158 | struct StorageBase<Error, void> | 162 | struct StorageBase<Error, void> |
159 | { | 163 | { |
160 | protected: | 164 | protected: |
161 | constexpr StorageBase(tags::Expected) : isValue(true) {} | 165 | constexpr StorageBase(tags::Expected) : mIsValue(true) {} |
162 | 166 | ||
163 | template <typename... Args> | 167 | template <typename... Args> |
164 | constexpr StorageBase(tags::Unexpected, Args &&... args) | 168 | constexpr StorageBase(tags::Unexpected, Args &&... args) |
165 | : mError(std::forward<Args>(args)...), isValue(false) | 169 | : mError(std::forward<Args>(args)...), mIsValue(false) |
166 | { | 170 | { |
167 | } | 171 | } |
168 | 172 | ||
169 | Unexpected<Error> mError; | 173 | Unexpected<Error> mError; |
170 | bool isValue; | 174 | bool mIsValue; |
171 | }; | 175 | }; |
172 | 176 | ||
173 | // Write functions here when storage related, whether Type is void or not | 177 | // Write functions here when storage related, whether Type is void or not |
@@ -205,8 +209,11 @@ struct ExpectedBase : detail::Storage<Error, Type> | |||
205 | { | 209 | { |
206 | } | 210 | } |
207 | 211 | ||
212 | // Warning: will crash if this is an error. You should always check this is | ||
213 | // an expected value before calling `.value()` | ||
208 | constexpr const Type &value() const & | 214 | constexpr const Type &value() const & |
209 | { | 215 | { |
216 | Q_ASSERT(this->mIsValue); | ||
210 | return this->mValue; | 217 | return this->mValue; |
211 | } | 218 | } |
212 | }; | 219 | }; |
@@ -243,4 +250,13 @@ public: | |||
243 | { | 250 | { |
244 | return this->mError.value(); | 251 | return this->mError.value(); |
245 | } | 252 | } |
253 | |||
254 | constexpr bool isValue() const | ||
255 | { | ||
256 | return this->mIsValue; | ||
257 | } | ||
258 | constexpr explicit operator bool() const | ||
259 | { | ||
260 | return this->mIsValue; | ||
261 | } | ||
246 | }; | 262 | }; |