Loading external/leaf/all.hpp +4709 File added.Preview size limit exceeded, changes collapsed. Show changes external/leaf/capture.hpp +300 Changes for external/leaf/capture.hpp: 300 added lines, 0 removed lines. Original line number Diff line number Diff line #ifndef BOOST_LEAF_CAPTURE_HPP_INCLUDED #define BOOST_LEAF_CAPTURE_HPP_INCLUDED // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc. // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_LEAF_ENABLE_WARNINGS # if defined(__clang__) # pragma clang system_header # elif (__GNUC__*100+__GNUC_MINOR__>301) # pragma GCC system_header # elif defined(_MSC_VER) # pragma warning(push,1) # endif #endif #include "exception.hpp" #include "on_error.hpp" #include "../../../../../../usr/include/c++/8/memory" namespace boost { namespace leaf { namespace leaf_detail { template <class R, bool IsResult = is_result_type<R>::value> struct is_result_tag; template <class R> struct is_result_tag<R, false> { }; template <class R> struct is_result_tag<R, true> { }; } #ifdef BOOST_LEAF_NO_EXCEPTIONS namespace leaf_detail { template <class R, class F, class... A> inline decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...)) capture_impl(is_result_tag<R, false>, context_ptr && ctx, F && f, A... a) noexcept { auto active_context = activate_context(*ctx); return std::forward<F>(f)(std::forward<A>(a)...); } template <class R, class F, class... A> inline decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...)) capture_impl(is_result_tag<R, true>, context_ptr && ctx, F && f, A... a) noexcept { auto active_context = activate_context(*ctx); if( auto r = std::forward<F>(f)(std::forward<A>(a)...) ) return r; else { ctx->captured_id_ = r.error(); return std::move(ctx); } } template <class R, class Future> inline decltype(std::declval<Future>().get()) future_get_impl(is_result_tag<R, false>, Future & fut) noexcept { return fut.get(); } template <class R, class Future> inline decltype(std::declval<Future>().get()) future_get_impl(is_result_tag<R, true>, Future & fut) noexcept { if( auto r = fut.get() ) return r; else return error_id(r.error()); // unloads } } #else namespace leaf_detail { class capturing_exception: public std::exception { std::exception_ptr ex_; context_ptr ctx_; public: capturing_exception(std::exception_ptr && ex, context_ptr && ctx) noexcept: ex_(std::move(ex)), ctx_(std::move(ctx)) { BOOST_LEAF_ASSERT(ex_); BOOST_LEAF_ASSERT(ctx_); BOOST_LEAF_ASSERT(ctx_->captured_id_); } [[noreturn]] void unload_and_rethrow_original_exception() const { BOOST_LEAF_ASSERT(ctx_->captured_id_); auto active_context = activate_context(*ctx_); id_factory<>::current_id = ctx_->captured_id_.value(); std::rethrow_exception(ex_); } void print( std::ostream & os ) const { ctx_->print(os); } }; template <class R, class F, class... A> inline decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...)) capture_impl(is_result_tag<R, false>, context_ptr && ctx, F && f, A... a) { auto active_context = activate_context(*ctx); error_monitor cur_err; try { return std::forward<F>(f)(std::forward<A>(a)...); } catch( capturing_exception const & ) { throw; } catch( exception_base const & e ) { ctx->captured_id_ = e.get_error_id(); throw_exception( capturing_exception(std::current_exception(), std::move(ctx)) ); } catch(...) { ctx->captured_id_ = cur_err.assigned_error_id(); throw_exception( capturing_exception(std::current_exception(), std::move(ctx)) ); } } template <class R, class F, class... A> inline decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...)) capture_impl(is_result_tag<R, true>, context_ptr && ctx, F && f, A... a) { auto active_context = activate_context(*ctx); error_monitor cur_err; try { if( auto && r = std::forward<F>(f)(std::forward<A>(a)...) ) return std::move(r); else { ctx->captured_id_ = r.error(); return std::move(ctx); } } catch( capturing_exception const & ) { throw; } catch( exception_base const & e ) { ctx->captured_id_ = e.get_error_id(); throw_exception( capturing_exception(std::current_exception(), std::move(ctx)) ); } catch(...) { ctx->captured_id_ = cur_err.assigned_error_id(); throw_exception( capturing_exception(std::current_exception(), std::move(ctx)) ); } } template <class R, class Future> inline decltype(std::declval<Future>().get()) future_get_impl(is_result_tag<R, false>, Future & fut ) { try { return fut.get(); } catch( capturing_exception const & cap ) { cap.unload_and_rethrow_original_exception(); } } template <class R, class Future> inline decltype(std::declval<Future>().get()) future_get_impl(is_result_tag<R, true>, Future & fut ) { try { if( auto r = fut.get() ) return r; else return error_id(r.error()); // unloads } catch( capturing_exception const & cap ) { cap.unload_and_rethrow_original_exception(); } } } #endif template <class F, class... A> inline decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...)) capture(context_ptr && ctx, F && f, A... a) { using namespace leaf_detail; 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)...); } template <class Future> inline decltype(std::declval<Future>().get()) future_get( Future & fut ) { using namespace leaf_detail; return future_get_impl(is_result_tag<decltype(std::declval<Future>().get())>(), fut); } //////////////////////////////////////// #ifndef BOOST_LEAF_NO_EXCEPTIONS template <class T> class result; namespace leaf_detail { inline error_id catch_exceptions_helper( std::exception const & ex, leaf_detail_mp11::mp_list<> ) { return leaf::new_error(std::current_exception()); } template <class Ex1, class... Ex> inline error_id catch_exceptions_helper( std::exception const & ex, leaf_detail_mp11::mp_list<Ex1,Ex...> ) { if( Ex1 const * p = dynamic_cast<Ex1 const *>(&ex) ) return catch_exceptions_helper(ex, leaf_detail_mp11::mp_list<Ex...>{ }).load(*p); else return catch_exceptions_helper(ex, leaf_detail_mp11::mp_list<Ex...>{ }); } template <class T> struct deduce_exception_to_result_return_type_impl { using type = result<T>; }; template <class T> struct deduce_exception_to_result_return_type_impl<result<T>> { using type = result<T>; }; template <class T> using deduce_exception_to_result_return_type = typename deduce_exception_to_result_return_type_impl<T>::type; } template <class... Ex, class F> inline leaf_detail::deduce_exception_to_result_return_type<leaf_detail::fn_return_type<F>> exception_to_result( F && f ) noexcept { try { return std::forward<F>(f)(); } catch( std::exception const & ex ) { return leaf_detail::catch_exceptions_helper(ex, leaf_detail_mp11::mp_list<Ex...>()); } catch(...) { return leaf::new_error(std::current_exception()); } } #endif } } #endif external/leaf/common.hpp +96 Changes for external/leaf/common.hpp: 96 added lines, 0 removed lines. Original line number Diff line number Diff line #ifndef BOOST_LEAF_COMMON_HPP_INCLUDED #define BOOST_LEAF_COMMON_HPP_INCLUDED // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc. // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_LEAF_ENABLE_WARNINGS # if defined(__clang__) # pragma clang system_header # elif (__GNUC__*100+__GNUC_MINOR__>301) # pragma GCC system_header # elif defined(_MSC_VER) # pragma warning(push,1) # endif #endif #include "detail/print.hpp" #include "../../../../../../usr/include/c++/8/string" #include "../../../../../../usr/include/c++/8/cerrno" #ifdef _WIN32 # include <Windows.h> # include <cstring> #ifdef min # undef min #endif #ifdef max # undef max #endif #endif namespace boost { namespace leaf { struct e_api_function { char const * value; }; struct e_file_name { std::string value; }; struct e_errno { int value; friend std::ostream & operator<<( std::ostream & os, e_errno const & err ) { return os << type<e_errno>() << ": " << err.value << ", \"" << std::strerror(err.value) << '"'; } }; struct e_type_info_name { char const * value; }; struct e_at_line { int value; }; namespace windows { struct e_LastError { unsigned value; #ifdef _WIN32 friend std::ostream & operator<<( std::ostream & os, e_LastError const & err ) { struct msg_buf { LPVOID * p; msg_buf(): p(0) { } ~msg_buf() noexcept { if(p) LocalFree(p); } }; msg_buf mb; if( FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, 0, err.value, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), (LPSTR)&mb.p, 0, 0) ) { BOOST_LEAF_ASSERT(mb.p != 0); char * z = std::strchr((LPSTR)mb.p,0); if( z[-1] == '\n' ) *--z = 0; if( z[-1] == '\r' ) *--z = 0; return os << type<e_LastError>() << ": " << err.value << ", \"" << (LPCSTR)mb.p << '"'; } return os; } #else // TODO : Other platforms #endif }; } } } #endif external/leaf/context.hpp +461 Changes for external/leaf/context.hpp: 461 added lines, 0 removed lines. Original line number Diff line number Diff line #ifndef BOOST_LEAF_CONTEXT_HPP_INCLUDED #define BOOST_LEAF_CONTEXT_HPP_INCLUDED // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc. // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_LEAF_ENABLE_WARNINGS # if defined(__clang__) # pragma clang system_header # elif (__GNUC__*100+__GNUC_MINOR__>301) # pragma GCC system_header # elif defined(_MSC_VER) # pragma warning(push,1) # endif #endif #include "error.hpp" namespace boost { namespace leaf { class error_info; class diagnostic_info; class verbose_diagnostic_info; template <class> struct is_predicate: std::false_type { }; namespace leaf_detail { template <class T> struct is_exception: std::is_base_of<std::exception, typename std::decay<T>::type> { }; template <class E> struct handler_argument_traits; template <class E, bool IsPredicate = is_predicate<E>::value> struct handler_argument_traits_defaults; template <class E> struct handler_argument_traits_defaults<E, false> { using error_type = typename std::decay<E>::type; constexpr static bool always_available = false; template <class Tup> BOOST_LEAF_CONSTEXPR static error_type const * check( Tup const &, error_info const & ) noexcept; template <class Tup> BOOST_LEAF_CONSTEXPR static error_type * check( Tup &, error_info const & ) noexcept; template <class Tup> BOOST_LEAF_CONSTEXPR static E get( Tup & tup, error_info const & ei ) noexcept { return *check(tup, ei); } static_assert(!is_predicate<error_type>::value, "Handlers must take predicate arguments by value"); static_assert(!std::is_same<E, error_info>::value, "Handlers must take leaf::error_info arguments by const &"); static_assert(!std::is_same<E, diagnostic_info>::value, "Handlers must take leaf::diagnostic_info arguments by const &"); static_assert(!std::is_same<E, verbose_diagnostic_info>::value, "Handlers must take leaf::verbose_diagnostic_info arguments by const &"); }; template <class Pred> struct handler_argument_traits_defaults<Pred, true>: handler_argument_traits<typename Pred::error_type> { using base = handler_argument_traits<typename Pred::error_type>; static_assert(!base::always_available, "Predicates can't use types that are always_available"); template <class Tup> BOOST_LEAF_CONSTEXPR static bool check( Tup & tup, error_info const & ei ) noexcept { auto e = base::check(tup, ei); return e && Pred::evaluate(*e); }; template <class Tup> BOOST_LEAF_CONSTEXPR static Pred get( Tup const & tup, error_info const & ei ) noexcept { return Pred{*base::check(tup, ei)}; } }; template <class E> struct handler_argument_always_available { using error_type = E; constexpr static bool always_available = true; template <class Tup> BOOST_LEAF_CONSTEXPR static bool check( Tup &, error_info const & ) noexcept { return true; }; }; template <class E> struct handler_argument_traits: handler_argument_traits_defaults<E> { }; template <> struct handler_argument_traits<void> { using error_type = void; constexpr static bool always_available = false; template <class Tup> BOOST_LEAF_CONSTEXPR static std::exception const * check( Tup const &, error_info const & ) noexcept; }; template <class E> struct handler_argument_traits<E &&> { static_assert(sizeof(E) == 0, "Error handlers may not take rvalue ref arguments"); }; template <class E> struct handler_argument_traits<E *>: handler_argument_always_available<typename std::remove_const<E>::type> { template <class Tup> BOOST_LEAF_CONSTEXPR static E * get( Tup & tup, error_info const & ei) noexcept { return handler_argument_traits_defaults<E>::check(tup, ei); } }; template <> struct handler_argument_traits<error_info const &>: handler_argument_always_available<void> { template <class Tup> BOOST_LEAF_CONSTEXPR static error_info const & get( Tup const &, error_info const & ei ) noexcept { return ei; } }; template <class E> struct handler_argument_traits_require_by_value { static_assert(sizeof(E) == 0, "Error handlers must take this type by value"); }; } //////////////////////////////////////// namespace leaf_detail { template <int I, class Tuple> struct tuple_for_each { BOOST_LEAF_CONSTEXPR static void activate( Tuple & tup ) noexcept { static_assert(!std::is_same<error_info, typename std::decay<decltype(std::get<I-1>(tup))>::type>::value, "Bug in LEAF: context type deduction"); tuple_for_each<I-1,Tuple>::activate(tup); std::get<I-1>(tup).activate(); } BOOST_LEAF_CONSTEXPR static void deactivate( Tuple & tup ) noexcept { static_assert(!std::is_same<error_info, typename std::decay<decltype(std::get<I-1>(tup))>::type>::value, "Bug in LEAF: context type deduction"); std::get<I-1>(tup).deactivate(); tuple_for_each<I-1,Tuple>::deactivate(tup); } BOOST_LEAF_CONSTEXPR static void propagate( Tuple & tup ) noexcept { static_assert(!std::is_same<error_info, typename std::decay<decltype(std::get<I-1>(tup))>::type>::value, "Bug in LEAF: context type deduction"); auto & sl = std::get<I-1>(tup); sl.propagate(); tuple_for_each<I-1,Tuple>::propagate(tup); } BOOST_LEAF_CONSTEXPR static void propagate_captured( Tuple & tup, int err_id ) noexcept { static_assert(!std::is_same<error_info, typename std::decay<decltype(std::get<I-1>(tup))>::type>::value, "Bug in LEAF: context type deduction"); auto & sl = std::get<I-1>(tup); if( sl.has_value(err_id) ) load_slot(err_id, std::move(sl).value(err_id)); tuple_for_each<I-1,Tuple>::propagate_captured(tup, err_id); } static void print( std::ostream & os, void const * tup, int key_to_print ) { BOOST_LEAF_ASSERT(tup != 0); tuple_for_each<I-1,Tuple>::print(os, tup, key_to_print); std::get<I-1>(*static_cast<Tuple const *>(tup)).print(os, key_to_print); } }; template <class Tuple> struct tuple_for_each<0, Tuple> { BOOST_LEAF_CONSTEXPR static void activate( Tuple & ) noexcept { } BOOST_LEAF_CONSTEXPR static void deactivate( Tuple & ) noexcept { } BOOST_LEAF_CONSTEXPR static void propagate( Tuple & tup ) noexcept { } BOOST_LEAF_CONSTEXPR static void propagate_captured( Tuple & tup, int ) noexcept { } static void print( std::ostream &, void const *, int ) { } }; } //////////////////////////////////////////// #if BOOST_LEAF_DIAGNOSTICS namespace leaf_detail { template <class T> struct requires_unexpected { constexpr static bool value = false; }; template <class T> struct requires_unexpected<T const> { constexpr static bool value = requires_unexpected<T>::value; }; template <class T> struct requires_unexpected<T const &> { constexpr static bool value = requires_unexpected<T>::value; }; template <class T> struct requires_unexpected<T const *> { constexpr static bool value = requires_unexpected<T>::value; }; template <> struct requires_unexpected<e_unexpected_count> { constexpr static bool value = true; }; template <> struct requires_unexpected<e_unexpected_info> { constexpr static bool value = true; }; template <class L> struct unexpected_requested; template <template <class ...> class L> struct unexpected_requested<L<>> { constexpr static bool value = false; }; template <template <class...> class L, template <class> class S, class Car, class... Cdr> struct unexpected_requested<L<S<Car>, S<Cdr>...>> { constexpr static bool value = requires_unexpected<Car>::value || unexpected_requested<L<S<Cdr>...>>::value; }; } #endif //////////////////////////////////////////// namespace leaf_detail { template <class T> struct does_not_participate_in_context_deduction: std::false_type { }; template <> struct does_not_participate_in_context_deduction<void>: std::true_type { }; template <class L> struct deduce_e_type_list; template <template<class...> class L, class... T> struct deduce_e_type_list<L<T...>> { using type = leaf_detail_mp11::mp_remove_if< leaf_detail_mp11::mp_unique< leaf_detail_mp11::mp_list<typename handler_argument_traits<T>::error_type...> >, does_not_participate_in_context_deduction >; }; template <class L> struct deduce_e_tuple_impl; template <template <class...> class L, class... E> struct deduce_e_tuple_impl<L<E...>> { using type = std::tuple<slot<E>...>; }; template <class... E> using deduce_e_tuple = typename deduce_e_tuple_impl<typename deduce_e_type_list<leaf_detail_mp11::mp_list<E...>>::type>::type; } //////////////////////////////////////////// template <class... E> class context { context( context const & ) = delete; context & operator=( context const & ) = delete; using Tup = leaf_detail::deduce_e_tuple<E...>; Tup tup_; #if !defined(BOOST_LEAF_NO_THREADS) && !defined(NDEBUG) std::thread::id thread_id_; #endif bool is_active_; protected: BOOST_LEAF_CONSTEXPR error_id propagate_captured_errors( error_id err_id ) noexcept { leaf_detail::tuple_for_each<std::tuple_size<Tup>::value,Tup>::propagate_captured(tup_, err_id.value()); return err_id; } public: BOOST_LEAF_CONSTEXPR context( context && x ) noexcept: tup_(std::move(x.tup_)), is_active_(false) { BOOST_LEAF_ASSERT(!x.is_active()); } BOOST_LEAF_CONSTEXPR context() noexcept: is_active_(false) { } ~context() noexcept { BOOST_LEAF_ASSERT(!is_active()); } BOOST_LEAF_CONSTEXPR Tup const & tup() const noexcept { return tup_; } BOOST_LEAF_CONSTEXPR Tup & tup() noexcept { return tup_; } BOOST_LEAF_CONSTEXPR void activate() noexcept { using namespace leaf_detail; BOOST_LEAF_ASSERT(!is_active()); tuple_for_each<std::tuple_size<Tup>::value,Tup>::activate(tup_); #if BOOST_LEAF_DIAGNOSTICS if( unexpected_requested<Tup>::value ) ++tl_unexpected_enabled<>::counter; #endif #if !defined(BOOST_LEAF_NO_THREADS) && !defined(NDEBUG) thread_id_ = std::this_thread::get_id(); #endif is_active_ = true; } BOOST_LEAF_CONSTEXPR void deactivate() noexcept { using namespace leaf_detail; BOOST_LEAF_ASSERT(is_active()); is_active_ = false; #if !defined(BOOST_LEAF_NO_THREADS) && !defined(NDEBUG) BOOST_LEAF_ASSERT(std::this_thread::get_id() == thread_id_); thread_id_ = std::thread::id(); #endif #if BOOST_LEAF_DIAGNOSTICS if( unexpected_requested<Tup>::value ) --tl_unexpected_enabled<>::counter; #endif tuple_for_each<std::tuple_size<Tup>::value,Tup>::deactivate(tup_); } BOOST_LEAF_CONSTEXPR void propagate() noexcept { leaf_detail::tuple_for_each<std::tuple_size<Tup>::value,Tup>::propagate(tup_); } BOOST_LEAF_CONSTEXPR bool is_active() const noexcept { return is_active_; } void print( std::ostream & os ) const { leaf_detail::tuple_for_each<std::tuple_size<Tup>::value,Tup>::print(os, &tup_, 0); } template <class R, class... H> BOOST_LEAF_CONSTEXPR R handle_error( error_id, H && ... ) const; template <class R, class... H> BOOST_LEAF_CONSTEXPR R handle_error( error_id, H && ... ); }; //////////////////////////////////////// namespace leaf_detail { template <class TypeList> struct deduce_context_impl; template <template <class...> class L, class... E> struct deduce_context_impl<L<E...>> { using type = context<E...>; }; template <class TypeList> using deduce_context = typename deduce_context_impl<TypeList>::type; template <class H> struct fn_mp_args_fwd { using type = fn_mp_args<H>; }; template <class... H> struct fn_mp_args_fwd<std::tuple<H...> &>: fn_mp_args_fwd<std::tuple<H...>> { }; template <class... H> struct fn_mp_args_fwd<std::tuple<H...>> { using type = leaf_detail_mp11::mp_append<typename fn_mp_args_fwd<H>::type...>; }; template <class... H> struct context_type_from_handlers_impl { using type = deduce_context<leaf_detail_mp11::mp_append<typename fn_mp_args_fwd<H>::type...>>; }; template <class Ctx> struct polymorphic_context_impl: polymorphic_context, Ctx { error_id propagate_captured_errors() noexcept final override { return Ctx::propagate_captured_errors(captured_id_); } void activate() noexcept final override { Ctx::activate(); } void deactivate() noexcept final override { Ctx::deactivate(); } void propagate() noexcept final override { Ctx::propagate(); } bool is_active() const noexcept final override { return Ctx::is_active(); } void print( std::ostream & os ) const final override { return Ctx::print(os); } }; } template <class... H> using context_type_from_handlers = typename leaf_detail::context_type_from_handlers_impl<H...>::type; //////////////////////////////////////////// template <class... H> BOOST_LEAF_CONSTEXPR inline context_type_from_handlers<H...> make_context() noexcept { return { }; } template <class... H> BOOST_LEAF_CONSTEXPR inline context_type_from_handlers<H...> make_context( H && ... ) noexcept { return { }; } //////////////////////////////////////////// template <class... H> inline context_ptr make_shared_context() noexcept { return std::make_shared<leaf_detail::polymorphic_context_impl<context_type_from_handlers<H...>>>(); } template <class... H> inline context_ptr make_shared_context( H && ... ) noexcept { return std::make_shared<leaf_detail::polymorphic_context_impl<context_type_from_handlers<H...>>>(); } } } #endif external/leaf/detail/all.hpp +19 Changes for external/leaf/detail/all.hpp: 19 added lines, 0 removed lines. Original line number Diff line number Diff line #ifndef BOOST_LEAF_ALL_HPP_INCLUDED #define BOOST_LEAF_ALL_HPP_INCLUDED // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc. // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #include <boost/leaf/capture.hpp> #include <boost/leaf/common.hpp> #include <boost/leaf/context.hpp> #include <boost/leaf/error.hpp> #include <boost/leaf/exception.hpp> #include <boost/leaf/handle_errors.hpp> #include <boost/leaf/on_error.hpp> #include <boost/leaf/pred.hpp> #include <boost/leaf/result.hpp> #endif Loading
external/leaf/capture.hpp +300 Changes for external/leaf/capture.hpp: 300 added lines, 0 removed lines. Original line number Diff line number Diff line #ifndef BOOST_LEAF_CAPTURE_HPP_INCLUDED #define BOOST_LEAF_CAPTURE_HPP_INCLUDED // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc. // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_LEAF_ENABLE_WARNINGS # if defined(__clang__) # pragma clang system_header # elif (__GNUC__*100+__GNUC_MINOR__>301) # pragma GCC system_header # elif defined(_MSC_VER) # pragma warning(push,1) # endif #endif #include "exception.hpp" #include "on_error.hpp" #include "../../../../../../usr/include/c++/8/memory" namespace boost { namespace leaf { namespace leaf_detail { template <class R, bool IsResult = is_result_type<R>::value> struct is_result_tag; template <class R> struct is_result_tag<R, false> { }; template <class R> struct is_result_tag<R, true> { }; } #ifdef BOOST_LEAF_NO_EXCEPTIONS namespace leaf_detail { template <class R, class F, class... A> inline decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...)) capture_impl(is_result_tag<R, false>, context_ptr && ctx, F && f, A... a) noexcept { auto active_context = activate_context(*ctx); return std::forward<F>(f)(std::forward<A>(a)...); } template <class R, class F, class... A> inline decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...)) capture_impl(is_result_tag<R, true>, context_ptr && ctx, F && f, A... a) noexcept { auto active_context = activate_context(*ctx); if( auto r = std::forward<F>(f)(std::forward<A>(a)...) ) return r; else { ctx->captured_id_ = r.error(); return std::move(ctx); } } template <class R, class Future> inline decltype(std::declval<Future>().get()) future_get_impl(is_result_tag<R, false>, Future & fut) noexcept { return fut.get(); } template <class R, class Future> inline decltype(std::declval<Future>().get()) future_get_impl(is_result_tag<R, true>, Future & fut) noexcept { if( auto r = fut.get() ) return r; else return error_id(r.error()); // unloads } } #else namespace leaf_detail { class capturing_exception: public std::exception { std::exception_ptr ex_; context_ptr ctx_; public: capturing_exception(std::exception_ptr && ex, context_ptr && ctx) noexcept: ex_(std::move(ex)), ctx_(std::move(ctx)) { BOOST_LEAF_ASSERT(ex_); BOOST_LEAF_ASSERT(ctx_); BOOST_LEAF_ASSERT(ctx_->captured_id_); } [[noreturn]] void unload_and_rethrow_original_exception() const { BOOST_LEAF_ASSERT(ctx_->captured_id_); auto active_context = activate_context(*ctx_); id_factory<>::current_id = ctx_->captured_id_.value(); std::rethrow_exception(ex_); } void print( std::ostream & os ) const { ctx_->print(os); } }; template <class R, class F, class... A> inline decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...)) capture_impl(is_result_tag<R, false>, context_ptr && ctx, F && f, A... a) { auto active_context = activate_context(*ctx); error_monitor cur_err; try { return std::forward<F>(f)(std::forward<A>(a)...); } catch( capturing_exception const & ) { throw; } catch( exception_base const & e ) { ctx->captured_id_ = e.get_error_id(); throw_exception( capturing_exception(std::current_exception(), std::move(ctx)) ); } catch(...) { ctx->captured_id_ = cur_err.assigned_error_id(); throw_exception( capturing_exception(std::current_exception(), std::move(ctx)) ); } } template <class R, class F, class... A> inline decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...)) capture_impl(is_result_tag<R, true>, context_ptr && ctx, F && f, A... a) { auto active_context = activate_context(*ctx); error_monitor cur_err; try { if( auto && r = std::forward<F>(f)(std::forward<A>(a)...) ) return std::move(r); else { ctx->captured_id_ = r.error(); return std::move(ctx); } } catch( capturing_exception const & ) { throw; } catch( exception_base const & e ) { ctx->captured_id_ = e.get_error_id(); throw_exception( capturing_exception(std::current_exception(), std::move(ctx)) ); } catch(...) { ctx->captured_id_ = cur_err.assigned_error_id(); throw_exception( capturing_exception(std::current_exception(), std::move(ctx)) ); } } template <class R, class Future> inline decltype(std::declval<Future>().get()) future_get_impl(is_result_tag<R, false>, Future & fut ) { try { return fut.get(); } catch( capturing_exception const & cap ) { cap.unload_and_rethrow_original_exception(); } } template <class R, class Future> inline decltype(std::declval<Future>().get()) future_get_impl(is_result_tag<R, true>, Future & fut ) { try { if( auto r = fut.get() ) return r; else return error_id(r.error()); // unloads } catch( capturing_exception const & cap ) { cap.unload_and_rethrow_original_exception(); } } } #endif template <class F, class... A> inline decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...)) capture(context_ptr && ctx, F && f, A... a) { using namespace leaf_detail; 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)...); } template <class Future> inline decltype(std::declval<Future>().get()) future_get( Future & fut ) { using namespace leaf_detail; return future_get_impl(is_result_tag<decltype(std::declval<Future>().get())>(), fut); } //////////////////////////////////////// #ifndef BOOST_LEAF_NO_EXCEPTIONS template <class T> class result; namespace leaf_detail { inline error_id catch_exceptions_helper( std::exception const & ex, leaf_detail_mp11::mp_list<> ) { return leaf::new_error(std::current_exception()); } template <class Ex1, class... Ex> inline error_id catch_exceptions_helper( std::exception const & ex, leaf_detail_mp11::mp_list<Ex1,Ex...> ) { if( Ex1 const * p = dynamic_cast<Ex1 const *>(&ex) ) return catch_exceptions_helper(ex, leaf_detail_mp11::mp_list<Ex...>{ }).load(*p); else return catch_exceptions_helper(ex, leaf_detail_mp11::mp_list<Ex...>{ }); } template <class T> struct deduce_exception_to_result_return_type_impl { using type = result<T>; }; template <class T> struct deduce_exception_to_result_return_type_impl<result<T>> { using type = result<T>; }; template <class T> using deduce_exception_to_result_return_type = typename deduce_exception_to_result_return_type_impl<T>::type; } template <class... Ex, class F> inline leaf_detail::deduce_exception_to_result_return_type<leaf_detail::fn_return_type<F>> exception_to_result( F && f ) noexcept { try { return std::forward<F>(f)(); } catch( std::exception const & ex ) { return leaf_detail::catch_exceptions_helper(ex, leaf_detail_mp11::mp_list<Ex...>()); } catch(...) { return leaf::new_error(std::current_exception()); } } #endif } } #endif
external/leaf/common.hpp +96 Changes for external/leaf/common.hpp: 96 added lines, 0 removed lines. Original line number Diff line number Diff line #ifndef BOOST_LEAF_COMMON_HPP_INCLUDED #define BOOST_LEAF_COMMON_HPP_INCLUDED // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc. // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_LEAF_ENABLE_WARNINGS # if defined(__clang__) # pragma clang system_header # elif (__GNUC__*100+__GNUC_MINOR__>301) # pragma GCC system_header # elif defined(_MSC_VER) # pragma warning(push,1) # endif #endif #include "detail/print.hpp" #include "../../../../../../usr/include/c++/8/string" #include "../../../../../../usr/include/c++/8/cerrno" #ifdef _WIN32 # include <Windows.h> # include <cstring> #ifdef min # undef min #endif #ifdef max # undef max #endif #endif namespace boost { namespace leaf { struct e_api_function { char const * value; }; struct e_file_name { std::string value; }; struct e_errno { int value; friend std::ostream & operator<<( std::ostream & os, e_errno const & err ) { return os << type<e_errno>() << ": " << err.value << ", \"" << std::strerror(err.value) << '"'; } }; struct e_type_info_name { char const * value; }; struct e_at_line { int value; }; namespace windows { struct e_LastError { unsigned value; #ifdef _WIN32 friend std::ostream & operator<<( std::ostream & os, e_LastError const & err ) { struct msg_buf { LPVOID * p; msg_buf(): p(0) { } ~msg_buf() noexcept { if(p) LocalFree(p); } }; msg_buf mb; if( FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, 0, err.value, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), (LPSTR)&mb.p, 0, 0) ) { BOOST_LEAF_ASSERT(mb.p != 0); char * z = std::strchr((LPSTR)mb.p,0); if( z[-1] == '\n' ) *--z = 0; if( z[-1] == '\r' ) *--z = 0; return os << type<e_LastError>() << ": " << err.value << ", \"" << (LPCSTR)mb.p << '"'; } return os; } #else // TODO : Other platforms #endif }; } } } #endif
external/leaf/context.hpp +461 Changes for external/leaf/context.hpp: 461 added lines, 0 removed lines. Original line number Diff line number Diff line #ifndef BOOST_LEAF_CONTEXT_HPP_INCLUDED #define BOOST_LEAF_CONTEXT_HPP_INCLUDED // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc. // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_LEAF_ENABLE_WARNINGS # if defined(__clang__) # pragma clang system_header # elif (__GNUC__*100+__GNUC_MINOR__>301) # pragma GCC system_header # elif defined(_MSC_VER) # pragma warning(push,1) # endif #endif #include "error.hpp" namespace boost { namespace leaf { class error_info; class diagnostic_info; class verbose_diagnostic_info; template <class> struct is_predicate: std::false_type { }; namespace leaf_detail { template <class T> struct is_exception: std::is_base_of<std::exception, typename std::decay<T>::type> { }; template <class E> struct handler_argument_traits; template <class E, bool IsPredicate = is_predicate<E>::value> struct handler_argument_traits_defaults; template <class E> struct handler_argument_traits_defaults<E, false> { using error_type = typename std::decay<E>::type; constexpr static bool always_available = false; template <class Tup> BOOST_LEAF_CONSTEXPR static error_type const * check( Tup const &, error_info const & ) noexcept; template <class Tup> BOOST_LEAF_CONSTEXPR static error_type * check( Tup &, error_info const & ) noexcept; template <class Tup> BOOST_LEAF_CONSTEXPR static E get( Tup & tup, error_info const & ei ) noexcept { return *check(tup, ei); } static_assert(!is_predicate<error_type>::value, "Handlers must take predicate arguments by value"); static_assert(!std::is_same<E, error_info>::value, "Handlers must take leaf::error_info arguments by const &"); static_assert(!std::is_same<E, diagnostic_info>::value, "Handlers must take leaf::diagnostic_info arguments by const &"); static_assert(!std::is_same<E, verbose_diagnostic_info>::value, "Handlers must take leaf::verbose_diagnostic_info arguments by const &"); }; template <class Pred> struct handler_argument_traits_defaults<Pred, true>: handler_argument_traits<typename Pred::error_type> { using base = handler_argument_traits<typename Pred::error_type>; static_assert(!base::always_available, "Predicates can't use types that are always_available"); template <class Tup> BOOST_LEAF_CONSTEXPR static bool check( Tup & tup, error_info const & ei ) noexcept { auto e = base::check(tup, ei); return e && Pred::evaluate(*e); }; template <class Tup> BOOST_LEAF_CONSTEXPR static Pred get( Tup const & tup, error_info const & ei ) noexcept { return Pred{*base::check(tup, ei)}; } }; template <class E> struct handler_argument_always_available { using error_type = E; constexpr static bool always_available = true; template <class Tup> BOOST_LEAF_CONSTEXPR static bool check( Tup &, error_info const & ) noexcept { return true; }; }; template <class E> struct handler_argument_traits: handler_argument_traits_defaults<E> { }; template <> struct handler_argument_traits<void> { using error_type = void; constexpr static bool always_available = false; template <class Tup> BOOST_LEAF_CONSTEXPR static std::exception const * check( Tup const &, error_info const & ) noexcept; }; template <class E> struct handler_argument_traits<E &&> { static_assert(sizeof(E) == 0, "Error handlers may not take rvalue ref arguments"); }; template <class E> struct handler_argument_traits<E *>: handler_argument_always_available<typename std::remove_const<E>::type> { template <class Tup> BOOST_LEAF_CONSTEXPR static E * get( Tup & tup, error_info const & ei) noexcept { return handler_argument_traits_defaults<E>::check(tup, ei); } }; template <> struct handler_argument_traits<error_info const &>: handler_argument_always_available<void> { template <class Tup> BOOST_LEAF_CONSTEXPR static error_info const & get( Tup const &, error_info const & ei ) noexcept { return ei; } }; template <class E> struct handler_argument_traits_require_by_value { static_assert(sizeof(E) == 0, "Error handlers must take this type by value"); }; } //////////////////////////////////////// namespace leaf_detail { template <int I, class Tuple> struct tuple_for_each { BOOST_LEAF_CONSTEXPR static void activate( Tuple & tup ) noexcept { static_assert(!std::is_same<error_info, typename std::decay<decltype(std::get<I-1>(tup))>::type>::value, "Bug in LEAF: context type deduction"); tuple_for_each<I-1,Tuple>::activate(tup); std::get<I-1>(tup).activate(); } BOOST_LEAF_CONSTEXPR static void deactivate( Tuple & tup ) noexcept { static_assert(!std::is_same<error_info, typename std::decay<decltype(std::get<I-1>(tup))>::type>::value, "Bug in LEAF: context type deduction"); std::get<I-1>(tup).deactivate(); tuple_for_each<I-1,Tuple>::deactivate(tup); } BOOST_LEAF_CONSTEXPR static void propagate( Tuple & tup ) noexcept { static_assert(!std::is_same<error_info, typename std::decay<decltype(std::get<I-1>(tup))>::type>::value, "Bug in LEAF: context type deduction"); auto & sl = std::get<I-1>(tup); sl.propagate(); tuple_for_each<I-1,Tuple>::propagate(tup); } BOOST_LEAF_CONSTEXPR static void propagate_captured( Tuple & tup, int err_id ) noexcept { static_assert(!std::is_same<error_info, typename std::decay<decltype(std::get<I-1>(tup))>::type>::value, "Bug in LEAF: context type deduction"); auto & sl = std::get<I-1>(tup); if( sl.has_value(err_id) ) load_slot(err_id, std::move(sl).value(err_id)); tuple_for_each<I-1,Tuple>::propagate_captured(tup, err_id); } static void print( std::ostream & os, void const * tup, int key_to_print ) { BOOST_LEAF_ASSERT(tup != 0); tuple_for_each<I-1,Tuple>::print(os, tup, key_to_print); std::get<I-1>(*static_cast<Tuple const *>(tup)).print(os, key_to_print); } }; template <class Tuple> struct tuple_for_each<0, Tuple> { BOOST_LEAF_CONSTEXPR static void activate( Tuple & ) noexcept { } BOOST_LEAF_CONSTEXPR static void deactivate( Tuple & ) noexcept { } BOOST_LEAF_CONSTEXPR static void propagate( Tuple & tup ) noexcept { } BOOST_LEAF_CONSTEXPR static void propagate_captured( Tuple & tup, int ) noexcept { } static void print( std::ostream &, void const *, int ) { } }; } //////////////////////////////////////////// #if BOOST_LEAF_DIAGNOSTICS namespace leaf_detail { template <class T> struct requires_unexpected { constexpr static bool value = false; }; template <class T> struct requires_unexpected<T const> { constexpr static bool value = requires_unexpected<T>::value; }; template <class T> struct requires_unexpected<T const &> { constexpr static bool value = requires_unexpected<T>::value; }; template <class T> struct requires_unexpected<T const *> { constexpr static bool value = requires_unexpected<T>::value; }; template <> struct requires_unexpected<e_unexpected_count> { constexpr static bool value = true; }; template <> struct requires_unexpected<e_unexpected_info> { constexpr static bool value = true; }; template <class L> struct unexpected_requested; template <template <class ...> class L> struct unexpected_requested<L<>> { constexpr static bool value = false; }; template <template <class...> class L, template <class> class S, class Car, class... Cdr> struct unexpected_requested<L<S<Car>, S<Cdr>...>> { constexpr static bool value = requires_unexpected<Car>::value || unexpected_requested<L<S<Cdr>...>>::value; }; } #endif //////////////////////////////////////////// namespace leaf_detail { template <class T> struct does_not_participate_in_context_deduction: std::false_type { }; template <> struct does_not_participate_in_context_deduction<void>: std::true_type { }; template <class L> struct deduce_e_type_list; template <template<class...> class L, class... T> struct deduce_e_type_list<L<T...>> { using type = leaf_detail_mp11::mp_remove_if< leaf_detail_mp11::mp_unique< leaf_detail_mp11::mp_list<typename handler_argument_traits<T>::error_type...> >, does_not_participate_in_context_deduction >; }; template <class L> struct deduce_e_tuple_impl; template <template <class...> class L, class... E> struct deduce_e_tuple_impl<L<E...>> { using type = std::tuple<slot<E>...>; }; template <class... E> using deduce_e_tuple = typename deduce_e_tuple_impl<typename deduce_e_type_list<leaf_detail_mp11::mp_list<E...>>::type>::type; } //////////////////////////////////////////// template <class... E> class context { context( context const & ) = delete; context & operator=( context const & ) = delete; using Tup = leaf_detail::deduce_e_tuple<E...>; Tup tup_; #if !defined(BOOST_LEAF_NO_THREADS) && !defined(NDEBUG) std::thread::id thread_id_; #endif bool is_active_; protected: BOOST_LEAF_CONSTEXPR error_id propagate_captured_errors( error_id err_id ) noexcept { leaf_detail::tuple_for_each<std::tuple_size<Tup>::value,Tup>::propagate_captured(tup_, err_id.value()); return err_id; } public: BOOST_LEAF_CONSTEXPR context( context && x ) noexcept: tup_(std::move(x.tup_)), is_active_(false) { BOOST_LEAF_ASSERT(!x.is_active()); } BOOST_LEAF_CONSTEXPR context() noexcept: is_active_(false) { } ~context() noexcept { BOOST_LEAF_ASSERT(!is_active()); } BOOST_LEAF_CONSTEXPR Tup const & tup() const noexcept { return tup_; } BOOST_LEAF_CONSTEXPR Tup & tup() noexcept { return tup_; } BOOST_LEAF_CONSTEXPR void activate() noexcept { using namespace leaf_detail; BOOST_LEAF_ASSERT(!is_active()); tuple_for_each<std::tuple_size<Tup>::value,Tup>::activate(tup_); #if BOOST_LEAF_DIAGNOSTICS if( unexpected_requested<Tup>::value ) ++tl_unexpected_enabled<>::counter; #endif #if !defined(BOOST_LEAF_NO_THREADS) && !defined(NDEBUG) thread_id_ = std::this_thread::get_id(); #endif is_active_ = true; } BOOST_LEAF_CONSTEXPR void deactivate() noexcept { using namespace leaf_detail; BOOST_LEAF_ASSERT(is_active()); is_active_ = false; #if !defined(BOOST_LEAF_NO_THREADS) && !defined(NDEBUG) BOOST_LEAF_ASSERT(std::this_thread::get_id() == thread_id_); thread_id_ = std::thread::id(); #endif #if BOOST_LEAF_DIAGNOSTICS if( unexpected_requested<Tup>::value ) --tl_unexpected_enabled<>::counter; #endif tuple_for_each<std::tuple_size<Tup>::value,Tup>::deactivate(tup_); } BOOST_LEAF_CONSTEXPR void propagate() noexcept { leaf_detail::tuple_for_each<std::tuple_size<Tup>::value,Tup>::propagate(tup_); } BOOST_LEAF_CONSTEXPR bool is_active() const noexcept { return is_active_; } void print( std::ostream & os ) const { leaf_detail::tuple_for_each<std::tuple_size<Tup>::value,Tup>::print(os, &tup_, 0); } template <class R, class... H> BOOST_LEAF_CONSTEXPR R handle_error( error_id, H && ... ) const; template <class R, class... H> BOOST_LEAF_CONSTEXPR R handle_error( error_id, H && ... ); }; //////////////////////////////////////// namespace leaf_detail { template <class TypeList> struct deduce_context_impl; template <template <class...> class L, class... E> struct deduce_context_impl<L<E...>> { using type = context<E...>; }; template <class TypeList> using deduce_context = typename deduce_context_impl<TypeList>::type; template <class H> struct fn_mp_args_fwd { using type = fn_mp_args<H>; }; template <class... H> struct fn_mp_args_fwd<std::tuple<H...> &>: fn_mp_args_fwd<std::tuple<H...>> { }; template <class... H> struct fn_mp_args_fwd<std::tuple<H...>> { using type = leaf_detail_mp11::mp_append<typename fn_mp_args_fwd<H>::type...>; }; template <class... H> struct context_type_from_handlers_impl { using type = deduce_context<leaf_detail_mp11::mp_append<typename fn_mp_args_fwd<H>::type...>>; }; template <class Ctx> struct polymorphic_context_impl: polymorphic_context, Ctx { error_id propagate_captured_errors() noexcept final override { return Ctx::propagate_captured_errors(captured_id_); } void activate() noexcept final override { Ctx::activate(); } void deactivate() noexcept final override { Ctx::deactivate(); } void propagate() noexcept final override { Ctx::propagate(); } bool is_active() const noexcept final override { return Ctx::is_active(); } void print( std::ostream & os ) const final override { return Ctx::print(os); } }; } template <class... H> using context_type_from_handlers = typename leaf_detail::context_type_from_handlers_impl<H...>::type; //////////////////////////////////////////// template <class... H> BOOST_LEAF_CONSTEXPR inline context_type_from_handlers<H...> make_context() noexcept { return { }; } template <class... H> BOOST_LEAF_CONSTEXPR inline context_type_from_handlers<H...> make_context( H && ... ) noexcept { return { }; } //////////////////////////////////////////// template <class... H> inline context_ptr make_shared_context() noexcept { return std::make_shared<leaf_detail::polymorphic_context_impl<context_type_from_handlers<H...>>>(); } template <class... H> inline context_ptr make_shared_context( H && ... ) noexcept { return std::make_shared<leaf_detail::polymorphic_context_impl<context_type_from_handlers<H...>>>(); } } } #endif
external/leaf/detail/all.hpp +19 Changes for external/leaf/detail/all.hpp: 19 added lines, 0 removed lines. Original line number Diff line number Diff line #ifndef BOOST_LEAF_ALL_HPP_INCLUDED #define BOOST_LEAF_ALL_HPP_INCLUDED // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc. // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #include <boost/leaf/capture.hpp> #include <boost/leaf/common.hpp> #include <boost/leaf/context.hpp> #include <boost/leaf/error.hpp> #include <boost/leaf/exception.hpp> #include <boost/leaf/handle_errors.hpp> #include <boost/leaf/on_error.hpp> #include <boost/leaf/pred.hpp> #include <boost/leaf/result.hpp> #endif