try.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* Try operation macros
  2. (C) 2017-2019 Niall Douglas <http://www.nedproductions.biz/> (59 commits)
  3. File Created: July 2017
  4. Boost Software License - Version 1.0 - August 17th, 2003
  5. Permission is hereby granted, free of charge, to any person or organization
  6. obtaining a copy of the software and accompanying documentation covered by
  7. this license (the "Software") to use, reproduce, display, distribute,
  8. execute, and transmit the Software, and to prepare derivative works of the
  9. Software, and to permit third-parties to whom the Software is furnished to
  10. do so, all subject to the following:
  11. The copyright notices in the Software and this entire statement, including
  12. the above license grant, this restriction and the following disclaimer,
  13. must be included in all copies of the Software, in whole or in part, and
  14. all derivative works of the Software, unless such copies or derivative
  15. works are solely in the form of machine-executable object code generated by
  16. a source language processor.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  20. SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  21. FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  22. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef BOOST_OUTCOME_TRY_HPP
  26. #define BOOST_OUTCOME_TRY_HPP
  27. #include "success_failure.hpp"
  28. namespace std // NOLINT
  29. {
  30. namespace experimental
  31. {
  32. template <class T, class E> class expected;
  33. template <class E> class unexpected;
  34. } // namespace experimental
  35. } // namespace std
  36. BOOST_OUTCOME_V2_NAMESPACE_BEGIN
  37. /*! AWAITING HUGO JSON CONVERSION TOOL
  38. SIGNATURE NOT RECOGNISED
  39. */
  40. BOOST_OUTCOME_TEMPLATE(class T)
  41. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(std::declval<T>().as_failure()))
  42. inline decltype(auto) try_operation_return_as(T &&v)
  43. {
  44. return static_cast<T &&>(v).as_failure();
  45. }
  46. /*! AWAITING HUGO JSON CONVERSION TOOL
  47. SIGNATURE NOT RECOGNISED
  48. */
  49. template <class T, class E> inline auto try_operation_return_as(const std::experimental::expected<T, E> &v)
  50. {
  51. return std::experimental::unexpected<E>(v.error());
  52. }
  53. /*! AWAITING HUGO JSON CONVERSION TOOL
  54. SIGNATURE NOT RECOGNISED
  55. */
  56. template <class T, class E> inline auto try_operation_return_as(std::experimental::expected<T, E> &&v)
  57. {
  58. return std::experimental::unexpected<E>(static_cast<std::experimental::expected<T, E> &&>(v).error());
  59. }
  60. namespace detail
  61. {
  62. BOOST_OUTCOME_TEMPLATE(class T)
  63. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(std::declval<T>().assume_value()))
  64. inline decltype(auto) try_extract_value(T &&v) { return static_cast<T &&>(v).assume_value(); }
  65. template <class T, class... Args> inline decltype(auto) try_extract_value(T &&v, Args &&... /*unused*/) { return static_cast<T &&>(v).value(); }
  66. } // namespace detail
  67. BOOST_OUTCOME_V2_NAMESPACE_END
  68. #define BOOST_OUTCOME_TRY_GLUE2(x, y) x##y
  69. #define BOOST_OUTCOME_TRY_GLUE(x, y) BOOST_OUTCOME_TRY_GLUE2(x, y)
  70. #define BOOST_OUTCOME_TRY_UNIQUE_NAME BOOST_OUTCOME_TRY_GLUE(_outcome_try_unique_name_temporary, __COUNTER__)
  71. #define BOOST_OUTCOME_TRY_RETURN_ARG_COUNT(_1_, _2_, _3_, _4_, _5_, _6_, _7_, _8_, count, ...) count
  72. #define BOOST_OUTCOME_TRY_EXPAND_ARGS(args) BOOST_OUTCOME_TRY_RETURN_ARG_COUNT args
  73. #define BOOST_OUTCOME_TRY_COUNT_ARGS_MAX8(...) BOOST_OUTCOME_TRY_EXPAND_ARGS((__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1, 0))
  74. #define BOOST_OUTCOME_TRY_OVERLOAD_MACRO2(name, count) name##count
  75. #define BOOST_OUTCOME_TRY_OVERLOAD_MACRO1(name, count) BOOST_OUTCOME_TRY_OVERLOAD_MACRO2(name, count)
  76. #define BOOST_OUTCOME_TRY_OVERLOAD_MACRO(name, count) BOOST_OUTCOME_TRY_OVERLOAD_MACRO1(name, count)
  77. #define BOOST_OUTCOME_TRY_OVERLOAD_GLUE(x, y) x y
  78. #define BOOST_OUTCOME_TRY_CALL_OVERLOAD(name, ...) BOOST_OUTCOME_TRY_OVERLOAD_GLUE(BOOST_OUTCOME_TRY_OVERLOAD_MACRO(name, BOOST_OUTCOME_TRY_COUNT_ARGS_MAX8(__VA_ARGS__)), (__VA_ARGS__))
  79. #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 8
  80. #pragma GCC diagnostic push
  81. #pragma GCC diagnostic ignored "-Wparentheses"
  82. #endif
  83. #define BOOST_OUTCOME_TRYV2(unique, ...) \
  84. auto && (unique) = (__VA_ARGS__); \
  85. if(!(unique).has_value()) \
  86. return BOOST_OUTCOME_V2_NAMESPACE::try_operation_return_as(static_cast<decltype(unique) &&>(unique))
  87. #define BOOST_OUTCOME_TRY2(unique, v, ...) \
  88. BOOST_OUTCOME_TRYV2(unique, __VA_ARGS__); \
  89. auto && (v) = BOOST_OUTCOME_V2_NAMESPACE::detail::try_extract_value(static_cast<decltype(unique) &&>(unique))
  90. #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 8
  91. #pragma GCC diagnostic pop
  92. #endif
  93. /*! AWAITING HUGO JSON CONVERSION TOOL
  94. SIGNATURE NOT RECOGNISED
  95. */
  96. #define BOOST_OUTCOME_TRYV(...) BOOST_OUTCOME_TRYV2(BOOST_OUTCOME_TRY_UNIQUE_NAME, __VA_ARGS__)
  97. #if defined(__GNUC__) || defined(__clang__)
  98. /*! AWAITING HUGO JSON CONVERSION TOOL
  99. SIGNATURE NOT RECOGNISED
  100. */
  101. #define BOOST_OUTCOME_TRYX(...) \
  102. ({ \
  103. auto &&res = (__VA_ARGS__); \
  104. if(!res.has_value()) \
  105. return BOOST_OUTCOME_V2_NAMESPACE::try_operation_return_as(static_cast<decltype(res) &&>(res)); \
  106. BOOST_OUTCOME_V2_NAMESPACE::detail::try_extract_value(static_cast<decltype(res) &&>(res)); \
  107. \
  108. })
  109. #endif
  110. /*! AWAITING HUGO JSON CONVERSION TOOL
  111. SIGNATURE NOT RECOGNISED
  112. */
  113. #define BOOST_OUTCOME_TRYA(v, ...) BOOST_OUTCOME_TRY2(BOOST_OUTCOME_TRY_UNIQUE_NAME, v, __VA_ARGS__)
  114. #define BOOST_OUTCOME_TRY_INVOKE_TRY8(a, b, c, d, e, f, g, h) BOOST_OUTCOME_TRYA(a, b, c, d, e, f, g, h)
  115. #define BOOST_OUTCOME_TRY_INVOKE_TRY7(a, b, c, d, e, f, g) BOOST_OUTCOME_TRYA(a, b, c, d, e, f, g)
  116. #define BOOST_OUTCOME_TRY_INVOKE_TRY6(a, b, c, d, e, f) BOOST_OUTCOME_TRYA(a, b, c, d, e, f)
  117. #define BOOST_OUTCOME_TRY_INVOKE_TRY5(a, b, c, d, e) BOOST_OUTCOME_TRYA(a, b, c, d, e)
  118. #define BOOST_OUTCOME_TRY_INVOKE_TRY4(a, b, c, d) BOOST_OUTCOME_TRYA(a, b, c, d)
  119. #define BOOST_OUTCOME_TRY_INVOKE_TRY3(a, b, c) BOOST_OUTCOME_TRYA(a, b, c)
  120. #define BOOST_OUTCOME_TRY_INVOKE_TRY2(a, b) BOOST_OUTCOME_TRYA(a, b)
  121. #define BOOST_OUTCOME_TRY_INVOKE_TRY1(a) BOOST_OUTCOME_TRYV(a)
  122. /*! AWAITING HUGO JSON CONVERSION TOOL
  123. SIGNATURE NOT RECOGNISED
  124. */
  125. #define BOOST_OUTCOME_TRY(...) BOOST_OUTCOME_TRY_CALL_OVERLOAD(BOOST_OUTCOME_TRY_INVOKE_TRY, __VA_ARGS__)
  126. #endif