| 1 | /* Storage for a very simple basic_result type |
| 2 | (C) 2017-2024 Niall Douglas <http://www.nedproductions.biz/> (6 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_STORAGE_HPP |
| 32 | #define BOOST_OUTCOME_BASIC_RESULT_STORAGE_HPP |
| 33 | |
| 34 | #include "../success_failure.hpp" |
| 35 | #include "../trait.hpp" |
| 36 | #include "value_storage.hpp" |
| 37 | |
| 38 | BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN |
| 39 | |
| 40 | namespace detail |
| 41 | { |
| 42 | template <class R, class EC, class NoValuePolicy> class basic_result_storage; |
| 43 | } // namespace detail |
| 44 | |
| 45 | namespace hooks |
| 46 | { |
| 47 | template <class R, class S, class NoValuePolicy> constexpr inline uint16_t spare_storage(const detail::basic_result_storage<R, S, NoValuePolicy> *r) noexcept; |
| 48 | template <class R, class S, class NoValuePolicy> |
| 49 | constexpr inline void set_spare_storage(detail::basic_result_storage<R, S, NoValuePolicy> *r, uint16_t v) noexcept; |
| 50 | } // namespace hooks |
| 51 | |
| 52 | namespace policy |
| 53 | { |
| 54 | struct base; |
| 55 | } // namespace policy |
| 56 | |
| 57 | namespace detail |
| 58 | { |
| 59 | template <class R, class EC, class NoValuePolicy> // |
| 60 | class basic_result_storage |
| 61 | { |
| 62 | static_assert(trait::type_can_be_used_in_basic_result<R>, "The type R cannot be used in a basic_result" ); |
| 63 | static_assert(trait::type_can_be_used_in_basic_result<EC>, "The type S cannot be used in a basic_result" ); |
| 64 | |
| 65 | friend struct policy::base; |
| 66 | template <class T, class U, class V> // |
| 67 | friend class basic_result_storage; |
| 68 | template <class T, class U, class V> friend class basic_result_final; |
| 69 | template <class T, class U, class V> |
| 70 | friend constexpr inline uint16_t hooks::spare_storage(const detail::basic_result_storage<T, U, V> *r) noexcept; // NOLINT |
| 71 | template <class T, class U, class V> |
| 72 | friend constexpr inline void hooks::set_spare_storage(detail::basic_result_storage<T, U, V> *r, uint16_t v) noexcept; // NOLINT |
| 73 | |
| 74 | struct disable_in_place_value_type |
| 75 | { |
| 76 | }; |
| 77 | struct disable_in_place_error_type |
| 78 | { |
| 79 | }; |
| 80 | |
| 81 | protected: |
| 82 | using _value_type = std::conditional_t<std::is_same<R, EC>::value, disable_in_place_value_type, R>; |
| 83 | using _error_type = std::conditional_t<std::is_same<R, EC>::value, disable_in_place_error_type, EC>; |
| 84 | |
| 85 | using _state_type = value_storage_select_impl<_value_type, _error_type>; |
| 86 | |
| 87 | #ifdef BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE |
| 88 | value_storage_trivial<_value_type, _error_type> _state; |
| 89 | #else |
| 90 | _state_type _state; |
| 91 | #endif |
| 92 | |
| 93 | public: |
| 94 | // Used by iostream support to access state |
| 95 | _state_type &_iostreams_state() { return _state; } |
| 96 | const _state_type &_iostreams_state() const { return _state; } |
| 97 | |
| 98 | protected: |
| 99 | basic_result_storage() = default; |
| 100 | basic_result_storage(const basic_result_storage &) = default; // NOLINT |
| 101 | basic_result_storage(basic_result_storage &&) = default; // NOLINT |
| 102 | basic_result_storage &operator=(const basic_result_storage &) = default; // NOLINT |
| 103 | basic_result_storage &operator=(basic_result_storage &&) = default; // NOLINT |
| 104 | ~basic_result_storage() = default; |
| 105 | |
| 106 | template <class... Args> |
| 107 | constexpr explicit basic_result_storage(in_place_type_t<_value_type> _, |
| 108 | Args &&... args) noexcept(detail::is_nothrow_constructible<_value_type, Args...>) |
| 109 | : _state{_, static_cast<Args &&>(args)...} |
| 110 | { |
| 111 | } |
| 112 | template <class U, class... Args> |
| 113 | constexpr basic_result_storage(in_place_type_t<_value_type> _, std::initializer_list<U> il, |
| 114 | Args &&... args) noexcept(detail::is_nothrow_constructible<_value_type, std::initializer_list<U>, Args...>) |
| 115 | : _state{_, il, static_cast<Args &&>(args)...} |
| 116 | { |
| 117 | } |
| 118 | template <class... Args> |
| 119 | constexpr explicit basic_result_storage(in_place_type_t<_error_type> _, |
| 120 | Args &&... args) noexcept(detail::is_nothrow_constructible<_error_type, Args...>) |
| 121 | : _state{_, static_cast<Args &&>(args)...} |
| 122 | { |
| 123 | } |
| 124 | template <class U, class... Args> |
| 125 | constexpr basic_result_storage(in_place_type_t<_error_type> _, std::initializer_list<U> il, |
| 126 | Args &&... args) noexcept(detail::is_nothrow_constructible<_error_type, std::initializer_list<U>, Args...>) |
| 127 | : _state{_, il, static_cast<Args &&>(args)...} |
| 128 | { |
| 129 | } |
| 130 | |
| 131 | struct compatible_conversion_tag |
| 132 | { |
| 133 | }; |
| 134 | template <class T, class U, class V> |
| 135 | constexpr basic_result_storage(compatible_conversion_tag /*unused*/, const basic_result_storage<T, U, V> &o) noexcept( |
| 136 | detail::is_nothrow_constructible<_value_type, T> &&detail::is_nothrow_constructible<_error_type, U>) |
| 137 | : _state(o._state) |
| 138 | { |
| 139 | } |
| 140 | template <class T, class U, class V> |
| 141 | constexpr basic_result_storage(compatible_conversion_tag /*unused*/, basic_result_storage<T, U, V> &&o) noexcept( |
| 142 | detail::is_nothrow_constructible<_value_type, T> &&detail::is_nothrow_constructible<_error_type, U>) |
| 143 | : _state(static_cast<decltype(o._state) &&>(o._state)) |
| 144 | { |
| 145 | } |
| 146 | |
| 147 | struct make_error_code_compatible_conversion_tag |
| 148 | { |
| 149 | }; |
| 150 | template <class T, class U, class V> |
| 151 | constexpr basic_result_storage(make_error_code_compatible_conversion_tag /*unused*/, const basic_result_storage<T, U, V> &o) noexcept( |
| 152 | detail::is_nothrow_constructible<_value_type, T> &&noexcept(make_error_code(std::declval<U>()))) |
| 153 | : _state(o._state._status.have_value() ? _state_type(in_place_type<_value_type>, o._state._value) : |
| 154 | _state_type(in_place_type<_error_type>, make_error_code(o._state._error))) |
| 155 | { |
| 156 | } |
| 157 | template <class T, class U, class V> |
| 158 | constexpr basic_result_storage(make_error_code_compatible_conversion_tag /*unused*/, basic_result_storage<T, U, V> &&o) noexcept( |
| 159 | detail::is_nothrow_constructible<_value_type, T> &&noexcept(make_error_code(std::declval<U>()))) |
| 160 | : _state(o._state._status.have_value() ? _state_type(in_place_type<_value_type>, static_cast<T &&>(o._state._value)) : |
| 161 | _state_type(in_place_type<_error_type>, make_error_code(static_cast<U &&>(o._state._error)))) |
| 162 | { |
| 163 | } |
| 164 | |
| 165 | struct make_exception_ptr_compatible_conversion_tag |
| 166 | { |
| 167 | }; |
| 168 | template <class T, class U, class V> |
| 169 | constexpr basic_result_storage(make_exception_ptr_compatible_conversion_tag /*unused*/, const basic_result_storage<T, U, V> &o) noexcept( |
| 170 | detail::is_nothrow_constructible<_value_type, T> &&noexcept(make_exception_ptr(std::declval<U>()))) |
| 171 | : _state(o._state._status.have_value() ? _state_type(in_place_type<_value_type>, o._state._value) : |
| 172 | _state_type(in_place_type<_error_type>, make_exception_ptr(o._state._error))) |
| 173 | { |
| 174 | } |
| 175 | template <class T, class U, class V> |
| 176 | constexpr basic_result_storage(make_exception_ptr_compatible_conversion_tag /*unused*/, basic_result_storage<T, U, V> &&o) noexcept( |
| 177 | detail::is_nothrow_constructible<_value_type, T> &&noexcept(make_exception_ptr(std::declval<U>()))) |
| 178 | : _state(o._state._status.have_value() ? _state_type(in_place_type<_value_type>, static_cast<T &&>(o._state._value)) : |
| 179 | _state_type(in_place_type<_error_type>, make_exception_ptr(static_cast<U &&>(o._state._error)))) |
| 180 | { |
| 181 | } |
| 182 | }; |
| 183 | |
| 184 | } // namespace detail |
| 185 | BOOST_OUTCOME_V2_NAMESPACE_END |
| 186 | |
| 187 | #endif |
| 188 | |