Embedded Template Library 1.0
Loading...
Searching...
No Matches
expected.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) 2022 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_EXPECTED_INCLUDED
32#define ETL_EXPECTED_INCLUDED
33
36#include "platform.h"
37#include "exception.h"
38#include "error_handler.h"
39#include "utility.h"
40#include "variant.h"
41#include "initializer_list.h"
42
43namespace etl
44{
45 //***************************************************************************
47 //***************************************************************************
48 class expected_exception : public etl::exception
49 {
50 public:
51
52 expected_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
53 : exception(reason_, file_name_, line_number_)
54 {
55 }
56 };
57
58 //***************************************************************************
60 //***************************************************************************
61 class expected_invalid : public etl::expected_exception
62 {
63 public:
64
65 expected_invalid(string_type file_name_, numeric_type line_number_)
66 : expected_exception(ETL_ERROR_TEXT("expected:invalid", ETL_EXPECTED_FILE_ID"A"), file_name_, line_number_)
67 {
68 }
69 };
70
71 //***************************************************************************
74 //***************************************************************************
75 template <typename TError>
77 {
78 public:
79
80 typedef TError error_type;
81
82 //*******************************************
84 //*******************************************
85 ETL_CONSTEXPR unexpected(const unexpected& other)
86 : error_value(other.error_value)
87 {
88 }
89
90#if ETL_USING_CPP11
91 //*******************************************
93 //*******************************************
94 ETL_CONSTEXPR unexpected(unexpected&& other)
95 : error_value(etl::move(other.error_value))
96 {
97 }
98#endif
99
100 //*******************************************
102 //*******************************************
103 ETL_CONSTEXPR explicit unexpected(const TError& e)
104 : error_value(e)
105 {
106 }
107
108#if ETL_USING_CPP11
109 //*******************************************
111 //*******************************************
112 ETL_CONSTEXPR explicit unexpected(TError&& e)
113 : error_value(etl::forward<TError>(e))
114 {
115 }
116
117 //*******************************************
119 //*******************************************
120 template <typename... Args >
121 ETL_CONSTEXPR explicit unexpected(etl::in_place_t, Args&&... args)
122 : error_value(etl::forward<Args>(args)...)
123 {
124 }
125#endif
126
127#if ETL_HAS_INITIALIZER_LIST
128 //*******************************************
130 //*******************************************
131 template <typename U, typename... Args>
132 ETL_CONSTEXPR explicit unexpected(etl::in_place_t, std::initializer_list<U> init, Args&&... args)
133 : error_value(init, etl::forward<Args>(args)...)
134 {
135 }
136#endif
137
138 //*******************************************
140 //*******************************************
141 ETL_CONSTEXPR14
143 {
144#if ETL_USING_CPP11
145 ETL_STATIC_ASSERT(etl::is_copy_constructible<TError>::value, "Error not copy assignable");
146#endif
147
148 error_value = rhs.error_value;
149 return *this;
150 }
151
152#if ETL_USING_CPP11
153 //*******************************************
155 //*******************************************
156 ETL_CONSTEXPR14
158 {
159 ETL_STATIC_ASSERT(etl::is_move_constructible<TError>::value, "Error not move assignable");
160
161 error_value = etl::move(rhs.error_value);
162 return *this;
163 }
164#endif
165
166#if ETL_USING_CPP11
167 //*******************************************
169 //*******************************************
170 ETL_CONSTEXPR14 TError& error()& ETL_NOEXCEPT
171 {
172 return error_value;
173 }
174
175 //*******************************************
177 //*******************************************
178 ETL_CONSTEXPR14 const TError& error() const& ETL_NOEXCEPT
179 {
180 return error_value;
181 }
182
183 //*******************************************
185 //*******************************************
186 ETL_CONSTEXPR14 TError&& error()&& ETL_NOEXCEPT
187 {
188 return etl::move(error_value);
189 }
190
191 //*******************************************
193 //*******************************************
194 ETL_CONSTEXPR14 TError&& error() const&& ETL_NOEXCEPT
195 {
196 return etl::move(error_value);
197 }
198#else
199 //*******************************************
201 //*******************************************
202 const TError& error() const
203 {
204 return error_value;
205 }
206#endif
207
208 //*******************************************
210 //*******************************************
212 {
213 using ETL_OR_STD::swap;
214
215 swap(error_value, other.error_value);
216 }
217
218 private:
219
220 TError error_value;
221 };
222
223 //*****************************************************************************
225 //*****************************************************************************
226 struct unexpect_t
227 {
228 ETL_CONSTEXPR14 explicit unexpect_t()
229 {
230 }
231 };
232
233#if ETL_USING_CPP17
234 inline ETL_CONSTEXPR unexpect_t unexpect{};
235#else
236 static const unexpect_t unexpect;
237#endif
238
239 //*****************************************************************************
241 //*****************************************************************************
242 template <typename TValue, typename TError>
244 {
245 public:
246
247 typedef etl::expected<TValue, TError> this_type;
248 typedef TValue value_type;
249 typedef TError error_type;
250 typedef etl::unexpected<TError> unexpected_type;
251
252#if ETL_USING_CPP11
253 template <typename U>
254 using rebind = expected<U, TError>;
255#endif
256
257 //*******************************************
259 //*******************************************
260 ETL_CONSTEXPR14 expected() ETL_NOEXCEPT
261 : storage(etl::in_place_index_t<Value_Type>(), value_type())
262 {
263 }
264
265 //*******************************************
267 //*******************************************
268 ETL_CONSTEXPR14 expected(const value_type& value_) ETL_NOEXCEPT
269 : storage(etl::in_place_index_t<Value_Type>(), value_)
270 {
271 }
272
273#if ETL_USING_CPP11
274 //*******************************************
276 //*******************************************
277 ETL_CONSTEXPR14 expected(value_type&& value_) ETL_NOEXCEPT
278 : storage(etl::in_place_index_t<Value_Type>(), etl::move(value_))
279 {
280 }
281#endif
282
283 //*******************************************
285 //*******************************************
286 ETL_CONSTEXPR14 expected(const expected& other) ETL_NOEXCEPT
287 : storage(other.storage)
288 {
289 }
290
291#if ETL_USING_CPP11
292 //*******************************************
294 //*******************************************
295 ETL_CONSTEXPR14 expected(expected&& other) ETL_NOEXCEPT
296 : storage(etl::move(other.storage))
297 {
298 }
299#endif
300
301#if ETL_USING_CPP11
302 //*******************************************
304 //*******************************************
305 template <typename G, typename etl::enable_if<!etl::is_convertible<const G&, TError>::value, bool>::type = false>
306 ETL_CONSTEXPR14 explicit expected(const etl::unexpected<G>& ue)
307 : storage(etl::in_place_index_t<Error_Type>(), ue.error())
308 {
309 }
310
311 template <typename G, typename etl::enable_if<etl::is_convertible<const G&, TError>::value, bool>::type = false>
312 ETL_CONSTEXPR14 expected(const etl::unexpected<G>& ue)
313 : storage(etl::in_place_index_t<Error_Type>(), ue.error())
314 {
315 }
316#else
317 template <typename G>
318 explicit expected(const etl::unexpected<G>& ue)
319 : storage(etl::in_place_index_t<Error_Type>(), ue.error())
320 {
321 }
322#endif
323
324#if ETL_USING_CPP11
325 //*******************************************
327 //*******************************************
328 template <typename G, typename etl::enable_if<!etl::is_convertible<const G&, TError>::value, bool>::type = false>
329 ETL_CONSTEXPR14 explicit expected(etl::unexpected<G>&& ue)
330 : storage(etl::in_place_index_t<Error_Type>(), etl::move(ue.error()))
331 {
332 }
333
334 template <typename G, typename etl::enable_if<etl::is_convertible<const G&, TError>::value, bool>::type = false>
335 ETL_CONSTEXPR14 expected(etl::unexpected<G>&& ue)
336 : storage(etl::in_place_index_t<Error_Type>(), etl::move(ue.error()))
337 {
338 }
339#endif
340
341 //*******************************************
343 //*******************************************
344 ETL_CONSTEXPR14 explicit expected(etl::in_place_t) ETL_NOEXCEPT
345 : storage(value_type())
346 {
347 }
348
349#if ETL_USING_CPP11
350 //*******************************************
352 //*******************************************
353 template <typename... Args>
354 ETL_CONSTEXPR14 explicit expected(etl::in_place_t, Args&&... args)
355 : storage(etl::in_place_index_t<Value_Type>(), etl::forward<Args>(args)...)
356 {
357 }
358
359#if ETL_HAS_INITIALIZER_LIST
360 //*******************************************
362 //*******************************************
363 template <typename U, typename... Args>
364 ETL_CONSTEXPR14 explicit expected(etl::in_place_t, std::initializer_list<U> il, Args&&... args)
365 : storage(etl::in_place_index_t<Value_Type>(), il, etl::forward<Args>(args)...)
366 {
367 }
368#endif
369
370 //*******************************************
372 //*******************************************
373 template <typename... Args>
374 ETL_CONSTEXPR14 explicit expected(etl::unexpect_t, Args&&... args)
375 : storage(error_type(etl::forward<Args>(args)...))
376 {
377 }
378
379#if ETL_HAS_INITIALIZER_LIST
380 //*******************************************
382 //*******************************************
383 template <typename U, typename... Args>
384 ETL_CONSTEXPR14 explicit expected(etl::unexpect_t, std::initializer_list<U> il, Args&&... args)
385 : storage(error_type(il, etl::forward<Args>(args)...))
386 {
387 }
388#endif
389#endif
390
391 //*******************************************
393 //*******************************************
394 this_type& operator =(const this_type& other)
395 {
396 ETL_STATIC_ASSERT(etl::is_copy_constructible<TValue>::value && etl::is_copy_constructible<TError>::value, "Not copy assignable");
397
398 storage = other.storage;
399
400 return *this;
401 }
402
403#if ETL_USING_CPP11
404 //*******************************************
406 //*******************************************
407 this_type& operator =(this_type&& other)
408 {
409 ETL_STATIC_ASSERT(etl::is_move_constructible<TValue>::value && etl::is_move_constructible<TError>::value, "Not move assignable");
410
411 storage = etl::move(other.storage);
412
413 return *this;
414 }
415#endif
416
417 //*******************************************
419 //*******************************************
420 expected& operator =(const value_type& value)
421 {
422 ETL_STATIC_ASSERT(etl::is_copy_constructible<TValue>::value, "Value not copy assignable");
423
424 storage.template emplace<Value_Type>(value);
425
426 return *this;
427 }
428
429#if ETL_USING_CPP11
430 //*******************************************
432 //*******************************************
434 {
435 ETL_STATIC_ASSERT(etl::is_move_constructible<TValue>::value, "Value not move assignable");
436
437 storage.template emplace<Value_Type>(etl::move(value));
438
439 return *this;
440 }
441#endif
442
443 //*******************************************
445 //*******************************************
446 expected& operator =(const unexpected_type& ue)
447 {
448#if ETL_USING_CPP11
449 ETL_STATIC_ASSERT(etl::is_copy_constructible<TError>::value, "Error not copy assignable");
450#endif
451
452 storage.template emplace<Error_Type>(ue.error());
453
454 return *this;
455 }
456
457#if ETL_USING_CPP11
458 //*******************************************
460 //*******************************************
461 expected& operator =(unexpected_type&& ue)
462 {
463 ETL_STATIC_ASSERT(etl::is_move_constructible<TError>::value, "Error not move assignable");
464
465 storage.template emplace<Error_Type>(etl::move(ue.error()));
466
467 return *this;
468 }
469#endif
470
471#if ETL_USING_CPP11
472 //*******************************************
474 //*******************************************
475 ETL_CONSTEXPR14 value_type& value()&
476 {
477 return etl::get<Value_Type>(storage);
478 }
479
480 //*******************************************
482 //*******************************************
483 ETL_CONSTEXPR14 const value_type& value() const&
484 {
485 return etl::get<Value_Type>(storage);
486 }
487
488 //*******************************************
490 //*******************************************
491 ETL_CONSTEXPR14 value_type&& value()&&
492 {
493 return etl::move(etl::get<Value_Type>(storage));
494 }
495
496 //*******************************************
498 //*******************************************
499 ETL_CONSTEXPR14 const value_type&& value() const&&
500 {
501 return etl::move(etl::get<Value_Type>(storage));
502 }
503#else
504 //*******************************************
506 //*******************************************
507 const value_type& value() const
508 {
509 return etl::get<Value_Type>(storage);
510 }
511#endif
512
513 //*******************************************
515 //*******************************************
516 ETL_NODISCARD
517 ETL_CONSTEXPR14
518 bool has_value() const ETL_NOEXCEPT
519 {
520 return (storage.index() == Value_Type);
521 }
522
523 //*******************************************
525 //*******************************************
526 ETL_NODISCARD
527 ETL_CONSTEXPR14
528 ETL_EXPLICIT
529 operator bool() const ETL_NOEXCEPT
530 {
531 return has_value();
532 }
533
534#if ETL_USING_CPP11
535 //*******************************************
537 //*******************************************
538 template <typename U>
539 ETL_NODISCARD
540 ETL_CONSTEXPR14
541 etl::enable_if_t<etl::is_convertible<U, value_type>::value, value_type>
542 value_or(U&& default_value) const&
543 {
544 if (has_value())
545 {
546 return value();
547 }
548 else
549 {
550 return static_cast<value_type>(etl::forward<U>(default_value));
551 }
552 }
553
554 //*******************************************
556 //*******************************************
557 template <typename U>
558 ETL_NODISCARD
559 ETL_CONSTEXPR14
560 etl::enable_if_t<etl::is_convertible<U, value_type>::value, value_type>
561 value_or(U&& default_value)&&
562 {
563 if (has_value())
564 {
565 return etl::move(value());
566 }
567 else
568 {
569 return static_cast<value_type>(etl::forward<U>(default_value));
570 }
571 }
572
573 //*******************************************
575 //*******************************************
576 ETL_NODISCARD
577 ETL_CONSTEXPR14
578 error_type& error()& ETL_NOEXCEPT
579 {
580 return etl::get<Error_Type>(storage);
581 }
582
583 //*******************************************
585 //*******************************************
586 ETL_NODISCARD
587 ETL_CONSTEXPR14
588 const error_type& error() const& ETL_NOEXCEPT
589 {
590 return etl::get<Error_Type>(storage);
591 }
592
593 //*******************************************
595 //*******************************************
596 ETL_NODISCARD
597 ETL_CONSTEXPR14
598 error_type&& error()&& ETL_NOEXCEPT
599 {
600 return etl::move(etl::get<Error_Type>(storage));
601 }
602
603 //*******************************************
605 //*******************************************
606 ETL_NODISCARD
607 ETL_CONSTEXPR14
608 const error_type&& error() const&& ETL_NOEXCEPT
609 {
610 return etl::move(etl::get<Error_Type>(storage));
611 }
612
613
614 //*******************************************
616 //*******************************************
617 void swap(this_type& other)
618 {
619 using ETL_OR_STD::swap;
620
621 swap(storage, other.storage);
622 }
623
624 //*******************************************
626 //*******************************************
627 template <typename... Args>
628 ETL_CONSTEXPR14 value_type& emplace(Args&&... args) ETL_NOEXCEPT
629 {
630 storage.template emplace<value_type>(etl::forward<Args>(args)...);
631
632 return value();
633 }
634
635 //*******************************************
637 //*******************************************
638#if ETL_HAS_INITIALIZER_LIST
639 template <typename U, typename... Args>
640 ETL_CONSTEXPR14 value_type& emplace(std::initializer_list<U> il, Args&&... args) ETL_NOEXCEPT
641 {
642 storage.template emplace<value_type>(il, etl::forward<Args>(args)...);
643
644 return value();
645 }
646#endif
647#else
648 //*******************************************
650 //*******************************************
651 template <typename U>
652 value_type value_or(const U& default_value) const
653 {
654 if (has_value())
655 {
656 return value();
657 }
658 else
659 {
660 return default_value;
661 }
662 }
663
664 //*******************************************
666 //*******************************************
667 const error_type& error() const
668 {
669 return etl::get<Error_Type>(storage);
670 }
671#endif
672
673 //*******************************************
675 //*******************************************
676 value_type* operator ->()
677 {
678 ETL_ASSERT_OR_RETURN_VALUE(has_value(), ETL_ERROR(expected_invalid), ETL_NULLPTR);
679
680 return etl::addressof(etl::get<value_type>(storage));
681 }
682
683 //*******************************************
685 //*******************************************
686 const value_type* operator ->() const
687 {
688 ETL_ASSERT_OR_RETURN_VALUE(has_value(), ETL_ERROR(expected_invalid), ETL_NULLPTR);
689
690 return etl::addressof(etl::get<value_type>(storage));
691 }
692
693 //*******************************************
695 //*******************************************
696 value_type& operator *() ETL_LVALUE_REF_QUALIFIER
697 {
698 ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid));
699
700 return etl::get<value_type>(storage);
701 }
702
703 //*******************************************
705 //*******************************************
706 const value_type& operator *() const ETL_LVALUE_REF_QUALIFIER
707 {
708 ETL_ASSERT_OR_RETURN_VALUE(has_value(), ETL_ERROR(expected_invalid), ETL_NULLPTR);
709
710 return etl::get<value_type>(storage);
711 }
712
713#if ETL_USING_CPP11
714 //*******************************************
716 //*******************************************
717 value_type&& operator *()&&
718 {
719 ETL_ASSERT_OR_RETURN_VALUE(has_value(), ETL_ERROR(expected_invalid), ETL_NULLPTR);
720
721 return etl::move(etl::get<value_type>(storage));
722 }
723
724 //*******************************************
726 //*******************************************
727 const value_type&& operator *() const&&
728 {
729 ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid));
730
731 return etl::move(etl::get<value_type>(storage));
732 }
733#endif
734
735 private:
736
737 enum
738 {
739 Uninitialised,
740 Value_Type,
741 Error_Type
742 };
743
744 typedef etl::variant<etl::monostate, value_type, error_type> storage_type;
745 storage_type storage;
746 };
747
748 //*****************************************************************************
750 //*****************************************************************************
751 template<typename TError>
752 class expected<void, TError>
753 {
754 public:
755
756 typedef etl::expected<void, TError> this_type;
757 typedef void value_type;
758 typedef TError error_type;
759 typedef etl::unexpected<TError> unexpected_type;
760
761 //*******************************************
763 //*******************************************
764 ETL_CONSTEXPR14
766 {
767 }
768
769 //*******************************************
771 //*******************************************
772 ETL_CONSTEXPR14
773 expected(const unexpected_type& ue_)
774 : storage(ue_.error())
775 {
776 }
777
778#if ETL_USING_CPP11
779 //*******************************************
781 //*******************************************
782 ETL_CONSTEXPR14
783 expected(unexpected_type&& ue_)
784 : storage(etl::move(ue_.error()))
785 {
786 }
787#endif
788
789 //*******************************************
791 //*******************************************
792 ETL_CONSTEXPR14
793 expected(const this_type& other)
794 : storage(other.storage)
795 {
796 }
797
798#if ETL_USING_CPP11
799 //*******************************************
801 //*******************************************
802 ETL_CONSTEXPR14
803 expected(this_type&& other)
804 : storage(etl::move(other.storage))
805 {
806 }
807#endif
808
809 //*******************************************
811 //*******************************************
812 this_type& operator =(const this_type& other)
813 {
814 ETL_STATIC_ASSERT(etl::is_copy_constructible<TError>::value, "Not copy assignable");
815
816 storage = other.storage;
817 return *this;
818 }
819
820#if ETL_USING_CPP11
821 //*******************************************
823 //*******************************************
824 this_type& operator =(this_type&& other)
825 {
826 ETL_STATIC_ASSERT(etl::is_move_constructible<TError>::value, "Not move assignable");
827
828 storage = etl::move(other.storage);
829 return *this;
830 }
831#endif
832
833 //*******************************************
835 //*******************************************
836 expected& operator =(const unexpected_type& ue)
837 {
838#if ETL_USING_CPP11
839 ETL_STATIC_ASSERT(etl::is_copy_constructible<TError>::value, "Error not copy assignable");
840#endif
841
842 storage.template emplace<Error_Type>(ue.error());
843 return *this;
844 }
845
846#if ETL_USING_CPP11
847 //*******************************************
849 //*******************************************
850 expected& operator =(unexpected_type&& ue)
851 {
852 ETL_STATIC_ASSERT(etl::is_move_constructible<TError>::value, "Error not move assignable");
853
854 storage.template emplace<Error_Type>(etl::move(ue.error()));
855 return *this;
856 }
857#endif
858
859 //*******************************************
861 //*******************************************
862 ETL_NODISCARD
863 ETL_CONSTEXPR14
864 bool has_value() const ETL_NOEXCEPT
865 {
866 return (storage.index() != Error_Type);
867 }
868
869 //*******************************************
871 //*******************************************
872 ETL_NODISCARD
873 ETL_CONSTEXPR14
874 ETL_EXPLICIT
875 operator bool() const ETL_NOEXCEPT
876 {
877 return has_value();
878 }
879
880#if ETL_USING_CPP11
881 //*******************************************
884 //*******************************************
885 ETL_NODISCARD
886 ETL_CONSTEXPR14
887 error_type& error()& ETL_NOEXCEPT
888 {
889 return etl::get<Error_Type>(storage);
890 }
891
892 //*******************************************
895 //*******************************************
896 ETL_NODISCARD
897 ETL_CONSTEXPR14
898 const error_type& error() const& ETL_NOEXCEPT
899 {
900 return etl::get<Error_Type>(storage);
901 }
902
903 //*******************************************
906 //*******************************************
907 ETL_NODISCARD
908 ETL_CONSTEXPR14
909 error_type&& error() && ETL_NOEXCEPT
910 {
911 return etl::move(etl::get<Error_Type>(storage));
912 }
913
914 //*******************************************
917 //*******************************************
918 ETL_NODISCARD
919 ETL_CONSTEXPR14
920 const error_type&& error() const&& ETL_NOEXCEPT
921 {
922 return etl::move(etl::get<Error_Type>(storage));
923 }
924#else
925 //*******************************************
928 //*******************************************
929 const error_type& error() const
930 {
931 return etl::get<Error_Type>(storage);
932 }
933#endif
934
935 //*******************************************
937 //*******************************************
938 void swap(this_type& other)
939 {
940 using ETL_OR_STD::swap;
941
942 swap(storage, other.storage);
943 }
944
945 private:
946
947 enum
948 {
949 Uninitialised,
950 Error_Type
951 };
952
954 };
955}
956
957//*******************************************
959//*******************************************
960template <typename TValue, typename TError, typename TValue2, typename TError2>
961ETL_CONSTEXPR14
963{
964 if (lhs.has_value() != rhs.has_value())
965 {
966 return false;
967 }
968 if (lhs.has_value())
969 {
970 return lhs.value() == rhs.value();
971 }
972 return lhs.error() == rhs.error();
973}
974
975//*******************************************
976template <typename TValue, typename TError, typename TValue2>
977ETL_CONSTEXPR14
978bool operator ==(const etl::expected<TValue, TError>& lhs, const TValue2& rhs)
979{
980 if (!lhs.has_value())
981 {
982 return false;
983 }
984 return lhs.value() == rhs;
985}
986
987//*******************************************
988template <typename TValue, typename TError, typename TError2>
989ETL_CONSTEXPR14
991{
992 if (lhs.has_value())
993 {
994 return false;
995 }
996 return lhs.error() == rhs.error();
997}
998
999//*******************************************
1000template <typename TError, typename TError2>
1001ETL_CONSTEXPR14
1003{
1004 if (lhs.has_value() != rhs.has_value())
1005 {
1006 return false;
1007 }
1008 if (lhs.has_value())
1009 {
1010 return true;
1011 }
1012 return lhs.error() == rhs.error();
1013}
1014
1015//*******************************************
1016template <typename TError, typename TError2>
1017ETL_CONSTEXPR14
1019{
1020 if (lhs.has_value())
1021 {
1022 return false;
1023 }
1024 return lhs.error() == rhs.error();
1025}
1026
1027//*******************************************
1028template <typename TError, typename TError2>
1029ETL_CONSTEXPR14
1031{
1032 return lhs.error() == rhs.error();
1033}
1034
1035//*******************************************
1036template <typename TValue, typename TError, typename TValue2, typename TError2>
1037ETL_CONSTEXPR14
1038bool operator !=(const etl::expected<TValue, TError>& lhs, const etl::expected<TValue2, TError2>& rhs)
1039{
1040 return !(lhs == rhs);
1041}
1042
1043//*******************************************
1044template <typename TValue, typename TError, typename TValue2>
1045ETL_CONSTEXPR14
1046bool operator !=(const etl::expected<TValue, TError>& lhs, const TValue2& rhs)
1047{
1048 return !(lhs == rhs);
1049}
1050
1051//*******************************************
1052template <typename TValue, typename TError, typename TError2>
1053ETL_CONSTEXPR14
1054bool operator !=(const etl::expected<TValue, TError>& lhs, const etl::unexpected<TError2>& rhs)
1055{
1056 return !(lhs == rhs);
1057}
1058
1059//*******************************************
1060template <typename TError, typename TError2>
1061ETL_CONSTEXPR14
1062bool operator !=(const etl::expected<void, TError>& lhs, const etl::expected<void, TError2>& rhs)
1063{
1064 return !(lhs == rhs);
1065}
1066
1067//*******************************************
1068template <typename TError, typename TError2>
1069ETL_CONSTEXPR14
1070bool operator !=(const etl::expected<void, TError>& lhs, const etl::unexpected<TError2>& rhs)
1071{
1072 return !(lhs == rhs);
1073}
1074
1075//*******************************************
1076template <typename TError, typename TError2>
1077ETL_CONSTEXPR14
1078bool operator !=(const etl::unexpected<TError>& lhs, const etl::unexpected<TError2>& rhs)
1079{
1080 return !(lhs == rhs);
1081}
1082
1083//*******************************************
1085//*******************************************
1086template <typename TValue, typename TError>
1087ETL_CONSTEXPR14
1089{
1090 lhs.swap(rhs);
1091}
1092
1093//*******************************************
1095//*******************************************
1096template <typename TError>
1097ETL_CONSTEXPR14
1099{
1100 lhs.swap(rhs);
1101}
1102
1103#endif
ETL_CONSTEXPR14 expected()
Default constructor.
Definition expected.h:765
ETL_NODISCARD ETL_CONSTEXPR14 bool has_value() const ETL_NOEXCEPT
Returns true if expected has a value.
Definition expected.h:864
ETL_CONSTEXPR14 expected(const unexpected_type &ue_)
Copy construct from unexpected.
Definition expected.h:773
void swap(this_type &other)
Swap with another etl::expected.
Definition expected.h:938
ETL_CONSTEXPR14 expected(const this_type &other)
Copy construct.
Definition expected.h:793
const error_type & error() const
Definition expected.h:929
Base exception for et::expected.
Definition expected.h:49
Expected type.
Definition expected.h:244
ETL_CONSTEXPR14 expected() ETL_NOEXCEPT
Default constructor.
Definition expected.h:260
ETL_CONSTEXPR14 expected(const value_type &value_) ETL_NOEXCEPT
Constructor.
Definition expected.h:268
this_type & operator=(const this_type &other)
Copy assign from etl::expected.
Definition expected.h:394
ETL_CONSTEXPR14 expected(etl::in_place_t) ETL_NOEXCEPT
Construct with default value type.
Definition expected.h:344
ETL_CONSTEXPR14 expected(const expected &other) ETL_NOEXCEPT
Copy constructor.
Definition expected.h:286
const value_type & value() const
Get the value.
Definition expected.h:507
Definition expected.h:77
const TError & error() const
Get the error.
Definition expected.h:202
void swap(etl::unexpected< TError > &other)
Swap with another etl::unexpected.
Definition expected.h:211
ETL_CONSTEXPR unexpected(const unexpected &other)
Copy constructor.
Definition expected.h:85
ETL_CONSTEXPR unexpected(const TError &e)
Construct from an lvalue.
Definition expected.h:103
ETL_CONSTEXPR14 etl::unexpected< TError > & operator=(const etl::unexpected< TError > &rhs)
Assign from etl::unexpected.
Definition expected.h:142
ETL_CONSTEXPR14 bool operator==(const etl::expected< TValue, TError > &lhs, const etl::expected< TValue2, TError2 > &rhs)
Equivalence operators.
Definition expected.h:962
ETL_CONSTEXPR14 void swap(etl::expected< TValue, TError > &lhs, etl::expected< TValue, TError > &rhs)
Swap etl::expected.
Definition expected.h:1088
#define ETL_ASSERT(b, e)
Definition error_handler.h:356
ETL_CONSTEXPR exception(string_type reason_, string_type, numeric_type line_)
Constructor.
Definition exception.h:69
Definition exception.h:47
ETL_CONSTEXPR17 etl::enable_if<!etl::is_same< T, etl::nullptr_t >::value, T >::type * addressof(T &t)
Definition addressof.h:52
size_t index() const
Gets the index of the type currently stored or UNSUPPORTED_TYPE_ID.
Definition variant_legacy.h:734
Definition variant_legacy.h:146
bitset_ext
Definition absolute.h:39
ETL_CONSTEXPR14 void swap(etl::typed_storage_ext< T > &lhs, etl::typed_storage_ext< T > &rhs) ETL_NOEXCEPT
Swap two etl::typed_storage_ext.
Definition alignment.h:838
T & get(array< T, Size > &a)
Definition array.h:1216
Definition utility.h:638
in_place disambiguation tags.
Definition utility.h:617
unexpect_t
Definition expected.h:227