-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathexpected.hpp
More file actions
771 lines (670 loc) · 19.1 KB
/
expected.hpp
File metadata and controls
771 lines (670 loc) · 19.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
// itlib-expected v1.04
//
// A union-type of a value and an error
//
// SPDX-License-Identifier: MIT
// MIT License:
// Copyright(c) 2021-2025 Borislav Stanimirov
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files(the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and / or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions :
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//
// VERSION HISTORY
//
// 1.04 (2025-04-03) - Fix move assign in <void, E> specialization
// - Add emplace for all specializations
// 1.03 (2025-01-23) Add value and error "getters" in void specializations
// 1.02 (2022-09-02) Specializations for ref and void values and void errors
// 1.01 (2021-09-27) Fixed value_or which could return a ref to temporary
// 1.00 (2021-09-26) Initial release
//
//
// DOCUMENTATION
//
// Simply include this file wherever you need.
// It defines the class itlib::expected, which is a union type of a value and
// an error. It is similar to C++23's std::expected
//
// It is also somewhat similar to std::optional. In a way you can think of
// std::optional<T> as an itlib::expected<T, bool> WITH THE NOTABLE DIFFERENCE
// that a default-constructed expected is truthy (it invokes the default
// contstructor) of T
//
// It is also similar to the the proposed std::expected
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0323r3.pdf
// however it is not the same. itlib::expected does 90% of the job of the
// proposed std::expected with 10% of the code.
//
// Notably itlib::expected is a non-copyable type. It is simply too much effort
// to take care of the case where a copy-constructor throws and copying it
// seems like a very, very rare need.
//
// In most cases one would return an itlib::expected from a function and the
// lifetime of the object will span only the caller's scope.
//
// expected defines specializations for ref and void values and void errors.
// A void error essentially makes the class equivalent to std::optional, but
// again WITH THE NOTABLE DIFFERENCE that default-constructed expected<T, void>
// is truthy.
//
// Basics
//
// itlib::expected has two template arguments:
// * an "expected" or "value" type T: typedef-ed as value_type
// * an "unexpected" or "error" type E: typedef-ed as error_type
//
// It is a union type: It holds either a value or an error.
//
// It is a boolean type: It is truthy if it holds a value and falsy if it holds
// an error.
//
// It is an accessor type: It has dereference operators (* and ->) which lead
// to the value type inside. Invoking them if itlib::expected holds an error
// leads to undefined behavior.
//
// It is a non-copyable type: Only move ops are allowed, even if the value or
// error inside are copyable.
//
// Ways to construct it with an "expected" type:
// * The default constructor. It will in turn invoke the default constructor
// of value_type.
// * With an existing variable of value_type which can be copied or moved
// inside. itlib::expected has a implicit constructor from T&&, so you can
// simply return a T from a function which returns itlib::expected.
//
// Ways to costruct with an "unexpected" type
// * Use `unexpected()`. It can be use to initialize itlib::expected with the
// default constructor of error_type
// * Use `unexpected(E&&)`. It can construct an itlib::expected with an
// existing error type variable
//
// Example
//
// enum class int_error { division_by_zero, signed_overflow };
//
// itlib::expected<int, int_error> divide(int a, int b) {
// if (b == 0) return unexpected(int_error::division_by_zero);
// return a/b;
// }
// ...
// auto res = divide(x, y);
// if (!res) cerr << int_error_to_string(res.error()) << '\n';
// else cout << *res << '\n';
//
// Reference
//
// Helpers
// * unexpected() - free function used to create an itlib::expected with a
// default-constructed error_type
// * unexpected(E&&) - free function used to create an itlib::expected with a
// error_type from E
// * unexpected_t<E> - type which can be used to create an itlib::expected with
// any value_type and an error which can be constructed from E
// * unexpected_t<void> - type which can be used to create an itlib::expected
// with any value_type and any error_type
// Main class: expected<T, E> - union type of a value T and error E
// * expected() - constructs with a default-constructed value T
// * expected(T&& t) - constructs with a value T from t
// * expected(unexpected_t<E>) - constructs with an error
// * expected(expected&&) - move ctor
// * operator=(expected&&) - move assignment
// Boolean interface:
// * has_value() - true if it holds a value
// * has_error() - true if it holds an error
// * operator bool() = has_value()
// Get value:
// * value() - returns value
// * value_or(T t) - returns value if truthy or t if falsy
// * operator* - returns value
// * operator-> - returns value
// Get error:
// * error() - return error
//
// TESTS
//
// You can find unit tests in the official repo:
// https://github.com/iboB/itlib/blob/master/test/
//
#pragma once
#include <cassert>
#include <utility>
#include <new>
namespace itlib
{
template <typename E>
class unexpected_t
{
public:
explicit unexpected_t(E&& e) : m_error(std::forward<E>(e)) {}
private:
template<typename T, typename E2>
friend class expected;
E m_error;
};
template <typename E>
unexpected_t<E> unexpected(E&& e)
{
return unexpected_t<E>(std::forward<E>(e));
}
template <>
class unexpected_t<void> {};
inline unexpected_t<void> unexpected() noexcept { return {}; }
template <typename T, typename E>
class expected
{
public:
using value_type = T;
using error_type = E;
expected() : m_value(), m_has_value(true) {}
expected(T&& t) : m_value(std::forward<T>(t)), m_has_value(true) {}
template <typename E2>
expected(unexpected_t<E2>&& u) : m_error(std::move(u.m_error)), m_has_value(false) {}
expected(unexpected_t<void>) : m_error(), m_has_value(false) {}
// do not copy
expected(const expected&) = delete;
expected& operator=(const expected&) = delete;
// do move
expected(expected&& other) noexcept
: m_has_value(other.has_value())
{
if (m_has_value)
{
::new (&m_value) T(std::move(other.m_value));
}
else
{
::new (&m_error) E(std::move(other.m_error));
}
}
expected& operator=(expected&& other) noexcept
{
if (m_has_value && other.has_value())
{
m_value = std::move(other.m_value);
}
else if(m_has_value && !other.has_value())
{
m_has_value = false;
m_value.~T();
::new (&m_error) E(std::move(other.m_error));
}
else if(!m_has_value && other.has_value())
{
m_has_value = true;
m_error.~E();
::new (&m_value) T(std::move(other.m_value));
}
else
{
m_error = std::move(other.m_error);
}
return *this;
}
~expected()
{
if (m_has_value)
{
m_value.~T();
}
else
{
m_error.~E();
}
}
template <typename... Args>
T& emplace(Args&&... args)
{
if (m_has_value)
{
m_value.~T();
}
else
{
m_error.~E();
}
::new (&m_value) T(std::forward<Args>(args)...);
m_has_value = true;
return m_value;
}
// bool interface
bool has_value() const { return m_has_value; }
bool has_error() const { return !m_has_value; }
explicit operator bool() const { return m_has_value; }
// value getters
T& value() &
{
assert(has_value());
return m_value;
}
const T& value() const &
{
assert(has_value());
return m_value;
}
T&& value() &&
{
assert(has_value());
return std::move(m_value);
}
T& operator*() & { return value(); }
const T& operator*() const & { return value(); }
T&& operator*() && { return std::move(value()); }
template <typename U>
T value_or(U&& v) const & { return has_value() ? value() : std::forward<U>(v); }
template <typename U>
T value_or(U&& v) && { return has_value() ? std::move(value()) : std::forward<U>(v); }
T* operator->() { return &value(); }
const T* operator->() const { return &value(); }
// error getters
E& error() &
{
assert(has_error());
return m_error;
}
const E& error() const &
{
assert(has_error());
return m_error;
}
E&& error() &&
{
assert(has_error());
return std::move(m_error);
}
private:
union
{
value_type m_value;
error_type m_error;
};
bool m_has_value;
};
template <typename T, typename E>
class expected<T&, E> {
public:
using value_type = T;
using error_type = E;
expected(T& t) : m_value(&t) {}
template <typename E2>
expected(unexpected_t<E2>&& u) : m_value(nullptr), m_error(std::move(u.m_error)) {}
expected(unexpected_t<void>) : m_value(nullptr), m_error() {}
// do not copy
expected(const expected&) = delete;
expected& operator=(const expected&) = delete;
// do move
expected(expected&& other) noexcept
: m_value(other.m_value)
{
if (!m_value)
{
new (&m_error) E(std::move(other.m_error));
}
}
expected& operator=(expected&& other) noexcept
{
if (m_value && other.m_value)
{
m_value = other.m_value;
}
else if (m_value && !other.m_value)
{
m_value = nullptr;
::new (&m_error) E(std::move(other.m_error));
}
else if (!m_value && other.m_value)
{
m_value = other.m_value;
m_error.~E();
}
else
{
m_error = std::move(other.m_error);
}
return *this;
}
~expected()
{
if (!m_value)
{
m_error.~E();
}
}
T& emplace(T& t)
{
if (!m_value)
{
m_error.~E();
}
m_value = &t;
return t;
}
// bool interface
bool has_value() const { return !!m_value; }
bool has_error() const { return !m_value; }
explicit operator bool() const { return !!m_value; }
// value getters (pointer semantics)
T& value() const
{
assert(has_value());
return *m_value;
}
T& operator*() const { return value(); }
T& value_or(T& v) const { return has_value() ? value() : v; }
T* operator->() const { return &value(); }
// error getters
E& error()&
{
assert(has_error());
return m_error;
}
const E& error() const&
{
assert(has_error());
return m_error;
}
E&& error()&&
{
assert(has_error());
return std::move(m_error);
}
private:
value_type* m_value;
union {
error_type m_error;
};
};
template <typename E>
class expected<void, E> {
public:
using value_type = void;
using error_type = E;
expected() : m_has_value(true) {}
template <typename E2>
expected(unexpected_t<E2>&& u) : m_error(std::move(u.m_error)), m_has_value(false) {}
expected(unexpected_t<void>) : m_error(), m_has_value(false) {}
// do not copy
expected(const expected&) = delete;
expected& operator=(const expected&) = delete;
// do move
expected(expected&& other) noexcept
: m_has_value(other.has_value())
{
if (!m_has_value)
{
new (&m_error) E(std::move(other.m_error));
}
}
expected& operator=(expected&& other) noexcept
{
if (m_has_value && !other.has_value())
{
m_has_value = false;
::new (&m_error) E(std::move(other.m_error));
}
else if (!m_has_value && other.has_value())
{
m_has_value = true;
m_error.~E();
}
else if (!m_has_value && !other.has_value())
{
m_error = std::move(other.m_error);
}
// else nothing to do
return *this;
}
~expected()
{
if (!m_has_value)
{
m_error.~E();
}
}
void emplace()
{
if (!m_has_value)
{
m_error.~E();
}
m_has_value = true;
}
// bool interface
bool has_value() const { return m_has_value; }
bool has_error() const { return !m_has_value; }
explicit operator bool() const { return m_has_value; }
// value "getter"
void value() const noexcept {
assert(has_value());
}
// error getters
E& error()&
{
assert(has_error());
return m_error;
}
const E& error() const&
{
assert(has_error());
return m_error;
}
E&& error()&&
{
assert(has_error());
return std::move(m_error);
}
private:
union {
error_type m_error;
};
bool m_has_value;
};
template <typename T>
class expected<T, void>
{
public:
using value_type = T;
using error_type = void;
expected() : m_value(), m_has_value(true) {}
expected(T&& t) : m_value(std::forward<T>(t)), m_has_value(true) {}
expected(unexpected_t<void>) : m_has_value(false) {}
expected(const expected& other)
: m_has_value(other.m_has_value)
{
if (m_has_value)
{
::new (&m_value) T(other.m_value);
}
}
expected& operator=(const expected& other)
{
if (m_has_value && other.has_value())
{
m_value = other.m_value;
}
else if (m_has_value && !other.has_value())
{
m_has_value = false;
m_value.~T();
}
else if (!m_has_value && other.has_value())
{
m_has_value = true;
::new (&m_value) T(other.m_value);
}
return *this;
}
expected(expected&& other) noexcept
: m_has_value(other.has_value())
{
if (m_has_value)
{
::new (&m_value) T(std::move(other.m_value));
}
}
expected& operator=(expected&& other) noexcept
{
if (m_has_value && other.has_value())
{
m_value = std::move(other.m_value);
}
else if (m_has_value && !other.has_value())
{
m_has_value = false;
m_value.~T();
}
else if (!m_has_value && other.has_value())
{
m_has_value = true;
::new (&m_value) T(std::move(other.m_value));
}
return *this;
}
~expected()
{
if (m_has_value)
{
m_value.~T();
}
}
// bool interface
bool has_value() const { return m_has_value; }
bool has_error() const { return !m_has_value; }
explicit operator bool() const { return m_has_value; }
// optional interface
void clear()
{
if (!m_has_value) return;
m_value.~T();
m_has_value = false;
}
template <typename... Args>
T& emplace(Args&&... args)
{
clear();
::new (&m_value) T(std::forward<Args>(args)...);
m_has_value = true;
return m_value;
}
// value getters
T& value()&
{
assert(has_value());
return m_value;
}
const T& value() const&
{
assert(has_value());
return m_value;
}
T&& value()&&
{
assert(has_value());
return std::move(m_value);
}
T& operator*()& { return value(); }
const T& operator*() const& { return value(); }
T&& operator*()&& { return std::move(value()); }
template <typename U>
T value_or(U&& v) const& { return has_value() ? value() : std::forward<U>(v); }
template <typename U>
T value_or(U&& v)&& { return has_value() ? std::move(value()) : std::forward<U>(v); }
T* operator->() { return &value(); }
const T* operator->() const { return &value(); }
// error "getter"
void error() const noexcept {
assert(has_error());
}
private:
union
{
value_type m_value;
};
bool m_has_value;
};
template <typename T>
class expected<T&, void> {
public:
using value_type = T;
using error_type = void;
expected(T& t) : m_value(&t) {}
expected(unexpected_t<void>) : m_value(nullptr) {}
expected(const expected&) = default;
expected& operator=(const expected&) = default;
expected(expected&& other) noexcept = default;
expected& operator=(expected&& other) noexcept = default;
// bool interface
bool has_value() const { return !!m_value; }
bool has_error() const { return !m_value; }
explicit operator bool() const { return !!m_value; }
// optional interface
void clear() { m_value = nullptr; }
T& emplace(T& t)
{
m_value = &t;
return t;
}
// value getters (pointer semantics)
T& value() const
{
assert(has_value());
return *m_value;
}
T& operator*() const { return value(); }
T& value_or(T& v) const { return has_value() ? value() : v; }
T* operator->() const { return &value(); }
// error "getter"
void error() const noexcept {
assert(has_error());
}
private:
value_type* m_value;
};
template <>
class expected<void, void> {
public:
using value_type = void;
using error_type = void;
expected() : m_has_value(true) {}
expected(unexpected_t<void>) : m_has_value(false) {}
expected(const expected&) = default;
expected& operator=(const expected&) = default;
expected(expected&& other) noexcept = default;
expected& operator=(expected&& other) noexcept = default;
// bool interface
bool has_value() const { return m_has_value; }
bool has_error() const { return !m_has_value; }
explicit operator bool() const { return m_has_value; }
// optional interface
void clear() { m_has_value = false; }
void emplace() { m_has_value = true; }
// value "getter"
void value() const noexcept {
assert(has_value());
}
// error "getter"
void error() const noexcept {
assert(has_error());
}
private:
bool m_has_value;
};
template <typename T>
using eoptional = expected<T, void>;
}