basic_result_final.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* Finaliser for a very simple result type
  2. (C) 2017-2021 Niall Douglas <http://www.nedproductions.biz/> (5 commits)
  3. File Created: Oct 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_BASIC_RESULT_FINAL_HPP
  26. #define BOOST_OUTCOME_BASIC_RESULT_FINAL_HPP
  27. #include "basic_result_error_observers.hpp"
  28. #include "basic_result_value_observers.hpp"
  29. BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN
  30. namespace detail
  31. {
  32. template <class R, class EC, class NoValuePolicy> using select_basic_result_impl = basic_result_error_observers<basic_result_value_observers<basic_result_storage<R, EC, NoValuePolicy>, R, NoValuePolicy>, EC, NoValuePolicy>;
  33. template <class R, class S, class NoValuePolicy>
  34. class basic_result_final
  35. #if defined(BOOST_OUTCOME_DOXYGEN_IS_IN_THE_HOUSE)
  36. : public basic_result_error_observers<basic_result_value_observers<basic_result_storage<R, S, NoValuePolicy>, R, NoValuePolicy>, S, NoValuePolicy>
  37. #else
  38. : public select_basic_result_impl<R, S, NoValuePolicy>
  39. #endif
  40. {
  41. using base = select_basic_result_impl<R, S, NoValuePolicy>;
  42. public:
  43. using base::base;
  44. constexpr explicit operator bool() const noexcept { return this->_state._status.have_value(); }
  45. constexpr bool has_value() const noexcept { return this->_state._status.have_value(); }
  46. constexpr bool has_error() const noexcept { return this->_state._status.have_error(); }
  47. constexpr bool has_exception() const noexcept { return this->_state._status.have_exception(); }
  48. constexpr bool has_lost_consistency() const noexcept { return this->_state._status.have_lost_consistency(); }
  49. constexpr bool has_failure() const noexcept { return this->_state._status.have_error() || this->_state._status.have_exception(); }
  50. BOOST_OUTCOME_TEMPLATE(class T, class U, class V)
  51. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(std::declval<detail::devoid<R>>() == std::declval<detail::devoid<T>>()), //
  52. BOOST_OUTCOME_TEXPR(std::declval<detail::devoid<S>>() == std::declval<detail::devoid<U>>()))
  53. constexpr bool operator==(const basic_result_final<T, U, V> &o) const noexcept( //
  54. noexcept(std::declval<detail::devoid<R>>() == std::declval<detail::devoid<T>>()) && noexcept(std::declval<detail::devoid<S>>() == std::declval<detail::devoid<U>>()))
  55. {
  56. if(this->_state._status.have_value() && o._state._status.have_value())
  57. {
  58. return this->_state._value == o._state._value; // NOLINT
  59. }
  60. if(this->_state._status.have_error() && o._state._status.have_error())
  61. {
  62. return this->_state._error == o._state._error;
  63. }
  64. return false;
  65. }
  66. BOOST_OUTCOME_TEMPLATE(class T)
  67. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(std::declval<R>() == std::declval<T>()))
  68. constexpr bool operator==(const success_type<T> &o) const noexcept( //
  69. noexcept(std::declval<R>() == std::declval<T>()))
  70. {
  71. if(this->_state._status.have_value())
  72. {
  73. return this->_state._value == o.value();
  74. }
  75. return false;
  76. }
  77. constexpr bool operator==(const success_type<void> &o) const noexcept
  78. {
  79. (void) o;
  80. return this->_state._status.have_value();
  81. }
  82. BOOST_OUTCOME_TEMPLATE(class T)
  83. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(std::declval<S>() == std::declval<T>()))
  84. constexpr bool operator==(const failure_type<T, void> &o) const noexcept( //
  85. noexcept(std::declval<S>() == std::declval<T>()))
  86. {
  87. if(this->_state._status.have_error())
  88. {
  89. return this->_state._error == o.error();
  90. }
  91. return false;
  92. }
  93. BOOST_OUTCOME_TEMPLATE(class T, class U, class V)
  94. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(std::declval<detail::devoid<R>>() != std::declval<detail::devoid<T>>()), //
  95. BOOST_OUTCOME_TEXPR(std::declval<detail::devoid<S>>() != std::declval<detail::devoid<U>>()))
  96. constexpr bool operator!=(const basic_result_final<T, U, V> &o) const noexcept( //
  97. noexcept(std::declval<detail::devoid<R>>() != std::declval<detail::devoid<T>>()) && noexcept(std::declval<detail::devoid<S>>() != std::declval<detail::devoid<U>>()))
  98. {
  99. if(this->_state._status.have_value() && o._state._status.have_value())
  100. {
  101. return this->_state._value != o._state._value;
  102. }
  103. if(this->_state._status.have_error() && o._state._status.have_error())
  104. {
  105. return this->_state._error != o._state._error;
  106. }
  107. return true;
  108. }
  109. BOOST_OUTCOME_TEMPLATE(class T)
  110. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(std::declval<R>() != std::declval<T>()))
  111. constexpr bool operator!=(const success_type<T> &o) const noexcept( //
  112. noexcept(std::declval<R>() != std::declval<T>()))
  113. {
  114. if(this->_state._status.have_value())
  115. {
  116. return this->_state._value != o.value();
  117. }
  118. return false;
  119. }
  120. constexpr bool operator!=(const success_type<void> &o) const noexcept
  121. {
  122. (void) o;
  123. return !this->_state._status.have_value();
  124. }
  125. BOOST_OUTCOME_TEMPLATE(class T)
  126. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(std::declval<S>() != std::declval<T>()))
  127. constexpr bool operator!=(const failure_type<T, void> &o) const noexcept( //
  128. noexcept(std::declval<S>() != std::declval<T>()))
  129. {
  130. if(this->_state._status.have_error())
  131. {
  132. return this->_state._error != o.error();
  133. }
  134. return true;
  135. }
  136. };
  137. template <class T, class U, class V, class W> constexpr inline bool operator==(const success_type<W> &a, const basic_result_final<T, U, V> &b) noexcept(noexcept(b == a)) { return b == a; }
  138. template <class T, class U, class V, class W> constexpr inline bool operator==(const failure_type<W, void> &a, const basic_result_final<T, U, V> &b) noexcept(noexcept(b == a)) { return b == a; }
  139. template <class T, class U, class V, class W> constexpr inline bool operator!=(const success_type<W> &a, const basic_result_final<T, U, V> &b) noexcept(noexcept(b == a)) { return b != a; }
  140. template <class T, class U, class V, class W> constexpr inline bool operator!=(const failure_type<W, void> &a, const basic_result_final<T, U, V> &b) noexcept(noexcept(b == a)) { return b != a; }
  141. } // namespace detail
  142. BOOST_OUTCOME_V2_NAMESPACE_END
  143. #endif