convert.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* Says how to convert value, error and exception types
  2. (C) 2017-2021 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) && !BOOST_OUTCOME_FORCE_LEGACY_GCC_BOOST_OUTCOME_C_CONCEPTS
  33. #define BOOST_OUTCOME_GCC6_CONCEPT_BOOL
  34. #else
  35. #define BOOST_OUTCOME_GCC6_CONCEPT_BOOL bool
  36. #endif
  37. namespace detail
  38. {
  39. template <class T, class U> concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL SameHelper = std::is_same<T, U>::value;
  40. template <class T, class U> concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL same_as = detail::SameHelper<T, U> &&detail::SameHelper<U, T>;
  41. template <class T, class U> concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL convertible = std::is_convertible<T, U>::value;
  42. template <class T, class U> concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL base_of = std::is_base_of<T, U>::value;
  43. } // namespace detail
  44. /* The `value_or_none` concept.
  45. \requires That `U::value_type` exists and that `std::declval<U>().has_value()` returns a `bool` and `std::declval<U>().value()` exists.
  46. */
  47. template <class U> concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL value_or_none = requires(U a)
  48. {
  49. {
  50. a.has_value()
  51. }
  52. ->detail::same_as<bool>;
  53. {a.value()};
  54. };
  55. /* The `value_or_error` concept.
  56. \requires That `U::value_type` and `U::error_type` exist;
  57. that `std::declval<U>().has_value()` returns a `bool`, `std::declval<U>().value()` and `std::declval<U>().error()` exists.
  58. */
  59. template <class U> concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL value_or_error = requires(U a)
  60. {
  61. {
  62. a.has_value()
  63. }
  64. ->detail::same_as<bool>;
  65. {a.value()};
  66. {a.error()};
  67. };
  68. #else
  69. namespace detail
  70. {
  71. struct no_match
  72. {
  73. };
  74. inline no_match match_value_or_none(...);
  75. inline no_match match_value_or_error(...);
  76. BOOST_OUTCOME_TEMPLATE(class U)
  77. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(std::declval<U>().has_value()), BOOST_OUTCOME_TEXPR(std::declval<U>().value()))
  78. inline U match_value_or_none(U &&);
  79. BOOST_OUTCOME_TEMPLATE(class U)
  80. 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()))
  81. inline U match_value_or_error(U &&);
  82. template <class U>
  83. static constexpr bool value_or_none =
  84. !std::is_same<no_match, decltype(match_value_or_none(std::declval<BOOST_OUTCOME_V2_NAMESPACE::detail::devoid<U>>()))>::value;
  85. template <class U>
  86. static constexpr bool value_or_error =
  87. !std::is_same<no_match, decltype(match_value_or_error(std::declval<BOOST_OUTCOME_V2_NAMESPACE::detail::devoid<U>>()))>::value;
  88. } // namespace detail
  89. /* The `value_or_none` concept.
  90. \requires That `U::value_type` exists and that `std::declval<U>().has_value()` returns a `bool` and `std::declval<U>().value()` exists.
  91. */
  92. template <class U> static constexpr bool value_or_none = detail::value_or_none<U>;
  93. /* The `value_or_error` concept.
  94. \requires That `U::value_type` and `U::error_type` exist;
  95. that `std::declval<U>().has_value()` returns a `bool`, `std::declval<U>().value()` and `std::declval<U>().error()` exists.
  96. */
  97. template <class U> static constexpr bool value_or_error = detail::value_or_error<U>;
  98. #endif
  99. } // namespace concepts
  100. namespace convert
  101. {
  102. #if BOOST_OUTCOME_ENABLE_LEGACY_SUPPORT_FOR < 220
  103. #if defined(__cpp_concepts)
  104. template <class U> concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL ValueOrNone = concepts::value_or_none<U>;
  105. template <class U> concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL ValueOrError = concepts::value_or_error<U>;
  106. #else
  107. template <class U> static constexpr bool ValueOrNone = concepts::value_or_none<U>;
  108. template <class U> static constexpr bool ValueOrError = concepts::value_or_error<U>;
  109. #endif
  110. #endif
  111. namespace detail
  112. {
  113. template <class T, class X> struct make_type
  114. {
  115. template <class U> static constexpr T value(U &&v) { return T{in_place_type<typename T::value_type>, static_cast<U &&>(v).value()}; }
  116. template <class U> static constexpr T error(U &&v) { return T{in_place_type<typename T::error_type>, static_cast<U &&>(v).error()}; }
  117. static constexpr T error() { return T{in_place_type<typename T::error_type>}; }
  118. };
  119. template <class T> struct make_type<T, void>
  120. {
  121. template <class U> static constexpr T value(U && /*unused*/) { return T{in_place_type<typename T::value_type>}; }
  122. template <class U> static constexpr T error(U && /*unused*/) { return T{in_place_type<typename T::error_type>}; }
  123. static constexpr T error() { return T{in_place_type<typename T::error_type>}; }
  124. };
  125. } // namespace detail
  126. /*! AWAITING HUGO JSON CONVERSION TOOL
  127. type definition value_or_error. Potential doc page: NOT FOUND
  128. */
  129. template <class T, class U> struct value_or_error
  130. {
  131. static constexpr bool enable_result_inputs = false;
  132. static constexpr bool enable_outcome_inputs = false;
  133. BOOST_OUTCOME_TEMPLATE(class X)
  134. BOOST_OUTCOME_TREQUIRES(
  135. BOOST_OUTCOME_TPRED(std::is_same<U, std::decay_t<X>>::value //
  136. &&concepts::value_or_error<U> //
  137. && (std::is_void<typename std::decay_t<X>::value_type>::value ||
  138. BOOST_OUTCOME_V2_NAMESPACE::detail::is_explicitly_constructible<typename T::value_type, typename std::decay_t<X>::value_type>) //
  139. &&(std::is_void<typename std::decay_t<X>::error_type>::value ||
  140. BOOST_OUTCOME_V2_NAMESPACE::detail::is_explicitly_constructible<typename T::error_type, typename std::decay_t<X>::error_type>) ))
  141. constexpr T operator()(X &&v)
  142. {
  143. return v.has_value() ? detail::make_type<T, typename T::value_type>::value(static_cast<X &&>(v)) :
  144. detail::make_type<T, typename U::error_type>::error(static_cast<X &&>(v));
  145. }
  146. };
  147. } // namespace convert
  148. BOOST_OUTCOME_V2_NAMESPACE_END
  149. #endif