value_storage.hpp 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  1. /* Essentially an internal optional implementation :)
  2. (C) 2017-2021 Niall Douglas <http://www.nedproductions.biz/> (24 commits)
  3. File Created: June 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_VALUE_STORAGE_HPP
  26. #define BOOST_OUTCOME_VALUE_STORAGE_HPP
  27. #include "../config.hpp"
  28. #include <cassert>
  29. BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN
  30. namespace detail
  31. {
  32. template <class T, bool nothrow> struct strong_swap_impl
  33. {
  34. constexpr strong_swap_impl(bool &allgood, T &a, T &b)
  35. {
  36. allgood = true;
  37. using std::swap;
  38. swap(a, b);
  39. }
  40. };
  41. template <class T, bool nothrow> struct strong_placement_impl
  42. {
  43. template <class F> constexpr strong_placement_impl(bool &allgood, T *a, T *b, F &&f)
  44. {
  45. allgood = true;
  46. new(a) T(static_cast<T &&>(*b));
  47. b->~T();
  48. f();
  49. }
  50. };
  51. #ifndef BOOST_NO_EXCEPTIONS
  52. template <class T> struct strong_swap_impl<T, false>
  53. {
  54. strong_swap_impl(bool &allgood, T &a, T &b)
  55. {
  56. allgood = true;
  57. T v(static_cast<T &&>(a));
  58. try
  59. {
  60. a = static_cast<T &&>(b);
  61. }
  62. catch(...)
  63. {
  64. // Try to put back a
  65. try
  66. {
  67. a = static_cast<T &&>(v);
  68. // fall through as all good
  69. }
  70. catch(...)
  71. {
  72. // failed to completely restore
  73. allgood = false;
  74. // throw away second exception
  75. }
  76. throw; // rethrow original exception
  77. }
  78. // b has been moved to a, try to move v to b
  79. try
  80. {
  81. b = static_cast<T &&>(v);
  82. }
  83. catch(...)
  84. {
  85. // Try to restore a to b, and v to a
  86. try
  87. {
  88. b = static_cast<T &&>(a);
  89. a = static_cast<T &&>(v);
  90. // fall through as all good
  91. }
  92. catch(...)
  93. {
  94. // failed to completely restore
  95. allgood = false;
  96. // throw away second exception
  97. }
  98. throw; // rethrow original exception
  99. }
  100. }
  101. };
  102. template <class T> struct strong_placement_impl<T, false>
  103. {
  104. template <class F> strong_placement_impl(bool &allgood, T *a, T *b, F &&f)
  105. {
  106. new(a) T(static_cast<T &&>(*b));
  107. try
  108. {
  109. b->~T();
  110. f();
  111. }
  112. catch(...)
  113. {
  114. // Try to put back a, but only if we are still good
  115. if(allgood)
  116. {
  117. try
  118. {
  119. new(b) T(static_cast<T &&>(*a));
  120. // fall through as all good
  121. }
  122. catch(...)
  123. {
  124. // failed to completely restore
  125. allgood = false;
  126. // throw away second exception
  127. }
  128. throw; // rethrow original exception
  129. }
  130. }
  131. }
  132. };
  133. #endif
  134. } // namespace detail
  135. /*!
  136. */
  137. BOOST_OUTCOME_TEMPLATE(class T)
  138. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_move_constructible<T>::value &&std::is_move_assignable<T>::value))
  139. constexpr inline void strong_swap(bool &allgood, T &a, T &b) noexcept(detail::is_nothrow_swappable<T>::value)
  140. {
  141. detail::strong_swap_impl<T, detail::is_nothrow_swappable<T>::value>(allgood, a, b);
  142. }
  143. /*!
  144. */
  145. BOOST_OUTCOME_TEMPLATE(class T, class F)
  146. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_move_constructible<T>::value &&std::is_move_assignable<T>::value))
  147. constexpr inline void strong_placement(bool &allgood, T *a, T *b, F &&f) noexcept(std::is_nothrow_move_constructible<T>::value)
  148. {
  149. detail::strong_placement_impl<T, std::is_nothrow_move_constructible<T>::value>(allgood, a, b, static_cast<F &&>(f));
  150. }
  151. namespace detail
  152. {
  153. template <class T>
  154. constexpr
  155. #ifdef _MSC_VER
  156. __declspec(noreturn)
  157. #elif defined(__GNUC__) || defined(__clang__)
  158. __attribute__((noreturn))
  159. #endif
  160. void make_ub(T && /*unused*/)
  161. {
  162. assert(false); // NOLINT
  163. #if defined(__GNUC__) || defined(__clang__)
  164. __builtin_unreachable();
  165. #elif defined(_MSC_VER)
  166. __assume(0);
  167. #endif
  168. }
  169. /* Outcome v1 used a C bitfield whose values were tracked by compiler optimisers nicely,
  170. but that produces ICEs when used in constexpr.
  171. Outcome v2.0-v2.1 used a 32 bit integer and manually set and cleared bits. Unfortunately
  172. only GCC's optimiser tracks bit values during constant folding, and only per byte, and
  173. even then unreliably. https://wg21.link/P1886 "Error speed benchmarking" showed just how
  174. poorly clang and MSVC fails to optimise outcome-using code, if you manually set bits.
  175. Outcome v2.2 therefore uses an enum with fixed values, and constexpr manipulation functions
  176. to change the value to one of the enum's values. This is stupid to look at in source code,
  177. but it make clang's optimiser do the right thing, so it's worth it.
  178. */
  179. #define BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS 0
  180. enum class status : uint16_t
  181. {
  182. // WARNING: These bits are not tracked by abi-dumper, but changing them will break ABI!
  183. none = 0,
  184. have_value = (1U << 0U),
  185. have_error = (1U << 1U),
  186. have_exception = (2U << 1U),
  187. have_error_exception = (3U << 1U),
  188. // failed to complete a strong swap
  189. have_lost_consistency = (1U << 3U),
  190. have_value_lost_consistency = (1U << 0U) | (1U << 3U),
  191. have_error_lost_consistency = (1U << 1U) | (1U << 3U),
  192. have_exception_lost_consistency = (2U << 1U) | (1U << 3U),
  193. have_error_exception_lost_consistency = (3U << 1U) | (1U << 3U),
  194. // can errno be set from this error?
  195. have_error_is_errno = (1U << 4U),
  196. have_error_error_is_errno = (1U << 1U) | (1U << 4U),
  197. have_error_exception_error_is_errno = (3U << 1U) | (1U << 4U),
  198. have_error_lost_consistency_error_is_errno = (1U << 1U) | (1U << 3U) | (1U << 4U),
  199. have_error_exception_lost_consistency_error_is_errno = (3U << 1U) | (1U << 3U) | (1U << 4U),
  200. // value has been moved from
  201. have_moved_from = (1U << 5U)
  202. };
  203. struct status_bitfield_type
  204. {
  205. status status_value{status::none};
  206. uint16_t spare_storage_value{0}; // hooks::spare_storage()
  207. constexpr status_bitfield_type() = default;
  208. constexpr status_bitfield_type(status v) noexcept
  209. : status_value(v)
  210. {
  211. } // NOLINT
  212. constexpr status_bitfield_type(status v, uint16_t s) noexcept
  213. : status_value(v)
  214. , spare_storage_value(s)
  215. {
  216. }
  217. constexpr status_bitfield_type(const status_bitfield_type &) = default;
  218. constexpr status_bitfield_type(status_bitfield_type &&) = default;
  219. constexpr status_bitfield_type &operator=(const status_bitfield_type &) = default;
  220. constexpr status_bitfield_type &operator=(status_bitfield_type &&) = default;
  221. //~status_bitfield_type() = default; // Do NOT uncomment this, it breaks older clangs!
  222. constexpr bool have_value() const noexcept
  223. {
  224. #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
  225. return (status_value == status::have_value) //
  226. || (status_value == status::have_value_lost_consistency) //
  227. ;
  228. #else
  229. return (static_cast<uint16_t>(status_value) & static_cast<uint16_t>(status::have_value)) != 0;
  230. #endif
  231. }
  232. constexpr bool have_error() const noexcept
  233. {
  234. #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
  235. return (status_value == status::have_error) //
  236. || (status_value == status::have_error_exception) //
  237. || (status_value == status::have_error_lost_consistency) //
  238. || (status_value == status::have_error_exception_lost_consistency) //
  239. || (status_value == status::have_error_error_is_errno) //
  240. || (status_value == status::have_error_exception_error_is_errno) //
  241. || (status_value == status::have_error_lost_consistency_error_is_errno) //
  242. || (status_value == status::have_error_exception_lost_consistency_error_is_errno) //
  243. ;
  244. #else
  245. return (static_cast<uint16_t>(status_value) & static_cast<uint16_t>(status::have_error)) != 0;
  246. #endif
  247. }
  248. constexpr bool have_exception() const noexcept
  249. {
  250. #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
  251. return (status_value == status::have_exception) //
  252. || (status_value == status::have_error_exception) //
  253. || (status_value == status::have_exception_lost_consistency) //
  254. || (status_value == status::have_error_exception_lost_consistency) //
  255. || (status_value == status::have_error_exception_error_is_errno) //
  256. || (status_value == status::have_error_exception_lost_consistency_error_is_errno) //
  257. ;
  258. #else
  259. return (static_cast<uint16_t>(status_value) & static_cast<uint16_t>(status::have_exception)) != 0;
  260. #endif
  261. }
  262. constexpr bool have_lost_consistency() const noexcept
  263. {
  264. #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
  265. return (status_value == status::have_value_lost_consistency) //
  266. || (status_value == status::have_error_lost_consistency) //
  267. || (status_value == status::have_exception_lost_consistency) //
  268. || (status_value == status::have_error_lost_consistency_error_is_errno) //
  269. || (status_value == status::have_error_exception_lost_consistency_error_is_errno) //
  270. ;
  271. #else
  272. return (static_cast<uint16_t>(status_value) & static_cast<uint16_t>(status::have_lost_consistency)) != 0;
  273. #endif
  274. }
  275. constexpr bool have_error_is_errno() const noexcept
  276. {
  277. #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
  278. return (status_value == status::have_error_error_is_errno) //
  279. || (status_value == status::have_error_exception_error_is_errno) //
  280. || (status_value == status::have_error_lost_consistency_error_is_errno) //
  281. || (status_value == status::have_error_exception_lost_consistency_error_is_errno) //
  282. ;
  283. #else
  284. return (static_cast<uint16_t>(status_value) & static_cast<uint16_t>(status::have_error_is_errno)) != 0;
  285. #endif
  286. }
  287. constexpr bool have_moved_from() const noexcept
  288. {
  289. #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
  290. #error Fixme
  291. #else
  292. return (static_cast<uint16_t>(status_value) & static_cast<uint16_t>(status::have_moved_from)) != 0;
  293. #endif
  294. }
  295. constexpr status_bitfield_type &set_have_value(bool v) noexcept
  296. {
  297. #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
  298. switch(status_value)
  299. {
  300. case status::none:
  301. if(v)
  302. {
  303. status_value = status::have_value;
  304. }
  305. break;
  306. case status::have_value:
  307. if(!v)
  308. {
  309. status_value = status::none;
  310. }
  311. break;
  312. case status::have_error:
  313. if(v)
  314. {
  315. make_ub(*this);
  316. }
  317. break;
  318. case status::have_exception:
  319. if(v)
  320. {
  321. make_ub(*this);
  322. }
  323. break;
  324. case status::have_error_exception:
  325. if(v)
  326. {
  327. make_ub(*this);
  328. }
  329. break;
  330. case status::have_value_lost_consistency:
  331. if(!v)
  332. {
  333. status_value = status::none;
  334. }
  335. break;
  336. case status::have_error_lost_consistency:
  337. if(v)
  338. {
  339. make_ub(*this);
  340. }
  341. break;
  342. case status::have_exception_lost_consistency:
  343. if(v)
  344. {
  345. make_ub(*this);
  346. }
  347. break;
  348. case status::have_error_exception_lost_consistency:
  349. if(v)
  350. {
  351. make_ub(*this);
  352. }
  353. break;
  354. case status::have_error_error_is_errno:
  355. if(v)
  356. {
  357. make_ub(*this);
  358. }
  359. break;
  360. case status::have_error_exception_error_is_errno:
  361. if(v)
  362. {
  363. make_ub(*this);
  364. }
  365. break;
  366. case status::have_error_lost_consistency_error_is_errno:
  367. if(v)
  368. {
  369. make_ub(*this);
  370. }
  371. break;
  372. case status::have_error_exception_lost_consistency_error_is_errno:
  373. if(v)
  374. {
  375. make_ub(*this);
  376. }
  377. break;
  378. }
  379. #else
  380. status_value = static_cast<status>(v ? (static_cast<uint16_t>(status_value) | static_cast<uint16_t>(status::have_value)) :
  381. (static_cast<uint16_t>(status_value) & ~static_cast<uint16_t>(status::have_value)));
  382. #endif
  383. return *this;
  384. }
  385. constexpr status_bitfield_type &set_have_error(bool v) noexcept
  386. {
  387. #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
  388. switch(status_value)
  389. {
  390. case status::none:
  391. if(v)
  392. {
  393. status_value = status::have_error;
  394. }
  395. break;
  396. case status::have_value:
  397. if(v)
  398. {
  399. make_ub(*this);
  400. }
  401. break;
  402. case status::have_error:
  403. if(!v)
  404. {
  405. status_value = status::none;
  406. }
  407. break;
  408. case status::have_exception:
  409. if(v)
  410. {
  411. status_value = status::have_error_exception;
  412. }
  413. break;
  414. case status::have_error_exception:
  415. if(!v)
  416. {
  417. status_value = status::have_exception;
  418. }
  419. break;
  420. case status::have_value_lost_consistency:
  421. if(v)
  422. {
  423. make_ub(*this);
  424. }
  425. break;
  426. case status::have_error_lost_consistency:
  427. if(!v)
  428. {
  429. status_value = status::none;
  430. }
  431. break;
  432. case status::have_exception_lost_consistency:
  433. if(v)
  434. {
  435. status_value = status::have_error_exception_lost_consistency;
  436. }
  437. break;
  438. case status::have_error_exception_lost_consistency:
  439. if(!v)
  440. {
  441. status_value = status::have_exception_lost_consistency;
  442. }
  443. break;
  444. case status::have_error_error_is_errno:
  445. if(!v)
  446. {
  447. status_value = status::none;
  448. }
  449. break;
  450. case status::have_error_exception_error_is_errno:
  451. if(!v)
  452. {
  453. status_value = status::have_exception;
  454. }
  455. break;
  456. case status::have_error_lost_consistency_error_is_errno:
  457. if(!v)
  458. {
  459. status_value = status::none;
  460. }
  461. break;
  462. case status::have_error_exception_lost_consistency_error_is_errno:
  463. if(!v)
  464. {
  465. status_value = status::have_exception_lost_consistency;
  466. }
  467. break;
  468. }
  469. #else
  470. status_value = static_cast<status>(v ? (static_cast<uint16_t>(status_value) | static_cast<uint16_t>(status::have_error)) :
  471. (static_cast<uint16_t>(status_value) & ~static_cast<uint16_t>(status::have_error)));
  472. #endif
  473. return *this;
  474. }
  475. constexpr status_bitfield_type &set_have_exception(bool v) noexcept
  476. {
  477. #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
  478. switch(status_value)
  479. {
  480. case status::none:
  481. if(v)
  482. {
  483. status_value = status::have_exception;
  484. }
  485. break;
  486. case status::have_value:
  487. if(v)
  488. {
  489. make_ub(*this);
  490. }
  491. break;
  492. case status::have_error:
  493. if(v)
  494. {
  495. status_value = status::have_error_exception;
  496. }
  497. break;
  498. case status::have_exception:
  499. if(!v)
  500. {
  501. status_value = status::none;
  502. }
  503. break;
  504. case status::have_error_exception:
  505. if(!v)
  506. {
  507. status_value = status::have_error;
  508. }
  509. break;
  510. case status::have_value_lost_consistency:
  511. if(v)
  512. {
  513. make_ub(*this);
  514. }
  515. break;
  516. case status::have_error_lost_consistency:
  517. if(v)
  518. {
  519. status_value = status::have_error_exception_lost_consistency;
  520. }
  521. break;
  522. case status::have_exception_lost_consistency:
  523. if(!v)
  524. {
  525. status_value = status::none;
  526. }
  527. break;
  528. case status::have_error_exception_lost_consistency:
  529. if(!v)
  530. {
  531. status_value = status::have_error_lost_consistency;
  532. }
  533. break;
  534. case status::have_error_error_is_errno:
  535. if(v)
  536. {
  537. status_value = status::have_error_exception_error_is_errno;
  538. }
  539. break;
  540. case status::have_error_exception_error_is_errno:
  541. if(!v)
  542. {
  543. status_value = status::have_error_error_is_errno;
  544. }
  545. break;
  546. case status::have_error_lost_consistency_error_is_errno:
  547. if(v)
  548. {
  549. status_value = status::have_error_exception_lost_consistency_error_is_errno;
  550. }
  551. break;
  552. case status::have_error_exception_lost_consistency_error_is_errno:
  553. if(!v)
  554. {
  555. status_value = status::have_error_lost_consistency_error_is_errno;
  556. }
  557. break;
  558. }
  559. #else
  560. status_value = static_cast<status>(v ? (static_cast<uint16_t>(status_value) | static_cast<uint16_t>(status::have_exception)) :
  561. (static_cast<uint16_t>(status_value) & ~static_cast<uint16_t>(status::have_exception)));
  562. #endif
  563. return *this;
  564. }
  565. constexpr status_bitfield_type &set_have_error_is_errno(bool v) noexcept
  566. {
  567. #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
  568. switch(status_value)
  569. {
  570. case status::none:
  571. make_ub(*this);
  572. break;
  573. case status::have_value:
  574. make_ub(*this);
  575. break;
  576. case status::have_error:
  577. if(v)
  578. {
  579. status_value = status::have_error_error_is_errno;
  580. }
  581. break;
  582. case status::have_exception:
  583. make_ub(*this);
  584. break;
  585. case status::have_error_exception:
  586. if(v)
  587. {
  588. status_value = status::have_error_exception_error_is_errno;
  589. }
  590. break;
  591. case status::have_value_lost_consistency:
  592. make_ub(*this);
  593. break;
  594. case status::have_error_lost_consistency:
  595. if(v)
  596. {
  597. status_value = status::have_error_lost_consistency_error_is_errno;
  598. }
  599. break;
  600. case status::have_exception_lost_consistency:
  601. make_ub(*this);
  602. break;
  603. case status::have_error_exception_lost_consistency:
  604. if(v)
  605. {
  606. status_value = status::have_error_exception_lost_consistency_error_is_errno;
  607. }
  608. break;
  609. case status::have_error_error_is_errno:
  610. if(!v)
  611. {
  612. status_value = status::have_error;
  613. }
  614. break;
  615. case status::have_error_exception_error_is_errno:
  616. if(!v)
  617. {
  618. status_value = status::have_error_exception;
  619. }
  620. break;
  621. case status::have_error_lost_consistency_error_is_errno:
  622. if(!v)
  623. {
  624. status_value = status::have_error_lost_consistency;
  625. }
  626. break;
  627. case status::have_error_exception_lost_consistency_error_is_errno:
  628. if(!v)
  629. {
  630. status_value = status::have_error_exception_lost_consistency;
  631. }
  632. break;
  633. }
  634. #else
  635. status_value = static_cast<status>(v ? (static_cast<uint16_t>(status_value) | static_cast<uint16_t>(status::have_error_is_errno)) :
  636. (static_cast<uint16_t>(status_value) & ~static_cast<uint16_t>(status::have_error_is_errno)));
  637. #endif
  638. return *this;
  639. }
  640. constexpr status_bitfield_type &set_have_lost_consistency(bool v) noexcept
  641. {
  642. #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
  643. switch(status_value)
  644. {
  645. case status::none:
  646. if(v)
  647. {
  648. make_ub(*this);
  649. }
  650. break;
  651. case status::have_value:
  652. if(v)
  653. {
  654. status_value = status::have_value_lost_consistency;
  655. }
  656. break;
  657. case status::have_error:
  658. if(v)
  659. {
  660. status_value = status::have_error_lost_consistency;
  661. }
  662. break;
  663. case status::have_exception:
  664. if(v)
  665. {
  666. status_value = status::have_exception_lost_consistency;
  667. }
  668. break;
  669. case status::have_error_exception:
  670. if(v)
  671. {
  672. status_value = status::have_error_exception_lost_consistency;
  673. }
  674. break;
  675. case status::have_value_lost_consistency:
  676. if(!v)
  677. {
  678. status_value = status::have_value;
  679. }
  680. break;
  681. case status::have_error_lost_consistency:
  682. if(!v)
  683. {
  684. status_value = status::have_error;
  685. }
  686. break;
  687. case status::have_exception_lost_consistency:
  688. if(!v)
  689. {
  690. status_value = status::have_exception;
  691. }
  692. break;
  693. case status::have_error_exception_lost_consistency:
  694. if(!v)
  695. {
  696. status_value = status::have_error_exception;
  697. }
  698. break;
  699. case status::have_error_error_is_errno:
  700. if(v)
  701. {
  702. status_value = status::have_error_lost_consistency_error_is_errno;
  703. }
  704. break;
  705. case status::have_error_exception_error_is_errno:
  706. if(v)
  707. {
  708. status_value = status::have_error_exception_lost_consistency_error_is_errno;
  709. }
  710. break;
  711. case status::have_error_lost_consistency_error_is_errno:
  712. if(!v)
  713. {
  714. status_value = status::have_error_exception_error_is_errno;
  715. }
  716. break;
  717. case status::have_error_exception_lost_consistency_error_is_errno:
  718. if(!v)
  719. {
  720. status_value = status::have_error_exception_error_is_errno;
  721. }
  722. break;
  723. }
  724. #else
  725. status_value = static_cast<status>(v ? (static_cast<uint16_t>(status_value) | static_cast<uint16_t>(status::have_lost_consistency)) :
  726. (static_cast<uint16_t>(status_value) & ~static_cast<uint16_t>(status::have_lost_consistency)));
  727. #endif
  728. return *this;
  729. }
  730. constexpr status_bitfield_type &set_have_moved_from(bool v) noexcept
  731. {
  732. #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
  733. #error Fixme
  734. #else
  735. status_value = static_cast<status>(v ? (static_cast<uint16_t>(status_value) | static_cast<uint16_t>(status::have_moved_from)) :
  736. (static_cast<uint16_t>(status_value) & ~static_cast<uint16_t>(status::have_moved_from)));
  737. #endif
  738. return *this;
  739. }
  740. };
  741. #if !defined(NDEBUG)
  742. // Check is trivial in all ways except default constructibility
  743. static_assert(sizeof(status_bitfield_type) == 4, "status_bitfield_type is not sized 4 bytes!");
  744. static_assert(std::is_trivially_copyable<status_bitfield_type>::value, "status_bitfield_type is not trivially copyable!");
  745. static_assert(std::is_trivially_assignable<status_bitfield_type, status_bitfield_type>::value, "status_bitfield_type is not trivially assignable!");
  746. static_assert(std::is_trivially_destructible<status_bitfield_type>::value, "status_bitfield_type is not trivially destructible!");
  747. static_assert(std::is_trivially_copy_constructible<status_bitfield_type>::value, "status_bitfield_type is not trivially copy constructible!");
  748. static_assert(std::is_trivially_move_constructible<status_bitfield_type>::value, "status_bitfield_type is not trivially move constructible!");
  749. static_assert(std::is_trivially_copy_assignable<status_bitfield_type>::value, "status_bitfield_type is not trivially copy assignable!");
  750. static_assert(std::is_trivially_move_assignable<status_bitfield_type>::value, "status_bitfield_type is not trivially move assignable!");
  751. // Also check is standard layout
  752. static_assert(std::is_standard_layout<status_bitfield_type>::value, "status_bitfield_type is not a standard layout type!");
  753. #endif
  754. template <class State> constexpr inline void _set_error_is_errno(State & /*unused*/) {}
  755. #ifdef _MSC_VER
  756. #pragma warning(push)
  757. #pragma warning(disable : 4127) // conditional expression is constant
  758. #pragma warning(disable : 4624) // destructor was implicitly defined as deleted
  759. #endif
  760. // Used if both T and E are trivial
  761. template <class T, class E> struct value_storage_trivial
  762. {
  763. using value_type = T;
  764. using error_type = E;
  765. // Disable in place construction if they are the same type
  766. struct disable_in_place_value_type
  767. {
  768. };
  769. struct disable_in_place_error_type
  770. {
  771. };
  772. using _value_type = std::conditional_t<std::is_same<value_type, error_type>::value, disable_in_place_value_type, value_type>;
  773. using _error_type = std::conditional_t<std::is_same<value_type, error_type>::value, disable_in_place_error_type, error_type>;
  774. using _value_type_ = devoid<value_type>;
  775. using _error_type_ = devoid<error_type>;
  776. union {
  777. empty_type _empty;
  778. _value_type_ _value;
  779. _error_type_ _error;
  780. };
  781. status_bitfield_type _status;
  782. constexpr value_storage_trivial() noexcept
  783. : _empty{}
  784. {
  785. }
  786. value_storage_trivial(const value_storage_trivial &) = default; // NOLINT
  787. value_storage_trivial(value_storage_trivial &&) = default; // NOLINT
  788. value_storage_trivial &operator=(const value_storage_trivial &) = default; // NOLINT
  789. value_storage_trivial &operator=(value_storage_trivial &&) = default; // NOLINT
  790. ~value_storage_trivial() = default;
  791. constexpr explicit value_storage_trivial(status_bitfield_type status)
  792. : _empty()
  793. , _status(status)
  794. {
  795. }
  796. template <class... Args>
  797. constexpr explicit value_storage_trivial(in_place_type_t<_value_type> /*unused*/,
  798. Args &&... args) noexcept(detail::is_nothrow_constructible<_value_type_, Args...>)
  799. : _value(static_cast<Args &&>(args)...)
  800. , _status(status::have_value)
  801. {
  802. }
  803. template <class U, class... Args>
  804. constexpr value_storage_trivial(in_place_type_t<_value_type> /*unused*/, std::initializer_list<U> il,
  805. Args &&... args) noexcept(detail::is_nothrow_constructible<_value_type_, std::initializer_list<U>, Args...>)
  806. : _value(il, static_cast<Args &&>(args)...)
  807. , _status(status::have_value)
  808. {
  809. }
  810. template <class... Args>
  811. constexpr explicit value_storage_trivial(in_place_type_t<_error_type> /*unused*/,
  812. Args &&... args) noexcept(detail::is_nothrow_constructible<_error_type_, Args...>)
  813. : _error(static_cast<Args &&>(args)...)
  814. , _status(status::have_error)
  815. {
  816. _set_error_is_errno(*this);
  817. }
  818. template <class U, class... Args>
  819. constexpr value_storage_trivial(in_place_type_t<_error_type> /*unused*/, std::initializer_list<U> il,
  820. Args &&... args) noexcept(detail::is_nothrow_constructible<_error_type_, std::initializer_list<U>, Args...>)
  821. : _error(il, static_cast<Args &&>(args)...)
  822. , _status(status::have_error)
  823. {
  824. _set_error_is_errno(*this);
  825. }
  826. struct nonvoid_converting_constructor_tag
  827. {
  828. };
  829. template <class U, class V>
  830. static constexpr bool enable_nonvoid_converting_constructor =
  831. !(std::is_same<std::decay_t<U>, value_type>::value && std::is_same<std::decay_t<V>, error_type>::value) //
  832. && detail::is_constructible<value_type, U> && detail::is_constructible<error_type, V>;
  833. BOOST_OUTCOME_TEMPLATE(class U, class V)
  834. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_nonvoid_converting_constructor<U, V>))
  835. constexpr explicit value_storage_trivial(const value_storage_trivial<U, V> &o, nonvoid_converting_constructor_tag /*unused*/ = {}) noexcept(
  836. detail::is_nothrow_constructible<_value_type_, U> &&detail::is_nothrow_constructible<_error_type_, V>)
  837. : value_storage_trivial(o._status.have_value() ?
  838. value_storage_trivial(in_place_type<value_type>, o._value) :
  839. (o._status.have_error() ? value_storage_trivial(in_place_type<error_type>, o._error) : value_storage_trivial())) // NOLINT
  840. {
  841. _status = o._status;
  842. }
  843. BOOST_OUTCOME_TEMPLATE(class U, class V)
  844. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_nonvoid_converting_constructor<U, V>))
  845. constexpr explicit value_storage_trivial(value_storage_trivial<U, V> &&o, nonvoid_converting_constructor_tag /*unused*/ = {}) noexcept(
  846. detail::is_nothrow_constructible<_value_type_, U> &&detail::is_nothrow_constructible<_error_type_, V>)
  847. : value_storage_trivial(
  848. o._status.have_value() ?
  849. value_storage_trivial(in_place_type<value_type>, static_cast<U &&>(o._value)) :
  850. (o._status.have_error() ? value_storage_trivial(in_place_type<error_type>, static_cast<V &&>(o._error)) : value_storage_trivial())) // NOLINT
  851. {
  852. _status = o._status;
  853. }
  854. struct void_value_converting_constructor_tag
  855. {
  856. };
  857. template <class V>
  858. static constexpr bool enable_void_value_converting_constructor =
  859. std::is_default_constructible<value_type>::value &&detail::is_constructible<error_type, V>;
  860. BOOST_OUTCOME_TEMPLATE(class V)
  861. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_void_value_converting_constructor<V>))
  862. constexpr explicit value_storage_trivial(const value_storage_trivial<void, V> &o, void_value_converting_constructor_tag /*unused*/ = {}) noexcept(
  863. std::is_nothrow_default_constructible<_value_type_>::value &&detail::is_nothrow_constructible<_error_type_, V>)
  864. : value_storage_trivial(o._status.have_value() ?
  865. value_storage_trivial(in_place_type<value_type>) :
  866. (o._status.have_error() ? value_storage_trivial(in_place_type<error_type>, o._error) : value_storage_trivial())) // NOLINT
  867. {
  868. _status = o._status;
  869. }
  870. BOOST_OUTCOME_TEMPLATE(class V)
  871. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_void_value_converting_constructor<V>))
  872. constexpr explicit value_storage_trivial(value_storage_trivial<void, V> &&o, void_value_converting_constructor_tag /*unused*/ = {}) noexcept(
  873. std::is_nothrow_default_constructible<_value_type_>::value &&detail::is_nothrow_constructible<_error_type_, V>)
  874. : value_storage_trivial(
  875. o._status.have_value() ?
  876. value_storage_trivial(in_place_type<value_type>) :
  877. (o._status.have_error() ? value_storage_trivial(in_place_type<error_type>, static_cast<V &&>(o._error)) : value_storage_trivial())) // NOLINT
  878. {
  879. _status = o._status;
  880. }
  881. struct void_error_converting_constructor_tag
  882. {
  883. };
  884. template <class U>
  885. static constexpr bool enable_void_error_converting_constructor =
  886. std::is_default_constructible<error_type>::value &&detail::is_constructible<value_type, U>;
  887. BOOST_OUTCOME_TEMPLATE(class U)
  888. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_void_error_converting_constructor<U>))
  889. constexpr explicit value_storage_trivial(const value_storage_trivial<U, void> &o, void_error_converting_constructor_tag /*unused*/ = {}) noexcept(
  890. detail::is_nothrow_constructible<_value_type_, U> &&std::is_nothrow_default_constructible<_error_type_>::value)
  891. : value_storage_trivial(o._status.have_value() ?
  892. value_storage_trivial(in_place_type<value_type>, o._value) :
  893. (o._status.have_error() ? value_storage_trivial(in_place_type<error_type>) : value_storage_trivial())) // NOLINT
  894. {
  895. _status = o._status;
  896. }
  897. BOOST_OUTCOME_TEMPLATE(class U)
  898. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_void_error_converting_constructor<U>))
  899. constexpr explicit value_storage_trivial(value_storage_trivial<U, void> &&o, void_error_converting_constructor_tag /*unused*/ = {}) noexcept(
  900. detail::is_nothrow_constructible<_value_type_, U> &&std::is_nothrow_default_constructible<_error_type_>::value)
  901. : value_storage_trivial(o._status.have_value() ?
  902. value_storage_trivial(in_place_type<value_type>, static_cast<U &&>(o._value)) :
  903. (o._status.have_error() ? value_storage_trivial(in_place_type<error_type>) : value_storage_trivial())) // NOLINT
  904. {
  905. _status = o._status;
  906. }
  907. constexpr void swap(value_storage_trivial &o) noexcept
  908. {
  909. // storage is trivial, so just use assignment
  910. auto temp = static_cast<value_storage_trivial &&>(*this);
  911. *this = static_cast<value_storage_trivial &&>(o);
  912. o = static_cast<value_storage_trivial &&>(temp);
  913. }
  914. };
  915. // Used if T is non-trivial
  916. template <class T, class E> struct value_storage_nontrivial
  917. {
  918. using value_type = T;
  919. using error_type = E;
  920. struct disable_in_place_value_type
  921. {
  922. };
  923. struct disable_in_place_error_type
  924. {
  925. };
  926. using _value_type = std::conditional_t<std::is_same<value_type, error_type>::value, disable_in_place_value_type, value_type>;
  927. using _error_type = std::conditional_t<std::is_same<value_type, error_type>::value, disable_in_place_error_type, error_type>;
  928. using _value_type_ = devoid<value_type>;
  929. using _error_type_ = devoid<error_type>;
  930. union {
  931. empty_type _empty1;
  932. _value_type_ _value;
  933. };
  934. status_bitfield_type _status;
  935. union {
  936. empty_type _empty2;
  937. _error_type_ _error;
  938. };
  939. value_storage_nontrivial() noexcept
  940. : _empty1{}
  941. , _empty2{}
  942. {
  943. }
  944. value_storage_nontrivial &operator=(const value_storage_nontrivial &) = default; // if reaches here, copy assignment is trivial
  945. value_storage_nontrivial &operator=(value_storage_nontrivial &&) = default; // NOLINT if reaches here, move assignment is trivial
  946. value_storage_nontrivial(value_storage_nontrivial &&o) noexcept(
  947. std::is_nothrow_move_constructible<_value_type_>::value &&std::is_nothrow_move_constructible<_error_type_>::value) // NOLINT
  948. {
  949. if(o._status.have_value())
  950. {
  951. new(&_value) _value_type_(static_cast<_value_type_ &&>(o._value)); // NOLINT
  952. }
  953. else if(o._status.have_error())
  954. {
  955. new(&_error) _error_type_(static_cast<_error_type_ &&>(o._error)); // NOLINT
  956. }
  957. _status = o._status;
  958. o._status.set_have_moved_from(true);
  959. }
  960. value_storage_nontrivial(const value_storage_nontrivial &o) noexcept(
  961. std::is_nothrow_copy_constructible<_value_type_>::value &&std::is_nothrow_copy_constructible<_error_type_>::value)
  962. {
  963. if(o._status.have_value())
  964. {
  965. new(&_value) _value_type_(o._value); // NOLINT
  966. }
  967. else if(o._status.have_error())
  968. {
  969. new(&_error) _error_type_(o._error); // NOLINT
  970. }
  971. _status = o._status;
  972. }
  973. explicit value_storage_nontrivial(status_bitfield_type status)
  974. : _empty1()
  975. , _status(status)
  976. , _empty2()
  977. {
  978. }
  979. template <class... Args>
  980. explicit value_storage_nontrivial(in_place_type_t<_value_type> /*unused*/,
  981. Args &&... args) noexcept(detail::is_nothrow_constructible<_value_type_, Args...>)
  982. : _value(static_cast<Args &&>(args)...) // NOLINT
  983. , _status(status::have_value)
  984. {
  985. }
  986. template <class U, class... Args>
  987. value_storage_nontrivial(in_place_type_t<_value_type> /*unused*/, std::initializer_list<U> il,
  988. Args &&... args) noexcept(detail::is_nothrow_constructible<_value_type_, std::initializer_list<U>, Args...>)
  989. : _value(il, static_cast<Args &&>(args)...)
  990. , _status(status::have_value)
  991. {
  992. }
  993. template <class... Args>
  994. explicit value_storage_nontrivial(in_place_type_t<_error_type> /*unused*/,
  995. Args &&... args) noexcept(detail::is_nothrow_constructible<_error_type_, Args...>)
  996. : _status(status::have_error)
  997. , _error(static_cast<Args &&>(args)...) // NOLINT
  998. {
  999. _set_error_is_errno(*this);
  1000. }
  1001. template <class U, class... Args>
  1002. value_storage_nontrivial(in_place_type_t<_error_type> /*unused*/, std::initializer_list<U> il,
  1003. Args &&... args) noexcept(detail::is_nothrow_constructible<_error_type_, std::initializer_list<U>, Args...>)
  1004. : _status(status::have_error)
  1005. , _error(il, static_cast<Args &&>(args)...)
  1006. {
  1007. _set_error_is_errno(*this);
  1008. }
  1009. struct nonvoid_converting_constructor_tag
  1010. {
  1011. };
  1012. template <class U, class V>
  1013. static constexpr bool enable_nonvoid_converting_constructor =
  1014. !(std::is_same<std::decay_t<U>, value_type>::value && std::is_same<std::decay_t<V>, error_type>::value) //
  1015. && detail::is_constructible<value_type, U> && detail::is_constructible<error_type, V>;
  1016. BOOST_OUTCOME_TEMPLATE(class U, class V)
  1017. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_nonvoid_converting_constructor<U, V>))
  1018. constexpr explicit value_storage_nontrivial(const value_storage_trivial<U, V> &o, nonvoid_converting_constructor_tag /*unused*/ = {}) noexcept(
  1019. detail::is_nothrow_constructible<_value_type_, U> &&detail::is_nothrow_constructible<_error_type_, V>)
  1020. : value_storage_nontrivial(o._status.have_value() ?
  1021. value_storage_nontrivial(in_place_type<value_type>, o._value) :
  1022. (o._status.have_error() ? value_storage_nontrivial(in_place_type<error_type>, o._error) : value_storage_nontrivial()))
  1023. {
  1024. _status = o._status;
  1025. }
  1026. BOOST_OUTCOME_TEMPLATE(class U, class V)
  1027. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_nonvoid_converting_constructor<U, V>))
  1028. constexpr explicit value_storage_nontrivial(value_storage_trivial<U, V> &&o, nonvoid_converting_constructor_tag /*unused*/ = {}) noexcept(
  1029. detail::is_nothrow_constructible<_value_type_, U> &&detail::is_nothrow_constructible<_error_type_, V>)
  1030. : value_storage_nontrivial(
  1031. o._status.have_value() ?
  1032. value_storage_nontrivial(in_place_type<value_type>, static_cast<U &&>(o._value)) :
  1033. (o._status.have_error() ? value_storage_nontrivial(in_place_type<error_type>, static_cast<V &&>(o._error)) : value_storage_nontrivial()))
  1034. {
  1035. _status = o._status;
  1036. }
  1037. BOOST_OUTCOME_TEMPLATE(class U, class V)
  1038. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_nonvoid_converting_constructor<U, V>))
  1039. constexpr explicit value_storage_nontrivial(const value_storage_nontrivial<U, V> &o, nonvoid_converting_constructor_tag /*unused*/ = {}) noexcept(
  1040. detail::is_nothrow_constructible<_value_type_, U> &&detail::is_nothrow_constructible<_error_type_, V>)
  1041. : value_storage_nontrivial(o._status.have_value() ?
  1042. value_storage_nontrivial(in_place_type<value_type>, o._value) :
  1043. (o._status.have_error() ? value_storage_nontrivial(in_place_type<error_type>, o._error) : value_storage_nontrivial()))
  1044. {
  1045. _status = o._status;
  1046. }
  1047. BOOST_OUTCOME_TEMPLATE(class U, class V)
  1048. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_nonvoid_converting_constructor<U, V>))
  1049. constexpr explicit value_storage_nontrivial(value_storage_nontrivial<U, V> &&o, nonvoid_converting_constructor_tag /*unused*/ = {}) noexcept(
  1050. detail::is_nothrow_constructible<_value_type_, U> &&detail::is_nothrow_constructible<_error_type_, V>)
  1051. : value_storage_nontrivial(
  1052. o._status.have_value() ?
  1053. value_storage_nontrivial(in_place_type<value_type>, static_cast<U &&>(o._value)) :
  1054. (o._status.have_error() ? value_storage_nontrivial(in_place_type<error_type>, static_cast<V &&>(o._error)) : value_storage_nontrivial()))
  1055. {
  1056. _status = o._status;
  1057. }
  1058. struct void_value_converting_constructor_tag
  1059. {
  1060. };
  1061. template <class V>
  1062. static constexpr bool enable_void_value_converting_constructor =
  1063. std::is_default_constructible<value_type>::value &&detail::is_constructible<error_type, V>;
  1064. BOOST_OUTCOME_TEMPLATE(class V)
  1065. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_void_value_converting_constructor<V>))
  1066. explicit value_storage_nontrivial(const value_storage_trivial<void, V> &o, void_value_converting_constructor_tag /*unused*/ = {}) noexcept(
  1067. std::is_nothrow_default_constructible<_value_type_>::value &&detail::is_nothrow_constructible<_error_type_, V>)
  1068. {
  1069. if(o._status.have_value())
  1070. {
  1071. new(&_value) _value_type_(); // NOLINT
  1072. }
  1073. else if(o._status.have_error())
  1074. {
  1075. new(&_error) _error_type_(o._error); // NOLINT
  1076. }
  1077. _status = o._status;
  1078. }
  1079. BOOST_OUTCOME_TEMPLATE(class V)
  1080. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_void_value_converting_constructor<V>))
  1081. explicit value_storage_nontrivial(value_storage_trivial<void, V> &&o, void_value_converting_constructor_tag /*unused*/ = {}) noexcept(
  1082. std::is_nothrow_default_constructible<_value_type_>::value &&detail::is_nothrow_constructible<_error_type_, V>)
  1083. {
  1084. if(o._status.have_value())
  1085. {
  1086. new(&_value) _value_type_(); // NOLINT
  1087. }
  1088. else if(o._status.have_error())
  1089. {
  1090. new(&_error) _error_type_(static_cast<_error_type_ &&>(o._error)); // NOLINT
  1091. }
  1092. _status = o._status;
  1093. o._status.set_have_moved_from(true);
  1094. }
  1095. struct void_error_converting_constructor_tag
  1096. {
  1097. };
  1098. template <class U>
  1099. static constexpr bool enable_void_error_converting_constructor =
  1100. std::is_default_constructible<error_type>::value &&detail::is_constructible<value_type, U>;
  1101. BOOST_OUTCOME_TEMPLATE(class U)
  1102. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_void_error_converting_constructor<U>))
  1103. explicit value_storage_nontrivial(const value_storage_trivial<U, void> &o, void_error_converting_constructor_tag /*unused*/ = {}) noexcept(
  1104. detail::is_nothrow_constructible<_value_type_, U> &&std::is_nothrow_default_constructible<_error_type_>::value)
  1105. {
  1106. if(o._status.have_value())
  1107. {
  1108. new(&_value) _value_type_(o._value); // NOLINT
  1109. }
  1110. else if(o._status.have_error())
  1111. {
  1112. new(&_error) _error_type_(); // NOLINT
  1113. }
  1114. _status = o._status;
  1115. }
  1116. BOOST_OUTCOME_TEMPLATE(class U)
  1117. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_void_error_converting_constructor<U>))
  1118. explicit value_storage_nontrivial(value_storage_trivial<U, void> &&o, void_error_converting_constructor_tag /*unused*/ = {}) noexcept(
  1119. detail::is_nothrow_constructible<_value_type_, U> &&std::is_nothrow_default_constructible<_error_type_>::value)
  1120. {
  1121. if(o._status.have_value())
  1122. {
  1123. new(&_value) _value_type_(static_cast<_value_type_ &&>(o._value)); // NOLINT
  1124. }
  1125. else if(o._status.have_error())
  1126. {
  1127. new(&_error) _error_type_(); // NOLINT
  1128. }
  1129. _status = o._status;
  1130. o._status.set_have_moved_from(true);
  1131. }
  1132. ~value_storage_nontrivial() noexcept(std::is_nothrow_destructible<_value_type_>::value &&std::is_nothrow_destructible<_error_type_>::value)
  1133. {
  1134. if(this->_status.have_value())
  1135. {
  1136. if(!trait::is_move_bitcopying<value_type>::value || !this->_status.have_moved_from())
  1137. {
  1138. this->_value.~_value_type_(); // NOLINT
  1139. }
  1140. this->_status.set_have_value(false);
  1141. }
  1142. else if(this->_status.have_error())
  1143. {
  1144. if(!trait::is_move_bitcopying<error_type>::value || !this->_status.have_moved_from())
  1145. {
  1146. this->_error.~_error_type_(); // NOLINT
  1147. }
  1148. this->_status.set_have_error(false);
  1149. }
  1150. }
  1151. constexpr void
  1152. swap(value_storage_nontrivial &o) noexcept(detail::is_nothrow_swappable<_value_type_>::value &&detail::is_nothrow_swappable<_error_type_>::value)
  1153. {
  1154. using std::swap;
  1155. // empty/empty
  1156. if(!_status.have_value() && !o._status.have_value() && !_status.have_error() && !o._status.have_error())
  1157. {
  1158. swap(_status, o._status);
  1159. return;
  1160. }
  1161. // value/value
  1162. if(_status.have_value() && o._status.have_value())
  1163. {
  1164. struct _
  1165. {
  1166. status_bitfield_type &a, &b;
  1167. bool all_good{false};
  1168. ~_()
  1169. {
  1170. if(!all_good)
  1171. {
  1172. // We lost one of the values
  1173. a.set_have_lost_consistency(true);
  1174. b.set_have_lost_consistency(true);
  1175. }
  1176. }
  1177. } _{_status, o._status};
  1178. strong_swap(_.all_good, _value, o._value);
  1179. swap(_status, o._status);
  1180. return;
  1181. }
  1182. // error/error
  1183. if(_status.have_error() && o._status.have_error())
  1184. {
  1185. struct _
  1186. {
  1187. status_bitfield_type &a, &b;
  1188. bool all_good{false};
  1189. ~_()
  1190. {
  1191. if(!all_good)
  1192. {
  1193. // We lost one of the values
  1194. a.set_have_lost_consistency(true);
  1195. b.set_have_lost_consistency(true);
  1196. }
  1197. }
  1198. } _{_status, o._status};
  1199. strong_swap(_.all_good, _error, o._error);
  1200. swap(_status, o._status);
  1201. return;
  1202. }
  1203. // Could be value/empty, error/empty, etc
  1204. if(_status.have_value() && !o._status.have_error())
  1205. {
  1206. // Move construct me into other
  1207. new(&o._value) _value_type_(static_cast<_value_type_ &&>(_value)); // NOLINT
  1208. if(!trait::is_move_bitcopying<value_type>::value)
  1209. {
  1210. this->_value.~value_type(); // NOLINT
  1211. }
  1212. swap(_status, o._status);
  1213. return;
  1214. }
  1215. if(o._status.have_value() && !_status.have_error())
  1216. {
  1217. // Move construct other into me
  1218. new(&_value) _value_type_(static_cast<_value_type_ &&>(o._value)); // NOLINT
  1219. if(!trait::is_move_bitcopying<value_type>::value)
  1220. {
  1221. o._value.~value_type(); // NOLINT
  1222. }
  1223. swap(_status, o._status);
  1224. return;
  1225. }
  1226. if(_status.have_error() && !o._status.have_value())
  1227. {
  1228. // Move construct me into other
  1229. new(&o._error) _error_type_(static_cast<_error_type_ &&>(_error)); // NOLINT
  1230. if(!trait::is_move_bitcopying<error_type>::value)
  1231. {
  1232. this->_error.~error_type(); // NOLINT
  1233. }
  1234. swap(_status, o._status);
  1235. return;
  1236. }
  1237. if(o._status.have_error() && !_status.have_value())
  1238. {
  1239. // Move construct other into me
  1240. new(&_error) _error_type_(static_cast<_error_type_ &&>(o._error)); // NOLINT
  1241. if(!trait::is_move_bitcopying<error_type>::value)
  1242. {
  1243. o._error.~error_type(); // NOLINT
  1244. }
  1245. swap(_status, o._status);
  1246. return;
  1247. }
  1248. // It can now only be value/error, or error/value
  1249. struct _
  1250. {
  1251. status_bitfield_type &a, &b;
  1252. _value_type_ *value, *o_value;
  1253. _error_type_ *error, *o_error;
  1254. bool all_good{true};
  1255. ~_()
  1256. {
  1257. if(!all_good)
  1258. {
  1259. // We lost one of the values
  1260. a.set_have_lost_consistency(true);
  1261. b.set_have_lost_consistency(true);
  1262. }
  1263. }
  1264. } _{_status, o._status, &_value, &o._value, &_error, &o._error};
  1265. if(_status.have_value() && o._status.have_error())
  1266. {
  1267. strong_placement(_.all_good, _.o_value, _.value, [&_] { //
  1268. strong_placement(_.all_good, _.error, _.o_error, [&_] { //
  1269. swap(_.a, _.b); //
  1270. });
  1271. });
  1272. return;
  1273. }
  1274. if(_status.have_error() && o._status.have_value())
  1275. {
  1276. strong_placement(_.all_good, _.o_error, _.error, [&_] { //
  1277. strong_placement(_.all_good, _.value, _.o_value, [&_] { //
  1278. swap(_.a, _.b); //
  1279. });
  1280. });
  1281. return;
  1282. }
  1283. // Should never reach here
  1284. make_ub(_value);
  1285. }
  1286. };
  1287. template <class Base> struct value_storage_delete_copy_constructor : Base // NOLINT
  1288. {
  1289. using Base::Base;
  1290. using value_type = typename Base::value_type;
  1291. using error_type = typename Base::error_type;
  1292. value_storage_delete_copy_constructor() = default;
  1293. value_storage_delete_copy_constructor(const value_storage_delete_copy_constructor &) = delete;
  1294. value_storage_delete_copy_constructor(value_storage_delete_copy_constructor &&) = default; // NOLINT
  1295. };
  1296. template <class Base> struct value_storage_delete_copy_assignment : Base // NOLINT
  1297. {
  1298. using Base::Base;
  1299. using value_type = typename Base::value_type;
  1300. using error_type = typename Base::error_type;
  1301. value_storage_delete_copy_assignment() = default;
  1302. value_storage_delete_copy_assignment(const value_storage_delete_copy_assignment &) = default;
  1303. value_storage_delete_copy_assignment(value_storage_delete_copy_assignment &&) = default; // NOLINT
  1304. value_storage_delete_copy_assignment &operator=(const value_storage_delete_copy_assignment &o) = delete;
  1305. value_storage_delete_copy_assignment &operator=(value_storage_delete_copy_assignment &&o) = default; // NOLINT
  1306. };
  1307. template <class Base> struct value_storage_delete_move_assignment : Base // NOLINT
  1308. {
  1309. using Base::Base;
  1310. using value_type = typename Base::value_type;
  1311. using error_type = typename Base::error_type;
  1312. value_storage_delete_move_assignment() = default;
  1313. value_storage_delete_move_assignment(const value_storage_delete_move_assignment &) = default;
  1314. value_storage_delete_move_assignment(value_storage_delete_move_assignment &&) = default; // NOLINT
  1315. value_storage_delete_move_assignment &operator=(const value_storage_delete_move_assignment &o) = default;
  1316. value_storage_delete_move_assignment &operator=(value_storage_delete_move_assignment &&o) = delete;
  1317. };
  1318. template <class Base> struct value_storage_delete_move_constructor : Base // NOLINT
  1319. {
  1320. using Base::Base;
  1321. using value_type = typename Base::value_type;
  1322. using error_type = typename Base::error_type;
  1323. value_storage_delete_move_constructor() = default;
  1324. value_storage_delete_move_constructor(const value_storage_delete_move_constructor &) = default;
  1325. value_storage_delete_move_constructor(value_storage_delete_move_constructor &&) = delete;
  1326. };
  1327. template <class Base> struct value_storage_nontrivial_move_assignment : Base // NOLINT
  1328. {
  1329. using Base::Base;
  1330. using value_type = typename Base::value_type;
  1331. using error_type = typename Base::error_type;
  1332. value_storage_nontrivial_move_assignment() = default;
  1333. value_storage_nontrivial_move_assignment(const value_storage_nontrivial_move_assignment &) = default;
  1334. value_storage_nontrivial_move_assignment(value_storage_nontrivial_move_assignment &&) = default; // NOLINT
  1335. value_storage_nontrivial_move_assignment &operator=(const value_storage_nontrivial_move_assignment &o) = default;
  1336. value_storage_nontrivial_move_assignment &operator=(value_storage_nontrivial_move_assignment &&o) noexcept(
  1337. std::is_nothrow_move_assignable<value_type>::value &&std::is_nothrow_move_assignable<error_type>::value) // NOLINT
  1338. {
  1339. using _value_type_ = typename Base::_value_type_;
  1340. using _error_type_ = typename Base::_error_type_;
  1341. if(!this->_status.have_value() && !this->_status.have_error() && !o._status.have_value() && !o._status.have_error())
  1342. {
  1343. this->_status = o._status;
  1344. o._status.set_have_moved_from(true);
  1345. return *this;
  1346. }
  1347. if(this->_status.have_value() && o._status.have_value())
  1348. {
  1349. this->_value = static_cast<_value_type_&&>(o._value); // NOLINT
  1350. this->_status = o._status;
  1351. o._status.set_have_moved_from(true);
  1352. return *this;
  1353. }
  1354. if(this->_status.have_error() && o._status.have_error())
  1355. {
  1356. this->_error = static_cast<_error_type_&&>(o._error); // NOLINT
  1357. this->_status = o._status;
  1358. o._status.set_have_moved_from(true);
  1359. return *this;
  1360. }
  1361. if(this->_status.have_value() && !o._status.have_value() && !o._status.have_error())
  1362. {
  1363. if(!trait::is_move_bitcopying<value_type>::value || this->_status.have_moved_from())
  1364. {
  1365. this->_value.~_value_type_(); // NOLINT
  1366. }
  1367. this->_status = o._status;
  1368. o._status.set_have_moved_from(true);
  1369. return *this;
  1370. }
  1371. if(!this->_status.have_value() && !this->_status.have_error() && o._status.have_value())
  1372. {
  1373. new(&this->_value) _value_type_(static_cast<_value_type_&&>(o._value)); // NOLINT
  1374. this->_status = o._status;
  1375. o._status.set_have_moved_from(true);
  1376. return *this;
  1377. }
  1378. if(this->_status.have_error() && !o._status.have_value() && !o._status.have_error())
  1379. {
  1380. if(!trait::is_move_bitcopying<error_type>::value || this->_status.have_moved_from())
  1381. {
  1382. this->_error.~_error_type_(); // NOLINT
  1383. }
  1384. this->_status = o._status;
  1385. o._status.set_have_moved_from(true);
  1386. return *this;
  1387. }
  1388. if(!this->_status.have_value() && !this->_status.have_error() && o._status.have_error())
  1389. {
  1390. new(&this->_error) _error_type_(static_cast<_error_type_&&>(o._error)); // NOLINT
  1391. this->_status = o._status;
  1392. o._status.set_have_moved_from(true);
  1393. return *this;
  1394. }
  1395. if(this->_status.have_value() && o._status.have_error())
  1396. {
  1397. if(!trait::is_move_bitcopying<value_type>::value || this->_status.have_moved_from())
  1398. {
  1399. this->_value.~_value_type_(); // NOLINT
  1400. }
  1401. new(&this->_error) _error_type_(static_cast<_error_type_&&>(o._error)); // NOLINT
  1402. this->_status = o._status;
  1403. o._status.set_have_moved_from(true);
  1404. return *this;
  1405. }
  1406. if(this->_status.have_error() && o._status.have_value())
  1407. {
  1408. if(!trait::is_move_bitcopying<error_type>::value || this->_status.have_moved_from())
  1409. {
  1410. this->_error.~_error_type_(); // NOLINT
  1411. }
  1412. new(&this->_value) _value_type_(static_cast<_value_type_&&>(o._value)); // NOLINT
  1413. this->_status = o._status;
  1414. o._status.set_have_moved_from(true);
  1415. return *this;
  1416. }
  1417. // Should never reach here
  1418. make_ub(this->_value);
  1419. }
  1420. };
  1421. template <class Base> struct value_storage_nontrivial_copy_assignment : Base // NOLINT
  1422. {
  1423. using Base::Base;
  1424. using value_type = typename Base::value_type;
  1425. using error_type = typename Base::error_type;
  1426. value_storage_nontrivial_copy_assignment() = default;
  1427. value_storage_nontrivial_copy_assignment(const value_storage_nontrivial_copy_assignment &) = default;
  1428. value_storage_nontrivial_copy_assignment(value_storage_nontrivial_copy_assignment &&) = default; // NOLINT
  1429. value_storage_nontrivial_copy_assignment &operator=(value_storage_nontrivial_copy_assignment &&o) = default; // NOLINT
  1430. value_storage_nontrivial_copy_assignment &operator=(const value_storage_nontrivial_copy_assignment &o) noexcept(
  1431. std::is_nothrow_copy_assignable<value_type>::value &&std::is_nothrow_copy_assignable<error_type>::value)
  1432. {
  1433. using _value_type_ = typename Base::_value_type_;
  1434. using _error_type_ = typename Base::_error_type_;
  1435. if(!this->_status.have_value() && !this->_status.have_error() && !o._status.have_value()
  1436. && !o._status.have_error())
  1437. {
  1438. this->_status = o._status;
  1439. return *this;
  1440. }
  1441. if(this->_status.have_value() && o._status.have_value())
  1442. {
  1443. this->_value = o._value; // NOLINT
  1444. this->_status = o._status;
  1445. return *this;
  1446. }
  1447. if(this->_status.have_error() && o._status.have_error())
  1448. {
  1449. this->_error = o._error; // NOLINT
  1450. this->_status = o._status;
  1451. return *this;
  1452. }
  1453. if(this->_status.have_value() && !o._status.have_value() && !o._status.have_error())
  1454. {
  1455. if(!trait::is_move_bitcopying<value_type>::value || this->_status.have_moved_from())
  1456. {
  1457. this->_value.~_value_type_(); // NOLINT
  1458. }
  1459. this->_status = o._status;
  1460. return *this;
  1461. }
  1462. if(!this->_status.have_value() && !this->_status.have_error() && o._status.have_value())
  1463. {
  1464. new(&this->_value) _value_type_(o._value); // NOLINT
  1465. this->_status = o._status;
  1466. return *this;
  1467. }
  1468. if(this->_status.have_error() && !o._status.have_value() && !o._status.have_error())
  1469. {
  1470. if(!trait::is_move_bitcopying<error_type>::value || this->_status.have_moved_from())
  1471. {
  1472. this->_error.~_error_type_(); // NOLINT
  1473. }
  1474. this->_status = o._status;
  1475. return *this;
  1476. }
  1477. if(!this->_status.have_value() && !this->_status.have_error() && o._status.have_error())
  1478. {
  1479. new(&this->_error) _error_type_(o._error); // NOLINT
  1480. this->_status = o._status;
  1481. return *this;
  1482. }
  1483. if(this->_status.have_value() && o._status.have_error())
  1484. {
  1485. if(!trait::is_move_bitcopying<value_type>::value || this->_status.have_moved_from())
  1486. {
  1487. this->_value.~_value_type_(); // NOLINT
  1488. }
  1489. new(&this->_error) _error_type_(o._error); // NOLINT
  1490. this->_status = o._status;
  1491. return *this;
  1492. }
  1493. if(this->_status.have_error() && o._status.have_value())
  1494. {
  1495. if(!trait::is_move_bitcopying<error_type>::value || this->_status.have_moved_from())
  1496. {
  1497. this->_error.~_error_type_(); // NOLINT
  1498. }
  1499. new(&this->_value) _value_type_(o._value); // NOLINT
  1500. this->_status = o._status;
  1501. return *this;
  1502. }
  1503. // Should never reach here
  1504. make_ub(this->_value);
  1505. }
  1506. };
  1507. #ifdef _MSC_VER
  1508. #pragma warning(pop)
  1509. #endif
  1510. // is_trivially_copyable is true even if type is not copyable, so handle that here
  1511. template <class T> struct is_storage_trivial
  1512. {
  1513. static constexpr bool value = std::is_void<T>::value || (std::is_trivially_copy_constructible<T>::value && std::is_trivially_copyable<T>::value);
  1514. };
  1515. // work around libstdc++ 7 bug
  1516. template <> struct is_storage_trivial<void>
  1517. {
  1518. static constexpr bool value = true;
  1519. };
  1520. template <> struct is_storage_trivial<const void>
  1521. {
  1522. static constexpr bool value = true;
  1523. };
  1524. template <class T, class E>
  1525. using value_storage_select_trivality =
  1526. std::conditional_t<is_storage_trivial<T>::value && is_storage_trivial<E>::value, value_storage_trivial<T, E>, value_storage_nontrivial<T, E>>;
  1527. template <class T, class E>
  1528. using value_storage_select_move_constructor =
  1529. std::conditional_t<std::is_move_constructible<devoid<T>>::value && std::is_move_constructible<devoid<E>>::value, value_storage_select_trivality<T, E>,
  1530. value_storage_delete_move_constructor<value_storage_select_trivality<T, E>>>;
  1531. template <class T, class E>
  1532. using value_storage_select_copy_constructor =
  1533. std::conditional_t<std::is_copy_constructible<devoid<T>>::value && std::is_copy_constructible<devoid<E>>::value, value_storage_select_move_constructor<T, E>,
  1534. value_storage_delete_copy_constructor<value_storage_select_move_constructor<T, E>>>;
  1535. template <class T, class E>
  1536. using value_storage_select_move_assignment =
  1537. std::conditional_t<std::is_trivially_move_assignable<devoid<T>>::value && std::is_trivially_move_assignable<devoid<E>>::value,
  1538. value_storage_select_copy_constructor<T, E>,
  1539. std::conditional_t<std::is_move_assignable<devoid<T>>::value && std::is_move_assignable<devoid<E>>::value,
  1540. value_storage_nontrivial_move_assignment<value_storage_select_copy_constructor<T, E>>,
  1541. value_storage_delete_copy_assignment<value_storage_select_copy_constructor<T, E>>>>;
  1542. template <class T, class E>
  1543. using value_storage_select_copy_assignment =
  1544. std::conditional_t<std::is_trivially_copy_assignable<devoid<T>>::value && std::is_trivially_copy_assignable<devoid<E>>::value,
  1545. value_storage_select_move_assignment<T, E>,
  1546. std::conditional_t<std::is_copy_assignable<devoid<T>>::value && std::is_copy_assignable<devoid<E>>::value,
  1547. value_storage_nontrivial_copy_assignment<value_storage_select_move_assignment<T, E>>,
  1548. value_storage_delete_copy_assignment<value_storage_select_move_assignment<T, E>>>>;
  1549. template <class T, class E> using value_storage_select_impl = value_storage_select_copy_assignment<T, E>;
  1550. #ifndef NDEBUG
  1551. // Check is trivial in all ways except default constructibility
  1552. // static_assert(std::is_trivial<value_storage_select_impl<int, long>>::value, "value_storage_select_impl<int, long> is not trivial!");
  1553. // static_assert(std::is_trivially_default_constructible<value_storage_select_impl<int, long>>::value, "value_storage_select_impl<int, long> is not trivially
  1554. // default constructible!");
  1555. static_assert(std::is_trivially_copyable<value_storage_select_impl<int, long>>::value, "value_storage_select_impl<int, long> is not trivially copyable!");
  1556. static_assert(std::is_trivially_assignable<value_storage_select_impl<int, long>, value_storage_select_impl<int, long>>::value,
  1557. "value_storage_select_impl<int, long> is not trivially assignable!");
  1558. static_assert(std::is_trivially_destructible<value_storage_select_impl<int, long>>::value,
  1559. "value_storage_select_impl<int, long> is not trivially destructible!");
  1560. static_assert(std::is_trivially_copy_constructible<value_storage_select_impl<int, long>>::value,
  1561. "value_storage_select_impl<int, long> is not trivially copy constructible!");
  1562. static_assert(std::is_trivially_move_constructible<value_storage_select_impl<int, long>>::value,
  1563. "value_storage_select_impl<int, long> is not trivially move constructible!");
  1564. static_assert(std::is_trivially_copy_assignable<value_storage_select_impl<int, long>>::value,
  1565. "value_storage_select_impl<int, long> is not trivially copy assignable!");
  1566. static_assert(std::is_trivially_move_assignable<value_storage_select_impl<int, long>>::value,
  1567. "value_storage_select_impl<int, long> is not trivially move assignable!");
  1568. // Also check is standard layout
  1569. static_assert(std::is_standard_layout<value_storage_select_impl<int, long>>::value, "value_storage_select_impl<int, long> is not a standard layout type!");
  1570. #endif
  1571. } // namespace detail
  1572. BOOST_OUTCOME_V2_NAMESPACE_END
  1573. #endif