File indexing completed on 2025-01-18 09:39:13
0001 #ifndef BOOST_LEAF_TO_VARIANT_HPP_INCLUDED
0002 #define BOOST_LEAF_TO_VARIANT_HPP_INCLUDED
0003
0004
0005
0006
0007
0008
0009 #if __cplusplus >= 201703L
0010
0011 #include <boost/leaf/config.hpp>
0012 #include <boost/leaf/handle_errors.hpp>
0013 #include <boost/leaf/result.hpp>
0014 #include <variant>
0015 #include <optional>
0016 #include <tuple>
0017
0018 namespace boost { namespace leaf {
0019
0020 template <class... E, class TryBlock>
0021 std::variant<typename std::decay<decltype(std::declval<TryBlock>()().value())>::type,std::tuple<std::optional<E>...>>
0022 to_variant( TryBlock && try_block )
0023 {
0024 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");
0025 using T = typename std::decay<decltype(std::declval<TryBlock>()().value())>::type;
0026 using error_tuple_type = std::tuple<std::optional<E>...>;
0027 using variant_type = std::variant<T, error_tuple_type>;
0028 return try_handle_all(
0029 [&]() -> result<variant_type>
0030 {
0031 if( auto r = std::forward<TryBlock>(try_block)() )
0032 return *std::move(r);
0033 else
0034 return r.error();
0035 },
0036 []( E const * ... e ) -> variant_type
0037 {
0038 return error_tuple_type { e ? std::optional<E>(*e) : std::optional<E>{}... };
0039 },
0040 []() -> variant_type
0041 {
0042 return error_tuple_type { };
0043 } );
0044 }
0045
0046 } }
0047
0048 #endif
0049
0050 #endif