1 /* Storage for a very simple basic_result type 2 (C) 2017-2020 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 { _set_error_is_errno(State &,const E &)42 template <class State, class E> constexpr inline void _set_error_is_errno(State & /*unused*/, const E & /*unused*/) {} 43 template <class R, class EC, class NoValuePolicy> class basic_result_storage; 44 } // namespace detail 45 46 namespace hooks 47 { 48 template <class R, class S, class NoValuePolicy> constexpr inline uint16_t spare_storage(const detail::basic_result_storage<R, S, NoValuePolicy> *r) noexcept; 49 template <class R, class S, class NoValuePolicy> 50 constexpr inline void set_spare_storage(detail::basic_result_storage<R, S, NoValuePolicy> *r, uint16_t v) noexcept; 51 } // namespace hooks 52 53 namespace policy 54 { 55 struct base; 56 } // namespace policy 57 58 namespace detail 59 { 60 template <bool value_throws, bool error_throws> struct basic_result_storage_swap; 61 template <class R, class EC, class NoValuePolicy> // 62 class basic_result_storage 63 { 64 static_assert(trait::type_can_be_used_in_basic_result<R>, "The type R cannot be used in a basic_result"); 65 static_assert(trait::type_can_be_used_in_basic_result<EC>, "The type S cannot be used in a basic_result"); 66 static_assert(std::is_void<EC>::value || std::is_default_constructible<EC>::value, "The type S must be void or default constructible"); 67 68 friend struct policy::base; 69 template <class T, class U, class V> // 70 friend class basic_result_storage; 71 template <class T, class U, class V> friend class basic_result_final; 72 template <class T, class U, class V> 73 friend constexpr inline uint16_t hooks::spare_storage(const detail::basic_result_storage<T, U, V> *r) noexcept; // NOLINT 74 template <class T, class U, class V> 75 friend constexpr inline void hooks::set_spare_storage(detail::basic_result_storage<T, U, V> *r, uint16_t v) noexcept; // NOLINT 76 template <bool value_throws, bool error_throws> struct basic_result_storage_swap; 77 78 struct disable_in_place_value_type 79 { 80 }; 81 struct disable_in_place_error_type 82 { 83 }; 84 85 protected: 86 using _value_type = std::conditional_t<std::is_same<R, EC>::value, disable_in_place_value_type, R>; 87 using _error_type = std::conditional_t<std::is_same<R, EC>::value, disable_in_place_error_type, EC>; 88 89 using _state_type = value_storage_select_impl<_value_type>; 90 91 #ifdef BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE 92 detail::value_storage_trivial<_value_type> _state; 93 #else 94 _state_type _state; 95 #endif 96 devoid<_error_type> _error; 97 98 public: 99 // Used by iostream support to access state _iostreams_state()100 _state_type &_iostreams_state() { return _state; } _iostreams_state() const101 const _state_type &_iostreams_state() const { return _state; } 102 103 // Hack to work around MSVC bug in /permissive- _msvc_nonpermissive_state()104 _state_type &_msvc_nonpermissive_state() { return _state; } _msvc_nonpermissive_error()105 devoid<_error_type> &_msvc_nonpermissive_error() { return _error; } 106 107 protected: 108 basic_result_storage() = default; 109 basic_result_storage(const basic_result_storage &) = default; // NOLINT 110 basic_result_storage(basic_result_storage &&) = default; // NOLINT 111 basic_result_storage &operator=(const basic_result_storage &) = default; // NOLINT 112 basic_result_storage &operator=(basic_result_storage &&) = default; // NOLINT 113 ~basic_result_storage() = default; 114 115 template <class... Args> basic_result_storage(in_place_type_t<_value_type> _,Args &&...args)116 constexpr explicit basic_result_storage(in_place_type_t<_value_type> _, 117 Args &&... args) noexcept(detail::is_nothrow_constructible<_value_type, Args...>) 118 : _state{_, static_cast<Args &&>(args)...} 119 , _error() 120 { 121 } 122 template <class U, class... Args> basic_result_storage(in_place_type_t<_value_type> _,std::initializer_list<U> il,Args &&...args)123 constexpr basic_result_storage(in_place_type_t<_value_type> _, std::initializer_list<U> il, 124 Args &&... args) noexcept(detail::is_nothrow_constructible<_value_type, std::initializer_list<U>, Args...>) 125 : _state{_, il, static_cast<Args &&>(args)...} 126 , _error() 127 { 128 } 129 template <class... Args> basic_result_storage(in_place_type_t<_error_type>,Args &&...args)130 constexpr explicit basic_result_storage(in_place_type_t<_error_type> /*unused*/, Args &&... args) noexcept(detail::is_nothrow_constructible<_error_type, Args...>) 131 : _state{detail::status::have_error} 132 , _error(static_cast<Args &&>(args)...) 133 { 134 _set_error_is_errno(_state, _error); 135 } 136 template <class U, class... Args> basic_result_storage(in_place_type_t<_error_type>,std::initializer_list<U> il,Args &&...args)137 constexpr basic_result_storage(in_place_type_t<_error_type> /*unused*/, std::initializer_list<U> il, Args &&... args) noexcept(detail::is_nothrow_constructible<_error_type, std::initializer_list<U>, Args...>) 138 : _state{detail::status::have_error} 139 , _error{il, static_cast<Args &&>(args)...} 140 { 141 _set_error_is_errno(_state, _error); 142 } 143 struct compatible_conversion_tag 144 { 145 }; 146 template <class T, class U, class V> basic_result_storage(compatible_conversion_tag,const basic_result_storage<T,U,V> & o)147 constexpr basic_result_storage(compatible_conversion_tag /*unused*/, const basic_result_storage<T, U, V> &o) noexcept(detail::is_nothrow_constructible<_value_type, T> &&detail::is_nothrow_constructible<_error_type, U>) 148 : _state(o._state) 149 , _error(o._error) 150 { 151 } 152 template <class T, class V> basic_result_storage(compatible_conversion_tag,const basic_result_storage<T,void,V> & o)153 constexpr basic_result_storage(compatible_conversion_tag /*unused*/, const basic_result_storage<T, void, V> &o) noexcept(detail::is_nothrow_constructible<_value_type, T>) 154 : _state(o._state) 155 , _error(_error_type{}) 156 { 157 } 158 template <class T, class U, class V> basic_result_storage(compatible_conversion_tag,basic_result_storage<T,U,V> && o)159 constexpr basic_result_storage(compatible_conversion_tag /*unused*/, basic_result_storage<T, U, V> &&o) noexcept(detail::is_nothrow_constructible<_value_type, T> &&detail::is_nothrow_constructible<_error_type, U>) 160 : _state(static_cast<decltype(o._state) &&>(o._state)) 161 , _error(static_cast<U &&>(o._error)) 162 { 163 } 164 template <class T, class V> basic_result_storage(compatible_conversion_tag,basic_result_storage<T,void,V> && o)165 constexpr basic_result_storage(compatible_conversion_tag /*unused*/, basic_result_storage<T, void, V> &&o) noexcept(detail::is_nothrow_constructible<_value_type, T>) 166 : _state(static_cast<decltype(o._state) &&>(o._state)) 167 , _error(_error_type{}) 168 { 169 } 170 171 struct make_error_code_compatible_conversion_tag 172 { 173 }; 174 template <class T, class U, class V> basic_result_storage(make_error_code_compatible_conversion_tag,const basic_result_storage<T,U,V> & o)175 constexpr basic_result_storage(make_error_code_compatible_conversion_tag /*unused*/, const basic_result_storage<T, U, V> &o) noexcept(detail::is_nothrow_constructible<_value_type, T> &&noexcept(make_error_code(std::declval<U>()))) 176 : _state(o._state) 177 , _error(make_error_code(o._error)) 178 { 179 } 180 template <class T, class U, class V> basic_result_storage(make_error_code_compatible_conversion_tag,basic_result_storage<T,U,V> && o)181 constexpr basic_result_storage(make_error_code_compatible_conversion_tag /*unused*/, basic_result_storage<T, U, V> &&o) noexcept(detail::is_nothrow_constructible<_value_type, T> &&noexcept(make_error_code(std::declval<U>()))) 182 : _state(static_cast<decltype(o._state) &&>(o._state)) 183 , _error(make_error_code(static_cast<U &&>(o._error))) 184 { 185 } 186 187 struct make_exception_ptr_compatible_conversion_tag 188 { 189 }; 190 template <class T, class U, class V> basic_result_storage(make_exception_ptr_compatible_conversion_tag,const basic_result_storage<T,U,V> & o)191 constexpr basic_result_storage(make_exception_ptr_compatible_conversion_tag /*unused*/, const basic_result_storage<T, U, V> &o) noexcept(detail::is_nothrow_constructible<_value_type, T> &&noexcept(make_exception_ptr(std::declval<U>()))) 192 : _state(o._state) 193 , _error(make_exception_ptr(o._error)) 194 { 195 } 196 template <class T, class U, class V> basic_result_storage(make_exception_ptr_compatible_conversion_tag,basic_result_storage<T,U,V> && o)197 constexpr basic_result_storage(make_exception_ptr_compatible_conversion_tag /*unused*/, basic_result_storage<T, U, V> &&o) noexcept(detail::is_nothrow_constructible<_value_type, T> &&noexcept(make_exception_ptr(std::declval<U>()))) 198 : _state(static_cast<decltype(o._state) &&>(o._state)) 199 , _error(make_exception_ptr(static_cast<U &&>(o._error))) 200 { 201 } 202 }; 203 204 // Neither value nor error type can throw during swap 205 #ifndef BOOST_NO_EXCEPTIONS 206 template <> struct basic_result_storage_swap<false, false> 207 #else 208 template <bool value_throws, bool error_throws> struct basic_result_storage_swap 209 #endif 210 { basic_result_storage_swapdetail::basic_result_storage_swap211 template <class R, class EC, class NoValuePolicy> constexpr basic_result_storage_swap(basic_result_storage<R, EC, NoValuePolicy> &a, basic_result_storage<R, EC, NoValuePolicy> &b) 212 { 213 using std::swap; 214 a._msvc_nonpermissive_state().swap(b._msvc_nonpermissive_state()); 215 swap(a._msvc_nonpermissive_error(), b._msvc_nonpermissive_error()); 216 } 217 }; 218 #ifndef BOOST_NO_EXCEPTIONS 219 // Swap potentially throwing value first 220 template <> struct basic_result_storage_swap<true, false> 221 { basic_result_storage_swapdetail::basic_result_storage_swap222 template <class R, class EC, class NoValuePolicy> constexpr basic_result_storage_swap(basic_result_storage<R, EC, NoValuePolicy> &a, basic_result_storage<R, EC, NoValuePolicy> &b) 223 { 224 using std::swap; 225 a._msvc_nonpermissive_state().swap(b._msvc_nonpermissive_state()); 226 swap(a._msvc_nonpermissive_error(), b._msvc_nonpermissive_error()); 227 } 228 }; 229 // Swap potentially throwing error first 230 template <> struct basic_result_storage_swap<false, true> 231 { basic_result_storage_swapdetail::basic_result_storage_swap232 template <class R, class EC, class NoValuePolicy> constexpr basic_result_storage_swap(basic_result_storage<R, EC, NoValuePolicy> &a, basic_result_storage<R, EC, NoValuePolicy> &b) 233 { 234 struct _ 235 { 236 status_bitfield_type &a, &b; 237 bool all_good{false}; 238 ~_() 239 { 240 if(!all_good) 241 { 242 // We lost one of the values 243 a.set_have_lost_consistency(true); 244 b.set_have_lost_consistency(true); 245 } 246 } 247 } _{a._msvc_nonpermissive_state()._status, b._msvc_nonpermissive_state()._status}; 248 strong_swap(_.all_good, a._msvc_nonpermissive_error(), b._msvc_nonpermissive_error()); 249 a._msvc_nonpermissive_state().swap(b._msvc_nonpermissive_state()); 250 } 251 }; 252 // Both could throw 253 template <> struct basic_result_storage_swap<true, true> 254 { basic_result_storage_swapdetail::basic_result_storage_swap255 template <class R, class EC, class NoValuePolicy> basic_result_storage_swap(basic_result_storage<R, EC, NoValuePolicy> &a, basic_result_storage<R, EC, NoValuePolicy> &b) 256 { 257 using std::swap; 258 // Swap value and status first, if it throws, status will remain unchanged 259 a._msvc_nonpermissive_state().swap(b._msvc_nonpermissive_state()); 260 bool all_good = false; 261 try 262 { 263 strong_swap(all_good, a._msvc_nonpermissive_error(), b._msvc_nonpermissive_error()); 264 } 265 catch(...) 266 { 267 if(!all_good) 268 { 269 a._msvc_nonpermissive_state()._status.set_have_lost_consistency(true); 270 b._msvc_nonpermissive_state()._status.set_have_lost_consistency(true); 271 } 272 else 273 { 274 // We may still be able to rescue tis 275 // First try to put the value and status back 276 try 277 { 278 a._msvc_nonpermissive_state().swap(b._msvc_nonpermissive_state()); 279 // If that succeeded, continue by rethrowing the exception 280 } 281 catch(...) 282 { 283 all_good = false; 284 } 285 } 286 if(!all_good) 287 { 288 // We are now trapped. The value swapped, the error did not, 289 // trying to restore the value failed. We now have 290 // inconsistent result objects. Best we can do is fix up the 291 // status bits to prevent has_value() == has_error(). 292 auto check = [](basic_result_storage<R, EC, NoValuePolicy> &x) { 293 bool has_value = x._state._status.have_value(); 294 bool has_error = x._state._status.have_error(); 295 bool has_exception = x._state._status.have_exception(); 296 x._state._status.set_have_lost_consistency(true); 297 if(has_value == (has_error || has_exception)) 298 { 299 if(has_value) 300 { 301 // We know the value swapped and is now set, so clear error and exception 302 x._state._status.set_have_error(false).set_have_exception(false); 303 } 304 else 305 { 306 // We know the value swapped and is now unset, so set error 307 x._state._status.set_have_error(true); 308 // TODO: Should I default construct reset _error? It's guaranteed default constructible. 309 } 310 } 311 }; 312 check(a); 313 check(b); 314 } 315 throw; 316 } 317 } 318 }; 319 #endif 320 321 } // namespace detail 322 BOOST_OUTCOME_V2_NAMESPACE_END 323 324 #endif 325