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 "error_handler.h"
38#include "exception.h"
39#include "initializer_list.h"
40#include "invoke.h"
41#include "memory.h"
42#include "type_traits.h"
43#include "utility.h"
44#include "variant.h"
45
46namespace etl
47{
48 // Forward declaration for is_expected
49 template <typename TValue, typename TError>
50 class expected;
51
52 template <typename T>
54 {
55 };
56
57 template <typename TValue, typename TError>
58 struct is_expected<expected<TValue, TError> > : etl::true_type
59 {
60 };
61
62 //***************************************************************************
64 //***************************************************************************
65 class expected_exception : public etl::exception
66 {
67 public:
68
69 expected_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
70 : exception(reason_, file_name_, line_number_)
71 {
72 }
73 };
74
75 //***************************************************************************
77 //***************************************************************************
78 class expected_invalid : public etl::expected_exception
79 {
80 public:
81
82 expected_invalid(string_type file_name_, numeric_type line_number_)
83 : expected_exception(ETL_ERROR_TEXT("expected:invalid", ETL_EXPECTED_FILE_ID"A"), file_name_, line_number_)
84 {
85 }
86 };
87
88 //***************************************************************************
91 //***************************************************************************
92 template <typename TError>
94 {
95 public:
96
97 typedef TError error_type;
98
99 //*******************************************
101 //*******************************************
102 ETL_CONSTEXPR unexpected(const unexpected& other)
103 : error_value(other.error_value)
104 {
105 }
106
107#if ETL_USING_CPP11
108 //*******************************************
110 //*******************************************
111 ETL_CONSTEXPR unexpected(unexpected&& other)
112 : error_value(etl::move(other.error_value))
113 {
114 }
115#endif
116
117 //*******************************************
119 //*******************************************
120 ETL_CONSTEXPR explicit unexpected(const TError& e)
121 : error_value(e)
122 {
123 }
124
125#if ETL_USING_CPP11
126 //*******************************************
128 //*******************************************
129 ETL_CONSTEXPR explicit unexpected(TError&& e)
130 : error_value(etl::forward<TError>(e))
131 {
132 }
133
134 //*******************************************
136 //*******************************************
137 template <typename... Args >
138 ETL_CONSTEXPR explicit unexpected(etl::in_place_t, Args&&... args)
139 : error_value(etl::forward<Args>(args)...)
140 {
141 }
142#endif
143
144#if ETL_HAS_INITIALIZER_LIST
145 //*******************************************
147 //*******************************************
148 template <typename U, typename... Args>
149 ETL_CONSTEXPR explicit unexpected(etl::in_place_t, std::initializer_list<U> init, Args&&... args)
150 : error_value(init, etl::forward<Args>(args)...)
151 {
152 }
153#endif
154
155 //*******************************************
157 //*******************************************
159 {
160#if ETL_USING_CPP11
161 ETL_STATIC_ASSERT(etl::is_copy_constructible<TError>::value, "Error not copy assignable");
162#endif
163
164 error_value = rhs.error_value;
165 return *this;
166 }
167
168#if ETL_USING_CPP11
169 //*******************************************
171 //*******************************************
173 {
174 ETL_STATIC_ASSERT(etl::is_move_constructible<TError>::value, "Error not move assignable");
175
176 error_value = etl::move(rhs.error_value);
177 return *this;
178 }
179#endif
180
181#if ETL_USING_CPP11
182 //*******************************************
184 //*******************************************
185 ETL_CONSTEXPR14 TError& error() & ETL_NOEXCEPT
186 {
187 return error_value;
188 }
189
190 //*******************************************
192 //*******************************************
193 ETL_CONSTEXPR14 const TError& error() const& ETL_NOEXCEPT
194 {
195 return error_value;
196 }
197
198 //*******************************************
200 //*******************************************
201 ETL_CONSTEXPR14 TError&& error() && ETL_NOEXCEPT
202 {
203 return etl::move(error_value);
204 }
205
206 //*******************************************
208 //*******************************************
209 ETL_CONSTEXPR14 const TError&& error() const&& ETL_NOEXCEPT
210 {
211 return etl::move(error_value);
212 }
213#else
214 //*******************************************
216 //*******************************************
217 const TError& error() const
218 {
219 return error_value;
220 }
221#endif
222
223 //*******************************************
225 //*******************************************
227 {
228 using ETL_OR_STD::swap;
229
230 swap(error_value, other.error_value);
231 }
232
233 private:
234
235 TError error_value;
236 };
237
238 //*****************************************************************************
240 //*****************************************************************************
241 struct unexpect_t
242 {
243 ETL_CONSTEXPR14 explicit unexpect_t() {}
244 };
245
246#if ETL_USING_CPP17
247 inline ETL_CONSTEXPR unexpect_t unexpect{};
248#else
249 static const unexpect_t unexpect;
250#endif
251
252 //*****************************************************************************
254 //*****************************************************************************
255 template <typename TValue, typename TError>
257 {
258 public:
259
260 typedef etl::expected<TValue, TError> this_type;
261 typedef TValue value_type;
262 typedef TError error_type;
263 typedef etl::unexpected<TError> unexpected_type;
264
265#if ETL_USING_CPP11
266 template <typename U>
267 using rebind = expected<U, TError>;
268#endif
269
270 //*******************************************
272 //*******************************************
273 ETL_CONSTEXPR14 expected() ETL_NOEXCEPT
274 : storage(etl::in_place_index_t<Value_Type>(), value_type())
275 {
276 }
277
278 //*******************************************
280 //*******************************************
281 ETL_CONSTEXPR14 expected(const value_type& value_) ETL_NOEXCEPT
282 : storage(etl::in_place_index_t<Value_Type>(), value_)
283 {
284 }
285
286#if ETL_USING_CPP11
287 //*******************************************
289 //*******************************************
290 ETL_CONSTEXPR14 expected(value_type&& value_) ETL_NOEXCEPT
291 : storage(etl::in_place_index_t<Value_Type>(), etl::move(value_))
292 {
293 }
294#endif
295
296 //*******************************************
298 //*******************************************
299 ETL_CONSTEXPR14 expected(const expected& other) ETL_NOEXCEPT
300 : storage(other.storage)
301 {
302 }
303
304#if ETL_USING_CPP11
305 //*******************************************
307 //*******************************************
308 ETL_CONSTEXPR14 expected(expected&& other) ETL_NOEXCEPT
309 : storage(etl::move(other.storage))
310 {
311 }
312#endif
313
314#if ETL_USING_CPP11
315 //*******************************************
317 //*******************************************
318 template <typename G, typename etl::enable_if< !etl::is_convertible<const G&, TError>::value, bool>::type = false>
319 ETL_CONSTEXPR14 explicit expected(const etl::unexpected<G>& ue)
320 : storage(etl::in_place_index_t<Error_Type>(), ue.error())
321 {
322 }
323
324 template <typename G, typename etl::enable_if< etl::is_convertible<const G&, TError>::value, bool>::type = false>
325 ETL_CONSTEXPR14 expected(const etl::unexpected<G>& ue)
326 : storage(etl::in_place_index_t<Error_Type>(), ue.error())
327 {
328 }
329#else
330 template <typename G>
331 explicit expected(const etl::unexpected<G>& ue)
332 : storage(etl::in_place_index_t<Error_Type>(), ue.error())
333 {
334 }
335#endif
336
337#if ETL_USING_CPP11
338 //*******************************************
340 //*******************************************
341 template <typename G, typename etl::enable_if< !etl::is_convertible<const G&, TError>::value, bool>::type = false>
342 ETL_CONSTEXPR14 explicit expected(etl::unexpected<G>&& ue)
343 : storage(etl::in_place_index_t<Error_Type>(), etl::move(ue.error()))
344 {
345 }
346
347 template <typename G, typename etl::enable_if< etl::is_convertible<const G&, TError>::value, bool>::type = false>
348 ETL_CONSTEXPR14 expected(etl::unexpected<G>&& ue)
349 : storage(etl::in_place_index_t<Error_Type>(), etl::move(ue.error()))
350 {
351 }
352#endif
353
354 //*******************************************
356 //*******************************************
357 ETL_CONSTEXPR14 explicit expected(etl::in_place_t) ETL_NOEXCEPT
358 : storage(value_type())
359 {
360 }
361
362#if ETL_USING_CPP11
363 //*******************************************
365 //*******************************************
366 template <typename... Args>
367 ETL_CONSTEXPR14 explicit expected(etl::in_place_t, Args&&... args)
368 : storage(etl::in_place_index_t<Value_Type>(), etl::forward<Args>(args)...)
369 {
370 }
371
372 #if ETL_HAS_INITIALIZER_LIST
373 //*******************************************
375 //*******************************************
376 template <typename U, typename... Args>
377 ETL_CONSTEXPR14 explicit expected(etl::in_place_t, std::initializer_list<U> il, Args&&... args)
378 : storage(etl::in_place_index_t<Value_Type>(), il, etl::forward<Args>(args)...)
379 {
380 }
381 #endif
382
383 //*******************************************
385 //*******************************************
386 template <typename... Args>
387 ETL_CONSTEXPR14 explicit expected(etl::unexpect_t, Args&&... args)
388 : storage(error_type(etl::forward<Args>(args)...))
389 {
390 }
391
392 #if ETL_HAS_INITIALIZER_LIST
393 //*******************************************
395 //*******************************************
396 template <typename U, typename... Args>
397 ETL_CONSTEXPR14 explicit expected(etl::unexpect_t, std::initializer_list<U> il, Args&&... args)
398 : storage(error_type(il, etl::forward<Args>(args)...))
399 {
400 }
401 #endif
402#endif
403
404 //*******************************************
406 //*******************************************
407 this_type& operator=(const this_type& other)
408 {
409 ETL_STATIC_ASSERT(etl::is_copy_constructible<TValue>::value && etl::is_copy_constructible<TError>::value, "Not copy assignable");
410
411 storage = other.storage;
412
413 return *this;
414 }
415
416#if ETL_USING_CPP11
417 //*******************************************
419 //*******************************************
420 this_type& operator=(this_type&& other)
421 {
422 ETL_STATIC_ASSERT(etl::is_move_constructible<TValue>::value && etl::is_move_constructible<TError>::value, "Not move assignable");
423
424 storage = etl::move(other.storage);
425
426 return *this;
427 }
428#endif
429
430 //*******************************************
432 //*******************************************
433 expected& operator=(const value_type& value)
434 {
435 ETL_STATIC_ASSERT(etl::is_copy_constructible<TValue>::value, "Value not copy assignable");
436
437 storage.template emplace<Value_Type>(value);
438
439 return *this;
440 }
441
442#if ETL_USING_CPP11
443 //*******************************************
445 //*******************************************
447 {
448 ETL_STATIC_ASSERT(etl::is_move_constructible<TValue>::value, "Value not move assignable");
449
450 storage.template emplace<Value_Type>(etl::move(value));
451
452 return *this;
453 }
454#endif
455
456 //*******************************************
458 //*******************************************
459 expected& operator=(const unexpected_type& ue)
460 {
461#if ETL_USING_CPP11
462 ETL_STATIC_ASSERT(etl::is_copy_constructible<TError>::value, "Error not copy assignable");
463#endif
464
465 storage.template emplace<Error_Type>(ue.error());
466
467 return *this;
468 }
469
470#if ETL_USING_CPP11
471 //*******************************************
473 //*******************************************
474 expected& operator=(unexpected_type&& ue)
475 {
476 ETL_STATIC_ASSERT(etl::is_move_constructible<TError>::value, "Error not move assignable");
477
478 storage.template emplace<Error_Type>(etl::move(ue.error()));
479
480 return *this;
481 }
482#endif
483
484#if ETL_USING_CPP11
485 //*******************************************
487 //*******************************************
488 ETL_CONSTEXPR14 value_type& value() &
489 {
490 return etl::get<Value_Type>(storage);
491 }
492
493 //*******************************************
495 //*******************************************
496 ETL_CONSTEXPR14 const value_type& value() const&
497 {
498 return etl::get<Value_Type>(storage);
499 }
500
501 //*******************************************
503 //*******************************************
504 ETL_CONSTEXPR14 value_type&& value() &&
505 {
506 return etl::move(etl::get<Value_Type>(storage));
507 }
508
509 //*******************************************
511 //*******************************************
512 ETL_CONSTEXPR14 const value_type&& value() const&&
513 {
514 return etl::move(etl::get<Value_Type>(storage));
515 }
516#else
517 //*******************************************
519 //*******************************************
520 const value_type& value() const
521 {
522 return etl::get<Value_Type>(storage);
523 }
524#endif
525
526 //*******************************************
528 //*******************************************
529 ETL_NODISCARD ETL_CONSTEXPR14 bool has_value() const ETL_NOEXCEPT
530 {
531 return (storage.index() == Value_Type);
532 }
533
534 //*******************************************
536 //*******************************************
537 ETL_NODISCARD ETL_CONSTEXPR14 ETL_EXPLICIT operator bool() const ETL_NOEXCEPT
538 {
539 return has_value();
540 }
541
542#if ETL_USING_CPP11
543 //*******************************************
545 //*******************************************
546 template <typename U>
547 ETL_NODISCARD ETL_CONSTEXPR14 etl::enable_if_t<etl::is_convertible<U, value_type>::value, value_type> value_or(U&& default_value) const&
548 {
549 if (has_value())
550 {
551 return value();
552 }
553 else
554 {
555 return static_cast<value_type>(etl::forward<U>(default_value));
556 }
557 }
558
559 //*******************************************
561 //*******************************************
562 template <typename U>
563 ETL_NODISCARD ETL_CONSTEXPR14 etl::enable_if_t<etl::is_convertible<U, value_type>::value, value_type> value_or(U&& default_value) &&
564 {
565 if (has_value())
566 {
567 return etl::move(value());
568 }
569 else
570 {
571 return static_cast<value_type>(etl::forward<U>(default_value));
572 }
573 }
574
575 //*******************************************
577 //*******************************************
578 ETL_NODISCARD ETL_CONSTEXPR14 error_type& error() & ETL_NOEXCEPT
579 {
580 return etl::get<Error_Type>(storage);
581 }
582
583 //*******************************************
585 //*******************************************
586 ETL_NODISCARD ETL_CONSTEXPR14 const error_type& error() const& ETL_NOEXCEPT
587 {
588 return etl::get<Error_Type>(storage);
589 }
590
591 //*******************************************
593 //*******************************************
594 ETL_NODISCARD ETL_CONSTEXPR14 error_type&& error() && ETL_NOEXCEPT
595 {
596 return etl::move(etl::get<Error_Type>(storage));
597 }
598
599 //*******************************************
601 //*******************************************
602 ETL_NODISCARD ETL_CONSTEXPR14 const error_type&& error() const&& ETL_NOEXCEPT
603 {
604 return etl::move(etl::get<Error_Type>(storage));
605 }
606
607 //*******************************************
609 //*******************************************
610 template <typename G>
611 ETL_NODISCARD ETL_CONSTEXPR14 etl::enable_if_t<etl::is_convertible<G, error_type>::value, error_type> error_or(G&& default_error) const&
612 {
613 if (has_value())
614 {
615 return static_cast<error_type>(etl::forward<G>(default_error));
616 }
617 else
618 {
619 return error();
620 }
621 }
622
623 //*******************************************
625 //*******************************************
626 template <typename G>
627 ETL_NODISCARD ETL_CONSTEXPR14 etl::enable_if_t<etl::is_convertible<G, error_type>::value, error_type> error_or(G&& default_error) &&
628 {
629 if (has_value())
630 {
631 return static_cast<error_type>(etl::forward<G>(default_error));
632 }
633 else
634 {
635 return etl::move(error());
636 }
637 }
638
639 //*******************************************
641 //*******************************************
642 void swap(this_type& other)
643 {
644 using ETL_OR_STD::swap;
645
646 swap(storage, other.storage);
647 }
648
649 //*******************************************
651 //*******************************************
652 template <typename... Args>
653 ETL_CONSTEXPR14 value_type& emplace(Args&&... args) ETL_NOEXCEPT
654 {
655 storage.template emplace<value_type>(etl::forward<Args>(args)...);
656
657 return value();
658 }
659
660 //*******************************************
662 //*******************************************
663 #if ETL_HAS_INITIALIZER_LIST
664 template <typename U, typename... Args>
665 ETL_CONSTEXPR14 value_type& emplace(std::initializer_list<U> il, Args&&... args) ETL_NOEXCEPT
666 {
667 storage.template emplace<value_type>(il, etl::forward<Args>(args)...);
668
669 return value();
670 }
671 #endif
672#else
673 //*******************************************
675 //*******************************************
676 template <typename U>
677 value_type value_or(const U& default_value) const
678 {
679 if (has_value())
680 {
681 return value();
682 }
683 else
684 {
685 return default_value;
686 }
687 }
688
689 //*******************************************
691 //*******************************************
692 const error_type& error() const
693 {
694 return etl::get<Error_Type>(storage);
695 }
696
697 //*******************************************
699 //*******************************************
700 template <typename G>
701 error_type error_or(const G& default_error) const
702 {
703 if (has_value())
704 {
705 return static_cast<error_type>(default_error);
706 }
707 else
708 {
709 return error();
710 }
711 }
712#endif
713
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::addressof(etl::get<value_type>(storage));
722 }
723
724 //*******************************************
726 //*******************************************
727 const value_type* operator->() const
728 {
729 ETL_ASSERT_OR_RETURN_VALUE(has_value(), ETL_ERROR(expected_invalid), ETL_NULLPTR);
730
731 return etl::addressof(etl::get<value_type>(storage));
732 }
733
734 //*******************************************
736 //*******************************************
737 value_type& operator*() ETL_LVALUE_REF_QUALIFIER
738 {
739 ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid));
740
741 return etl::get<value_type>(storage);
742 }
743
744 //*******************************************
746 //*******************************************
747 const value_type& operator*() const ETL_LVALUE_REF_QUALIFIER
748 {
749 ETL_ASSERT_OR_RETURN_VALUE(has_value(), ETL_ERROR(expected_invalid), ETL_NULLPTR);
750
751 return etl::get<value_type>(storage);
752 }
753
754#if ETL_USING_CPP11
755 //*******************************************
757 //*******************************************
758 value_type&& operator*() &&
759 {
760 ETL_ASSERT_OR_RETURN_VALUE(has_value(), ETL_ERROR(expected_invalid), ETL_NULLPTR);
761
762 return etl::move(etl::get<value_type>(storage));
763 }
764
765 //*******************************************
767 //*******************************************
768 const value_type&& operator*() const&&
769 {
770 ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid));
771
772 return etl::move(etl::get<value_type>(storage));
773 }
774#endif
775
776 //*******************************************
779 //*******************************************
780 ETL_NODISCARD ETL_CONSTEXPR14 value_type* begin() ETL_NOEXCEPT
781 {
782 return has_value() ? &etl::get<value_type>(storage) : ETL_NULLPTR;
783 }
784
785 //*******************************************
787 //*******************************************
788 ETL_NODISCARD ETL_CONSTEXPR14 value_type* end() ETL_NOEXCEPT
789 {
790 return has_value() ? &etl::get<value_type>(storage) + 1 : ETL_NULLPTR;
791 }
792
793 //*******************************************
795 //*******************************************
796 ETL_NODISCARD ETL_CONSTEXPR14 const value_type* begin() const ETL_NOEXCEPT
797 {
798 return has_value() ? &etl::get<value_type>(storage) : ETL_NULLPTR;
799 }
800
801 //*******************************************
803 //*******************************************
804 ETL_NODISCARD ETL_CONSTEXPR14 const value_type* end() const ETL_NOEXCEPT
805 {
806 return has_value() ? &etl::get<value_type>(storage) + 1 : ETL_NULLPTR;
807 }
808
809#if ETL_USING_CPP11
810 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, TValue&>::type>::type>
811 auto transform(F&& f) & -> expected<U, TError>
812 {
813 return transform_impl<F, this_type&, U, TValue&>(etl::forward<F>(f), *this);
814 }
815
816 template < typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, const TValue&>::type>::type>
817 auto transform(F&& f) const& -> expected<U, TError>
818 {
819 return transform_impl<F, const this_type&, U, const TValue&>(etl::forward<F>(f), *this);
820 }
821
822 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, TValue&&>::type>::type>
823 auto transform(F&& f) && -> expected<U, TError>
824 {
825 return transform_impl<F, this_type&&, U, TValue&&>(etl::forward<F>(f), etl::move(*this));
826 }
827
828 template < typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, const TValue&&>::type>::type>
829 auto transform(F&& f) const&& -> expected<U, TError>
830 {
831 return transform_impl<F, const this_type&&, U, const TValue&&>(etl::forward<F>(f), etl::move(*this));
832 }
833
834 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, TValue&>::type>::type>
835 auto and_then(F&& f) & -> U
836 {
837 return and_then_impl<F, this_type&, U, TValue&>(etl::forward<F>(f), *this);
838 }
839
840 template < typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, const TValue&>::type>::type>
841 auto and_then(F&& f) const& -> U
842 {
843 return and_then_impl<F, const this_type&, U, const TValue&>(etl::forward<F>(f), *this);
844 }
845
846 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, TValue&&>::type>::type>
847 auto and_then(F&& f) && -> U
848 {
849 return and_then_impl<F, this_type&&, U, TValue&&>(etl::forward<F>(f), etl::move(*this));
850 }
851
852 template < typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, const TValue&&>::type>::type>
853 auto and_then(F&& f) const&& -> U
854 {
855 return and_then_impl<F, const this_type&&, U, const TValue&&>(etl::forward<F>(f), etl::move(*this));
856 }
857
858 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, TError&>::type>::type>
859 auto or_else(F&& f) & -> U
860 {
861 return or_else_impl<F, this_type&, U, TError&>(etl::forward<F>(f), *this);
862 }
863
864 template < typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, const TError&>::type>::type>
865 auto or_else(F&& f) const& -> U
866 {
867 return or_else_impl<F, const this_type&, U, const TError&>(etl::forward<F>(f), *this);
868 }
869
870 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, TError&&>::type>::type>
871 auto or_else(F&& f) && -> U
872 {
873 return or_else_impl<F, this_type&&, U, TError&&>(etl::forward<F>(f), etl::move(*this));
874 }
875
876 template < typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, const TError&&>::type>::type>
877 auto or_else(F&& f) const&& -> U
878 {
879 return or_else_impl<F, const this_type&&, U, const TError&&>(etl::forward<F>(f), etl::move(*this));
880 }
881
882 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, TError&>::type>::type>
883 auto transform_error(F&& f) & -> expected<TValue, U>
884 {
885 return transform_error_impl<F, this_type&, U, TError&>(etl::forward<F>(f), *this);
886 }
887
888 template < typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, const TError&>::type>::type>
889 auto transform_error(F&& f) const& -> expected<TValue, U>
890 {
891 return transform_error_impl<F, const this_type&, U, const TError&>(etl::forward<F>(f), *this);
892 }
893
894 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, TError&&>::type>::type>
895 auto transform_error(F&& f) && -> expected<TValue, U>
896 {
897 return transform_error_impl<F, this_type&&, U, TError&&>(etl::forward<F>(f), etl::move(*this));
898 }
899
900 template < typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, const TError&&>::type>::type>
901 auto transform_error(F&& f) const&& -> expected<TValue, U>
902 {
903 return transform_error_impl<F, const this_type&&, U, const TError&&>(etl::forward<F>(f), etl::move(*this));
904 }
905#endif
906
907 private:
908
909 enum
910 {
911 Value_Type,
912 Error_Type
913 };
914
915 typedef etl::variant<value_type, error_type> storage_type;
916 storage_type storage;
917
918#if ETL_USING_CPP11
919 template < typename F, typename TExp, typename TRet, typename TValueRef, typename = typename etl::enable_if<!etl::is_void<TRet>::value>::type>
920 auto transform_impl(F&& f, TExp&& exp) const -> expected<TRet, TError>
921 {
922 if (exp.has_value())
923 {
924 return expected<TRet, TError>(etl::invoke(etl::forward<F>(f), etl::forward<TValueRef>(etl::get<Value_Type>(exp.storage))));
925 }
926 else
927 {
928 return expected<TRet, TError>(unexpected<TError>(etl::forward<TExp>(exp).error()));
929 }
930 }
931
932 template < typename F, typename TExp, typename TRet, typename TValueRef, typename = typename etl::enable_if<etl::is_void<TRet>::value>::type>
933 auto transform_impl(F&& f, TExp&& exp) const -> expected<void, TError>
934 {
935 if (exp.has_value())
936 {
937 etl::invoke(etl::forward<F>(f), etl::forward<TValueRef>(etl::get<Value_Type>(exp.storage)));
938 return expected<void, TError>();
939 }
940 else
941 {
942 return expected<void, TError>(unexpected<TError>(etl::forward<TExp>(exp).error()));
943 }
944 }
945
946 template < typename F, typename TExp, typename TRet, typename TValueRef,
947 typename = typename etl::enable_if< !etl::is_void<TRet>::value && etl::is_expected<TRet>::value
948 && etl::is_same<typename TRet::error_type, TError>::value>::type>
949 auto and_then_impl(F&& f, TExp&& exp) const -> TRet
950 {
951 if (exp.has_value())
952 {
953 return etl::invoke(etl::forward<F>(f), etl::forward<TValueRef>(etl::get<Value_Type>(exp.storage)));
954 }
955 else
956 {
957 return TRet(unexpected<TError>(etl::forward<TExp>(exp).error()));
958 }
959 }
960
961 template < typename F, typename TExp, typename TRet, typename TErrorRef,
962 typename = typename etl::enable_if< !etl::is_void<TRet>::value && etl::is_expected<TRet>::value
963 && etl::is_same<typename TRet::value_type, TValue>::value>::type>
964 auto or_else_impl(F&& f, TExp&& exp) const -> TRet
965 {
966 if (exp.has_value())
967 {
968 return TRet(etl::forward<TExp>(exp).value());
969 }
970 else
971 {
972 return etl::invoke(etl::forward<F>(f), etl::forward<TErrorRef>(etl::get<Error_Type>(exp.storage)));
973 }
974 }
975
976 template < typename F, typename TExp, typename TRet, typename TErrorRef, typename = typename etl::enable_if<!etl::is_void<TRet>::value>::type>
977 auto transform_error_impl(F&& f, TExp&& exp) const -> expected<TValue, TRet>
978 {
979 if (exp.has_value())
980 {
981 return expected<TValue, TRet>(etl::forward<TExp>(exp).value());
982 }
983 else
984 {
985 return expected<TValue, TRet>(unexpected<TRet>(etl::invoke(etl::forward<F>(f), etl::forward<TErrorRef>(etl::get<Error_Type>(exp.storage)))));
986 }
987 }
988#endif
989 };
990
991 //*****************************************************************************
993 //*****************************************************************************
994 template <typename TError>
995 class expected<void, TError>
996 {
997 public:
998
999 typedef etl::expected<void, TError> this_type;
1000 typedef void value_type;
1001 typedef TError error_type;
1002 typedef etl::unexpected<TError> unexpected_type;
1003
1004 //*******************************************
1006 //*******************************************
1007 ETL_CONSTEXPR14 expected()
1008 {
1009 }
1010
1011 //*******************************************
1013 //*******************************************
1014 ETL_CONSTEXPR14 expected(const unexpected_type& ue_)
1015 : storage(ue_.error())
1016 {
1017 }
1018
1019#if ETL_USING_CPP11
1020 //*******************************************
1022 //*******************************************
1023 ETL_CONSTEXPR14 expected(unexpected_type&& ue_)
1024 : storage(etl::move(ue_.error()))
1025 {
1026 }
1027#endif
1028
1029 //*******************************************
1031 //*******************************************
1032 ETL_CONSTEXPR14 expected(const this_type& other)
1033 : storage(other.storage)
1034 {
1035 }
1036
1037#if ETL_USING_CPP11
1038 //*******************************************
1040 //*******************************************
1041 ETL_CONSTEXPR14 expected(this_type&& other)
1042 : storage(etl::move(other.storage))
1043 {
1044 }
1045#endif
1046
1047 //*******************************************
1049 //*******************************************
1050 this_type& operator=(const this_type& other)
1051 {
1052 ETL_STATIC_ASSERT(etl::is_copy_constructible<TError>::value, "Not copy assignable");
1053
1054 storage = other.storage;
1055 return *this;
1056 }
1057
1058#if ETL_USING_CPP11
1059 //*******************************************
1061 //*******************************************
1062 this_type& operator=(this_type&& other)
1063 {
1064 ETL_STATIC_ASSERT(etl::is_move_constructible<TError>::value, "Not move assignable");
1065
1066 storage = etl::move(other.storage);
1067 return *this;
1068 }
1069#endif
1070
1071 //*******************************************
1073 //*******************************************
1074 expected& operator=(const unexpected_type& ue)
1075 {
1076#if ETL_USING_CPP11
1077 ETL_STATIC_ASSERT(etl::is_copy_constructible<TError>::value, "Error not copy assignable");
1078#endif
1079
1080 storage.template emplace<Error_Type>(ue.error());
1081 return *this;
1082 }
1083
1084#if ETL_USING_CPP11
1085 //*******************************************
1087 //*******************************************
1088 expected& operator=(unexpected_type&& ue)
1089 {
1090 ETL_STATIC_ASSERT(etl::is_move_constructible<TError>::value, "Error not move assignable");
1091
1092 storage.template emplace<Error_Type>(etl::move(ue.error()));
1093 return *this;
1094 }
1095#endif
1096
1097 //*******************************************
1099 //*******************************************
1100 ETL_NODISCARD ETL_CONSTEXPR14 bool has_value() const ETL_NOEXCEPT
1101 {
1102 return (storage.index() != Error_Type);
1103 }
1104
1105 //*******************************************
1107 //*******************************************
1108 ETL_NODISCARD ETL_CONSTEXPR14 ETL_EXPLICIT operator bool() const ETL_NOEXCEPT
1109 {
1110 return has_value();
1111 }
1112
1113#if ETL_USING_CPP11
1114 //*******************************************
1117 //*******************************************
1118 ETL_NODISCARD ETL_CONSTEXPR14 error_type& error() & ETL_NOEXCEPT
1119 {
1120 return etl::get<Error_Type>(storage);
1121 }
1122
1123 //*******************************************
1126 //*******************************************
1127 ETL_NODISCARD ETL_CONSTEXPR14 const error_type& error() const& ETL_NOEXCEPT
1128 {
1129 return etl::get<Error_Type>(storage);
1130 }
1131
1132 //*******************************************
1135 //*******************************************
1136 ETL_NODISCARD ETL_CONSTEXPR14 error_type&& error() && ETL_NOEXCEPT
1137 {
1138 return etl::move(etl::get<Error_Type>(storage));
1139 }
1140
1141 //*******************************************
1144 //*******************************************
1145 ETL_NODISCARD ETL_CONSTEXPR14 const error_type&& error() const&& ETL_NOEXCEPT
1146 {
1147 return etl::move(etl::get<Error_Type>(storage));
1148 }
1149
1150 //*******************************************
1152 //*******************************************
1153 template <typename G>
1154 ETL_NODISCARD ETL_CONSTEXPR14 etl::enable_if_t<etl::is_convertible<G, error_type>::value, error_type> error_or(G&& default_error) const&
1155 {
1156 if (has_value())
1157 {
1158 return static_cast<error_type>(etl::forward<G>(default_error));
1159 }
1160 else
1161 {
1162 return error();
1163 }
1164 }
1165
1166 //*******************************************
1168 //*******************************************
1169 template <typename G>
1170 ETL_NODISCARD ETL_CONSTEXPR14 etl::enable_if_t<etl::is_convertible<G, error_type>::value, error_type> error_or(G&& default_error) &&
1171 {
1172 if (has_value())
1173 {
1174 return static_cast<error_type>(etl::forward<G>(default_error));
1175 }
1176 else
1177 {
1178 return etl::move(error());
1179 }
1180 }
1181#else
1182 //*******************************************
1185 //*******************************************
1186 const error_type& error() const
1187 {
1188 return etl::get<Error_Type>(storage);
1189 }
1190
1191 //*******************************************
1193 //*******************************************
1194 template <typename G>
1195 error_type error_or(const G& default_error) const
1196 {
1197 if (has_value())
1198 {
1199 return static_cast<error_type>(default_error);
1200 }
1201 else
1202 {
1203 return error();
1204 }
1205 }
1206#endif
1207
1208 //*******************************************
1210 //*******************************************
1211 void swap(this_type& other)
1212 {
1213 using ETL_OR_STD::swap;
1214
1215 swap(storage, other.storage);
1216 }
1217
1218#if ETL_USING_CPP11
1219 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void>::type>::type>
1220 auto transform(F&& f) & -> expected<U, TError>
1221 {
1222 return transform_impl<F, this_type&, U>(etl::forward<F>(f), *this);
1223 }
1224
1225 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void>::type>::type>
1226 auto transform(F&& f) const& -> expected<U, TError>
1227 {
1228 return transform_impl<F, const this_type&, U>(etl::forward<F>(f), *this);
1229 }
1230
1231 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void>::type>::type>
1232 auto transform(F&& f) && -> expected<U, TError>
1233 {
1234 return transform_impl<F, this_type&&, U>(etl::forward<F>(f), etl::move(*this));
1235 }
1236
1237 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void>::type>::type>
1238 auto transform(F&& f) const&& -> expected<U, TError>
1239 {
1240 return transform_impl<F, const this_type&&, U>(etl::forward<F>(f), etl::move(*this));
1241 }
1242
1243 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void>::type>::type>
1244 auto and_then(F&& f) & -> U
1245 {
1246 return and_then_impl<F, this_type&, U>(etl::forward<F>(f), *this);
1247 }
1248
1249 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void>::type>::type>
1250 auto and_then(F&& f) const& -> U
1251 {
1252 return and_then_impl<F, const this_type&, U>(etl::forward<F>(f), *this);
1253 }
1254
1255 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void>::type>::type>
1256 auto and_then(F&& f) && -> U
1257 {
1258 return and_then_impl<F, this_type&&, U>(etl::forward<F>(f), etl::move(*this));
1259 }
1260
1261 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void>::type>::type>
1262 auto and_then(F&& f) const&& -> U
1263 {
1264 return and_then_impl<F, const this_type&&, U>(etl::forward<F>(f), etl::move(*this));
1265 }
1266
1267 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, TError&>::type>::type>
1268 auto or_else(F&& f) & -> U
1269 {
1270 return or_else_impl<F, this_type&, U, TError&>(etl::forward<F>(f), *this);
1271 }
1272
1273 template < typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, const TError&>::type>::type>
1274 auto or_else(F&& f) const& -> U
1275 {
1276 return or_else_impl<F, const this_type&, U, const TError&>(etl::forward<F>(f), *this);
1277 }
1278
1279 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, TError&&>::type>::type>
1280 auto or_else(F&& f) && -> U
1281 {
1282 return or_else_impl<F, this_type&&, U, TError&&>(etl::forward<F>(f), etl::move(*this));
1283 }
1284
1285 template < typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, const TError&&>::type>::type>
1286 auto or_else(F&& f) const&& -> U
1287 {
1288 return or_else_impl<F, const this_type&&, U, const TError&&>(etl::forward<F>(f), etl::move(*this));
1289 }
1290
1291 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, TError&>::type>::type>
1292 auto transform_error(F&& f) & -> expected<void, U>
1293 {
1294 return transform_error_impl<F, this_type&, U, TError&>(etl::forward<F>(f), *this);
1295 }
1296
1297 template < typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, const TError&>::type>::type>
1298 auto transform_error(F&& f) const& -> expected<void, U>
1299 {
1300 return transform_error_impl<F, const this_type&, U, const TError&>(etl::forward<F>(f), *this);
1301 }
1302
1303 template <typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, TError&&>::type>::type>
1304 auto transform_error(F&& f) && -> expected<void, U>
1305 {
1306 return transform_error_impl<F, this_type&&, U, TError&&>(etl::forward<F>(f), etl::move(*this));
1307 }
1308
1309 template < typename F, typename U = typename etl::remove_cvref< typename etl::invoke_result<F, void, const TError&&>::type>::type>
1310 auto transform_error(F&& f) const&& -> expected<void, U>
1311 {
1312 return transform_error_impl<F, const this_type&&, U, const TError&&>(etl::forward<F>(f), etl::move(*this));
1313 }
1314#endif
1315
1316 private:
1317
1318 enum
1319 {
1320 Void_Type,
1321 Error_Type
1322 };
1323
1324 etl::variant<etl::monostate, error_type> storage;
1325
1326#if ETL_USING_CPP11
1327 template < typename F, typename TExp, typename TRet, typename = typename etl::enable_if<!etl::is_void<TRet>::value>::type>
1328 auto transform_impl(F&& f, TExp&& exp) const -> expected<TRet, TError>
1329 {
1330 if (exp.has_value())
1331 {
1332 return expected<TRet, TError>(etl::invoke(etl::forward<F>(f)));
1333 }
1334 else
1335 {
1336 return expected<TRet, TError>(unexpected<TError>(etl::forward<TExp>(exp).error()));
1337 }
1338 }
1339
1340 template < typename F, typename TExp, typename TRet, typename = typename etl::enable_if<etl::is_void<TRet>::value>::type>
1341 auto transform_impl(F&& f, TExp&& exp) const -> expected<void, TError>
1342 {
1343 if (exp.has_value())
1344 {
1345 etl::invoke(etl::forward<F>(f));
1346 return expected<void, TError>();
1347 }
1348 else
1349 {
1350 return expected<void, TError>(unexpected<TError>(etl::forward<TExp>(exp).error()));
1351 }
1352 }
1353
1354 template < typename F, typename TExp, typename TRet,
1355 typename = typename etl::enable_if< !etl::is_void<TRet>::value && etl::is_expected<TRet>::value
1356 && etl::is_same<typename TRet::error_type, TError>::value>::type>
1357 auto and_then_impl(F&& f, TExp&& exp) const -> TRet
1358 {
1359 if (exp.has_value())
1360 {
1361 return etl::invoke(etl::forward<F>(f));
1362 }
1363 else
1364 {
1365 return TRet(unexpected<TError>(etl::forward<TExp>(exp).error()));
1366 }
1367 }
1368
1369 template <typename F, typename TExp, typename TRet, typename TErrorRef,
1370 typename = typename etl::enable_if< !etl::is_void<TRet>::value && etl::is_expected<TRet>::value
1371 && etl::is_same<typename TRet::value_type, void>::value>::type>
1372 auto or_else_impl(F&& f, TExp&& exp) const -> TRet
1373 {
1374 if (exp.has_value())
1375 {
1376 return TRet();
1377 }
1378 else
1379 {
1380 return etl::invoke(etl::forward<F>(f), etl::forward<TErrorRef>(etl::get<Error_Type>(exp.storage)));
1381 }
1382 }
1383
1384 template < typename F, typename TExp, typename TRet, typename TErrorRef, typename = typename etl::enable_if<!etl::is_void<TRet>::value>::type>
1385 auto transform_error_impl(F&& f, TExp&& exp) const -> expected<void, TRet>
1386 {
1387 if (exp.has_value())
1388 {
1389 return expected<void, TRet>();
1390 }
1391 else
1392 {
1393 return expected<void, TRet>(unexpected<TRet>(etl::invoke(etl::forward<F>(f), etl::forward<TErrorRef>(etl::get<Error_Type>(exp.storage)))));
1394 }
1395 }
1396#endif
1397 };
1398
1399 //*******************************************
1401 //*******************************************
1402 template <typename TValue, typename TError, typename TValue2, typename TError2>
1404 {
1405 if (lhs.has_value() != rhs.has_value())
1406 {
1407 return false;
1408 }
1409 if (lhs.has_value())
1410 {
1411 return lhs.value() == rhs.value();
1412 }
1413 return lhs.error() == rhs.error();
1414 }
1415
1416 //*******************************************
1417 template <typename TValue, typename TError, typename TValue2>
1418 ETL_CONSTEXPR14 bool operator==(const etl::expected<TValue, TError>& lhs, const TValue2& rhs)
1419 {
1420 if (!lhs.has_value())
1421 {
1422 return false;
1423 }
1424 return lhs.value() == rhs;
1425 }
1426
1427 //*******************************************
1428 template <typename TValue, typename TError, typename TError2>
1429 ETL_CONSTEXPR14 bool operator==(const etl::expected<TValue, TError>& lhs, const etl::unexpected<TError2>& rhs)
1430 {
1431 if (lhs.has_value())
1432 {
1433 return false;
1434 }
1435 return lhs.error() == rhs.error();
1436 }
1437
1438 //*******************************************
1439 template <typename TError, typename TError2>
1440 ETL_CONSTEXPR14 bool operator==(const etl::expected<void, TError>& lhs, const etl::expected<void, TError2>& rhs)
1441 {
1442 if (lhs.has_value() != rhs.has_value())
1443 {
1444 return false;
1445 }
1446 if (lhs.has_value())
1447 {
1448 return true;
1449 }
1450 return lhs.error() == rhs.error();
1451 }
1452
1453 //*******************************************
1454 template <typename TError, typename TError2>
1455 ETL_CONSTEXPR14 bool operator==(const etl::expected<void, TError>& lhs, const etl::unexpected<TError2>& rhs)
1456 {
1457 if (lhs.has_value())
1458 {
1459 return false;
1460 }
1461 return lhs.error() == rhs.error();
1462 }
1463
1464 //*******************************************
1465 template <typename TError, typename TError2>
1466 ETL_CONSTEXPR14 bool operator==(const etl::unexpected<TError>& lhs, const etl::unexpected<TError2>& rhs)
1467 {
1468 return lhs.error() == rhs.error();
1469 }
1470
1471 //*******************************************
1472 template <typename TValue, typename TError, typename TValue2, typename TError2>
1473 ETL_CONSTEXPR14 bool operator!=(const etl::expected<TValue, TError>& lhs, const etl::expected<TValue2, TError2>& rhs)
1474 {
1475 return !(lhs == rhs);
1476 }
1477
1478 //*******************************************
1479 template <typename TValue, typename TError, typename TValue2>
1480 ETL_CONSTEXPR14 bool operator!=(const etl::expected<TValue, TError>& lhs, const TValue2& rhs)
1481 {
1482 return !(lhs == rhs);
1483 }
1484
1485 //*******************************************
1486 template <typename TValue, typename TError, typename TError2>
1487 ETL_CONSTEXPR14 bool operator!=(const etl::expected<TValue, TError>& lhs, const etl::unexpected<TError2>& rhs)
1488 {
1489 return !(lhs == rhs);
1490 }
1491
1492 //*******************************************
1493 template <typename TError, typename TError2>
1494 ETL_CONSTEXPR14 bool operator!=(const etl::expected<void, TError>& lhs, const etl::expected<void, TError2>& rhs)
1495 {
1496 return !(lhs == rhs);
1497 }
1498
1499 //*******************************************
1500 template <typename TError, typename TError2>
1501 ETL_CONSTEXPR14 bool operator!=(const etl::expected<void, TError>& lhs, const etl::unexpected<TError2>& rhs)
1502 {
1503 return !(lhs == rhs);
1504 }
1505
1506 //*******************************************
1507 template <typename TError, typename TError2>
1508 ETL_CONSTEXPR14 bool operator!=(const etl::unexpected<TError>& lhs, const etl::unexpected<TError2>& rhs)
1509 {
1510 return !(lhs == rhs);
1511 }
1512
1513 //*******************************************
1515 //*******************************************
1516 template <typename TValue, typename TError>
1518 {
1519 lhs.swap(rhs);
1520 }
1521
1522 //*******************************************
1524 //*******************************************
1525 template <typename TError>
1527 {
1528 lhs.swap(rhs);
1529 }
1530
1531} // namespace etl
1532
1533#endif
expected & operator=(const unexpected_type &ue)
Copy assign from unexpected.
Definition expected.h:1074
error_type error_or(const G &default_error) const
Get the error or a default value.
Definition expected.h:1195
ETL_CONSTEXPR14 expected()
Default constructor.
Definition expected.h:1007
ETL_NODISCARD ETL_CONSTEXPR14 bool has_value() const ETL_NOEXCEPT
Returns true if expected has a value.
Definition expected.h:1100
ETL_CONSTEXPR14 expected(const unexpected_type &ue_)
Copy construct from unexpected.
Definition expected.h:1014
void swap(this_type &other)
Swap with another etl::expected.
Definition expected.h:1211
ETL_CONSTEXPR14 expected(const this_type &other)
Copy construct.
Definition expected.h:1032
const error_type & error() const
Definition expected.h:1186
this_type & operator=(const this_type &other)
Copy assign.
Definition expected.h:1050
Base exception for et::expected.
Definition expected.h:66
expected_invalid
Definition expected.h:79
Expected type.
Definition expected.h:257
ETL_NODISCARD ETL_CONSTEXPR14 const value_type * end() const ETL_NOEXCEPT
Returns a const pointer past the value if has_value(), otherwise returns nullptr.
Definition expected.h:804
ETL_CONSTEXPR14 expected() ETL_NOEXCEPT
Default constructor.
Definition expected.h:273
ETL_CONSTEXPR14 expected(const value_type &value_) ETL_NOEXCEPT
Constructor.
Definition expected.h:281
this_type & operator=(const this_type &other)
Copy assign from etl::expected.
Definition expected.h:407
ETL_NODISCARD ETL_CONSTEXPR14 value_type * begin() ETL_NOEXCEPT
Definition expected.h:780
expected & operator=(const value_type &value)
Copy assign from value.
Definition expected.h:433
ETL_NODISCARD ETL_CONSTEXPR14 value_type * end() ETL_NOEXCEPT
Returns a pointer past the value if has_value(), otherwise returns nullptr.
Definition expected.h:788
ETL_CONSTEXPR14 expected(etl::in_place_t) ETL_NOEXCEPT
Construct with default value type.
Definition expected.h:357
ETL_NODISCARD ETL_CONSTEXPR14 const value_type * begin() const ETL_NOEXCEPT
Returns a const pointer to the value if has_value(), otherwise returns nullptr.
Definition expected.h:796
ETL_CONSTEXPR14 expected(const expected &other) ETL_NOEXCEPT
Copy constructor.
Definition expected.h:299
expected & operator=(const unexpected_type &ue)
Copy assign from unexpected.
Definition expected.h:459
error_type error_or(const G &default_error) const
Get the error or a default value.
Definition expected.h:701
const value_type & value() const
Get the value.
Definition expected.h:520
Definition expected.h:94
const TError & error() const
Get the error.
Definition expected.h:217
void swap(etl::unexpected< TError > &other)
Swap with another etl::unexpected.
Definition expected.h:226
ETL_CONSTEXPR unexpected(const unexpected &other)
Copy constructor.
Definition expected.h:102
ETL_CONSTEXPR unexpected(const TError &e)
Construct from an lvalue.
Definition expected.h:120
ETL_CONSTEXPR14 etl::unexpected< TError > & operator=(const etl::unexpected< TError > &rhs)
Assign from etl::unexpected.
Definition expected.h:158
#define ETL_ASSERT(b, e)
Definition error_handler.h:511
ETL_EXCEPTION_CONSTEXPR exception(string_type reason_, string_type, numeric_type)
Constructor.
Definition exception.h:81
Definition exception.h:59
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:718
Definition absolute.h:40
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:856
ETL_CONSTEXPR14 bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1078
integral_constant< bool, false > false_type
integral_constant specialisations
Definition type_traits.h:80
ETL_CONSTEXPR14 bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1090
T & get(array< T, Size > &a)
Definition array.h:1158
Definition utility.h:964
in_place disambiguation tags.
Definition utility.h:941
Definition expected.h:54
unexpect_t
Definition expected.h:242