Embedded Template Library
1.0
Toggle main menu visibility
Loading...
Searching...
No Matches
result.h
Go to the documentation of this file.
1
2
3
/******************************************************************************
4
The MIT License(MIT)
5
6
Embedded Template Library.
7
https://github.com/ETLCPP/etl
8
https://www.etlcpp.com
9
10
Copyright(c) 2021 John Wellbelove
11
12
Permission is hereby granted, free of charge, to any person obtaining a copy
13
of this software and associated documentation files(the "Software"), to deal
14
in the Software without restriction, including without limitation the rights
15
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16
copies of the Software, and to permit persons to whom the Software is
17
furnished to do so, subject to the following conditions :
18
19
The above copyright notice and this permission notice shall be included in all
20
copies or substantial portions of the Software.
21
22
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
SOFTWARE.
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_SUPPORTED
42
43
namespace
etl
44
{
45
//*****************************************************************************
47
//*****************************************************************************
48
template
<
typename
TValue,
typename
TError>
49
class
result
50
{
51
public
:
52
53
typedef
TValue value_type;
54
typedef
TError error_type;
55
56
//*******************************************
58
//*******************************************
59
result() =
delete
;
60
61
//*******************************************
63
//*******************************************
64
result(
const
result& other)
65
:
data
(other.
data
)
66
{
67
}
68
69
//*******************************************
71
//*******************************************
72
result(result&& other)
73
:
data
(etl::move(other.
data
))
74
{
75
}
76
77
//*******************************************
78
// Construct from a value
79
//*******************************************
80
result(
const
TValue& value)
81
:
data
(value)
82
{
83
}
84
85
//*******************************************
86
// Move construct from a value
87
//*******************************************
88
result(TValue&& value)
89
:
data
(etl::move(value))
90
{
91
}
92
93
//*******************************************
95
//*******************************************
96
result(
const
TError& error)
97
:
data
(error)
98
{
99
}
100
101
//*******************************************
103
//*******************************************
104
result(TError&& error)
105
:
data
(etl::move(error))
106
{
107
}
108
109
//*******************************************
111
//*******************************************
112
result& operator=(
const
result& other)
113
{
114
data
= other.data;
115
return
*
this
;
116
}
117
118
//*******************************************
120
//*******************************************
121
result& operator=(result&& other)
122
{
123
data
= etl::move(other.data);
124
return
*
this
;
125
}
126
127
//*******************************************
129
//*******************************************
130
result& operator=(
const
TValue& value)
131
{
132
data
= value;
133
return
*
this
;
134
}
135
136
//*******************************************
138
//*******************************************
139
result& operator=(TValue&& value)
140
{
141
data
= etl::move(value);
142
return
*
this
;
143
}
144
145
//*******************************************
147
//*******************************************
148
result& operator=(
const
TError& error)
149
{
150
data
= error;
151
return
*
this
;
152
}
153
154
//*******************************************
156
//*******************************************
157
result& operator=(TError&& error)
158
{
159
data
= etl::move(error);
160
return
*
this
;
161
}
162
163
//*******************************************
165
//*******************************************
166
bool
has_value()
const
167
{
168
return
(
data
.index() == 0U);
169
}
170
171
//*******************************************
173
//*******************************************
174
bool
is_value()
const
175
{
176
return
has_value();
177
}
178
179
//*******************************************
181
//*******************************************
182
bool
is_error()
const
183
{
184
return
!has_value();
185
}
186
187
//*******************************************
190
//*******************************************
191
const
TValue& value()
const
192
{
193
return
etl::get<TValue>
(data);
194
}
195
196
//*******************************************
199
//*******************************************
200
TValue&& value()
201
{
202
return
etl::move(
etl::get<TValue>
(data));
203
}
204
205
//*******************************************
208
//*******************************************
209
const
TError& error()
const
210
{
211
return
etl::get<TError>
(data);
212
}
213
214
//*******************************************
217
//*******************************************
218
TError&& error()
219
{
220
return
etl::move(
etl::get<TError>
(data));
221
}
222
223
private
:
224
225
etl::variant<TValue, TError>
data
;
226
};
227
228
//*****************************************************************************
231
//*****************************************************************************
232
template
<
typename
TError>
233
class
result<void, TError>
234
{
235
public
:
236
237
typedef
void
value_type;
238
typedef
TError error_type;
239
240
//*******************************************
242
//*******************************************
243
result() {}
244
245
//*******************************************
247
//*******************************************
248
result(
const
result& other)
249
:
data
(other.
data
)
250
{
251
}
252
253
//*******************************************
255
//*******************************************
256
result(result&& other)
257
:
data
(etl::move(other.
data
))
258
{
259
}
260
261
//*******************************************
263
//*******************************************
264
result(
const
TError& error)
265
:
data
(error)
266
{
267
}
268
269
//*******************************************
271
//*******************************************
272
result(TError&& error)
273
:
data
(etl::move(error))
274
{
275
}
276
277
//*******************************************
279
//*******************************************
280
result& operator=(
const
TError& error)
281
{
282
data
= error;
283
return
*
this
;
284
}
285
286
//*******************************************
288
//*******************************************
289
result& operator=(TError&& error)
290
{
291
data
= etl::move(error);
292
return
*
this
;
293
}
294
295
//*******************************************
297
//*******************************************
298
bool
has_value()
const
299
{
300
return
!
data
.has_value();
301
}
302
303
//*******************************************
305
//*******************************************
306
bool
is_value()
const
307
{
308
return
has_value();
309
}
310
311
//*******************************************
313
//*******************************************
314
bool
is_error()
const
315
{
316
return
!has_value();
317
}
318
319
//*******************************************
322
//*******************************************
323
const
TError& error()
const
324
{
325
return
data
.value();
326
}
327
328
//*******************************************
331
//*******************************************
332
TError&& error()
333
{
334
return
etl::move(
data
.value());
335
}
336
337
private
:
338
339
etl::optional<TError>
data
;
340
};
341
342
//*****************************************************************************
345
//*****************************************************************************
346
template
<
typename
TValue>
347
class
result<TValue, void>
348
{
349
public
:
350
351
//*******************************************
353
//*******************************************
354
result() {}
355
356
//*******************************************
358
//*******************************************
359
result(
const
result& other)
360
:
data
(other.
data
)
361
{
362
}
363
364
//*******************************************
366
//*******************************************
367
result(result&& other)
368
:
data
(etl::move(other.
data
))
369
{
370
}
371
372
//*******************************************
374
//*******************************************
375
result(
const
TValue& value)
376
:
data
(value)
377
{
378
}
379
380
//*******************************************
382
//*******************************************
383
result(TValue&& value)
384
:
data
(etl::move(value))
385
{
386
}
387
388
//*******************************************
390
//*******************************************
391
result& operator=(
const
TValue& value)
392
{
393
data
= value;
394
return
*
this
;
395
}
396
397
//*******************************************
399
//*******************************************
400
result& operator=(TValue&& value)
401
{
402
data
= etl::move(value);
403
return
*
this
;
404
}
405
406
//*******************************************
408
//*******************************************
409
bool
has_value()
const
410
{
411
return
data
.has_value();
412
}
413
414
//*******************************************
416
//*******************************************
417
bool
is_value()
const
418
{
419
return
has_value();
420
}
421
422
//*******************************************
424
//*******************************************
425
bool
is_error()
const
426
{
427
return
!has_value();
428
}
429
430
//*******************************************
433
//*******************************************
434
const
TValue& value()
const
435
{
436
return
data
.value();
437
}
438
439
//*******************************************
442
//*******************************************
443
TValue&& value()
444
{
445
return
etl::move(
data
.value());
446
}
447
448
private
:
449
450
etl::optional<TValue>
data
;
451
};
452
}
// namespace etl
453
#endif
454
455
#endif
etl
Definition
absolute.h:40
etl::data
ETL_CONSTEXPR TContainer::pointer data(TContainer &container)
Definition
iterator.h:1470
etl::get
T & get(array< T, Size > &a)
Definition
array.h:1158
optional.h
platform.h
variant.h
include
etl
result.h
Generated on
for Embedded Template Library by
1.17.0