1 #ifndef BOOST_LEAF_HPP_INCLUDED
2 #define BOOST_LEAF_HPP_INCLUDED
4 // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc.
6 // Distributed under the Boost Software License, Version 1.0. (See accompanying
7 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 // >>> #include <boost/leaf/capture.hpp>
10 #line 1 "boost/leaf/capture.hpp"
11 #ifndef BOOST_LEAF_CAPTURE_HPP_INCLUDED
12 #define BOOST_LEAF_CAPTURE_HPP_INCLUDED
14 // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc.
16 // Distributed under the Boost Software License, Version 1.0. (See accompanying
17 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
19 #ifndef BOOST_LEAF_ENABLE_WARNINGS
20 # if defined(__clang__)
21 # pragma clang system_header
22 # elif (__GNUC__*100+__GNUC_MINOR__>301)
23 # pragma GCC system_header
24 # elif defined(_MSC_VER)
25 # pragma warning(push,1)
29 // >>> #include <boost/leaf/exception.hpp>
30 #line 1 "boost/leaf/exception.hpp"
31 #ifndef BOOST_LEAF_EXCEPTION_HPP_INCLUDED
32 #define BOOST_LEAF_EXCEPTION_HPP_INCLUDED
34 // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc.
36 // Distributed under the Boost Software License, Version 1.0. (See accompanying
37 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
39 #ifndef BOOST_LEAF_ENABLE_WARNINGS
40 # if defined(__clang__)
41 # pragma clang system_header
42 # elif (__GNUC__*100+__GNUC_MINOR__>301)
43 # pragma GCC system_header
44 # elif defined(_MSC_VER)
45 # pragma warning(push,1)
49 // >>> #include <boost/leaf/error.hpp>
50 #line 1 "boost/leaf/error.hpp"
51 #ifndef BOOST_LEAF_ERROR_HPP_INCLUDED
52 #define BOOST_LEAF_ERROR_HPP_INCLUDED
54 // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc.
56 // Distributed under the Boost Software License, Version 1.0. (See accompanying
57 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
59 #ifndef BOOST_LEAF_ENABLE_WARNINGS
60 # if defined(__clang__)
61 # pragma clang system_header
62 # elif (__GNUC__*100+__GNUC_MINOR__>301)
63 # pragma GCC system_header
64 # elif defined(_MSC_VER)
65 # pragma warning(push,1)
69 // >>> #include <boost/leaf/detail/function_traits.hpp>
70 #line 1 "boost/leaf/detail/function_traits.hpp"
71 #ifndef BOOST_LEAF_DETAIL_FUNCTION_TRAITS_HPP_INCLUDED
72 #define BOOST_LEAF_DETAIL_FUNCTION_TRAITS_HPP_INCLUDED
74 // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc.
76 // Distributed under the Boost Software License, Version 1.0. (See accompanying
77 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
79 #ifndef BOOST_LEAF_ENABLE_WARNINGS
80 # if defined(__clang__)
81 # pragma clang system_header
82 # elif (__GNUC__*100+__GNUC_MINOR__>301)
83 # pragma GCC system_header
84 # elif defined(_MSC_VER)
85 # pragma warning(push,1)
89 // >>> #include <boost/leaf/detail/mp11.hpp>
90 #line 1 "boost/leaf/detail/mp11.hpp"
91 #ifndef BOOST_LEAF_DETAIL_MP11_HPP_INCLUDED
92 #define BOOST_LEAF_DETAIL_MP11_HPP_INCLUDED
94 // Copyright 2015-2017 Peter Dimov.
95 // Copyright 2019 Emil Dotchevski.
97 // Distributed under the Boost Software License, Version 1.0.
99 // See accompanying file LICENSE_1_0.txt or copy at
100 // http://www.boost.org/LICENSE_1_0.txt
102 #include <type_traits>
105 namespace boost { namespace leaf { namespace leaf_detail_mp11 {
108 template<class... T> struct mp_list
113 template<class T> struct mp_identity
119 template<class... T> struct mp_inherit: T... {};
125 template<bool C, class T, class... E> struct mp_if_c_impl
129 template<class T, class... E> struct mp_if_c_impl<true, T, E...>
134 template<class T, class E> struct mp_if_c_impl<false, T, E>
139 } // namespace detail
141 template<bool C, class T, class... E> using mp_if_c = typename detail::mp_if_c_impl<C, T, E...>::type;
142 template<class C, class T, class... E> using mp_if = typename detail::mp_if_c_impl<static_cast<bool>(C::value), T, E...>::type;
145 template<bool B> using mp_bool = std::integral_constant<bool, B>;
147 using mp_true = mp_bool<true>;
148 using mp_false = mp_bool<false>;
151 template<class T> using mp_to_bool = mp_bool<static_cast<bool>( T::value )>;
154 template<class T> using mp_not = mp_bool< !T::value >;
157 template<int I> using mp_int = std::integral_constant<int, I>;
160 template<std::size_t N> using mp_size_t = std::integral_constant<std::size_t, N>;
162 // mp_set_contains<S, V>
166 template<class S, class V> struct mp_set_contains_impl;
168 template<template<class...> class L, class... T, class V> struct mp_set_contains_impl<L<T...>, V>
170 using type = mp_to_bool<std::is_base_of<mp_identity<V>, mp_inherit<mp_identity<T>...> > >;
173 } // namespace detail
175 template<class S, class V> using mp_set_contains = typename detail::mp_set_contains_impl<S, V>::type;
177 // mp_set_push_back<S, T...>
181 template<class S, class... T> struct mp_set_push_back_impl;
183 template<template<class...> class L, class... U> struct mp_set_push_back_impl<L<U...>>
185 using type = L<U...>;
188 template<template<class...> class L, class... U, class T1, class... T> struct mp_set_push_back_impl<L<U...>, T1, T...>
190 using S = mp_if<mp_set_contains<L<U...>, T1>, L<U...>, L<U..., T1>>;
191 using type = typename mp_set_push_back_impl<S, T...>::type;
194 } // namespace detail
196 template<class S, class... T> using mp_set_push_back = typename detail::mp_set_push_back_impl<S, T...>::type;
202 template<class L> struct mp_unique_impl;
204 template<template<class...> class L, class... T> struct mp_unique_impl<L<T...>>
206 using type = mp_set_push_back<L<>, T...>;
209 } // namespace detail
211 template<class L> using mp_unique = typename detail::mp_unique_impl<L>::type;
218 template<class... L> struct mp_append_impl;
220 template<> struct mp_append_impl<>
222 using type = mp_list<>;
225 template<template<class...> class L, class... T> struct mp_append_impl<L<T...>>
227 using type = L<T...>;
230 template<template<class...> class L1, class... T1, template<class...> class L2, class... T2, class... Lr> struct mp_append_impl<L1<T1...>, L2<T2...>, Lr...>
232 using type = typename mp_append_impl<L1<T1..., T2...>, Lr...>::type;
237 template<class... L> using mp_append = typename detail::mp_append_impl<L...>::type;
243 template<class L> struct mp_front_impl
245 // An error "no type named 'type'" here means that the argument to mp_front
246 // is either not a list, or is an empty list
249 template<template<class...> class L, class T1, class... T> struct mp_front_impl<L<T1, T...>>
254 } // namespace detail
256 template<class L> using mp_front = typename detail::mp_front_impl<L>::type;
262 template<class L> struct mp_pop_front_impl
264 // An error "no type named 'type'" here means that the argument to mp_pop_front
265 // is either not a list, or is an empty list
268 template<template<class...> class L, class T1, class... T> struct mp_pop_front_impl<L<T1, T...>>
270 using type = L<T...>;
273 } // namespace detail
275 template<class L> using mp_pop_front = typename detail::mp_pop_front_impl<L>::type;
278 template<class L> using mp_first = mp_front<L>;
281 template<class L> using mp_rest = mp_pop_front<L>;
283 // mp_remove_if<L, P>
287 template<class L, template<class...> class P> struct mp_remove_if_impl;
289 template<template<class...> class L, class... T, template<class...> class P> struct mp_remove_if_impl<L<T...>, P>
291 template<class U> using _f = mp_if<P<U>, mp_list<>, mp_list<U>>;
292 using type = mp_append<L<>, _f<T>...>;
295 } // namespace detail
297 template<class L, template<class...> class P> using mp_remove_if = typename detail::mp_remove_if_impl<L, P>::type;
300 template<class T, T... I> struct integer_sequence
304 // detail::make_integer_sequence_impl
309 template<bool C, class T, class E> struct iseq_if_c_impl;
311 template<class T, class E> struct iseq_if_c_impl<true, T, E>
316 template<class T, class E> struct iseq_if_c_impl<false, T, E>
321 template<bool C, class T, class E> using iseq_if_c = typename iseq_if_c_impl<C, T, E>::type;
324 template<class T> struct iseq_identity
329 template<class S1, class S2> struct append_integer_sequence;
331 template<class T, T... I, T... J> struct append_integer_sequence<integer_sequence<T, I...>, integer_sequence<T, J...>>
333 using type = integer_sequence< T, I..., ( J + sizeof...(I) )... >;
336 template<class T, T N> struct make_integer_sequence_impl;
338 template<class T, T N> struct make_integer_sequence_impl_
342 static_assert( N >= 0, "make_integer_sequence<T, N>: N must not be negative" );
344 static T const M = N / 2;
345 static T const R = N % 2;
347 using S1 = typename make_integer_sequence_impl<T, M>::type;
348 using S2 = typename append_integer_sequence<S1, S1>::type;
349 using S3 = typename make_integer_sequence_impl<T, R>::type;
350 using S4 = typename append_integer_sequence<S2, S3>::type;
357 template<class T, T N> struct make_integer_sequence_impl: iseq_if_c<N == 0, iseq_identity<integer_sequence<T>>, iseq_if_c<N == 1, iseq_identity<integer_sequence<T, 0>>, make_integer_sequence_impl_<T, N> > >
361 } // namespace detail
363 // make_integer_sequence
364 template<class T, T N> using make_integer_sequence = typename detail::make_integer_sequence_impl<T, N>::type;
367 template<std::size_t... I> using index_sequence = integer_sequence<std::size_t, I...>;
369 // make_index_sequence
370 template<std::size_t N> using make_index_sequence = make_integer_sequence<std::size_t, N>;
372 // index_sequence_for
373 template<class... T> using index_sequence_for = make_integer_sequence<std::size_t, sizeof...(T)>;
375 // implementation by Bruno Dutra (by the name is_evaluable)
379 template<template<class...> class F, class... T> struct mp_valid_impl
381 template<template<class...> class G, class = G<T...>> static mp_true check(int);
382 template<template<class...> class> static mp_false check(...);
384 using type = decltype(check<F>(0));
387 } // namespace detail
389 template<template<class...> class F, class... T> using mp_valid = typename detail::mp_valid_impl<F, T...>::type;
394 // <<< #include <boost/leaf/detail/mp11.hpp>
395 #line 20 "boost/leaf/detail/function_traits.hpp"
398 namespace boost { namespace leaf {
400 namespace leaf_detail
403 struct gcc49_workaround //Thanks Glen Fernandes
409 using void_t = typename gcc49_workaround<T...>::type;
411 template<class F,class V=void>
412 struct function_traits
414 constexpr static int arity = -1;
418 struct function_traits<F, void_t<decltype(&F::operator())>>
422 using tr = function_traits<decltype(&F::operator())>;
426 using return_type = typename tr::return_type;
427 static constexpr int arity = tr::arity - 1;
429 using mp_args = typename leaf_detail_mp11::mp_rest<typename tr::mp_args>;
433 tr::template arg<I+1>
438 template<class R, class... A>
439 struct function_traits<R(A...)>
441 using return_type = R;
442 static constexpr int arity = sizeof...(A);
444 using mp_args = leaf_detail_mp11::mp_list<A...>;
449 static_assert(I < arity, "I out of range");
450 using type = typename std::tuple_element<I,std::tuple<A...>>::type;
454 template<class F> struct function_traits<F&> : function_traits<F> { };
455 template<class F> struct function_traits<F&&> : function_traits<F> { };
456 template<class R, class... A> struct function_traits<R(*)(A...)> : function_traits<R(A...)> { };
457 template<class R, class... A> struct function_traits<R(* &)(A...)> : function_traits<R(A...)> { };
458 template<class R, class... A> struct function_traits<R(* const &)(A...)> : function_traits<R(A...)> { };
459 template<class C, class R, class... A> struct function_traits<R(C::*)(A...)> : function_traits<R(C&,A...)> { };
460 template<class C, class R, class... A> struct function_traits<R(C::*)(A...) const> : function_traits<R(C const &,A...)> { };
461 template<class C, class R> struct function_traits<R(C::*)> : function_traits<R(C&)> { };
464 using fn_return_type = typename function_traits<F>::return_type;
466 template <class F, int I>
467 using fn_arg_type = typename function_traits<F>::template arg<I>::type;
470 using fn_mp_args = typename function_traits<F>::mp_args;
477 // <<< #include <boost/leaf/detail/function_traits.hpp>
478 #line 20 "boost/leaf/error.hpp"
479 // >>> #include <boost/leaf/detail/print.hpp>
480 #line 1 "boost/leaf/detail/print.hpp"
481 #ifndef BOOST_LEAF_DETAIL_PRINT_HPP_INCLUDED
482 #define BOOST_LEAF_DETAIL_PRINT_HPP_INCLUDED
484 // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc.
486 // Distributed under the Boost Software License, Version 1.0. (See accompanying
487 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
489 #ifndef BOOST_LEAF_ENABLE_WARNINGS
490 # if defined(__clang__)
491 # pragma clang system_header
492 # elif (__GNUC__*100+__GNUC_MINOR__>301)
493 # pragma GCC system_header
494 # elif defined(_MSC_VER)
495 # pragma warning(push,1)
499 // >>> #include <boost/leaf/detail/optional.hpp>
500 #line 1 "boost/leaf/detail/optional.hpp"
501 #ifndef BOOST_LEAF_DETAIL_OPTIONAL_HPP_INCLUDED
502 #define BOOST_LEAF_DETAIL_OPTIONAL_HPP_INCLUDED
504 // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc.
506 // Distributed under the Boost Software License, Version 1.0. (See accompanying
507 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
509 #ifndef BOOST_LEAF_ENABLE_WARNINGS
510 # if defined(__clang__)
511 # pragma clang system_header
512 # elif (__GNUC__*100+__GNUC_MINOR__>301)
513 # pragma GCC system_header
514 # elif defined(_MSC_VER)
515 # pragma warning(push,1)
519 // >>> #include <boost/leaf/detail/config.hpp>
520 #line 1 "boost/leaf/detail/config.hpp"
521 #ifndef BOOST_LEAF_CONFIG_HPP_INCLUDED
522 #define BOOST_LEAF_CONFIG_HPP_INCLUDED
524 // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc.
526 // Distributed under the Boost Software License, Version 1.0. (See accompanying
527 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
529 // The following is based on Boost Config.
531 // (C) Copyright John Maddock 2001 - 2003.
532 // (C) Copyright Martin Wille 2003.
533 // (C) Copyright Guillaume Melquiond 2003.
535 #ifndef BOOST_LEAF_ENABLE_WARNINGS
536 # if defined(__clang__)
537 # pragma clang system_header
538 # elif (__GNUC__*100+__GNUC_MINOR__>301)
539 # pragma GCC system_header
540 # elif defined(_MSC_VER)
541 # pragma warning(push,1)
545 ////////////////////////////////////////
547 // Configure BOOST_LEAF_NO_EXCEPTIONS, unless already #defined
548 #ifndef BOOST_LEAF_NO_EXCEPTIONS
550 # if defined(__clang__) && !defined(__ibmxl__)
551 // Clang C++ emulates GCC, so it has to appear early.
553 # if !__has_feature(cxx_exceptions)
554 # define BOOST_LEAF_NO_EXCEPTIONS
557 # elif defined(__DMC__)
560 # if !defined(_CPPUNWIND)
561 # define BOOST_LEAF_NO_EXCEPTIONS
564 # elif defined(__GNUC__) && !defined(__ibmxl__)
567 # if !defined(__EXCEPTIONS)
568 # define BOOST_LEAF_NO_EXCEPTIONS
571 # elif defined(__KCC)
574 # if !defined(_EXCEPTIONS)
575 # define BOOST_LEAF_NO_EXCEPTIONS
578 # elif defined(__CODEGEARC__)
579 // CodeGear - must be checked for before Borland
581 # if !defined(_CPPUNWIND) && !defined(__EXCEPTIONS)
582 # define BOOST_LEAF_NO_EXCEPTIONS
585 # elif defined(__BORLANDC__)
588 # if !defined(_CPPUNWIND) && !defined(__EXCEPTIONS)
589 # define BOOST_LEAF_NO_EXCEPTIONS
592 # elif defined(__MWERKS__)
593 // Metrowerks CodeWarrior
595 # if !__option(exceptions)
596 # define BOOST_LEAF_NO_EXCEPTIONS
599 # elif defined(__IBMCPP__) && defined(__COMPILER_VER__) && defined(__MVS__)
602 # if !defined(_CPPUNWIND) && !defined(__EXCEPTIONS)
603 # define BOOST_LEAF_NO_EXCEPTIONS
606 # elif defined(__ibmxl__)
607 // IBM XL C/C++ for Linux (Little Endian)
609 # if !__has_feature(cxx_exceptions)
610 # define BOOST_LEAF_NO_EXCEPTIONS
613 # elif defined(_MSC_VER)
614 // Microsoft Visual C++
616 // Must remain the last #elif since some other vendors (Metrowerks, for
617 // example) also #define _MSC_VER
619 # if !defined(_CPPUNWIND)
620 # define BOOST_LEAF_NO_EXCEPTIONS
626 #ifdef BOOST_NORETURN
627 # define BOOST_LEAF_NORETURN BOOST_NORETURN
629 # if defined(_MSC_VER)
630 # define BOOST_LEAF_NORETURN __declspec(noreturn)
631 # elif defined(__GNUC__)
632 # define BOOST_LEAF_NORETURN __attribute__ ((__noreturn__))
633 # elif defined(__has_attribute) && defined(__SUNPRO_CC) && (__SUNPRO_CC > 0x5130)
634 # if __has_attribute(noreturn)
635 # define BOOST_LEAF_NORETURN [[noreturn]]
637 # elif defined(__has_cpp_attribute)
638 # if __has_cpp_attribute(noreturn)
639 # define BOOST_LEAF_NORETURN [[noreturn]]
643 #if !defined(BOOST_LEAF_NORETURN)
644 # define BOOST_LEAF_NORETURN
647 ////////////////////////////////////////
649 #ifndef BOOST_LEAF_DIAGNOSTICS
650 # define BOOST_LEAF_DIAGNOSTICS 1
653 #if BOOST_LEAF_DIAGNOSTICS!=0 && BOOST_LEAF_DIAGNOSTICS!=1
654 # error BOOST_LEAF_DIAGNOSTICS must be 0 or 1.
657 ////////////////////////////////////////
660 # define BOOST_LEAF_ALWAYS_INLINE __forceinline
662 # define BOOST_LEAF_ALWAYS_INLINE __attribute__((always_inline)) inline
665 ////////////////////////////////////////
667 #ifndef BOOST_LEAF_NODISCARD
668 # if __cplusplus >= 201703L
669 # define BOOST_LEAF_NODISCARD [[nodiscard]]
671 # define BOOST_LEAF_NODISCARD
675 ////////////////////////////////////////
677 #ifndef BOOST_LEAF_CONSTEXPR
678 # if __cplusplus > 201402L
679 # define BOOST_LEAF_CONSTEXPR constexpr
681 # define BOOST_LEAF_CONSTEXPR
685 ////////////////////////////////////////
687 #ifndef BOOST_LEAF_ASSERT
689 # define BOOST_LEAF_ASSERT BOOST_ASSERT
692 # define BOOST_LEAF_ASSERT assert
696 ////////////////////////////////////////
698 #ifndef BOOST_LEAF_NO_EXCEPTIONS
699 # include <exception>
700 # if (defined(__cpp_lib_uncaught_exceptions) && __cpp_lib_uncaught_exceptions >= 201411L) || (defined(_MSC_VER) && _MSC_VER >= 1900)
701 # define BOOST_LEAF_STD_UNCAUGHT_EXCEPTIONS 1
703 # define BOOST_LEAF_STD_UNCAUGHT_EXCEPTIONS 0
708 // <<< #include <boost/leaf/detail/config.hpp>
709 #line 20 "boost/leaf/detail/optional.hpp"
713 namespace boost { namespace leaf {
715 namespace leaf_detail
725 typedef T value_type;
727 BOOST_LEAF_CONSTEXPR optional() noexcept:
732 BOOST_LEAF_CONSTEXPR optional( optional const & x ):
736 (void) new (&value_) T( x.value_ );
739 BOOST_LEAF_CONSTEXPR optional( optional && x ) noexcept:
744 (void) new (&value_) T( std::move(x.value_) );
749 BOOST_LEAF_CONSTEXPR optional( int key, T const & v ):
753 BOOST_LEAF_ASSERT(!empty());
756 BOOST_LEAF_CONSTEXPR optional( int key, T && v ) noexcept:
760 BOOST_LEAF_ASSERT(!empty());
763 BOOST_LEAF_CONSTEXPR optional & operator=( optional const & x )
766 if( int key = x.key() )
774 BOOST_LEAF_CONSTEXPR optional & operator=( optional && x ) noexcept
777 if( int key = x.key() )
779 put(key, std::move(x.value_));
790 BOOST_LEAF_CONSTEXPR bool empty() const noexcept
795 BOOST_LEAF_CONSTEXPR int key() const noexcept
800 BOOST_LEAF_CONSTEXPR void reset() noexcept
809 BOOST_LEAF_CONSTEXPR T & put( int key, T const & v )
811 BOOST_LEAF_ASSERT(key);
813 (void) new(&value_) T(v);
818 BOOST_LEAF_CONSTEXPR T & put( int key, T && v ) noexcept
820 BOOST_LEAF_ASSERT(key);
822 (void) new(&value_) T(std::move(v));
827 BOOST_LEAF_CONSTEXPR T const * has_value(int key) const noexcept
829 BOOST_LEAF_ASSERT(key);
830 return key_==key ? &value_ : 0;
833 BOOST_LEAF_CONSTEXPR T * has_value(int key) noexcept
835 BOOST_LEAF_ASSERT(key);
836 return key_==key ? &value_ : 0;
839 BOOST_LEAF_CONSTEXPR T const & value(int key) const & noexcept
841 BOOST_LEAF_ASSERT(has_value(key) != 0);
845 BOOST_LEAF_CONSTEXPR T & value(int key) & noexcept
847 BOOST_LEAF_ASSERT(has_value(key) != 0);
851 BOOST_LEAF_CONSTEXPR T const && value(int key) const && noexcept
853 BOOST_LEAF_ASSERT(has_value(key) != 0);
857 BOOST_LEAF_CONSTEXPR T value(int key) && noexcept
859 BOOST_LEAF_ASSERT(has_value(key) != 0);
860 T tmp(std::move(value_));
871 // <<< #include <boost/leaf/detail/optional.hpp>
872 #line 20 "boost/leaf/detail/print.hpp"
876 namespace boost { namespace leaf {
878 namespace leaf_detail
881 BOOST_LEAF_CONSTEXPR inline char const * check_prefix( char const * t, char const (&prefix)[N] )
883 return std::strncmp(t,prefix,sizeof(prefix)-1)==0 ? t+sizeof(prefix)-1 : t;
887 template <class Name>
888 inline char const * type()
890 using leaf_detail::check_prefix;
897 #if defined(__clang__)
898 BOOST_LEAF_ASSERT(check_prefix(t,"const char *boost::leaf::type() ")==t+32);
900 #elif defined(__GNUC__)
901 BOOST_LEAF_ASSERT(check_prefix(t,"const char* boost::leaf::type() ")==t+32);
904 char const * clang_style = check_prefix(t,"const char *boost::leaf::type() ");
907 char const * gcc_style = check_prefix(t,"const char* boost::leaf::type() ");
914 namespace leaf_detail
916 template <class T, class E = void>
917 struct is_printable: std::false_type
922 struct is_printable<T, decltype(std::declval<std::ostream&>()<<std::declval<T const &>(), void())>: std::true_type
926 ////////////////////////////////////////
928 template <class T, class E = void>
929 struct has_printable_member_value: std::false_type
934 struct has_printable_member_value<T, decltype(std::declval<std::ostream&>()<<std::declval<T const &>().value, void())>: std::true_type
938 ////////////////////////////////////////
940 template <class Wrapper, bool WrapperPrintable=is_printable<Wrapper>::value, bool ValuePrintable=has_printable_member_value<Wrapper>::value>
943 template <class Wrapper, bool ValuePrintable>
944 struct diagnostic<Wrapper, true, ValuePrintable>
946 static constexpr bool is_invisible = false;
947 static void print( std::ostream & os, Wrapper const & x )
953 template <class Wrapper>
954 struct diagnostic<Wrapper, false, true>
956 static constexpr bool is_invisible = false;
957 static void print( std::ostream & os, Wrapper const & x )
959 os << type<Wrapper>() << ": " << x.value;
963 template <class Wrapper>
964 struct diagnostic<Wrapper, false, false>
966 static constexpr bool is_invisible = false;
967 static void print( std::ostream & os, Wrapper const & )
969 os << type<Wrapper>() << ": {Non-Printable}";
973 #ifndef BOOST_LEAF_NO_EXCEPTIONS
975 struct diagnostic<std::exception_ptr, false, false>
977 static constexpr bool is_invisible = true;
978 BOOST_LEAF_CONSTEXPR static void print( std::ostream &, std::exception_ptr const & )
988 // <<< #include <boost/leaf/detail/print.hpp>
989 #line 21 "boost/leaf/error.hpp"
990 #include <system_error>
991 #include <type_traits>
995 #if BOOST_LEAF_DIAGNOSTICS
1000 #define BOOST_LEAF_TOKEN_PASTE(x, y) x ## y
1001 #define BOOST_LEAF_TOKEN_PASTE2(x, y) BOOST_LEAF_TOKEN_PASTE(x, y)
1002 #define BOOST_LEAF_TMP BOOST_LEAF_TOKEN_PASTE2(boost_leaf_tmp_, __LINE__)
1004 #define BOOST_LEAF_ASSIGN(v,r)\
1005 static_assert(::boost::leaf::is_result_type<typename std::decay<decltype(r)>::type>::value, "The BOOST_LEAF_ASSIGN macro requires a result type as the second argument");\
1006 auto && BOOST_LEAF_TMP = r;\
1007 if( !BOOST_LEAF_TMP )\
1008 return BOOST_LEAF_TMP.error();\
1009 v = std::forward<decltype(BOOST_LEAF_TMP)>(BOOST_LEAF_TMP).value()
1011 #define BOOST_LEAF_AUTO(v, r)\
1012 BOOST_LEAF_ASSIGN(auto v, r)
1014 #define BOOST_LEAF_CHECK(r)\
1016 static_assert(::boost::leaf::is_result_type<typename std::decay<decltype(r)>::type>::value, "BOOST_LEAF_CHECK requires a result type");\
1017 auto && BOOST_LEAF_TMP = r;\
1018 if( !BOOST_LEAF_TMP )\
1019 return BOOST_LEAF_TMP.error();\
1022 #define BOOST_LEAF_NEW_ERROR ::leaf::leaf_detail::inject_loc{__FILE__,__LINE__,__FUNCTION__}+::boost::leaf::new_error
1024 namespace boost { namespace leaf {
1026 namespace leaf_detail
1030 char const * const file;
1032 char const * const fn;
1035 friend T operator+( inject_loc loc, T && x ) noexcept
1037 x.load_source_location_(loc.file, loc.line, loc.fn);
1038 return std::move(x);
1045 ////////////////////////////////////////
1047 #ifdef BOOST_LEAF_NO_EXCEPTIONS
1051 BOOST_LEAF_NORETURN void throw_exception( std::exception const & ); // user defined
1054 namespace boost { namespace leaf {
1057 BOOST_LEAF_NORETURN void throw_exception( T const & e )
1059 ::boost::throw_exception(e);
1066 namespace boost { namespace leaf {
1069 BOOST_LEAF_NORETURN void throw_exception( T const & e )
1078 ////////////////////////////////////////
1080 #ifdef BOOST_LEAF_NO_THREADS
1082 # define BOOST_LEAF_THREAD_LOCAL
1083 namespace boost { namespace leaf {
1084 namespace leaf_detail
1086 using atomic_unsigned_int = unsigned int;
1094 # define BOOST_LEAF_THREAD_LOCAL thread_local
1095 namespace boost { namespace leaf {
1096 namespace leaf_detail
1098 using atomic_unsigned_int = std::atomic<unsigned int>;
1104 ////////////////////////////////////////
1106 namespace boost { namespace leaf {
1108 #if BOOST_LEAF_DIAGNOSTICS
1110 namespace leaf_detail
1112 class e_unexpected_count
1116 char const * (*first_type)();
1119 BOOST_LEAF_CONSTEXPR explicit e_unexpected_count(char const * (*first_type)()) noexcept:
1120 first_type(first_type),
1125 template <class CharT, class Traits>
1126 void print( std::basic_ostream<CharT, Traits> & os ) const
1128 BOOST_LEAF_ASSERT(first_type != 0);
1129 BOOST_LEAF_ASSERT(count>0);
1132 os << "1 attempt to communicate an unexpected error object";
1134 os << count << " attempts to communicate unexpected error objects, the first one";
1135 (os << " of type " << first_type() << '\n').flush();
1140 struct diagnostic<e_unexpected_count, false, false>
1142 static constexpr bool is_invisible = true;
1143 BOOST_LEAF_CONSTEXPR static void print(std::ostream &, e_unexpected_count const &) noexcept { }
1146 class e_unexpected_info
1149 std::set<char const *(*)()> already_;
1153 e_unexpected_info() noexcept
1160 if( !diagnostic<E>::is_invisible && already_.insert(&type<E>).second )
1162 std::stringstream s;
1163 diagnostic<E>::print(s,e);
1164 (s << '\n').flush();
1169 template <class CharT, class Traits>
1170 void print( std::basic_ostream<CharT, Traits> & os ) const
1172 os << "Unhandled error objects:\n" << s_;
1177 struct diagnostic<e_unexpected_info, false, false>
1179 static constexpr bool is_invisible = true;
1180 BOOST_LEAF_CONSTEXPR static void print(std::ostream &, e_unexpected_info const &) noexcept { }
1183 template <class=void>
1184 struct tl_unexpected_enabled
1186 static BOOST_LEAF_THREAD_LOCAL int counter;
1190 BOOST_LEAF_THREAD_LOCAL int tl_unexpected_enabled<T>::counter;
1197 ////////////////////////////////////////
1199 namespace boost { namespace leaf {
1201 struct e_source_location
1203 char const * const file;
1205 char const * const function;
1207 template <class CharT, class Traits>
1208 friend std::basic_ostream<CharT, Traits> & operator<<( std::basic_ostream<CharT, Traits> & os, e_source_location const & x )
1210 return os << leaf::type<e_source_location>() << ": " << x.file << '(' << x.line << ") in function " << x.function;
1214 ////////////////////////////////////////
1216 namespace leaf_detail
1224 static BOOST_LEAF_THREAD_LOCAL slot<E> * p;
1228 BOOST_LEAF_THREAD_LOCAL slot<E> * tl_slot_ptr<E>::p;
1234 slot( slot const & ) = delete;
1235 slot & operator=( slot const & ) = delete;
1237 using impl = optional<E>;
1243 BOOST_LEAF_CONSTEXPR slot() noexcept:
1248 BOOST_LEAF_CONSTEXPR slot( slot && x ) noexcept:
1249 optional<E>(std::move(x)),
1252 BOOST_LEAF_ASSERT(x.top_==0);
1255 BOOST_LEAF_CONSTEXPR void activate() noexcept
1257 BOOST_LEAF_ASSERT(top_==0 || *top_!=this);
1258 top_ = &tl_slot_ptr<E>::p;
1263 BOOST_LEAF_CONSTEXPR void deactivate() noexcept
1265 BOOST_LEAF_ASSERT(top_!=0 && *top_==this);
1269 BOOST_LEAF_CONSTEXPR void propagate() noexcept;
1271 template <class CharT, class Traits>
1272 void print( std::basic_ostream<CharT, Traits> & os, int key_to_print ) const
1274 if( !diagnostic<E>::is_invisible )
1275 if( int k = this->key() )
1279 if( key_to_print!=k )
1283 os << '[' << k << ']';
1284 diagnostic<E>::print(os, value(k));
1285 (os << '\n').flush();
1290 using impl::has_value;
1294 #if BOOST_LEAF_DIAGNOSTICS
1297 BOOST_LEAF_CONSTEXPR inline void load_unexpected_count( int err_id ) noexcept
1299 if( slot<e_unexpected_count> * sl = tl_slot_ptr<e_unexpected_count>::p )
1300 if( e_unexpected_count * unx = sl->has_value(err_id) )
1303 sl->put(err_id, e_unexpected_count(&type<E>));
1307 BOOST_LEAF_CONSTEXPR inline void load_unexpected_info( int err_id, E && e ) noexcept
1309 if( slot<e_unexpected_info> * sl = tl_slot_ptr<e_unexpected_info>::p )
1310 if( e_unexpected_info * unx = sl->has_value(err_id) )
1311 unx->add(std::forward<E>(e));
1313 sl->put(err_id, e_unexpected_info()).add(std::forward<E>(e));
1317 BOOST_LEAF_CONSTEXPR inline void load_unexpected( int err_id, E && e ) noexcept
1319 load_unexpected_count<E>(err_id);
1320 load_unexpected_info(err_id, std::forward<E>(e));
1326 BOOST_LEAF_CONSTEXPR inline void slot<E>::propagate() noexcept
1328 BOOST_LEAF_ASSERT(top_!=0 && (*top_==prev_ || *top_==this));
1331 impl & that_ = *prev_;
1334 impl & this_ = *this;
1335 that_ = std::move(this_);
1338 #if BOOST_LEAF_DIAGNOSTICS
1341 int c = tl_unexpected_enabled<>::counter;
1342 BOOST_LEAF_ASSERT(c>=0);
1344 if( int err_id = impl::key() )
1345 load_unexpected(err_id, std::move(*this).value(err_id));
1351 BOOST_LEAF_CONSTEXPR inline int load_slot( int err_id, E && e ) noexcept
1353 static_assert(!std::is_pointer<E>::value, "Error objects of pointer types are not allowed");
1354 using T = typename std::decay<E>::type;
1355 BOOST_LEAF_ASSERT((err_id&3)==1);
1356 if( slot<T> * p = tl_slot_ptr<T>::p )
1357 (void) p->put(err_id, std::forward<E>(e));
1358 #if BOOST_LEAF_DIAGNOSTICS
1361 int c = tl_unexpected_enabled<>::counter;
1362 BOOST_LEAF_ASSERT(c>=0);
1364 load_unexpected(err_id, std::forward<E>(e));
1371 BOOST_LEAF_CONSTEXPR inline int accumulate_slot( int err_id, F && f ) noexcept
1373 static_assert(function_traits<F>::arity==1, "Lambdas passed to accumulate must take a single e-type argument by reference");
1374 using E = typename std::decay<fn_arg_type<F,0>>::type;
1375 static_assert(!std::is_pointer<E>::value, "Error objects of pointer types are not allowed");
1376 BOOST_LEAF_ASSERT((err_id&3)==1);
1377 if( auto sl = tl_slot_ptr<E>::p )
1378 if( auto v = sl->has_value(err_id) )
1379 (void) std::forward<F>(f)(*v);
1381 (void) std::forward<F>(f)(sl->put(err_id,E()));
1386 ////////////////////////////////////////
1388 namespace leaf_detail
1390 template <class=void>
1393 static atomic_unsigned_int counter;
1394 static BOOST_LEAF_THREAD_LOCAL unsigned current_id;
1396 BOOST_LEAF_CONSTEXPR static unsigned generate_next_id() noexcept
1398 auto id = (counter+=4);
1399 BOOST_LEAF_ASSERT((id&3)==1);
1405 atomic_unsigned_int id_factory<T>::counter(-3);
1408 BOOST_LEAF_THREAD_LOCAL unsigned id_factory<T>::current_id(0);
1410 inline int current_id() noexcept
1412 auto id = id_factory<>::current_id;
1413 BOOST_LEAF_ASSERT(id==0 || (id&3)==1);
1417 inline int new_id() noexcept
1419 auto id = id_factory<>::generate_next_id();
1420 return id_factory<>::current_id = id;
1424 ////////////////////////////////////////
1426 namespace leaf_detail
1428 template <class T, int Arity = function_traits<T>::arity>
1431 static_assert(Arity==0 || Arity==1, "If a functions is passed to new_error or load, it must take zero or one argument");
1435 struct load_item<E, -1>
1437 BOOST_LEAF_CONSTEXPR static int load( int err_id, E && e ) noexcept
1439 return load_slot(err_id, std::forward<E>(e));
1444 struct load_item<F, 0>
1446 BOOST_LEAF_CONSTEXPR static int load( int err_id, F && f ) noexcept
1448 return load_slot(err_id, std::forward<F>(f)());
1453 struct load_item<F, 1>
1455 BOOST_LEAF_CONSTEXPR static int load( int err_id, F && f ) noexcept
1457 return accumulate_slot(err_id, std::forward<F>(f));
1462 ////////////////////////////////////////
1464 namespace leaf_detail
1466 class leaf_category final: public std::error_category
1468 bool equivalent( int, std::error_condition const & ) const noexcept final override { return false; }
1469 bool equivalent( std::error_code const &, int ) const noexcept final override { return false; }
1470 char const * name() const noexcept final override { return "LEAF error"; }
1471 std::string message( int condition ) const final override { return name(); }
1473 ~leaf_category() noexcept final override { }
1476 template <class=void>
1477 struct get_error_category
1479 static leaf_category cat;
1483 leaf_category get_error_category<T>::cat;
1485 inline int import_error_code( std::error_code const & ec ) noexcept
1487 if( int err_id = ec.value() )
1489 std::error_category const & cat = get_error_category<>::cat;
1490 if( &ec.category()==&cat )
1492 BOOST_LEAF_ASSERT((err_id&3)==1);
1493 return (err_id&~3)|1;
1498 (void) load_slot(err_id, ec);
1499 return (err_id&~3)|1;
1507 inline bool is_error_id( std::error_code const & ec ) noexcept
1509 bool res = (&ec.category() == &leaf_detail::get_error_category<>::cat);
1510 BOOST_LEAF_ASSERT(!res || !ec.value() || ((ec.value()&3)==1));
1514 ////////////////////////////////////////
1518 namespace leaf_detail
1520 BOOST_LEAF_CONSTEXPR error_id make_error_id(int) noexcept;
1525 friend error_id BOOST_LEAF_CONSTEXPR leaf_detail::make_error_id(int) noexcept;
1529 BOOST_LEAF_CONSTEXPR explicit error_id( int value ) noexcept:
1532 BOOST_LEAF_ASSERT(value_==0 || ((value_&3)==1));
1537 BOOST_LEAF_CONSTEXPR error_id() noexcept:
1542 error_id( std::error_code const & ec ) noexcept:
1543 value_(leaf_detail::import_error_code(ec))
1545 BOOST_LEAF_ASSERT(!value_ || ((value_&3)==1));
1548 template <class Enum>
1549 error_id( Enum e, typename std::enable_if<std::is_error_code_enum<Enum>::value, Enum>::type * = 0 ) noexcept:
1550 value_(leaf_detail::import_error_code(e))
1554 BOOST_LEAF_CONSTEXPR error_id load() const noexcept
1559 template <class... Item>
1560 BOOST_LEAF_CONSTEXPR error_id load( Item && ... item ) const noexcept
1562 if( int err_id = value() )
1564 int const unused[ ] = { 42, leaf_detail::load_item<Item>::load(err_id, std::forward<Item>(item))... };
1570 std::error_code to_error_code() const noexcept
1572 return std::error_code(value_, leaf_detail::get_error_category<>::cat);
1575 BOOST_LEAF_CONSTEXPR int value() const noexcept
1577 if( int v = value_ )
1579 BOOST_LEAF_ASSERT((v&3)==1);
1586 BOOST_LEAF_CONSTEXPR explicit operator bool() const noexcept
1591 BOOST_LEAF_CONSTEXPR friend bool operator==( error_id a, error_id b ) noexcept
1593 return a.value_ == b.value_;
1596 BOOST_LEAF_CONSTEXPR friend bool operator!=( error_id a, error_id b ) noexcept
1601 BOOST_LEAF_CONSTEXPR friend bool operator<( error_id a, error_id b ) noexcept
1603 return a.value_ < b.value_;
1606 template <class CharT, class Traits>
1607 friend std::basic_ostream<CharT, Traits> & operator<<( std::basic_ostream<CharT, Traits> & os, error_id x )
1609 return os << x.value_;
1612 BOOST_LEAF_CONSTEXPR void load_source_location_( char const * file, int line, char const * function ) const noexcept
1614 BOOST_LEAF_ASSERT(file&&*file);
1615 BOOST_LEAF_ASSERT(line>0);
1616 BOOST_LEAF_ASSERT(function&&*function);
1617 BOOST_LEAF_ASSERT(value_);
1618 (void) load(e_source_location {file,line,function});
1622 namespace leaf_detail
1624 BOOST_LEAF_CONSTEXPR inline error_id make_error_id( int err_id ) noexcept
1626 BOOST_LEAF_ASSERT(err_id==0 || (err_id&3)==1);
1627 return error_id((err_id&~3)|1);
1631 inline error_id new_error() noexcept
1633 return leaf_detail::make_error_id(leaf_detail::new_id());
1636 template <class... Item>
1637 inline error_id new_error( Item && ... item ) noexcept
1639 return leaf_detail::make_error_id(leaf_detail::new_id()).load(std::forward<Item>(item)...);
1642 inline error_id current_error() noexcept
1644 return leaf_detail::make_error_id(leaf_detail::current_id());
1647 ////////////////////////////////////////////
1649 class polymorphic_context
1653 polymorphic_context() noexcept = default;
1654 ~polymorphic_context() noexcept = default;
1658 virtual error_id propagate_captured_errors() noexcept = 0;
1659 virtual void activate() noexcept = 0;
1660 virtual void deactivate() noexcept = 0;
1661 virtual void propagate() noexcept = 0;
1662 virtual bool is_active() const noexcept = 0;
1663 virtual void print( std::ostream & ) const = 0;
1664 error_id captured_id_;
1667 using context_ptr = std::shared_ptr<polymorphic_context>;
1669 ////////////////////////////////////////////
1671 template <class Ctx>
1672 class context_activator
1674 context_activator( context_activator const & ) = delete;
1675 context_activator & operator=( context_activator const & ) = delete;
1677 #if !defined(BOOST_LEAF_NO_EXCEPTIONS) && BOOST_LEAF_STD_UNCAUGHT_EXCEPTIONS
1678 int const uncaught_exceptions_;
1684 explicit BOOST_LEAF_CONSTEXPR BOOST_LEAF_ALWAYS_INLINE context_activator(Ctx & ctx) noexcept:
1685 #if !defined(BOOST_LEAF_NO_EXCEPTIONS) && BOOST_LEAF_STD_UNCAUGHT_EXCEPTIONS
1686 uncaught_exceptions_(std::uncaught_exceptions()),
1688 ctx_(ctx.is_active() ? 0 : &ctx)
1694 BOOST_LEAF_CONSTEXPR BOOST_LEAF_ALWAYS_INLINE context_activator( context_activator && x ) noexcept:
1695 #if !defined(BOOST_LEAF_NO_EXCEPTIONS) && BOOST_LEAF_STD_UNCAUGHT_EXCEPTIONS
1696 uncaught_exceptions_(x.uncaught_exceptions_),
1703 BOOST_LEAF_ALWAYS_INLINE ~context_activator() noexcept
1707 if( ctx_->is_active() )
1709 #ifndef BOOST_LEAF_NO_EXCEPTIONS
1710 # if BOOST_LEAF_STD_UNCAUGHT_EXCEPTIONS
1711 if( std::uncaught_exceptions() > uncaught_exceptions_ )
1713 if( std::uncaught_exception() )
1720 template <class Ctx>
1721 BOOST_LEAF_CONSTEXPR BOOST_LEAF_ALWAYS_INLINE context_activator<Ctx> activate_context(Ctx & ctx) noexcept
1723 return context_activator<Ctx>(ctx);
1726 ////////////////////////////////////////////
1729 struct is_result_type: std::false_type
1734 struct is_result_type<R const>: is_result_type<R>
1740 #undef BOOST_LEAF_THREAD_LOCAL
1743 // <<< #include <boost/leaf/error.hpp>
1744 #line 20 "boost/leaf/exception.hpp"
1745 #include <exception>
1747 #define BOOST_LEAF_EXCEPTION ::boost::leaf::leaf_detail::inject_loc{__FILE__,__LINE__,__FUNCTION__}+::boost::leaf::exception
1748 #define BOOST_LEAF_THROW_EXCEPTION ::boost::leaf::leaf_detail::throw_with_loc{__FILE__,__LINE__,__FUNCTION__}+::boost::leaf::exception
1750 ////////////////////////////////////////
1752 namespace boost { namespace leaf {
1754 namespace leaf_detail
1756 struct throw_with_loc
1758 char const * const file;
1760 char const * const fn;
1763 [[noreturn]] friend void operator+( throw_with_loc loc, Ex const & ex )
1765 ex.load_source_location_(loc.file, loc.line, loc.fn);
1766 ::boost::leaf::throw_exception(ex);
1773 ////////////////////////////////////////
1775 namespace boost { namespace leaf {
1777 namespace leaf_detail
1779 inline void enforce_std_exception( std::exception const & ) noexcept { }
1781 class exception_base
1783 std::shared_ptr<void const> auto_id_bump_;
1786 virtual error_id get_error_id() const noexcept = 0;
1791 auto_id_bump_(0, [](void const *) { (void) new_id(); })
1795 ~exception_base() noexcept { }
1801 public exception_base,
1804 error_id get_error_id() const noexcept final override
1811 exception( exception const & ) = default;
1812 exception( exception && ) = default;
1814 BOOST_LEAF_CONSTEXPR exception( error_id id, Ex && ex ) noexcept:
1818 enforce_std_exception(*this);
1821 explicit BOOST_LEAF_CONSTEXPR exception( error_id id ) noexcept:
1824 enforce_std_exception(*this);
1828 template <class... T>
1829 struct at_least_one_derives_from_std_exception;
1832 struct at_least_one_derives_from_std_exception<>: std::false_type { };
1834 template <class T, class... Rest>
1835 struct at_least_one_derives_from_std_exception<T, Rest...>
1837 constexpr static const bool value = std::is_base_of<std::exception,T>::value || at_least_one_derives_from_std_exception<Rest...>::value;
1841 template <class Ex, class... E>
1843 typename std::enable_if<std::is_base_of<std::exception,Ex>::value, leaf_detail::exception<Ex>>::type
1844 exception( Ex && ex, E && ... e ) noexcept
1846 static_assert(!leaf_detail::at_least_one_derives_from_std_exception<E...>::value, "Error objects passed to leaf::exception may not derive from std::exception");
1847 auto id = leaf::new_error(std::forward<E>(e)...);
1848 return leaf_detail::exception<Ex>(id, std::forward<Ex>(ex));
1851 template <class E1, class... E>
1853 typename std::enable_if<!std::is_base_of<std::exception,E1>::value, leaf_detail::exception<std::exception>>::type
1854 exception( E1 && car, E && ... cdr ) noexcept
1856 static_assert(!leaf_detail::at_least_one_derives_from_std_exception<E...>::value, "Error objects passed to leaf::exception may not derive from std::exception");
1857 auto id = leaf::new_error(std::forward<E1>(car), std::forward<E>(cdr)...);
1858 return leaf_detail::exception<std::exception>(id);
1861 inline leaf_detail::exception<std::exception> exception() noexcept
1863 return leaf_detail::exception<std::exception>(leaf::new_error());
1869 // <<< #include <boost/leaf/exception.hpp>
1870 #line 20 "boost/leaf/capture.hpp"
1871 // >>> #include <boost/leaf/on_error.hpp>
1872 #line 1 "boost/leaf/on_error.hpp"
1873 #ifndef BOOST_LEAF_ON_ERROR_HPP_INCLUDED
1874 #define BOOST_LEAF_ON_ERROR_HPP_INCLUDED
1876 // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc.
1878 // Distributed under the Boost Software License, Version 1.0. (See accompanying
1879 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
1881 #ifndef BOOST_LEAF_ENABLE_WARNINGS
1882 # if defined(__clang__)
1883 # pragma clang system_header
1884 # elif (__GNUC__*100+__GNUC_MINOR__>301)
1885 # pragma GCC system_header
1886 # elif defined(_MSC_VER)
1887 # pragma warning(push,1)
1892 namespace boost { namespace leaf {
1896 #if !defined(BOOST_LEAF_NO_EXCEPTIONS) && BOOST_LEAF_STD_UNCAUGHT_EXCEPTIONS
1897 int const uncaught_exceptions_;
1903 error_monitor() noexcept:
1904 #if !defined(BOOST_LEAF_NO_EXCEPTIONS) && BOOST_LEAF_STD_UNCAUGHT_EXCEPTIONS
1905 uncaught_exceptions_(std::uncaught_exceptions()),
1907 err_id_(leaf_detail::current_id())
1911 int check_id() const noexcept
1913 int err_id = leaf_detail::current_id();
1914 if( err_id != err_id_ )
1918 #ifndef BOOST_LEAF_NO_EXCEPTIONS
1919 # if BOOST_LEAF_STD_UNCAUGHT_EXCEPTIONS
1920 if( std::uncaught_exceptions() > uncaught_exceptions_ )
1922 if( std::uncaught_exception() )
1924 return leaf_detail::new_id();
1930 int get_id() const noexcept
1932 int err_id = leaf_detail::current_id();
1933 if( err_id != err_id_ )
1936 return leaf_detail::new_id();
1939 error_id check() const noexcept
1941 return leaf_detail::make_error_id(check_id());
1944 error_id assigned_error_id() const noexcept
1946 return leaf_detail::make_error_id(get_id());
1950 ////////////////////////////////////////////
1952 namespace leaf_detail
1954 template <int I, class Tuple>
1955 struct tuple_for_each_preload
1957 BOOST_LEAF_CONSTEXPR static void trigger( Tuple & tup, int err_id ) noexcept
1959 BOOST_LEAF_ASSERT((err_id&3)==1);
1960 tuple_for_each_preload<I-1,Tuple>::trigger(tup,err_id);
1961 std::get<I-1>(tup).trigger(err_id);
1965 template <class Tuple>
1966 struct tuple_for_each_preload<0, Tuple>
1968 BOOST_LEAF_CONSTEXPR static void trigger( Tuple const &, int ) noexcept { }
1972 class preloaded_item
1974 using decay_E = typename std::decay<E>::type;
1980 BOOST_LEAF_CONSTEXPR preloaded_item( E && e ):
1981 s_(tl_slot_ptr<decay_E>::p),
1982 e_(std::forward<E>(e))
1986 BOOST_LEAF_CONSTEXPR void trigger( int err_id ) noexcept
1988 BOOST_LEAF_ASSERT((err_id&3)==1);
1991 if( !s_->has_value(err_id) )
1992 s_->put(err_id, std::move(e_));
1994 #if BOOST_LEAF_DIAGNOSTICS
1997 int c = tl_unexpected_enabled<>::counter;
1998 BOOST_LEAF_ASSERT(c>=0);
2000 load_unexpected(err_id, std::move(e_));
2009 using E = decltype(std::declval<F>()());
2015 BOOST_LEAF_CONSTEXPR deferred_item( F && f ) noexcept:
2016 s_(tl_slot_ptr<E>::p),
2017 f_(std::forward<F>(f))
2021 BOOST_LEAF_CONSTEXPR void trigger( int err_id ) noexcept
2023 BOOST_LEAF_ASSERT((err_id&3)==1);
2026 if( !s_->has_value(err_id) )
2027 s_->put(err_id, f_());
2029 #if BOOST_LEAF_DIAGNOSTICS
2032 int c = tl_unexpected_enabled<>::counter;
2033 BOOST_LEAF_ASSERT(c>=0);
2035 load_unexpected(err_id, std::forward<E>(f_()));
2041 template <class F, class A0 = fn_arg_type<F,0>, int arity = function_traits<F>::arity>
2042 class accumulating_item;
2044 template <class F, class A0>
2045 class accumulating_item<F, A0 &, 1>
2053 BOOST_LEAF_CONSTEXPR accumulating_item( F && f ) noexcept:
2054 s_(tl_slot_ptr<E>::p),
2055 f_(std::forward<F>(f))
2059 BOOST_LEAF_CONSTEXPR void trigger( int err_id ) noexcept
2061 BOOST_LEAF_ASSERT((err_id&3)==1);
2063 if( E * e = s_->has_value(err_id) )
2066 (void) f_(s_->put(err_id, E()));
2070 template <class... Item>
2073 preloaded & operator=( preloaded const & ) = delete;
2075 std::tuple<Item...> p_;
2081 BOOST_LEAF_CONSTEXPR explicit preloaded( Item && ... i ):
2082 p_(std::forward<Item>(i)...),
2087 BOOST_LEAF_CONSTEXPR preloaded( preloaded && x ) noexcept:
2088 p_(std::move(x.p_)),
2090 id_(std::move(x.id_))
2095 ~preloaded() noexcept
2099 if( auto id = id_.check_id() )
2100 tuple_for_each_preload<sizeof...(Item),decltype(p_)>::trigger(p_,id);
2104 template <class T, int arity = function_traits<T>::arity>
2105 struct deduce_item_type;
2108 struct deduce_item_type<T, -1>
2110 using type = preloaded_item<T>;
2114 struct deduce_item_type<F, 0>
2116 using type = deferred_item<F>;
2120 struct deduce_item_type<F, 1>
2122 using type = accumulating_item<F>;
2126 template <class... Item>
2127 BOOST_LEAF_NODISCARD BOOST_LEAF_CONSTEXPR inline
2128 leaf_detail::preloaded<typename leaf_detail::deduce_item_type<Item>::type...>
2129 on_error( Item && ... i )
2131 return leaf_detail::preloaded<typename leaf_detail::deduce_item_type<Item>::type...>(std::forward<Item>(i)...);
2137 // <<< #include <boost/leaf/on_error.hpp>
2138 #line 21 "boost/leaf/capture.hpp"
2140 namespace boost { namespace leaf {
2142 namespace leaf_detail
2144 template <class R, bool IsResult = is_result_type<R>::value>
2145 struct is_result_tag;
2148 struct is_result_tag<R, false>
2153 struct is_result_tag<R, true>
2158 #ifdef BOOST_LEAF_NO_EXCEPTIONS
2160 namespace leaf_detail
2162 template <class R, class F, class... A>
2164 decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...))
2165 capture_impl(is_result_tag<R, false>, context_ptr && ctx, F && f, A... a) noexcept
2167 auto active_context = activate_context(*ctx);
2168 return std::forward<F>(f)(std::forward<A>(a)...);
2171 template <class R, class F, class... A>
2173 decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...))
2174 capture_impl(is_result_tag<R, true>, context_ptr && ctx, F && f, A... a) noexcept
2176 auto active_context = activate_context(*ctx);
2177 if( auto r = std::forward<F>(f)(std::forward<A>(a)...) )
2181 ctx->captured_id_ = r.error();
2182 return std::move(ctx);
2186 template <class R, class Future>
2188 decltype(std::declval<Future>().get())
2189 future_get_impl(is_result_tag<R, false>, Future & fut) noexcept
2194 template <class R, class Future>
2196 decltype(std::declval<Future>().get())
2197 future_get_impl(is_result_tag<R, true>, Future & fut) noexcept
2199 if( auto r = fut.get() )
2202 return error_id(r.error()); // unloads
2208 namespace leaf_detail
2210 class capturing_exception:
2211 public std::exception
2213 std::exception_ptr ex_;
2218 capturing_exception(std::exception_ptr && ex, context_ptr && ctx) noexcept:
2220 ctx_(std::move(ctx))
2222 BOOST_LEAF_ASSERT(ex_);
2223 BOOST_LEAF_ASSERT(ctx_);
2224 BOOST_LEAF_ASSERT(ctx_->captured_id_);
2227 [[noreturn]] void unload_and_rethrow_original_exception() const
2229 BOOST_LEAF_ASSERT(ctx_->captured_id_);
2230 auto active_context = activate_context(*ctx_);
2231 id_factory<>::current_id = ctx_->captured_id_.value();
2232 std::rethrow_exception(ex_);
2235 template <class CharT, class Traits>
2236 void print( std::basic_ostream<CharT, Traits> & os ) const
2242 template <class R, class F, class... A>
2244 decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...))
2245 capture_impl(is_result_tag<R, false>, context_ptr && ctx, F && f, A... a)
2247 auto active_context = activate_context(*ctx);
2248 error_monitor cur_err;
2251 return std::forward<F>(f)(std::forward<A>(a)...);
2253 catch( capturing_exception const & )
2257 catch( exception_base const & e )
2259 ctx->captured_id_ = e.get_error_id();
2260 throw_exception( capturing_exception(std::current_exception(), std::move(ctx)) );
2264 ctx->captured_id_ = cur_err.assigned_error_id();
2265 throw_exception( capturing_exception(std::current_exception(), std::move(ctx)) );
2269 template <class R, class F, class... A>
2271 decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...))
2272 capture_impl(is_result_tag<R, true>, context_ptr && ctx, F && f, A... a)
2274 auto active_context = activate_context(*ctx);
2275 error_monitor cur_err;
2278 if( auto && r = std::forward<F>(f)(std::forward<A>(a)...) )
2279 return std::move(r);
2282 ctx->captured_id_ = r.error();
2283 return std::move(ctx);
2286 catch( capturing_exception const & )
2290 catch( exception_base const & e )
2292 ctx->captured_id_ = e.get_error_id();
2293 throw_exception( capturing_exception(std::current_exception(), std::move(ctx)) );
2297 ctx->captured_id_ = cur_err.assigned_error_id();
2298 throw_exception( capturing_exception(std::current_exception(), std::move(ctx)) );
2302 template <class R, class Future>
2304 decltype(std::declval<Future>().get())
2305 future_get_impl(is_result_tag<R, false>, Future & fut )
2311 catch( capturing_exception const & cap )
2313 cap.unload_and_rethrow_original_exception();
2317 template <class R, class Future>
2319 decltype(std::declval<Future>().get())
2320 future_get_impl(is_result_tag<R, true>, Future & fut )
2324 if( auto r = fut.get() )
2327 return error_id(r.error()); // unloads
2329 catch( capturing_exception const & cap )
2331 cap.unload_and_rethrow_original_exception();
2338 template <class F, class... A>
2340 decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...))
2341 capture(context_ptr && ctx, F && f, A... a)
2343 using namespace leaf_detail;
2344 return capture_impl(is_result_tag<decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...))>(), std::move(ctx), std::forward<F>(f), std::forward<A>(a)...);
2347 template <class Future>
2349 decltype(std::declval<Future>().get())
2350 future_get( Future & fut )
2352 using namespace leaf_detail;
2353 return future_get_impl(is_result_tag<decltype(std::declval<Future>().get())>(), fut);
2356 ////////////////////////////////////////
2358 #ifndef BOOST_LEAF_NO_EXCEPTIONS
2363 namespace leaf_detail
2365 inline error_id catch_exceptions_helper( std::exception const & ex, leaf_detail_mp11::mp_list<> )
2367 return leaf::new_error(std::current_exception());
2370 template <class Ex1, class... Ex>
2371 inline error_id catch_exceptions_helper( std::exception const & ex, leaf_detail_mp11::mp_list<Ex1,Ex...> )
2373 if( Ex1 const * p = dynamic_cast<Ex1 const *>(&ex) )
2374 return catch_exceptions_helper(ex, leaf_detail_mp11::mp_list<Ex...>{ }).load(*p);
2376 return catch_exceptions_helper(ex, leaf_detail_mp11::mp_list<Ex...>{ });
2380 struct deduce_exception_to_result_return_type_impl
2382 using type = result<T>;
2386 struct deduce_exception_to_result_return_type_impl<result<T>>
2388 using type = result<T>;
2392 using deduce_exception_to_result_return_type = typename deduce_exception_to_result_return_type_impl<T>::type;
2395 template <class... Ex, class F>
2397 leaf_detail::deduce_exception_to_result_return_type<leaf_detail::fn_return_type<F>>
2398 exception_to_result( F && f ) noexcept
2402 return std::forward<F>(f)();
2404 catch( std::exception const & ex )
2406 return leaf_detail::catch_exceptions_helper(ex, leaf_detail_mp11::mp_list<Ex...>());
2410 return leaf::new_error(std::current_exception());
2419 // <<< #include <boost/leaf/capture.hpp>
2420 #line 10 "../../include/boost/leaf/detail/all.hpp"
2421 // >>> #include <boost/leaf/common.hpp>
2422 #line 1 "boost/leaf/common.hpp"
2423 #ifndef BOOST_LEAF_COMMON_HPP_INCLUDED
2424 #define BOOST_LEAF_COMMON_HPP_INCLUDED
2426 // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc.
2428 // Distributed under the Boost Software License, Version 1.0. (See accompanying
2429 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
2431 #ifndef BOOST_LEAF_ENABLE_WARNINGS
2432 # if defined(__clang__)
2433 # pragma clang system_header
2434 # elif (__GNUC__*100+__GNUC_MINOR__>301)
2435 # pragma GCC system_header
2436 # elif defined(_MSC_VER)
2437 # pragma warning(push,1)
2444 # include <Windows.h>
2454 namespace boost { namespace leaf {
2456 struct e_api_function { char const * value; };
2458 struct e_file_name { std::string value; };
2464 template <class CharT, class Traits>
2465 friend std::basic_ostream<CharT, Traits> & operator<<( std::basic_ostream<CharT, Traits> & os, e_errno const & err )
2467 return os << type<e_errno>() << ": " << err.value << ", \"" << std::strerror(err.value) << '"';
2471 struct e_type_info_name { char const * value; };
2473 struct e_at_line { int value; };
2482 template <class CharT, class Traits>
2483 friend std::basic_ostream<CharT, Traits> & operator<<( std::basic_ostream<CharT, Traits> os, e_LastError const & err )
2489 ~msg_buf() noexcept { if(p) LocalFree(p); }
2493 FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
2496 MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
2501 BOOST_LEAF_ASSERT(mb.p != 0);
2502 char * z = std::strchr((LPSTR)mb.p,0);
2507 return os << type<e_LastError>() << ": " << err.value << ", \"" << (LPCSTR)mb.p << '"';
2512 // TODO : Other platforms
2520 // <<< #include <boost/leaf/common.hpp>
2521 #line 11 "../../include/boost/leaf/detail/all.hpp"
2522 // >>> #include <boost/leaf/context.hpp>
2523 #line 1 "boost/leaf/context.hpp"
2524 #ifndef BOOST_LEAF_CONTEXT_HPP_INCLUDED
2525 #define BOOST_LEAF_CONTEXT_HPP_INCLUDED
2527 // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc.
2529 // Distributed under the Boost Software License, Version 1.0. (See accompanying
2530 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
2532 #ifndef BOOST_LEAF_ENABLE_WARNINGS
2533 # if defined(__clang__)
2534 # pragma clang system_header
2535 # elif (__GNUC__*100+__GNUC_MINOR__>301)
2536 # pragma GCC system_header
2537 # elif defined(_MSC_VER)
2538 # pragma warning(push,1)
2543 namespace boost { namespace leaf {
2546 class diagnostic_info;
2547 class verbose_diagnostic_info;
2550 struct is_predicate: std::false_type
2554 namespace leaf_detail
2557 struct is_exception: std::is_base_of<std::exception, typename std::decay<T>::type>
2562 struct handler_argument_traits;
2564 template <class E, bool IsPredicate = is_predicate<E>::value>
2565 struct handler_argument_traits_defaults;
2568 struct handler_argument_traits_defaults<E, false>
2570 using error_type = typename std::decay<E>::type;
2571 constexpr static bool always_available = false;
2573 template <class Tup>
2574 BOOST_LEAF_CONSTEXPR static error_type const * check( Tup const &, error_info const & ) noexcept;
2576 template <class Tup>
2577 BOOST_LEAF_CONSTEXPR static error_type * check( Tup &, error_info const & ) noexcept;
2579 template <class Tup>
2580 BOOST_LEAF_CONSTEXPR static E get( Tup & tup, error_info const & ei ) noexcept
2582 return *check(tup, ei);
2585 static_assert(!is_predicate<error_type>::value, "Handlers must take predicate arguments by value");
2586 static_assert(!std::is_same<E, error_info>::value, "Handlers must take leaf::error_info arguments by const &");
2587 static_assert(!std::is_same<E, diagnostic_info>::value, "Handlers must take leaf::diagnostic_info arguments by const &");
2588 static_assert(!std::is_same<E, verbose_diagnostic_info>::value, "Handlers must take leaf::verbose_diagnostic_info arguments by const &");
2591 template <class Pred>
2592 struct handler_argument_traits_defaults<Pred, true>: handler_argument_traits<typename Pred::error_type>
2594 using base = handler_argument_traits<typename Pred::error_type>;
2595 static_assert(!base::always_available, "Predicates can't use types that are always_available");
2597 template <class Tup>
2598 BOOST_LEAF_CONSTEXPR static bool check( Tup const & tup, error_info const & ei ) noexcept
2600 auto e = base::check(tup, ei);
2601 return e && Pred::evaluate(*e);
2604 template <class Tup>
2605 BOOST_LEAF_CONSTEXPR static Pred get( Tup const & tup, error_info const & ei ) noexcept
2607 return Pred{*base::check(tup, ei)};
2612 struct handler_argument_always_available
2614 using error_type = E;
2615 constexpr static bool always_available = true;
2617 template <class Tup>
2618 BOOST_LEAF_CONSTEXPR static bool check( Tup &, error_info const & ) noexcept
2625 struct handler_argument_traits: handler_argument_traits_defaults<E>
2630 struct handler_argument_traits<void>
2632 using error_type = void;
2633 constexpr static bool always_available = false;
2635 template <class Tup>
2636 BOOST_LEAF_CONSTEXPR static std::exception const * check( Tup const &, error_info const & ) noexcept;
2640 struct handler_argument_traits<E &&>
2642 static_assert(sizeof(E) == 0, "Error handlers may not take rvalue ref arguments");
2646 struct handler_argument_traits<E *>: handler_argument_always_available<typename std::remove_const<E>::type>
2648 template <class Tup>
2649 BOOST_LEAF_CONSTEXPR static E * get( Tup & tup, error_info const & ei) noexcept
2651 return handler_argument_traits_defaults<E>::check(tup, ei);
2656 struct handler_argument_traits<error_info const &>: handler_argument_always_available<void>
2658 template <class Tup>
2659 BOOST_LEAF_CONSTEXPR static error_info const & get( Tup const &, error_info const & ei ) noexcept
2666 struct handler_argument_traits_require_by_value
2668 static_assert(sizeof(E) == 0, "Error handlers must take this type by value");
2672 ////////////////////////////////////////
2674 namespace leaf_detail
2676 template <int I, class Tuple>
2677 struct tuple_for_each
2679 BOOST_LEAF_CONSTEXPR static void activate( Tuple & tup ) noexcept
2681 static_assert(!std::is_same<error_info, typename std::decay<decltype(std::get<I-1>(tup))>::type>::value, "Bug in LEAF: context type deduction");
2682 tuple_for_each<I-1,Tuple>::activate(tup);
2683 std::get<I-1>(tup).activate();
2686 BOOST_LEAF_CONSTEXPR static void deactivate( Tuple & tup ) noexcept
2688 static_assert(!std::is_same<error_info, typename std::decay<decltype(std::get<I-1>(tup))>::type>::value, "Bug in LEAF: context type deduction");
2689 std::get<I-1>(tup).deactivate();
2690 tuple_for_each<I-1,Tuple>::deactivate(tup);
2693 BOOST_LEAF_CONSTEXPR static void propagate( Tuple & tup ) noexcept
2695 static_assert(!std::is_same<error_info, typename std::decay<decltype(std::get<I-1>(tup))>::type>::value, "Bug in LEAF: context type deduction");
2696 auto & sl = std::get<I-1>(tup);
2698 tuple_for_each<I-1,Tuple>::propagate(tup);
2701 BOOST_LEAF_CONSTEXPR static void propagate_captured( Tuple & tup, int err_id ) noexcept
2703 static_assert(!std::is_same<error_info, typename std::decay<decltype(std::get<I-1>(tup))>::type>::value, "Bug in LEAF: context type deduction");
2704 auto & sl = std::get<I-1>(tup);
2705 if( sl.has_value(err_id) )
2706 load_slot(err_id, std::move(sl).value(err_id));
2707 tuple_for_each<I-1,Tuple>::propagate_captured(tup, err_id);
2710 static void print( std::ostream & os, void const * tup, int key_to_print )
2712 BOOST_LEAF_ASSERT(tup != 0);
2713 tuple_for_each<I-1,Tuple>::print(os, tup, key_to_print);
2714 std::get<I-1>(*static_cast<Tuple const *>(tup)).print(os, key_to_print);
2718 template <class Tuple>
2719 struct tuple_for_each<0, Tuple>
2721 BOOST_LEAF_CONSTEXPR static void activate( Tuple & ) noexcept { }
2722 BOOST_LEAF_CONSTEXPR static void deactivate( Tuple & ) noexcept { }
2723 BOOST_LEAF_CONSTEXPR static void propagate( Tuple & tup ) noexcept { }
2724 BOOST_LEAF_CONSTEXPR static void propagate_captured( Tuple & tup, int ) noexcept { }
2725 static void print( std::ostream &, void const *, int ) { }
2729 ////////////////////////////////////////////
2731 #if BOOST_LEAF_DIAGNOSTICS
2733 namespace leaf_detail
2735 template <class T> struct requires_unexpected { constexpr static bool value = false; };
2736 template <class T> struct requires_unexpected<T const> { constexpr static bool value = requires_unexpected<T>::value; };
2737 template <class T> struct requires_unexpected<T const &> { constexpr static bool value = requires_unexpected<T>::value; };
2738 template <class T> struct requires_unexpected<T const *> { constexpr static bool value = requires_unexpected<T>::value; };
2739 template <> struct requires_unexpected<e_unexpected_count> { constexpr static bool value = true; };
2740 template <> struct requires_unexpected<e_unexpected_info> { constexpr static bool value = true; };
2743 struct unexpected_requested;
2745 template <template <class ...> class L>
2746 struct unexpected_requested<L<>>
2748 constexpr static bool value = false;
2751 template <template <class...> class L, template <class> class S, class Car, class... Cdr>
2752 struct unexpected_requested<L<S<Car>, S<Cdr>...>>
2754 constexpr static bool value = requires_unexpected<Car>::value || unexpected_requested<L<S<Cdr>...>>::value;
2760 ////////////////////////////////////////////
2762 namespace leaf_detail
2764 template <class T> struct does_not_participate_in_context_deduction: std::false_type { };
2765 template <> struct does_not_participate_in_context_deduction<void>: std::true_type { };
2768 struct deduce_e_type_list;
2770 template <template<class...> class L, class... T>
2771 struct deduce_e_type_list<L<T...>>
2774 leaf_detail_mp11::mp_remove_if<
2775 leaf_detail_mp11::mp_unique<
2776 leaf_detail_mp11::mp_list<typename handler_argument_traits<T>::error_type...>
2778 does_not_participate_in_context_deduction
2783 struct deduce_e_tuple_impl;
2785 template <template <class...> class L, class... E>
2786 struct deduce_e_tuple_impl<L<E...>>
2788 using type = std::tuple<slot<E>...>;
2791 template <class... E>
2792 using deduce_e_tuple = typename deduce_e_tuple_impl<typename deduce_e_type_list<leaf_detail_mp11::mp_list<E...>>::type>::type;
2795 ////////////////////////////////////////////
2797 template <class... E>
2800 context( context const & ) = delete;
2801 context & operator=( context const & ) = delete;
2803 using Tup = leaf_detail::deduce_e_tuple<E...>;
2806 #if !defined(BOOST_LEAF_NO_THREADS) && !defined(NDEBUG)
2807 std::thread::id thread_id_;
2813 BOOST_LEAF_CONSTEXPR error_id propagate_captured_errors( error_id err_id ) noexcept
2815 leaf_detail::tuple_for_each<std::tuple_size<Tup>::value,Tup>::propagate_captured(tup_, err_id.value());
2821 BOOST_LEAF_CONSTEXPR context( context && x ) noexcept:
2822 tup_(std::move(x.tup_)),
2825 BOOST_LEAF_ASSERT(!x.is_active());
2828 BOOST_LEAF_CONSTEXPR context() noexcept:
2835 BOOST_LEAF_ASSERT(!is_active());
2838 BOOST_LEAF_CONSTEXPR Tup const & tup() const noexcept
2843 BOOST_LEAF_CONSTEXPR Tup & tup() noexcept
2848 BOOST_LEAF_CONSTEXPR void activate() noexcept
2850 using namespace leaf_detail;
2851 BOOST_LEAF_ASSERT(!is_active());
2852 tuple_for_each<std::tuple_size<Tup>::value,Tup>::activate(tup_);
2853 #if BOOST_LEAF_DIAGNOSTICS
2854 if( unexpected_requested<Tup>::value )
2855 ++tl_unexpected_enabled<>::counter;
2857 #if !defined(BOOST_LEAF_NO_THREADS) && !defined(NDEBUG)
2858 thread_id_ = std::this_thread::get_id();
2863 BOOST_LEAF_CONSTEXPR void deactivate() noexcept
2865 using namespace leaf_detail;
2866 BOOST_LEAF_ASSERT(is_active());
2868 #if !defined(BOOST_LEAF_NO_THREADS) && !defined(NDEBUG)
2869 BOOST_LEAF_ASSERT(std::this_thread::get_id() == thread_id_);
2870 thread_id_ = std::thread::id();
2872 #if BOOST_LEAF_DIAGNOSTICS
2873 if( unexpected_requested<Tup>::value )
2874 --tl_unexpected_enabled<>::counter;
2876 tuple_for_each<std::tuple_size<Tup>::value,Tup>::deactivate(tup_);
2879 BOOST_LEAF_CONSTEXPR void propagate() noexcept
2881 leaf_detail::tuple_for_each<std::tuple_size<Tup>::value,Tup>::propagate(tup_);
2884 BOOST_LEAF_CONSTEXPR bool is_active() const noexcept
2889 void print( std::ostream & os ) const
2891 leaf_detail::tuple_for_each<std::tuple_size<Tup>::value,Tup>::print(os, &tup_, 0);
2894 template <class R, class... H>
2895 BOOST_LEAF_CONSTEXPR R handle_error( error_id, H && ... ) const;
2897 template <class R, class... H>
2898 BOOST_LEAF_CONSTEXPR R handle_error( error_id, H && ... );
2901 ////////////////////////////////////////
2903 namespace leaf_detail
2905 template <class TypeList>
2906 struct deduce_context_impl;
2908 template <template <class...> class L, class... E>
2909 struct deduce_context_impl<L<E...>>
2911 using type = context<E...>;
2914 template <class TypeList>
2915 using deduce_context = typename deduce_context_impl<TypeList>::type;
2918 struct fn_mp_args_fwd
2920 using type = fn_mp_args<H>;
2923 template <class... H>
2924 struct fn_mp_args_fwd<std::tuple<H...> &>: fn_mp_args_fwd<std::tuple<H...>> { };
2926 template <class... H>
2927 struct fn_mp_args_fwd<std::tuple<H...>>
2929 using type = leaf_detail_mp11::mp_append<typename fn_mp_args_fwd<H>::type...>;
2932 template <class... H>
2933 struct context_type_from_handlers_impl
2935 using type = deduce_context<leaf_detail_mp11::mp_append<typename fn_mp_args_fwd<H>::type...>>;
2938 template <class Ctx>
2939 struct polymorphic_context_impl: polymorphic_context, Ctx
2941 error_id propagate_captured_errors() noexcept final override { return Ctx::propagate_captured_errors(captured_id_); }
2942 void activate() noexcept final override { Ctx::activate(); }
2943 void deactivate() noexcept final override { Ctx::deactivate(); }
2944 void propagate() noexcept final override { Ctx::propagate(); }
2945 bool is_active() const noexcept final override { return Ctx::is_active(); }
2946 void print( std::ostream & os ) const final override { return Ctx::print(os); }
2950 template <class... H>
2951 using context_type_from_handlers = typename leaf_detail::context_type_from_handlers_impl<H...>::type;
2953 ////////////////////////////////////////////
2955 template <class... H>
2956 BOOST_LEAF_CONSTEXPR inline context_type_from_handlers<H...> make_context() noexcept
2961 template <class... H>
2962 BOOST_LEAF_CONSTEXPR inline context_type_from_handlers<H...> make_context( H && ... ) noexcept
2967 ////////////////////////////////////////////
2969 template <class... H>
2970 inline context_ptr make_shared_context() noexcept
2972 return std::make_shared<leaf_detail::polymorphic_context_impl<context_type_from_handlers<H...>>>();
2975 template <class... H>
2976 inline context_ptr make_shared_context( H && ... ) noexcept
2978 return std::make_shared<leaf_detail::polymorphic_context_impl<context_type_from_handlers<H...>>>();
2984 // <<< #include <boost/leaf/context.hpp>
2985 #line 12 "../../include/boost/leaf/detail/all.hpp"
2986 // >>> #include <boost/leaf/handle_errors.hpp>
2987 #line 1 "boost/leaf/handle_errors.hpp"
2988 #ifndef BOOST_LEAF_HANDLE_ERRORS_HPP_INCLUDED
2989 #define BOOST_LEAF_HANDLE_ERRORS_HPP_INCLUDED
2991 // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc.
2993 // Distributed under the Boost Software License, Version 1.0. (See accompanying
2994 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
2996 #ifndef BOOST_LEAF_ENABLE_WARNINGS
2997 # if defined(__clang__)
2998 # pragma clang system_header
2999 # elif (__GNUC__*100+__GNUC_MINOR__>301)
3000 # pragma GCC system_header
3001 # elif defined(_MSC_VER)
3002 # pragma warning(push,1)
3006 // >>> #include <boost/leaf/detail/demangle.hpp>
3007 #line 1 "boost/leaf/detail/demangle.hpp"
3008 #ifndef BOOST_LEAF_DETAIL_DEMANGLE_HPP_INCLUDED
3009 #define BOOST_LEAF_DETAIL_DEMANGLE_HPP_INCLUDED
3011 // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc.
3013 // Distributed under the Boost Software License, Version 1.0. (See accompanying
3014 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
3018 // Copyright 2014 Peter Dimov
3019 // Copyright 2014 Andrey Semashev
3021 // Distributed under the Boost Software License, Version 1.0.
3022 // See accompanying file LICENSE_1_0.txt or copy at
3023 // http://www.boost.org/LICENSE_1_0.txt
3025 #ifndef BOOST_LEAF_ENABLE_WARNINGS
3026 # if defined(__clang__)
3027 # pragma clang system_header
3028 # elif (__GNUC__*100+__GNUC_MINOR__>301)
3029 # pragma GCC system_header
3030 # elif defined(_MSC_VER)
3031 # pragma warning(push,1)
3035 #if !defined(_MSC_VER)
3036 # if defined(__has_include) && __has_include(<cxxabi.h>)
3037 # define BOOST_LEAF_HAS_CXXABI_H
3041 #if defined( BOOST_LEAF_HAS_CXXABI_H )
3042 # include <cxxabi.h>
3043 // For some architectures (mips, mips64, x86, x86_64) cxxabi.h in Android NDK is implemented by gabi++ library
3044 // (https://android.googlesource.com/platform/ndk/+/master/sources/cxx-stl/gabi++/), which does not implement
3045 // abi::__cxa_demangle(). We detect this implementation by checking the include guard here.
3046 # if defined( __GABIXX_CXXABI_H__ )
3047 # undef BOOST_LEAF_HAS_CXXABI_H
3053 namespace boost { namespace leaf {
3055 namespace leaf_detail
3057 inline char const * demangle_alloc( char const * name ) noexcept;
3058 inline void demangle_free( char const * name ) noexcept;
3060 class scoped_demangled_name
3068 explicit scoped_demangled_name( char const * name ) noexcept :
3069 m_p( demangle_alloc( name ) )
3073 ~scoped_demangled_name() noexcept
3075 demangle_free( m_p );
3078 char const * get() const noexcept
3083 scoped_demangled_name( scoped_demangled_name const& ) = delete;
3084 scoped_demangled_name& operator= ( scoped_demangled_name const& ) = delete;
3087 #if defined( BOOST_LEAF_HAS_CXXABI_H )
3089 inline char const * demangle_alloc( char const * name ) noexcept
3092 std::size_t size = 0;
3093 return abi::__cxa_demangle( name, NULL, &size, &status );
3096 inline void demangle_free( char const * name ) noexcept
3098 std::free( const_cast< char* >( name ) );
3101 inline char const * demangle( char const * name )
3103 scoped_demangled_name demangled_name( name );
3104 char const * p = demangled_name.get();
3112 inline char const * demangle_alloc( char const * name ) noexcept
3117 inline void demangle_free( char const * ) noexcept
3121 inline char const * demangle( char const * name )
3131 #ifdef BOOST_LEAF_HAS_CXXABI_H
3132 # undef BOOST_LEAF_HAS_CXXABI_H
3136 // <<< #include <boost/leaf/detail/demangle.hpp>
3137 #line 21 "boost/leaf/handle_errors.hpp"
3139 #ifndef BOOST_LEAF_NO_EXCEPTIONS
3142 namespace boost { namespace leaf {
3146 error_info & operator=( error_info const & ) = delete;
3148 #ifndef BOOST_LEAF_NO_EXCEPTIONS
3149 static error_id unpack_error_id( std::exception const * ex ) noexcept
3151 if( std::system_error const * se = dynamic_cast<std::system_error const *>(ex) )
3152 if( is_error_id(se->code()) )
3153 return leaf_detail::make_error_id(se->code().value());
3154 if( std::error_code const * ec = dynamic_cast<std::error_code const *>(ex) )
3155 if( is_error_id(*ec) )
3156 return leaf_detail::make_error_id(ec->value());
3157 if( error_id const * err_id = dynamic_cast<error_id const *>(ex) )
3159 return current_error();
3162 std::exception * const ex_;
3165 error_id const err_id_;
3169 error_info( error_info const & ) noexcept = default;
3171 template <class CharT, class Traits>
3172 void print( std::basic_ostream<CharT, Traits> & os ) const
3174 os << "Error ID = " << err_id_.value();
3175 #ifndef BOOST_LEAF_NO_EXCEPTIONS
3179 "\nException dynamic type: " << leaf_detail::demangle(typeid(*ex_).name()) <<
3180 "\nstd::exception::what(): " << ex_->what();
3187 BOOST_LEAF_CONSTEXPR explicit error_info( error_id id ) noexcept:
3188 #ifndef BOOST_LEAF_NO_EXCEPTIONS
3195 #ifndef BOOST_LEAF_NO_EXCEPTIONS
3196 explicit error_info( std::exception * ex ) noexcept:
3198 err_id_(unpack_error_id(ex_))
3203 BOOST_LEAF_CONSTEXPR error_id error() const noexcept
3208 BOOST_LEAF_CONSTEXPR std::exception * exception() const noexcept
3210 #ifdef BOOST_LEAF_NO_EXCEPTIONS
3217 template <class CharT, class Traits>
3218 friend std::basic_ostream<CharT, Traits> & operator<<( std::basic_ostream<CharT, Traits> & os, error_info const & x )
3220 os << "leaf::error_info: ";
3226 ////////////////////////////////////////
3228 #if BOOST_LEAF_DIAGNOSTICS
3230 class diagnostic_info: public error_info
3232 leaf_detail::e_unexpected_count const * e_uc_;
3234 void (*print_)( std::ostream &, void const * tup, int key_to_print );
3238 diagnostic_info( diagnostic_info const & ) noexcept = default;
3240 template <class Tup>
3241 BOOST_LEAF_CONSTEXPR diagnostic_info( error_info const & ei, leaf_detail::e_unexpected_count const * e_uc, Tup const & tup ) noexcept:
3245 print_(&leaf_detail::tuple_for_each<std::tuple_size<Tup>::value, Tup>::print)
3251 template <class CharT, class Traits>
3252 friend std::basic_ostream<CharT, Traits> & operator<<( std::basic_ostream<CharT, Traits> & os, diagnostic_info const & x )
3254 os << "leaf::diagnostic_info for ";
3257 x.print_(os, x.tup_, x.error().value());
3264 namespace leaf_detail
3266 struct diagnostic_info_: diagnostic_info
3268 template <class Tup>
3269 BOOST_LEAF_CONSTEXPR diagnostic_info_( error_info const & ei, leaf_detail::e_unexpected_count const * e_uc, Tup const & tup ) noexcept:
3270 diagnostic_info(ei, e_uc, tup)
3276 struct handler_argument_traits<diagnostic_info const &>: handler_argument_always_available<e_unexpected_count>
3278 template <class Tup>
3279 BOOST_LEAF_CONSTEXPR static diagnostic_info_ get( Tup const & tup, error_info const & ei ) noexcept
3281 return diagnostic_info_(ei, handler_argument_traits_defaults<e_unexpected_count>::check(tup, ei), tup);
3288 class diagnostic_info: public error_info
3292 diagnostic_info( diagnostic_info const & ) noexcept = default;
3294 BOOST_LEAF_CONSTEXPR diagnostic_info( error_info const & ei ) noexcept:
3301 template <class CharT, class Traits>
3302 friend std::basic_ostream<CharT, Traits> & operator<<( std::basic_ostream<CharT, Traits> & os, diagnostic_info const & x )
3305 "leaf::diagnostic_info requires #define BOOST_LEAF_DIAGNOSTICS 1\n"
3306 "leaf::error_info: ";
3312 namespace leaf_detail
3314 struct diagnostic_info_: diagnostic_info
3316 BOOST_LEAF_CONSTEXPR diagnostic_info_( error_info const & ei ) noexcept:
3323 struct handler_argument_traits<diagnostic_info const &>: handler_argument_always_available<void>
3325 template <class Tup>
3326 BOOST_LEAF_CONSTEXPR static diagnostic_info_ get( Tup const & tup, error_info const & ei ) noexcept
3328 return diagnostic_info_(ei);
3335 ////////////////////////////////////////
3337 #if BOOST_LEAF_DIAGNOSTICS
3339 class verbose_diagnostic_info: public error_info
3341 leaf_detail::e_unexpected_info const * e_ui_;
3343 void (*print_)( std::ostream &, void const * tup, int key_to_print );
3347 verbose_diagnostic_info( verbose_diagnostic_info const & ) noexcept = default;
3349 template <class Tup>
3350 BOOST_LEAF_CONSTEXPR verbose_diagnostic_info( error_info const & ei, leaf_detail::e_unexpected_info const * e_ui, Tup const & tup ) noexcept:
3354 print_(&leaf_detail::tuple_for_each<std::tuple_size<Tup>::value, Tup>::print)
3360 template <class CharT, class Traits>
3361 friend std::basic_ostream<CharT, Traits> & operator<<( std::basic_ostream<CharT, Traits> & os, verbose_diagnostic_info const & x )
3363 os << "leaf::verbose_diagnostic_info for ";
3366 x.print_(os, x.tup_, x.error().value());
3373 namespace leaf_detail
3375 struct verbose_diagnostic_info_: verbose_diagnostic_info
3377 template <class Tup>
3378 BOOST_LEAF_CONSTEXPR verbose_diagnostic_info_( error_info const & ei, leaf_detail::e_unexpected_info const * e_ui, Tup const & tup ) noexcept:
3379 verbose_diagnostic_info(ei, e_ui, tup)
3385 struct handler_argument_traits<verbose_diagnostic_info const &>: handler_argument_always_available<e_unexpected_info>
3387 template <class Tup>
3388 BOOST_LEAF_CONSTEXPR static verbose_diagnostic_info_ get( Tup const & tup, error_info const & ei ) noexcept
3390 return verbose_diagnostic_info_(ei, handler_argument_traits_defaults<e_unexpected_info>::check(tup, ei), tup);
3397 class verbose_diagnostic_info: public error_info
3401 verbose_diagnostic_info( verbose_diagnostic_info const & ) noexcept = default;
3403 BOOST_LEAF_CONSTEXPR verbose_diagnostic_info( error_info const & ei ) noexcept:
3410 template <class CharT, class Traits>
3411 friend std::basic_ostream<CharT, Traits> & operator<<( std::basic_ostream<CharT, Traits> & os, verbose_diagnostic_info const & x )
3414 "leaf::verbose_diagnostic_info requires #define BOOST_LEAF_DIAGNOSTICS 1\n"
3415 "leaf::error_info: ";
3421 namespace leaf_detail
3423 struct verbose_diagnostic_info_: verbose_diagnostic_info
3425 BOOST_LEAF_CONSTEXPR verbose_diagnostic_info_( error_info const & ei ) noexcept:
3426 verbose_diagnostic_info(ei)
3433 struct handler_argument_traits<verbose_diagnostic_info const &>: handler_argument_always_available<void>
3435 template <class Tup>
3436 BOOST_LEAF_CONSTEXPR static verbose_diagnostic_info_ get( Tup const & tup, error_info const & ei ) noexcept
3438 return verbose_diagnostic_info_(ei);
3445 ////////////////////////////////////////
3447 namespace leaf_detail
3449 template <class T, class... List>
3452 template <class T, class... Cdr>
3453 struct type_index<T, T, Cdr...>
3455 constexpr static int value = 0;
3458 template <class T, class Car, class... Cdr>
3459 struct type_index<T, Car, Cdr...>
3461 constexpr static int value = 1 + type_index<T,Cdr...>::value;
3464 template <class T, class Tuple>
3465 struct tuple_type_index;
3467 template <class T, class... TupleTypes>
3468 struct tuple_type_index<T,std::tuple<TupleTypes...>>
3470 constexpr static int value = type_index<T,TupleTypes...>::value;
3473 #ifndef BOOST_LEAF_NO_EXCEPTIONS
3475 template <class E, bool = std::is_class<E>::value>
3476 struct peek_exception;
3479 struct peek_exception<std::exception const, true>
3481 BOOST_LEAF_CONSTEXPR static std::exception const * peek( error_info const & ei ) noexcept
3483 return ei.exception();
3488 struct peek_exception<std::exception, true>
3490 BOOST_LEAF_CONSTEXPR static std::exception * peek( error_info const & ei ) noexcept
3492 return ei.exception();
3497 struct peek_exception<std::error_code const, true>
3499 static std::error_code const * peek( error_info const & ei ) noexcept
3501 auto const ex = ei.exception();
3502 if( std::system_error * se = dynamic_cast<std::system_error *>(ex) )
3504 else if( std::error_code * ec = dynamic_cast<std::error_code *>(ex) )
3512 struct peek_exception<std::error_code, true>
3514 static std::error_code * peek( error_info const & ei ) noexcept
3516 auto const ex = ei.exception();
3517 if( std::system_error * se = dynamic_cast<std::system_error *>(ex) )
3518 return const_cast<std::error_code *>(&se->code());
3519 else if( std::error_code * ec = dynamic_cast<std::error_code *>(ex) )
3527 struct peek_exception<E, true>
3529 static E * peek( error_info const & ei ) noexcept
3531 return dynamic_cast<E *>(ei.exception());
3536 struct peek_exception<E, false>
3538 BOOST_LEAF_CONSTEXPR static E * peek( error_info const & ) noexcept
3546 template <class E, class SlotsTuple>
3547 BOOST_LEAF_CONSTEXPR inline
3549 peek( SlotsTuple const & tup, error_info const & ei ) noexcept
3551 if( error_id err = ei.error() )
3552 if( E const * e = std::get<tuple_type_index<slot<E>,SlotsTuple>::value>(tup).has_value(err.value()) )
3554 #ifndef BOOST_LEAF_NO_EXCEPTIONS
3556 return peek_exception<E const>::peek(ei);
3561 template <class E, class SlotsTuple>
3562 BOOST_LEAF_CONSTEXPR inline
3564 peek( SlotsTuple & tup, error_info const & ei ) noexcept
3566 if( error_id err = ei.error() )
3567 if( E * e = std::get<tuple_type_index<slot<E>,SlotsTuple>::value>(tup).has_value(err.value()) )
3569 #ifndef BOOST_LEAF_NO_EXCEPTIONS
3571 return peek_exception<E>::peek(ei);
3577 ////////////////////////////////////////
3579 namespace leaf_detail
3582 template <class Tup>
3583 BOOST_LEAF_CONSTEXPR inline
3584 typename handler_argument_traits_defaults<A, false>::error_type const *
3585 handler_argument_traits_defaults<A, false>::
3586 check( Tup const & tup, error_info const & ei ) noexcept
3588 return peek<typename std::decay<A>::type>(tup, ei);
3592 template <class Tup>
3593 BOOST_LEAF_CONSTEXPR inline
3594 typename handler_argument_traits_defaults<A, false>::error_type *
3595 handler_argument_traits_defaults<A, false>::
3596 check( Tup & tup, error_info const & ei ) noexcept
3598 return peek<typename std::decay<A>::type>(tup, ei);
3601 template <class Tup>
3602 BOOST_LEAF_CONSTEXPR inline
3603 std::exception const *
3604 handler_argument_traits<void>::
3605 check( Tup const &, error_info const & ei ) noexcept
3607 return ei.exception();
3610 template <class Tup, class... List>
3611 struct check_arguments;
3613 template <class Tup>
3614 struct check_arguments<Tup>
3616 BOOST_LEAF_CONSTEXPR static bool check( Tup const &, error_info const & )
3622 template <class Tup, class Car, class... Cdr>
3623 struct check_arguments<Tup, Car, Cdr...>
3625 BOOST_LEAF_CONSTEXPR static bool check( Tup & tup, error_info const & ei ) noexcept
3627 return handler_argument_traits<Car>::check(tup, ei) && check_arguments<Tup, Cdr...>::check(tup, ei);
3632 ////////////////////////////////////////
3634 namespace leaf_detail
3637 struct handler_matches_any_error: std::false_type
3641 template <template<class...> class L>
3642 struct handler_matches_any_error<L<>>: std::true_type
3646 template <template<class...> class L, class Car, class... Cdr>
3647 struct handler_matches_any_error<L<Car, Cdr...>>
3649 constexpr static bool value = handler_argument_traits<Car>::always_available && handler_matches_any_error<L<Cdr...>>::value;
3653 ////////////////////////////////////////
3655 namespace leaf_detail
3657 template <class Tup, class... A>
3658 BOOST_LEAF_CONSTEXPR inline bool check_handler_( Tup & tup, error_info const & ei, leaf_detail_mp11::mp_list<A...> ) noexcept
3660 return check_arguments<Tup, A...>::check(tup, ei);
3663 template <class R, class F, bool IsResult = is_result_type<R>::value, class FReturnType = fn_return_type<F>>
3664 struct handler_caller
3666 template <class Tup, class... A>
3667 BOOST_LEAF_CONSTEXPR static R call( Tup & tup, error_info const & ei, F && f, leaf_detail_mp11::mp_list<A...> )
3669 return std::forward<F>(f)( handler_argument_traits<A>::get(tup, ei)... );
3673 template <template <class...> class Result, class... E, class F>
3674 struct handler_caller<Result<void, E...>, F, true, void>
3676 using R = Result<void, E...>;
3678 template <class Tup, class... A>
3679 BOOST_LEAF_CONSTEXPR static R call( Tup & tup, error_info const & ei, F && f, leaf_detail_mp11::mp_list<A...> )
3681 std::forward<F>(f)( handler_argument_traits<A>::get(tup, ei)... );
3687 struct is_tuple: std::false_type { };
3689 template <class... T>
3690 struct is_tuple<std::tuple<T...>>: std::true_type { };
3692 template <class... T>
3693 struct is_tuple<std::tuple<T...> &>: std::true_type { };
3695 template <class R, class Tup, class H>
3696 BOOST_LEAF_CONSTEXPR
3698 typename std::enable_if<!is_tuple<H>::value, R>::type
3699 handle_error_( Tup & tup, error_info const & ei, H && h )
3701 static_assert( handler_matches_any_error<fn_mp_args<H>>::value, "The last handler passed to handle_all must match any error." );
3702 return handler_caller<R, H>::call( tup, ei, std::forward<H>(h), fn_mp_args<H>{ } );
3705 template <class R, class Tup, class Car, class... Cdr>
3706 BOOST_LEAF_CONSTEXPR inline
3707 typename std::enable_if<!is_tuple<Car>::value, R>::type
3708 handle_error_( Tup & tup, error_info const & ei, Car && car, Cdr && ... cdr )
3710 if( handler_matches_any_error<fn_mp_args<Car>>::value || check_handler_( tup, ei, fn_mp_args<Car>{ } ) )
3711 return handler_caller<R, Car>::call( tup, ei, std::forward<Car>(car), fn_mp_args<Car>{ } );
3713 return handle_error_<R>( tup, ei, std::forward<Cdr>(cdr)...);
3716 template <class R, class Tup, class HTup, size_t ... I>
3717 BOOST_LEAF_CONSTEXPR inline
3719 handle_error_tuple_( Tup & tup, error_info const & ei, leaf_detail_mp11::index_sequence<I...>, HTup && htup )
3721 return handle_error_<R>(tup, ei, std::get<I>(std::forward<HTup>(htup))...);
3724 template <class R, class Tup, class HTup, class... Cdr, size_t ... I>
3725 BOOST_LEAF_CONSTEXPR inline
3727 handle_error_tuple_( Tup & tup, error_info const & ei, leaf_detail_mp11::index_sequence<I...>, HTup && htup, Cdr && ... cdr )
3729 return handle_error_<R>(tup, ei, std::get<I>(std::forward<HTup>(htup))..., std::forward<Cdr>(cdr)...);
3732 template <class R, class Tup, class H>
3733 BOOST_LEAF_CONSTEXPR inline
3734 typename std::enable_if<is_tuple<H>::value, R>::type
3735 handle_error_( Tup & tup, error_info const & ei, H && h )
3737 return handle_error_tuple_<R>(
3740 leaf_detail_mp11::make_index_sequence<std::tuple_size<typename std::decay<H>::type>::value>(),
3741 std::forward<H>(h));
3744 template <class R, class Tup, class Car, class... Cdr>
3745 BOOST_LEAF_CONSTEXPR inline
3746 typename std::enable_if<is_tuple<Car>::value, R>::type
3747 handle_error_( Tup & tup, error_info const & ei, Car && car, Cdr && ... cdr )
3749 return handle_error_tuple_<R>(
3752 leaf_detail_mp11::make_index_sequence<std::tuple_size<typename std::decay<Car>::type>::value>(),
3753 std::forward<Car>(car),
3754 std::forward<Cdr>(cdr)...);
3758 ////////////////////////////////////////
3760 template <class... E>
3761 template <class R, class... H>
3762 BOOST_LEAF_CONSTEXPR BOOST_LEAF_ALWAYS_INLINE
3765 handle_error( error_id id, H && ... h ) const
3767 BOOST_LEAF_ASSERT(!is_active());
3768 return leaf_detail::handle_error_<R>(tup(), error_info(id), std::forward<H>(h)...);
3771 template <class... E>
3772 template <class R, class... H>
3773 BOOST_LEAF_CONSTEXPR BOOST_LEAF_ALWAYS_INLINE
3776 handle_error( error_id id, H && ... h )
3778 BOOST_LEAF_ASSERT(!is_active());
3779 return leaf_detail::handle_error_<R>(tup(), error_info(id), std::forward<H>(h)...);
3782 ////////////////////////////////////////
3784 #ifdef BOOST_LEAF_NO_EXCEPTIONS
3786 template <class TryBlock, class... H>
3787 BOOST_LEAF_CONSTEXPR inline
3788 typename std::decay<decltype(std::declval<TryBlock>()().value())>::type
3789 try_handle_all( TryBlock && try_block, H && ... h ) noexcept
3791 static_assert(is_result_type<decltype(std::declval<TryBlock>()())>::value, "The return type of the try_block passed to a try_handle_all function must be registered with leaf::is_result_type");
3792 context_type_from_handlers<H...> ctx;
3793 auto active_context = activate_context(ctx);
3794 if( auto r = std::forward<TryBlock>(try_block)() )
3798 error_id id = r.error();
3800 using R = typename std::decay<decltype(std::declval<TryBlock>()().value())>::type;
3801 return ctx.template handle_error<R>(std::move(id), std::forward<H>(h)...);
3805 template <class TryBlock, class... H>
3806 BOOST_LEAF_NODISCARD BOOST_LEAF_CONSTEXPR inline
3807 typename std::decay<decltype(std::declval<TryBlock>()())>::type
3808 try_handle_some( TryBlock && try_block, H && ... h ) noexcept
3810 static_assert(is_result_type<decltype(std::declval<TryBlock>()())>::value, "The return type of the try_block passed to a try_handle_some function must be registered with leaf::is_result_type");
3811 context_type_from_handlers<H...> ctx;
3812 auto active_context = activate_context(ctx);
3813 if( auto r = std::forward<TryBlock>(try_block)() )
3817 error_id id = r.error();
3819 using R = typename std::decay<decltype(std::declval<TryBlock>()())>::type;
3820 auto rr = ctx.template handle_error<R>(std::move(id), std::forward<H>(h)..., [&r]()->R { return std::move(r); });
3827 template <class TryBlock, class... H>
3828 BOOST_LEAF_CONSTEXPR inline
3829 decltype(std::declval<TryBlock>()())
3830 try_catch( TryBlock && try_block, H && ... ) noexcept
3832 static_assert(sizeof(context_type_from_handlers<H...>) > 0,
3833 "When exceptions are disabled, try_catch can't fail and has no use for the handlers, but this ensures that the supplied H... types are compatible.");
3834 return std::forward<TryBlock>(try_block)();
3839 namespace leaf_detail
3841 template <class Ctx, class TryBlock, class... H>
3842 decltype(std::declval<TryBlock>()())
3843 try_catch_( Ctx & ctx, TryBlock && try_block, H && ... h )
3845 using namespace leaf_detail;
3846 BOOST_LEAF_ASSERT(ctx.is_active());
3847 using R = decltype(std::declval<TryBlock>()());
3850 return std::forward<TryBlock>(try_block)();
3852 catch( capturing_exception const & cap )
3856 cap.unload_and_rethrow_original_exception();
3858 catch( std::exception & ex )
3861 return handle_error_<R>(ctx.tup(), error_info(&ex), std::forward<H>(h)...,
3862 []() -> R { throw; } );
3867 return handle_error_<R>(ctx.tup(), error_info(nullptr), std::forward<H>(h)...,
3868 []() -> R { throw; } );
3871 catch( std::exception & ex )
3874 return handle_error_<R>(ctx.tup(), error_info(&ex), std::forward<H>(h)...,
3875 []() -> R { throw; } );
3880 return handle_error_<R>(ctx.tup(), error_info(nullptr), std::forward<H>(h)...,
3881 []() -> R { throw; } );
3886 template <class TryBlock, class... H>
3887 BOOST_LEAF_CONSTEXPR inline
3888 typename std::decay<decltype(std::declval<TryBlock>()().value())>::type
3889 try_handle_all( TryBlock && try_block, H && ... h )
3891 static_assert(is_result_type<decltype(std::declval<TryBlock>()())>::value, "The return type of the try_block passed to a try_handle_all function must be registered with leaf::is_result_type");
3892 context_type_from_handlers<H...> ctx;
3893 auto active_context = activate_context(ctx);
3894 if( auto r = leaf_detail::try_catch_(
3898 return std::forward<TryBlock>(try_block)();
3900 std::forward<H>(h)...) )
3904 error_id id = r.error();
3905 if( ctx.is_active() )
3907 using R = typename std::decay<decltype(std::declval<TryBlock>()().value())>::type;
3908 return ctx.template handle_error<R>(std::move(id), std::forward<H>(h)...);
3912 template <class TryBlock, class... H>
3913 BOOST_LEAF_NODISCARD BOOST_LEAF_CONSTEXPR inline
3914 typename std::decay<decltype(std::declval<TryBlock>()())>::type
3915 try_handle_some( TryBlock && try_block, H && ... h )
3917 static_assert(is_result_type<decltype(std::declval<TryBlock>()())>::value, "The return type of the try_block passed to a try_handle_some function must be registered with leaf::is_result_type");
3918 context_type_from_handlers<H...> ctx;
3919 auto active_context = activate_context(ctx);
3920 if( auto r = leaf_detail::try_catch_(
3924 return std::forward<TryBlock>(try_block)();
3926 std::forward<H>(h)...) )
3930 error_id id = r.error();
3931 if( ctx.is_active() )
3933 using R = typename std::decay<decltype(std::declval<TryBlock>()())>::type;
3934 auto rr = ctx.template handle_error<R>(std::move(id), std::forward<H>(h)..., [&r]()->R { return std::move(r); });
3941 template <class TryBlock, class... H>
3942 BOOST_LEAF_CONSTEXPR inline
3943 decltype(std::declval<TryBlock>()())
3944 try_catch( TryBlock && try_block, H && ... h )
3946 context_type_from_handlers<H...> ctx;
3947 auto active_context = activate_context(ctx);
3948 return leaf_detail::try_catch_(
3952 return std::forward<TryBlock>(try_block)();
3954 std::forward<H>(h)...);
3961 // Boost Exception Integration
3963 namespace boost { class exception; }
3964 namespace boost { template <class Tag,class T> class error_info; }
3965 namespace boost { namespace exception_detail { template <class ErrorInfo> struct get_info; } }
3967 namespace boost { namespace leaf {
3969 namespace leaf_detail
3972 struct match_enum_type;
3974 template <class Tag, class T>
3975 struct match_enum_type<boost::error_info<Tag, T>>
3981 BOOST_LEAF_CONSTEXPR inline Ex * get_exception( error_info const & ei )
3983 return dynamic_cast<Ex *>(ei.exception());
3986 template <class, class T>
3987 struct dependent_type { using type = T; };
3989 template <class Dep, class T>
3990 using dependent_type_t = typename dependent_type<Dep, T>::type;
3992 template <class Tag, class T>
3993 struct handler_argument_traits<boost::error_info<Tag, T>>
3995 using error_type = void;
3996 constexpr static bool always_available = false;
3998 template <class Tup>
3999 BOOST_LEAF_CONSTEXPR static T * check( Tup & tup, error_info const & ei ) noexcept
4001 using boost_exception = dependent_type_t<T, boost::exception>;
4002 if( auto * be = get_exception<boost_exception>(ei) )
4003 return exception_detail::get_info<boost::error_info<Tag, T>>::get(*be);
4008 template <class Tup>
4009 BOOST_LEAF_CONSTEXPR static boost::error_info<Tag, T> get( Tup const & tup, error_info const & ei ) noexcept
4011 return boost::error_info<Tag, T>(*check(tup, ei));
4015 template <class Tag, class T> struct handler_argument_traits<boost::error_info<Tag, T> const &>: handler_argument_traits_require_by_value<boost::error_info<Tag, T>> { };
4016 template <class Tag, class T> struct handler_argument_traits<boost::error_info<Tag, T> const *>: handler_argument_traits_require_by_value<boost::error_info<Tag, T>> { };
4017 template <class Tag, class T> struct handler_argument_traits<boost::error_info<Tag, T> &>: handler_argument_traits_require_by_value<boost::error_info<Tag, T>> { };
4018 template <class Tag, class T> struct handler_argument_traits<boost::error_info<Tag, T> *>: handler_argument_traits_require_by_value<boost::error_info<Tag, T>> { };
4024 // <<< #include <boost/leaf/handle_errors.hpp>
4025 #line 15 "../../include/boost/leaf/detail/all.hpp"
4026 // >>> #include <boost/leaf/pred.hpp>
4027 #line 1 "boost/leaf/pred.hpp"
4028 #ifndef BOOST_LEAF_PRED_HPP_INCLUDED
4029 #define BOOST_LEAF_PRED_HPP_INCLUDED
4031 // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc.
4033 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4034 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
4036 #ifndef BOOST_LEAF_ENABLE_WARNINGS
4037 # if defined(__clang__)
4038 # pragma clang system_header
4039 # elif (__GNUC__*100+__GNUC_MINOR__>301)
4040 # pragma GCC system_header
4041 # elif defined(_MSC_VER)
4042 # pragma warning(push,1)
4047 #if __cplusplus >= 201703L
4048 # define BOOST_LEAF_MATCH_ARGS(et,v1,v) auto v1, auto... v
4050 # define BOOST_LEAF_MATCH_ARGS(et,v1,v) typename leaf_detail::et::type v1, typename leaf_detail::et::type... v
4052 #define BOOST_LEAF_ESC(...) __VA_ARGS__
4054 namespace boost { namespace leaf {
4056 namespace leaf_detail
4058 #if __cplusplus >= 201703L
4059 template <class MatchType, class T>
4060 BOOST_LEAF_CONSTEXPR BOOST_LEAF_ALWAYS_INLINE bool cmp_value_pack( MatchType const & e, bool (*P)(T) noexcept ) noexcept
4062 BOOST_LEAF_ASSERT(P != 0);
4066 template <class MatchType, class T>
4067 BOOST_LEAF_CONSTEXPR BOOST_LEAF_ALWAYS_INLINE bool cmp_value_pack( MatchType const & e, bool (*P)(T) )
4069 BOOST_LEAF_ASSERT(P != 0);
4074 template <class MatchType, class V>
4075 BOOST_LEAF_CONSTEXPR BOOST_LEAF_ALWAYS_INLINE bool cmp_value_pack( MatchType const & e, V v )
4080 template <class MatchType, class VCar, class... VCdr>
4081 BOOST_LEAF_CONSTEXPR BOOST_LEAF_ALWAYS_INLINE bool cmp_value_pack( MatchType const & e, VCar car, VCdr ... cdr )
4083 return cmp_value_pack(e, car) || cmp_value_pack(e, cdr...);
4087 ////////////////////////////////////////
4089 template <class E, class Enum = E>
4092 static_assert(std::is_error_condition_enum<Enum>::value || std::is_error_code_enum<Enum>::value, "leaf::condition<E, Enum> requires Enum to be registered either with std::is_error_condition_enum or std::is_error_code_enum.");
4095 template <class Enum>
4096 struct condition<Enum, Enum>
4098 static_assert(std::is_error_condition_enum<Enum>::value || std::is_error_code_enum<Enum>::value, "leaf::condition<Enum> requires Enum to be registered either with std::is_error_condition_enum or std::is_error_code_enum.");
4101 #if __cplusplus >= 201703L
4102 template <class ErrorCodeEnum>
4103 BOOST_LEAF_CONSTEXPR inline bool category( std::error_code const & ec )
4105 static_assert(std::is_error_code_enum<ErrorCodeEnum>::value, "leaf::category requires an error code enum");
4106 return &ec.category() == &std::error_code(ErrorCodeEnum{}).category();
4110 ////////////////////////////////////////
4112 namespace leaf_detail
4115 struct match_enum_type
4120 template <class Enum>
4121 struct match_enum_type<condition<Enum, Enum>>
4126 template <class E, class Enum>
4127 struct match_enum_type<condition<E, Enum>>
4129 static_assert(sizeof(Enum) == 0, "leaf::condition<E, Enum> should be used with leaf::match_value<>, not with leaf::match<>");
4133 template <class E, BOOST_LEAF_MATCH_ARGS(match_enum_type<E>, V1, V)>
4136 using error_type = E;
4140 BOOST_LEAF_CONSTEXPR static bool evaluate(T && x)
4142 return leaf_detail::cmp_value_pack(std::forward<T>(x), V1, V...);
4146 template <class Enum, BOOST_LEAF_MATCH_ARGS(BOOST_LEAF_ESC(match_enum_type<condition<Enum, Enum>>), V1, V)>
4147 struct match<condition<Enum, Enum>, V1, V...>
4149 using error_type = std::error_code;
4150 std::error_code const & matched;
4152 BOOST_LEAF_CONSTEXPR static bool evaluate(std::error_code const & e) noexcept
4154 return leaf_detail::cmp_value_pack(e, V1, V...);
4158 template <class E, BOOST_LEAF_MATCH_ARGS(match_enum_type<E>, V1, V)>
4159 struct is_predicate<match<E, V1, V...>>: std::true_type
4163 ////////////////////////////////////////
4165 namespace leaf_detail
4168 struct match_value_enum_type
4170 using type = typename std::remove_reference<decltype(std::declval<E>().value)>::type;
4173 template <class E, class Enum>
4174 struct match_value_enum_type<condition<E, Enum>>
4179 template <class Enum>
4180 struct match_value_enum_type<condition<Enum, Enum>>
4182 static_assert(sizeof(Enum)==0, "leaf::condition<Enum> should be used with leaf::match<>, not with leaf::match_value<>");
4186 template <class E, BOOST_LEAF_MATCH_ARGS(match_value_enum_type<E>, V1, V)>
4189 using error_type = E;
4192 BOOST_LEAF_CONSTEXPR static bool evaluate(E const & e) noexcept
4194 return leaf_detail::cmp_value_pack(e.value, V1, V...);
4198 template <class E, class Enum, BOOST_LEAF_MATCH_ARGS(BOOST_LEAF_ESC(match_value_enum_type<condition<E, Enum>>), V1, V)>
4199 struct match_value<condition<E, Enum>, V1, V...>
4201 using error_type = E;
4204 BOOST_LEAF_CONSTEXPR static bool evaluate(E const & e)
4206 return leaf_detail::cmp_value_pack(e.value, V1, V...);
4210 template <class E, BOOST_LEAF_MATCH_ARGS(match_value_enum_type<E>, V1, V)>
4211 struct is_predicate<match_value<E, V1, V...>>: std::true_type
4215 ////////////////////////////////////////
4217 #if __cplusplus >= 201703L
4218 template <auto, auto, auto...>
4219 struct match_member;
4221 template <class T, class E, T E::* P, auto V1, auto... V>
4222 struct match_member<P, V1, V...>
4224 using error_type = E;
4227 BOOST_LEAF_CONSTEXPR static bool evaluate(E const & e) noexcept
4229 return leaf_detail::cmp_value_pack(e.*P, V1, V...);
4233 template <auto P, auto V1, auto... V>
4234 struct is_predicate<match_member<P, V1, V...>>: std::true_type
4239 ////////////////////////////////////////
4244 using error_type = typename P::error_type;;
4245 decltype(std::declval<P>().matched) matched;
4248 BOOST_LEAF_CONSTEXPR static bool evaluate(E && e) noexcept
4250 return !P::evaluate(std::forward<E>(e));
4255 struct is_predicate<if_not<P>>: std::true_type
4259 ////////////////////////////////////////
4262 #ifndef BOOST_LEAF_NO_EXCEPTIONS
4264 namespace leaf_detail
4267 BOOST_LEAF_CONSTEXPR inline bool check_exception_pack( std::exception const & ex, Ex const * ) noexcept
4269 return dynamic_cast<Ex const *>(&ex)!=0;
4272 template <class Ex, class... ExRest>
4273 BOOST_LEAF_CONSTEXPR inline bool check_exception_pack( std::exception const & ex, Ex const *, ExRest const * ... ex_rest ) noexcept
4275 return dynamic_cast<Ex const *>(&ex)!=0 || check_exception_pack(ex, ex_rest...);
4278 BOOST_LEAF_CONSTEXPR inline bool check_exception_pack( std::exception const & ) noexcept
4284 template <class... Ex>
4287 using error_type = void;
4288 std::exception const & matched;
4290 BOOST_LEAF_CONSTEXPR static bool evaluate(std::exception const & ex) noexcept
4292 return leaf_detail::check_exception_pack(ex, static_cast<Ex const *>(0)...);
4299 using error_type = void;
4302 BOOST_LEAF_CONSTEXPR static Ex const * evaluate(std::exception const & ex) noexcept
4304 return dynamic_cast<Ex const *>(&ex);
4307 explicit catch_( std::exception const & ex ):
4308 matched(*dynamic_cast<Ex const *>(&ex))
4313 template <class... Ex>
4314 struct is_predicate<catch_<Ex...>>: std::true_type
4323 // <<< #include <boost/leaf/pred.hpp>
4324 #line 17 "../../include/boost/leaf/detail/all.hpp"
4325 // >>> #include <boost/leaf/result.hpp>
4326 #line 1 "boost/leaf/result.hpp"
4327 #ifndef BOOST_LEAF_RESULT_HPP_INCLUDED
4328 #define BOOST_LEAF_RESULT_HPP_INCLUDED
4330 // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc.
4332 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4333 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
4335 #ifndef BOOST_LEAF_ENABLE_WARNINGS
4336 # if defined(__clang__)
4337 # pragma clang system_header
4338 # elif (__GNUC__*100+__GNUC_MINOR__>301)
4339 # pragma GCC system_header
4340 # elif defined(_MSC_VER)
4341 # pragma warning(push,1)
4347 namespace boost { namespace leaf {
4350 public std::exception,
4353 char const * what() const noexcept final override
4355 return "boost::leaf::bad_result";
4360 explicit bad_result( error_id id ) noexcept:
4363 BOOST_LEAF_ASSERT(value());
4367 ////////////////////////////////////////
4369 namespace leaf_detail
4375 using value_type = T;
4376 using value_type_const = T const;
4377 using value_cref = T const &;
4378 using value_ref = T &;
4379 using value_rv_cref = T const &&;
4380 using value_rv_ref = T &&;
4386 using type = std::reference_wrapper<T>;
4387 using value_type_const = T;
4388 using value_type = T;
4389 using value_ref = T &;
4390 using value_cref = T &;
4391 using value_rv_ref = T &;
4392 using value_rv_cref = T &;
4395 class result_discriminant
4409 explicit result_discriminant( error_id id ) noexcept:
4412 BOOST_LEAF_ASSERT(state_==0 || (state_&3)==1);
4415 struct kind_val { };
4416 explicit result_discriminant( kind_val ) noexcept:
4421 struct kind_ctx_ptr { };
4422 explicit result_discriminant( kind_ctx_ptr ) noexcept:
4427 kind_t kind() const noexcept
4429 return kind_t(state_&3);
4432 error_id get_error_id() const noexcept
4434 BOOST_LEAF_ASSERT(kind()==no_error || kind()==err_id);
4435 return make_error_id(state_);
4440 ////////////////////////////////////////
4446 friend class result;
4448 using result_discriminant = leaf_detail::result_discriminant;
4452 error_result( error_result && ) = default;
4453 error_result( error_result const & ) = delete;
4454 error_result & operator=( error_result const & ) = delete;
4458 error_result( result & r ) noexcept:
4464 operator result<U>() noexcept
4466 switch(r_.what_.kind())
4468 case result_discriminant::val:
4469 return result<U>(error_id());
4470 case result_discriminant::ctx_ptr:
4471 return result<U>(std::move(r_.ctx_));
4473 return result<U>(std::move(r_.what_));
4477 operator error_id() noexcept
4479 switch(r_.what_.kind())
4481 case result_discriminant::val:
4483 case result_discriminant::ctx_ptr:
4485 error_id captured_id = r_.ctx_->propagate_captured_errors();
4486 leaf_detail::id_factory<>::current_id = captured_id.value();
4490 return r_.what_.get_error_id();
4495 using stored_type = typename leaf_detail::stored<T>::type;
4496 using value_type = typename leaf_detail::stored<T>::value_type;
4497 using value_type_const = typename leaf_detail::stored<T>::value_type_const;
4498 using value_ref = typename leaf_detail::stored<T>::value_ref;
4499 using value_cref = typename leaf_detail::stored<T>::value_cref;
4500 using value_rv_ref = typename leaf_detail::stored<T>::value_rv_ref;
4501 using value_rv_cref = typename leaf_detail::stored<T>::value_rv_cref;
4505 stored_type stored_;
4509 result_discriminant what_;
4511 void destroy() const noexcept
4513 switch(this->what_.kind())
4515 case result_discriminant::val:
4516 stored_.~stored_type();
4518 case result_discriminant::ctx_ptr:
4519 BOOST_LEAF_ASSERT(!ctx_ || ctx_->captured_id_);
4520 ctx_.~context_ptr();
4527 result_discriminant move_from( result<U> && x ) noexcept
4529 auto x_what = x.what_;
4530 switch(x_what.kind())
4532 case result_discriminant::val:
4533 (void) new(&stored_) stored_type(std::move(x.stored_));
4535 case result_discriminant::ctx_ptr:
4536 BOOST_LEAF_ASSERT(!x.ctx_ || x.ctx_->captured_id_);
4537 (void) new(&ctx_) context_ptr(std::move(x.ctx_));
4544 result( result_discriminant && what ) noexcept:
4545 what_(std::move(what))
4547 BOOST_LEAF_ASSERT(what_.kind()==result_discriminant::err_id || what_.kind()==result_discriminant::no_error);
4550 error_id get_error_id() const noexcept
4552 BOOST_LEAF_ASSERT(what_.kind()!=result_discriminant::val);
4553 return what_.kind()==result_discriminant::ctx_ptr ? ctx_->captured_id_ : what_.get_error_id();
4556 static int init_T_with_U( T && );
4560 void enforce_value_state() const
4562 if( what_.kind() != result_discriminant::val )
4563 ::boost::leaf::throw_exception(bad_result(get_error_id()));
4568 result( result && x ) noexcept:
4569 what_(move_from(std::move(x)))
4574 result( result<U> && x ) noexcept:
4575 what_(move_from(std::move(x)))
4581 stored_(stored_type()),
4582 what_(result_discriminant::kind_val{})
4586 result( value_type && v ) noexcept:
4587 stored_(std::forward<value_type>(v)),
4588 what_(result_discriminant::kind_val{})
4592 result( value_type const & v ):
4594 what_(result_discriminant::kind_val{})
4598 result( error_id err ) noexcept:
4603 // SFINAE: T can be initialized with a U, e.g. result<std::string>("literal").
4604 // Not using is_constructible on purpose, bug with COMPILER=/usr/bin/clang++ CXXSTD=11 clang 3.3.
4606 result( U && u, decltype(init_T_with_U(std::forward<U>(u))) * = 0 ):
4607 stored_(std::forward<U>(u)),
4608 what_(result_discriminant::kind_val{})
4612 result( std::error_code const & ec ) noexcept:
4617 template <class Enum>
4618 result( Enum e, typename std::enable_if<std::is_error_code_enum<Enum>::value, int>::type * = 0 ) noexcept:
4623 result( context_ptr && ctx ) noexcept:
4624 ctx_(std::move(ctx)),
4625 what_(result_discriminant::kind_ctx_ptr{})
4634 result & operator=( result && x ) noexcept
4637 what_ = move_from(std::move(x));
4642 result & operator=( result<U> && x ) noexcept
4645 what_ = move_from(std::move(x));
4649 explicit operator bool() const noexcept
4651 return what_.kind() == result_discriminant::val;
4654 value_cref value() const &
4656 enforce_value_state();
4662 enforce_value_state();
4666 value_rv_cref value() const &&
4668 enforce_value_state();
4669 return std::move(stored_);
4672 value_rv_ref value() &&
4674 enforce_value_state();
4675 return std::move(stored_);
4678 value_cref operator*() const &
4683 value_ref operator*() &
4688 value_rv_cref operator*() const &&
4693 value_rv_ref operator*() &&
4698 value_type_const * operator->() const
4703 value_type * operator->()
4708 error_result error() noexcept
4710 return error_result{*this};
4713 template <class... Item>
4714 error_id load( Item && ... item ) noexcept
4716 return error_id(error()).load(std::forward<Item>(item)...);
4720 ////////////////////////////////////////
4722 namespace leaf_detail
4729 result<leaf_detail::void_>
4731 using result_discriminant = leaf_detail::result_discriminant;
4732 using void_ = leaf_detail::void_;
4733 using base = result<void_>;
4736 friend class result;
4738 result( result_discriminant && what ) noexcept:
4739 base(std::move(what))
4745 using value_type = void;
4747 result( result && x ) noexcept:
4756 result( error_id err ) noexcept:
4761 result( std::error_code const & ec ) noexcept:
4766 template <class Enum>
4767 result( Enum e, typename std::enable_if<std::is_error_code_enum<Enum>::value, Enum>::type * = 0 ) noexcept:
4772 result( context_ptr && ctx ) noexcept:
4773 base(std::move(ctx))
4783 base::enforce_value_state();
4786 using base::operator=;
4787 using base::operator bool;
4788 using base::get_error_id;
4793 ////////////////////////////////////////
4796 struct is_result_type;
4799 struct is_result_type<result<T>>: std::true_type
4806 // <<< #include <boost/leaf/result.hpp>
4807 #line 18 "../../include/boost/leaf/detail/all.hpp"