Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:34:56

0001 #ifndef BOOST_LEAF_TO_VARIANT_HPP_INCLUDED
0002 #define BOOST_LEAF_TO_VARIANT_HPP_INCLUDED
0003 
0004 // Copyright 2018-2024 Emil Dotchevski and Reverge Studios, Inc.
0005 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0006 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0007 
0008 #if __cplusplus >= 201703L
0009 
0010 #include <boost/leaf/config.hpp>
0011 #include <boost/leaf/handle_errors.hpp>
0012 #include <boost/leaf/result.hpp>
0013 #include <variant>
0014 #include <optional>
0015 #include <tuple>
0016 
0017 namespace boost { namespace leaf {
0018 
0019 template <class... E, class TryBlock>
0020 std::variant<typename std::decay<decltype(std::declval<TryBlock>()().value())>::type,std::tuple<std::optional<E>...>>
0021 to_variant( TryBlock && try_block )
0022 {
0023     static_assert(is_result_type<decltype(std::declval<TryBlock>()())>::value, "The return type of the try_block passed to a to_variant function must be registered with leaf::is_result_type");
0024     using T = typename std::decay<decltype(std::declval<TryBlock>()().value())>::type;
0025     using error_tuple_type = std::tuple<std::optional<E>...>;
0026     using variant_type = std::variant<T, error_tuple_type>;
0027     return try_handle_all(
0028         [&]() -> result<variant_type>
0029         {
0030             if( auto r = std::forward<TryBlock>(try_block)() )
0031                 return *std::move(r);
0032             else
0033                 return r.error();
0034         },
0035         []( E const * ... e ) -> variant_type
0036         {
0037             return error_tuple_type { e ? std::optional<E>(*e) : std::optional<E>{}... };
0038         },
0039         []() -> variant_type
0040         {
0041             return error_tuple_type { };
0042         } );
0043 }
0044 
0045 } }
0046 
0047 #endif
0048 
0049 #endif // BOOST_LEAF_TO_VARIANT_HPP_INCLUDED