| 1 | /* Finaliser for a very simple result type |
| 2 | (C) 2017-2024 Niall Douglas <http://www.nedproductions.biz/> (5 commits) |
| 3 | File Created: Oct 2017 |
| 4 | |
| 5 | |
| 6 | Boost Software License - Version 1.0 - August 17th, 2003 |
| 7 | |
| 8 | Permission is hereby granted, free of charge, to any person or organization |
| 9 | obtaining a copy of the software and accompanying documentation covered by |
| 10 | this license (the "Software") to use, reproduce, display, distribute, |
| 11 | execute, and transmit the Software, and to prepare derivative works of the |
| 12 | Software, and to permit third-parties to whom the Software is furnished to |
| 13 | do so, all subject to the following: |
| 14 | |
| 15 | The copyright notices in the Software and this entire statement, including |
| 16 | the above license grant, this restriction and the following disclaimer, |
| 17 | must be included in all copies of the Software, in whole or in part, and |
| 18 | all derivative works of the Software, unless such copies or derivative |
| 19 | works are solely in the form of machine-executable object code generated by |
| 20 | a source language processor. |
| 21 | |
| 22 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 23 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 24 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
| 25 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
| 26 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
| 27 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 28 | DEALINGS IN THE SOFTWARE. |
| 29 | */ |
| 30 | |
| 31 | #ifndef BOOST_OUTCOME_BASIC_RESULT_FINAL_HPP |
| 32 | #define BOOST_OUTCOME_BASIC_RESULT_FINAL_HPP |
| 33 | |
| 34 | #include "basic_result_error_observers.hpp" |
| 35 | #include "basic_result_value_observers.hpp" |
| 36 | |
| 37 | BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN |
| 38 | |
| 39 | namespace detail |
| 40 | { |
| 41 | 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>; |
| 42 | |
| 43 | template <class R, class S, class NoValuePolicy> |
| 44 | class basic_result_final |
| 45 | #if defined(BOOST_OUTCOME_DOXYGEN_IS_IN_THE_HOUSE) |
| 46 | : public basic_result_error_observers<basic_result_value_observers<basic_result_storage<R, S, NoValuePolicy>, R, NoValuePolicy>, S, NoValuePolicy> |
| 47 | #else |
| 48 | : public select_basic_result_impl<R, S, NoValuePolicy> |
| 49 | #endif |
| 50 | { |
| 51 | using base = select_basic_result_impl<R, S, NoValuePolicy>; |
| 52 | |
| 53 | public: |
| 54 | using base::base; |
| 55 | |
| 56 | constexpr explicit operator bool() const noexcept { return this->_state._status.have_value(); } |
| 57 | constexpr bool has_value() const noexcept { return this->_state._status.have_value(); } |
| 58 | constexpr bool has_error() const noexcept { return this->_state._status.have_error(); } |
| 59 | constexpr bool has_exception() const noexcept { return this->_state._status.have_exception(); } |
| 60 | constexpr bool has_lost_consistency() const noexcept { return this->_state._status.have_lost_consistency(); } |
| 61 | constexpr bool has_failure() const noexcept { return this->_state._status.have_error() || this->_state._status.have_exception(); } |
| 62 | |
| 63 | BOOST_OUTCOME_TEMPLATE(class T, class U, class V) |
| 64 | BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(std::declval<detail::devoid<R>>() == std::declval<detail::devoid<T>>()), // |
| 65 | BOOST_OUTCOME_TEXPR(std::declval<detail::devoid<S>>() == std::declval<detail::devoid<U>>())) |
| 66 | constexpr bool operator==(const basic_result_final<T, U, V> &o) const noexcept( // |
| 67 | noexcept(std::declval<detail::devoid<R>>() == std::declval<detail::devoid<T>>()) && noexcept(std::declval<detail::devoid<S>>() == std::declval<detail::devoid<U>>())) |
| 68 | { |
| 69 | if(this->_state._status.have_value() && o._state._status.have_value()) |
| 70 | { |
| 71 | return this->_state._value == o._state._value; // NOLINT |
| 72 | } |
| 73 | if(this->_state._status.have_error() && o._state._status.have_error()) |
| 74 | { |
| 75 | return this->_state._error == o._state._error; |
| 76 | } |
| 77 | return false; |
| 78 | } |
| 79 | BOOST_OUTCOME_TEMPLATE(class T) |
| 80 | BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(std::declval<R>() == std::declval<T>())) |
| 81 | constexpr bool operator==(const success_type<T> &o) const noexcept( // |
| 82 | noexcept(std::declval<R>() == std::declval<T>())) |
| 83 | { |
| 84 | if(this->_state._status.have_value()) |
| 85 | { |
| 86 | return this->_state._value == o.value(); |
| 87 | } |
| 88 | return false; |
| 89 | } |
| 90 | constexpr bool operator==(const success_type<void> &o) const noexcept |
| 91 | { |
| 92 | (void) o; |
| 93 | return this->_state._status.have_value(); |
| 94 | } |
| 95 | BOOST_OUTCOME_TEMPLATE(class T) |
| 96 | BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(std::declval<S>() == std::declval<T>())) |
| 97 | constexpr bool operator==(const failure_type<T, void> &o) const noexcept( // |
| 98 | noexcept(std::declval<S>() == std::declval<T>())) |
| 99 | { |
| 100 | if(this->_state._status.have_error()) |
| 101 | { |
| 102 | return this->_state._error == o.error(); |
| 103 | } |
| 104 | return false; |
| 105 | } |
| 106 | BOOST_OUTCOME_TEMPLATE(class T, class U, class V) |
| 107 | BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(std::declval<detail::devoid<R>>() != std::declval<detail::devoid<T>>()), // |
| 108 | BOOST_OUTCOME_TEXPR(std::declval<detail::devoid<S>>() != std::declval<detail::devoid<U>>())) |
| 109 | constexpr bool operator!=(const basic_result_final<T, U, V> &o) const noexcept( // |
| 110 | noexcept(std::declval<detail::devoid<R>>() != std::declval<detail::devoid<T>>()) && noexcept(std::declval<detail::devoid<S>>() != std::declval<detail::devoid<U>>())) |
| 111 | { |
| 112 | if(this->_state._status.have_value() && o._state._status.have_value()) |
| 113 | { |
| 114 | return this->_state._value != o._state._value; |
| 115 | } |
| 116 | if(this->_state._status.have_error() && o._state._status.have_error()) |
| 117 | { |
| 118 | return this->_state._error != o._state._error; |
| 119 | } |
| 120 | return true; |
| 121 | } |
| 122 | BOOST_OUTCOME_TEMPLATE(class T) |
| 123 | BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(std::declval<R>() != std::declval<T>())) |
| 124 | constexpr bool operator!=(const success_type<T> &o) const noexcept( // |
| 125 | noexcept(std::declval<R>() != std::declval<T>())) |
| 126 | { |
| 127 | if(this->_state._status.have_value()) |
| 128 | { |
| 129 | return this->_state._value != o.value(); |
| 130 | } |
| 131 | return false; |
| 132 | } |
| 133 | constexpr bool operator!=(const success_type<void> &o) const noexcept |
| 134 | { |
| 135 | (void) o; |
| 136 | return !this->_state._status.have_value(); |
| 137 | } |
| 138 | BOOST_OUTCOME_TEMPLATE(class T) |
| 139 | BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(std::declval<S>() != std::declval<T>())) |
| 140 | constexpr bool operator!=(const failure_type<T, void> &o) const noexcept( // |
| 141 | noexcept(std::declval<S>() != std::declval<T>())) |
| 142 | { |
| 143 | if(this->_state._status.have_error()) |
| 144 | { |
| 145 | return this->_state._error != o.error(); |
| 146 | } |
| 147 | return true; |
| 148 | } |
| 149 | }; |
| 150 | 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; } |
| 151 | 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; } |
| 152 | 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; } |
| 153 | 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; } |
| 154 | } // namespace detail |
| 155 | |
| 156 | BOOST_OUTCOME_V2_NAMESPACE_END |
| 157 | |
| 158 | #endif |
| 159 | |