Embedded Template Library 1.0
Loading...
Searching...
No Matches
result.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2021 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_RESULT_INCLUDED
32#define ETL_RESULT_INCLUDED
33
36
37#include "platform.h"
38#include "optional.h"
39#include "variant.h"
40
41#if ETL_CPP11_NOT_SUPPORTED
42 #if !defined(ETL_IN_UNIT_TEST)
43 #error NOT SUPPORTED FOR C++03 OR BELOW
44 #endif
45#else
46
47namespace etl
48{
49 //*****************************************************************************
51 //*****************************************************************************
52 template <typename TValue, typename TError>
53 class result
54 {
55 public:
56
57 typedef TValue value_type;
58 typedef TError error_type;
59
60 //*******************************************
62 //*******************************************
63 result() = delete;
64
65 //*******************************************
67 //*******************************************
68 result(const result& other)
69 : data(other.data)
70 {
71 }
72
73 #if ETL_CPP11_SUPPORTED
74 //*******************************************
76 //*******************************************
77 result(result&& other)
78 : data(etl::move(other.data))
79 {
80 }
81 #endif
82
83 //*******************************************
84 // Construct from a value
85 //*******************************************
86 result(const TValue& value)
87 : data(value)
88 {
89 }
90
91 //*******************************************
92 // Move construct from a value
93 //*******************************************
94 result(TValue&& value)
95 : data(etl::move(value))
96 {
97 }
98
99 //*******************************************
101 //*******************************************
102 result(const TError& error)
103 : data(error)
104 {
105 }
106
107 //*******************************************
109 //*******************************************
110 #if ETL_CPP11_SUPPORTED
111 result(TError&& error)
112 : data(etl::move(error))
113 {
114 }
115 #endif
116
117 //*******************************************
119 //*******************************************
120 result& operator=(const result& other)
121 {
122 data = other.data;
123 return *this;
124 }
125
126 //*******************************************
128 //*******************************************
129 result& operator=(result&& other)
130 {
131 data = etl::move(other.data);
132 return *this;
133 }
134
135 //*******************************************
137 //*******************************************
138 result& operator=(const TValue& value)
139 {
140 data = value;
141 return *this;
142 }
143
144 //*******************************************
146 //*******************************************
147 #if ETL_CPP11_SUPPORTED
148 result& operator=(TValue&& value)
149 {
150 data = etl::move(value);
151 return *this;
152 }
153 #endif
154
155 //*******************************************
157 //*******************************************
158 result& operator=(const TError& error)
159 {
160 data = error;
161 return *this;
162 }
163
164 //*******************************************
166 //*******************************************
167 #if ETL_CPP11_SUPPORTED
168 result& operator=(TError&& error)
169 {
170 data = etl::move(error);
171 return *this;
172 }
173 #endif
174
175 //*******************************************
177 //*******************************************
178 bool has_value() const
179 {
180 return (data.index() == 0U);
181 }
182
183 //*******************************************
185 //*******************************************
186 bool is_value() const
187 {
188 return has_value();
189 }
190
191 //*******************************************
193 //*******************************************
194 bool is_error() const
195 {
196 return !has_value();
197 }
198
199 //*******************************************
202 //*******************************************
203 const TValue& value() const
204 {
205 return etl::get<TValue>(data);
206 }
207
208 //*******************************************
211 //*******************************************
212 TValue&& value()
213 {
214 return etl::move(etl::get<TValue>(data));
215 }
216
217 //*******************************************
220 //*******************************************
221 const TError& error() const
222 {
223 return etl::get<TError>(data);
224 }
225
226 //*******************************************
229 //*******************************************
230 #if ETL_CPP11_SUPPORTED
231 TError&& error()
232 {
233 return etl::move(etl::get<TError>(data));
234 }
235 #endif
236
237 private:
238
239 etl::variant<TValue, TError> data;
240 };
241
242 //*****************************************************************************
245 //*****************************************************************************
246 template <typename TError>
247 class result<void, TError>
248 {
249 public:
250
251 typedef void value_type;
252 typedef TError error_type;
253
254 //*******************************************
256 //*******************************************
257 result() {}
258
259 //*******************************************
261 //*******************************************
262 result(const result& other)
263 : data(other.data)
264 {
265 }
266
267 //*******************************************
269 //*******************************************
270 result(result&& other)
271 : data(etl::move(other.data))
272 {
273 }
274
275 //*******************************************
277 //*******************************************
278 result(const TError& error)
279 : data(error)
280 {
281 }
282
283 //*******************************************
285 //*******************************************
286 #if ETL_CPP11_SUPPORTED
287 result(TError&& error)
288 : data(etl::move(error))
289 {
290 }
291 #endif
292
293 //*******************************************
295 //*******************************************
296 result& operator=(const TError& error)
297 {
298 data = error;
299 return *this;
300 }
301
302 //*******************************************
304 //*******************************************
305 #if ETL_CPP11_SUPPORTED
306 result& operator=(TError&& error)
307 {
308 data = etl::move(error);
309 return *this;
310 }
311 #endif
312
313 //*******************************************
315 //*******************************************
316 bool has_value() const
317 {
318 return !data.has_value();
319 }
320
321 //*******************************************
323 //*******************************************
324 bool is_value() const
325 {
326 return has_value();
327 }
328
329 //*******************************************
331 //*******************************************
332 bool is_error() const
333 {
334 return !has_value();
335 }
336
337 //*******************************************
340 //*******************************************
341 const TError& error() const
342 {
343 return data.value();
344 }
345
346 //*******************************************
349 //*******************************************
350 #if ETL_CPP11_SUPPORTED
351 TError&& error()
352 {
353 return etl::move(data.value());
354 }
355 #endif
356
357 private:
358
359 etl::optional<TError> data;
360 };
361
362 //*****************************************************************************
365 //*****************************************************************************
366 template <typename TValue>
367 class result<TValue, void>
368 {
369 public:
370
371 //*******************************************
373 //*******************************************
374 result() {}
375
376 //*******************************************
378 //*******************************************
379 result(const result& other)
380 : data(other.data)
381 {
382 }
383
384 //*******************************************
386 //*******************************************
387 result(result&& other)
388 : data(etl::move(other.data))
389 {
390 }
391
392 //*******************************************
394 //*******************************************
395 result(const TValue& value)
396 : data(value)
397 {
398 }
399
400 //*******************************************
402 //*******************************************
403 result(TValue&& value)
404 : data(etl::move(value))
405 {
406 }
407
408 //*******************************************
410 //*******************************************
411 result& operator=(const TValue& value)
412 {
413 data = value;
414 return *this;
415 }
416
417 //*******************************************
419 //*******************************************
420 result& operator=(TValue&& value)
421 {
422 data = etl::move(value);
423 return *this;
424 }
425
426 //*******************************************
428 //*******************************************
429 bool has_value() const
430 {
431 return data.has_value();
432 }
433
434 //*******************************************
436 //*******************************************
437 bool is_value() const
438 {
439 return has_value();
440 }
441
442 //*******************************************
444 //*******************************************
445 bool is_error() const
446 {
447 return !has_value();
448 }
449
450 //*******************************************
453 //*******************************************
454 const TValue& value() const
455 {
456 return data.value();
457 }
458
459 //*******************************************
462 //*******************************************
463 TValue&& value()
464 {
465 return etl::move(data.value());
466 }
467
468 private:
469
470 etl::optional<TValue> data;
471 };
472} // namespace etl
473#endif
474
475#endif
bitset_ext
Definition absolute.h:40
ETL_CONSTEXPR TContainer::pointer data(TContainer &container)
Definition iterator.h:1228
T & get(array< T, Size > &a)
Definition array.h:1161