convert.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Says how to convert value, error and exception types
  2. (C) 2017-2019 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 convert
  30. {
  31. #if defined(__cpp_concepts)
  32. #if !defined(_MSC_VER) && !defined(__clang__) && __GNUC__ < 9
  33. #define BOOST_OUTCOME_GCC6_CONCEPT_BOOL bool
  34. #else
  35. #define BOOST_OUTCOME_GCC6_CONCEPT_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. } // namespace detail
  42. /* The `ValueOrNone` concept.
  43. \requires That `U::value_type` exists and that `std::declval<U>().has_value()` returns a `bool` and `std::declval<U>().value()` exists.
  44. */
  45. template <class U> concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL ValueOrNone = requires(U a)
  46. {
  47. {
  48. a.has_value()
  49. }
  50. ->detail::same_as<bool>;
  51. {a.value()};
  52. };
  53. /* The `ValueOrError` concept.
  54. \requires That `U::value_type` and `U::error_type` exist;
  55. that `std::declval<U>().has_value()` returns a `bool`, `std::declval<U>().value()` and `std::declval<U>().error()` exists.
  56. */
  57. template <class U> concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL ValueOrError = requires(U a)
  58. {
  59. {
  60. a.has_value()
  61. }
  62. ->detail::same_as<bool>;
  63. {a.value()};
  64. {a.error()};
  65. };
  66. #else
  67. namespace detail
  68. {
  69. struct no_match
  70. {
  71. };
  72. inline no_match match_value_or_none(...);
  73. inline no_match match_value_or_error(...);
  74. BOOST_OUTCOME_TEMPLATE(class U)
  75. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(std::declval<U>().has_value()), BOOST_OUTCOME_TEXPR(std::declval<U>().value()))
  76. inline U match_value_or_none(U &&);
  77. BOOST_OUTCOME_TEMPLATE(class U)
  78. 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()))
  79. inline U match_value_or_error(U &&);
  80. template <class U> static constexpr bool ValueOrNone = !std::is_same<no_match, decltype(match_value_or_none(std::declval<BOOST_OUTCOME_V2_NAMESPACE::detail::devoid<U>>()))>::value;
  81. template <class U> static constexpr bool ValueOrError = !std::is_same<no_match, decltype(match_value_or_error(std::declval<BOOST_OUTCOME_V2_NAMESPACE::detail::devoid<U>>()))>::value;
  82. } // namespace detail
  83. /* The `ValueOrNone` concept.
  84. \requires That `U::value_type` exists and that `std::declval<U>().has_value()` returns a `bool` and `std::declval<U>().value()` exists.
  85. */
  86. template <class U> static constexpr bool ValueOrNone = detail::ValueOrNone<U>;
  87. /* The `ValueOrError` concept.
  88. \requires That `U::value_type` and `U::error_type` exist;
  89. that `std::declval<U>().has_value()` returns a `bool`, `std::declval<U>().value()` and `std::declval<U>().error()` exists.
  90. */
  91. template <class U> static constexpr bool ValueOrError = detail::ValueOrError<U>;
  92. #endif
  93. namespace detail
  94. {
  95. template <class T, class X> struct make_type
  96. {
  97. template <class U> static constexpr T value(U &&v) { return T{in_place_type<typename T::value_type>, static_cast<U &&>(v).value()}; }
  98. template <class U> static constexpr T error(U &&v) { return T{in_place_type<typename T::error_type>, static_cast<U &&>(v).error()}; }
  99. static constexpr T error() { return T{in_place_type<typename T::error_type>}; }
  100. };
  101. template <class T> struct make_type<T, void>
  102. {
  103. template <class U> static constexpr T value(U && /*unused*/) { return T{in_place_type<typename T::value_type>}; }
  104. template <class U> static constexpr T error(U && /*unused*/) { return T{in_place_type<typename T::error_type>}; }
  105. static constexpr T error() { return T{in_place_type<typename T::error_type>}; }
  106. };
  107. } // namespace detail
  108. /*! AWAITING HUGO JSON CONVERSION TOOL
  109. type definition value_or_error. Potential doc page: NOT FOUND
  110. */
  111. template <class T, class U> struct value_or_error
  112. {
  113. static constexpr bool enable_result_inputs = false;
  114. static constexpr bool enable_outcome_inputs = false;
  115. BOOST_OUTCOME_TEMPLATE(class X)
  116. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_same<U, std::decay_t<X>>::value //
  117. &&ValueOrError<U> //
  118. && (std::is_void<typename std::decay_t<X>::value_type>::value || BOOST_OUTCOME_V2_NAMESPACE::detail::is_explicitly_constructible<typename T::value_type, typename std::decay_t<X>::value_type>) //
  119. &&(std::is_void<typename std::decay_t<X>::error_type>::value || BOOST_OUTCOME_V2_NAMESPACE::detail::is_explicitly_constructible<typename T::error_type, typename std::decay_t<X>::error_type>) ))
  120. constexpr T operator()(X &&v) { return v.has_value() ? detail::make_type<T, typename T::value_type>::value(static_cast<X &&>(v)) : detail::make_type<T, typename U::error_type>::error(static_cast<X &&>(v)); }
  121. };
  122. } // namespace convert
  123. BOOST_OUTCOME_V2_NAMESPACE_END
  124. #endif