to_variant.hpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #ifndef BOOST_LEAF_TO_VARIANT_HPP_INCLUDED
  2. #define BOOST_LEAF_TO_VARIANT_HPP_INCLUDED
  3. // Copyright 2018-2023 Emil Dotchevski and Reverge Studios, Inc.
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #if __cplusplus >= 201703L
  7. #include <boost/leaf/config.hpp>
  8. #include <boost/leaf/handle_errors.hpp>
  9. #include <boost/leaf/result.hpp>
  10. #include <variant>
  11. #include <optional>
  12. #include <tuple>
  13. namespace boost { namespace leaf {
  14. template <class... E, class TryBlock>
  15. std::variant<typename std::decay<decltype(std::declval<TryBlock>()().value())>::type,std::tuple<std::optional<E>...>>
  16. to_variant( TryBlock && try_block )
  17. {
  18. 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");
  19. using T = typename std::decay<decltype(std::declval<TryBlock>()().value())>::type;
  20. using error_tuple_type = std::tuple<std::optional<E>...>;
  21. using variant_type = std::variant<T, error_tuple_type>;
  22. return try_handle_all(
  23. [&]() -> result<variant_type>
  24. {
  25. if( auto r = std::forward<TryBlock>(try_block)() )
  26. return *std::move(r);
  27. else
  28. return r.error();
  29. },
  30. []( E const * ... e ) -> variant_type
  31. {
  32. return error_tuple_type { e ? std::optional<E>(*e) : std::optional<E>{}... };
  33. },
  34. []() -> variant_type
  35. {
  36. return error_tuple_type { };
  37. } );
  38. }
  39. } }
  40. #endif
  41. #endif