result.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // Copyright (c) 2023 Klemens D. Morgenstern
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_COBALT_RESULT_HPP
  6. #define BOOST_COBALT_RESULT_HPP
  7. #include <boost/cobalt/concepts.hpp>
  8. #include <boost/system/result.hpp>
  9. namespace boost::cobalt
  10. {
  11. namespace detail
  12. {
  13. template<typename T>
  14. concept result_error =
  15. requires (const T & t, const source_location & loc)
  16. {
  17. system::throw_exception_from_error(t, loc);
  18. }
  19. || // ADL
  20. requires (const T & t, const source_location & loc)
  21. {
  22. throw_exception_from_error(t, loc);
  23. }
  24. ;
  25. }
  26. inline constexpr auto interpret_as_result(std::tuple<> &&)
  27. {
  28. return system::result<void>();
  29. }
  30. template<typename Arg>
  31. auto interpret_as_result(std::tuple<Arg> && args)
  32. {
  33. if constexpr (detail::result_error<Arg>)
  34. {
  35. if (std::get<0>(args))
  36. return system::result<void, Arg>(system::in_place_error, std::get<0>(args));
  37. else
  38. return system::result<void, Arg>(system::in_place_value);
  39. }
  40. else
  41. return system::result<Arg>(std::move(std::get<0>(args)));
  42. }
  43. template<typename First, typename ... Args>
  44. requires (!detail::result_error<First> && sizeof...(Args) > 0u)
  45. auto interpret_as_result(std::tuple<First, Args...> && args) -> system::result<std::tuple<First, Args...>>
  46. {
  47. return std::move(args);
  48. }
  49. template<detail::result_error Error, typename ... Args>
  50. requires (sizeof...(Args) > 1u)
  51. auto interpret_as_result(std::tuple<Error, Args...> && args) -> system::result<std::tuple<Args...>, Error>
  52. {
  53. if (std::get<0>(args))
  54. return {system::in_place_error, std::move(std::get<0>(args))};
  55. return {
  56. system::in_place_value,
  57. std::apply([](auto, auto && ... rest) {return std::make_tuple(std::move(rest)...);})
  58. };
  59. }
  60. template<detail::result_error Error, typename Arg>
  61. auto interpret_as_result(std::tuple<Error, Arg> && args) -> system::result<Arg, Error>
  62. {
  63. if (std::get<0>(args))
  64. return {system::in_place_error, std::get<0>(args)};
  65. return {system::in_place_value, std::get<1>(std::move(args))};
  66. }
  67. struct as_result_tag {};
  68. struct as_tuple_tag {};
  69. template<awaitable Aw>
  70. struct as_result_t
  71. {
  72. as_result_t(Aw && aw) : aw_(std::forward<Aw>(aw)) {}
  73. bool await_ready() { return aw_.await_ready();}
  74. template<typename T>
  75. auto await_suspend(std::coroutine_handle<T> h) { return aw_.await_suspend(h);}
  76. auto await_resume()
  77. {
  78. if constexpr (requires {aw_.await_resume(as_result_tag{});})
  79. return aw_.await_resume(as_result_tag{});
  80. else
  81. {
  82. using type = decltype(aw_.await_resume());
  83. if constexpr (std::is_void_v<type>)
  84. {
  85. using res_t = system::result<type, std::exception_ptr>;
  86. try
  87. {
  88. aw_.await_resume();
  89. return res_t{system::in_place_value};
  90. }
  91. catch (...)
  92. {
  93. return res_t{system::in_place_error, std::current_exception()};
  94. }
  95. }
  96. else
  97. {
  98. using res_t = system::result<type, std::exception_ptr>;
  99. try
  100. {
  101. return res_t{system::in_place_value, aw_.await_resume()};
  102. }
  103. catch (...)
  104. {
  105. return res_t{system::in_place_error, std::current_exception()};
  106. }
  107. }
  108. }
  109. }
  110. private:
  111. Aw aw_;
  112. };
  113. template<awaitable Aw>
  114. as_result_t(Aw &&) -> as_result_t<Aw>;
  115. template<awaitable_type Aw>
  116. auto as_result(Aw && aw) -> as_result_t<Aw>
  117. {
  118. return as_result_t<Aw>(std::forward<Aw>(aw));
  119. }
  120. template<typename Aw>
  121. requires requires (Aw && aw)
  122. {
  123. {std::forward<Aw>(aw).operator co_await()} -> awaitable_type;
  124. }
  125. auto as_result(Aw && aw)
  126. {
  127. struct lazy_tuple
  128. {
  129. Aw aw;
  130. auto operator co_await ()
  131. {
  132. return as_result(std::forward<Aw>(aw).operator co_await());
  133. }
  134. };
  135. return lazy_tuple{std::forward<Aw>(aw)};
  136. }
  137. template<typename Aw>
  138. requires requires (Aw && aw)
  139. {
  140. {operator co_await(std::forward<Aw>(aw))} -> awaitable_type;
  141. }
  142. auto as_result(Aw && aw)
  143. {
  144. struct lazy_tuple
  145. {
  146. Aw aw;
  147. auto operator co_await ()
  148. {
  149. return as_result(operator co_await(std::forward<Aw>(aw)));
  150. }
  151. };
  152. return lazy_tuple{std::forward<Aw>(aw)};
  153. }
  154. template<awaitable Aw>
  155. struct as_tuple_t
  156. {
  157. as_tuple_t(Aw && aw) : aw_(std::forward<Aw>(aw)) {}
  158. bool await_ready() { return aw_.await_ready();}
  159. template<typename T>
  160. auto await_suspend(std::coroutine_handle<T> h) { return aw_.await_suspend(h);}
  161. auto await_resume()
  162. {
  163. if constexpr (requires {aw_.await_resume(as_tuple_tag{});})
  164. return aw_.await_resume(as_tuple_tag{});
  165. else
  166. {
  167. using type = decltype(aw_.await_resume());
  168. if constexpr (std::is_void_v<type>)
  169. {
  170. try
  171. {
  172. aw_.await_resume();
  173. return std::make_tuple(std::exception_ptr());
  174. }
  175. catch (...)
  176. {
  177. return make_tuple_(std::current_exception());
  178. }
  179. }
  180. else
  181. {
  182. try
  183. {
  184. return make_tuple_(std::exception_ptr(), aw_.await_resume());
  185. }
  186. catch (...)
  187. {
  188. return make_tuple_(std::current_exception(), type());
  189. }
  190. }
  191. }
  192. }
  193. private:
  194. template<typename ... Args>
  195. std::tuple<std::exception_ptr, Args...> make_tuple_(std::exception_ptr ep, std::tuple<Args...> && tup)
  196. {
  197. return std::apply(
  198. [&](auto ... args)
  199. {
  200. return std::make_tuple(std::move(ep), std::move(args)...);
  201. }, std::move(tup));
  202. }
  203. template<typename Arg>
  204. std::tuple<std::exception_ptr, Arg> make_tuple_(std::exception_ptr ep, Arg && arg)
  205. {
  206. return std::make_tuple(std::move(ep), std::move(arg));
  207. }
  208. private:
  209. Aw aw_;
  210. };
  211. template<awaitable Aw>
  212. as_tuple_t(Aw &&) -> as_tuple_t<Aw>;
  213. template<awaitable_type Aw>
  214. auto as_tuple(Aw && aw) -> as_tuple_t<Aw>
  215. {
  216. return as_tuple_t<Aw>(std::forward<Aw>(aw));
  217. }
  218. template<typename Aw>
  219. requires requires (Aw && aw)
  220. {
  221. {std::forward<Aw>(aw).operator co_await()} -> awaitable_type;
  222. }
  223. auto as_tuple(Aw && aw)
  224. {
  225. struct lazy_tuple
  226. {
  227. Aw aw;
  228. auto operator co_await ()
  229. {
  230. return as_tuple(std::forward<Aw>(aw).operator co_await());
  231. }
  232. };
  233. return lazy_tuple{std::forward<Aw>(aw)};
  234. }
  235. template<typename Aw>
  236. requires requires (Aw && aw)
  237. {
  238. {operator co_await(std::forward<Aw>(aw))} -> awaitable_type;
  239. }
  240. auto as_tuple(Aw && aw)
  241. {
  242. struct lazy_tuple
  243. {
  244. Aw aw;
  245. auto operator co_await ()
  246. {
  247. return as_tuple(operator co_await(std::forward<Aw>(aw)));
  248. }
  249. };
  250. return lazy_tuple{std::forward<Aw>(aw)};
  251. }
  252. }
  253. #endif //BOOST_COBALT_RESULT_HPP