Embedded Template Library 1.0
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 "variant.h"
39#include "optional.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
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 }
260
261 //*******************************************
263 //*******************************************
264 result(const result& other)
265 : data(other.data)
266 {
267 }
268
269 //*******************************************
271 //*******************************************
272 result(result&& other)
273 : data(etl::move(other.data))
274 {
275 }
276
277 //*******************************************
279 //*******************************************
280 result(const TError& error)
281 : data(error)
282 {
283 }
284
285 //*******************************************
287 //*******************************************
288#if ETL_CPP11_SUPPORTED
289 result(TError&& error)
290 : data(etl::move(error))
291 {
292 }
293#endif
294
295 //*******************************************
297 //*******************************************
298 result& operator =(const TError& error)
299 {
300 data = error;
301 return *this;
302 }
303
304 //*******************************************
306 //*******************************************
307#if ETL_CPP11_SUPPORTED
308 result& operator =(TError&& error)
309 {
310 data = etl::move(error);
311 return *this;
312 }
313#endif
314
315 //*******************************************
317 //*******************************************
318 bool has_value() const
319 {
320 return !data.has_value();
321 }
322
323 //*******************************************
325 //*******************************************
326 bool is_value() const
327 {
328 return has_value();
329 }
330
331 //*******************************************
333 //*******************************************
334 bool is_error() const
335 {
336 return !has_value();
337 }
338
339 //*******************************************
342 //*******************************************
343 const TError& error() const
344 {
345 return data.value();
346 }
347
348 //*******************************************
351 //*******************************************
352#if ETL_CPP11_SUPPORTED
353 TError&& error()
354 {
355 return etl::move(data.value());
356 }
357#endif
358
359 private:
360
362 };
363
364 //*****************************************************************************
367 //*****************************************************************************
368 template<typename TValue>
369 class result<TValue, void>
370 {
371 public:
372
373 //*******************************************
375 //*******************************************
376 result()
377 {
378 }
379
380 //*******************************************
382 //*******************************************
383 result(const result& other)
384 : data(other.data)
385 {
386 }
387
388 //*******************************************
390 //*******************************************
391 result(result&& other)
392 : data(etl::move(other.data))
393 {
394 }
395
396 //*******************************************
398 //*******************************************
399 result(const TValue& value)
400 : data(value)
401 {
402 }
403
404 //*******************************************
406 //*******************************************
407 result(TValue&& value)
408 : data(etl::move(value))
409 {
410 }
411
412 //*******************************************
414 //*******************************************
415 result& operator =(const TValue& value)
416 {
417 data = value;
418 return *this;
419 }
420
421 //*******************************************
423 //*******************************************
424 result& operator =(TValue&& value)
425 {
426 data = etl::move(value);
427 return *this;
428 }
429
430 //*******************************************
432 //*******************************************
433 bool has_value() const
434 {
435 return data.has_value();
436 }
437
438 //*******************************************
440 //*******************************************
441 bool is_value() const
442 {
443 return has_value();
444 }
445
446 //*******************************************
448 //*******************************************
449 bool is_error() const
450 {
451 return !has_value();
452 }
453
454 //*******************************************
457 //*******************************************
458 const TValue& value() const
459 {
460 return data.value();
461 }
462
463 //*******************************************
466 //*******************************************
467 TValue&& value()
468 {
469 return etl::move(data.value());
470 }
471#endif
472
473 private:
474
476 };
477}
478
479#endif
Definition: optional.h:108
Definition: variant_legacy.h:147
bitset_ext
Definition: absolute.h:38