convert.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* Says how to convert value, error and exception types
  2. (C) 2017-2024 Niall Douglas <http://www.nedproductions.biz/> (12 commits)
  3. File Created: Nov 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_CONVERT_HPP
  26. #define BOOST_OUTCOME_CONVERT_HPP
  27. #include "detail/basic_result_storage.hpp"
  28. BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN
  29. namespace concepts
  30. {
  31. #if defined(__cpp_concepts)
  32. #if(defined(_MSC_VER) || defined(__clang__) || (defined(__GNUC__) && __cpp_concepts >= 201707) || BOOST_OUTCOME_FORCE_STD_BOOST_OUTCOME_C_CONCEPTS) && \
  33. !BOOST_OUTCOME_FORCE_LEGACY_GCC_BOOST_OUTCOME_C_CONCEPTS
  34. #define BOOST_OUTCOME_GCC6_CONCEPT_BOOL
  35. #else
  36. #ifndef BOOST_OUTCOME_SUPPRESS_LEGACY_CONCEPTS_WARNING
  37. #warning "WARNING: Legacy GCC concepts are known to fail to compile in a number of important situations!"
  38. #endif
  39. #define BOOST_OUTCOME_GCC6_CONCEPT_BOOL bool
  40. #endif
  41. namespace detail
  42. {
  43. template <class T, class U>
  44. concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL SameHelper = std::is_same<T, U>::value;
  45. template <class T, class U>
  46. concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL same_as = detail::SameHelper<T, U> && detail::SameHelper<U, T>;
  47. template <class T, class U>
  48. concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL convertible = std::is_convertible<T, U>::value;
  49. template <class T, class U>
  50. concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL base_of = std::is_base_of<T, U>::value;
  51. } // namespace detail
  52. /* The `value_or_none` concept.
  53. \requires That `U::value_type` exists and that `std::declval<U>().has_value()` returns a `bool` and `std::declval<U>().value()` exists.
  54. */
  55. template <class U>
  56. concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL value_or_none = requires(U a) {
  57. {
  58. a.has_value()
  59. } -> detail::same_as<bool>;
  60. {
  61. a.value()
  62. };
  63. };
  64. /* The `value_or_error` concept.
  65. \requires That `U::value_type` and `U::error_type` exist;
  66. that `std::declval<U>().has_value()` returns a `bool`, `std::declval<U>().value()` and `std::declval<U>().error()` exists.
  67. */
  68. template <class U>
  69. concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL value_or_error = requires(U a) {
  70. {
  71. a.has_value()
  72. } -> detail::same_as<bool>;
  73. {
  74. a.value()
  75. };
  76. {
  77. a.error()
  78. };
  79. };
  80. #else
  81. namespace detail
  82. {
  83. struct no_match
  84. {
  85. };
  86. inline no_match match_value_or_none(...);
  87. inline no_match match_value_or_error(...);
  88. BOOST_OUTCOME_TEMPLATE(class U)
  89. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(std::declval<U>().has_value()), BOOST_OUTCOME_TEXPR(std::declval<U>().value()))
  90. inline U match_value_or_none(U &&);
  91. BOOST_OUTCOME_TEMPLATE(class U)
  92. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(std::declval<U>().has_value()), BOOST_OUTCOME_TEXPR(std::declval<U>().value()), BOOST_OUTCOME_TEXPR(std::declval<U>().error()))
  93. inline U match_value_or_error(U &&);
  94. template <class U>
  95. static constexpr bool value_or_none =
  96. !std::is_same<no_match, decltype(match_value_or_none(std::declval<BOOST_OUTCOME_V2_NAMESPACE::detail::devoid<U>>()))>::value;
  97. template <class U>
  98. static constexpr bool value_or_error =
  99. !std::is_same<no_match, decltype(match_value_or_error(std::declval<BOOST_OUTCOME_V2_NAMESPACE::detail::devoid<U>>()))>::value;
  100. } // namespace detail
  101. /* The `value_or_none` concept.
  102. \requires That `U::value_type` exists and that `std::declval<U>().has_value()` returns a `bool` and `std::declval<U>().value()` exists.
  103. */
  104. template <class U> static constexpr bool value_or_none = detail::value_or_none<U>;
  105. /* The `value_or_error` concept.
  106. \requires That `U::value_type` and `U::error_type` exist;
  107. that `std::declval<U>().has_value()` returns a `bool`, `std::declval<U>().value()` and `std::declval<U>().error()` exists.
  108. */
  109. template <class U> static constexpr bool value_or_error = detail::value_or_error<U>;
  110. #endif
  111. } // namespace concepts
  112. namespace convert
  113. {
  114. #if BOOST_OUTCOME_ENABLE_LEGACY_SUPPORT_FOR < 220
  115. #if defined(__cpp_concepts)
  116. template <class U>
  117. concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL ValueOrNone = concepts::value_or_none<U>;
  118. template <class U>
  119. concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL ValueOrError = concepts::value_or_error<U>;
  120. #else
  121. template <class U> static constexpr bool ValueOrNone = concepts::value_or_none<U>;
  122. template <class U> static constexpr bool ValueOrError = concepts::value_or_error<U>;
  123. #endif
  124. #endif
  125. namespace detail
  126. {
  127. template <class T, class X> struct make_type
  128. {
  129. template <class U> static constexpr T value(U &&v) { return T{in_place_type<typename T::value_type>, static_cast<U &&>(v).value()}; }
  130. template <class U> static constexpr T error(U &&v) { return T{in_place_type<typename T::error_type>, static_cast<U &&>(v).error()}; }
  131. static constexpr T error() { return T{in_place_type<typename T::error_type>}; }
  132. };
  133. template <class T> struct make_type<T, void>
  134. {
  135. template <class U> static constexpr T value(U && /*unused*/) { return T{in_place_type<typename T::value_type>}; }
  136. template <class U> static constexpr T error(U && /*unused*/) { return T{in_place_type<typename T::error_type>}; }
  137. static constexpr T error() { return T{in_place_type<typename T::error_type>}; }
  138. };
  139. } // namespace detail
  140. /*! AWAITING HUGO JSON CONVERSION TOOL
  141. type definition value_or_error. Potential doc page: NOT FOUND
  142. */
  143. template <class T, class U> struct value_or_error
  144. {
  145. static constexpr bool enable_result_inputs = false;
  146. static constexpr bool enable_outcome_inputs = false;
  147. BOOST_OUTCOME_TEMPLATE(class X)
  148. BOOST_OUTCOME_TREQUIRES(
  149. BOOST_OUTCOME_TPRED(std::is_same<U, std::decay_t<X>>::value //
  150. &&concepts::value_or_error<U> //
  151. && (std::is_void<typename std::decay_t<X>::value_type>::value ||
  152. BOOST_OUTCOME_V2_NAMESPACE::detail::is_explicitly_constructible<typename T::value_type, typename std::decay_t<X>::value_type>) //
  153. &&(std::is_void<typename std::decay_t<X>::error_type>::value ||
  154. BOOST_OUTCOME_V2_NAMESPACE::detail::is_explicitly_constructible<typename T::error_type, typename std::decay_t<X>::error_type>) ))
  155. constexpr T operator()(X &&v)
  156. {
  157. return v.has_value() ? detail::make_type<T, typename T::value_type>::value(static_cast<X &&>(v)) :
  158. detail::make_type<T, typename U::error_type>::error(static_cast<X &&>(v));
  159. }
  160. };
  161. } // namespace convert
  162. BOOST_OUTCOME_V2_NAMESPACE_END
  163. #endif