12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613 |
- /* Essentially an internal optional implementation :)
- (C) 2017-2021 Niall Douglas <http://www.nedproductions.biz/> (24 commits)
- File Created: June 2017
- Boost Software License - Version 1.0 - August 17th, 2003
- Permission is hereby granted, free of charge, to any person or organization
- obtaining a copy of the software and accompanying documentation covered by
- this license (the "Software") to use, reproduce, display, distribute,
- execute, and transmit the Software, and to prepare derivative works of the
- Software, and to permit third-parties to whom the Software is furnished to
- do so, all subject to the following:
- The copyright notices in the Software and this entire statement, including
- the above license grant, this restriction and the following disclaimer,
- must be included in all copies of the Software, in whole or in part, and
- all derivative works of the Software, unless such copies or derivative
- works are solely in the form of machine-executable object code generated by
- a source language processor.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
- SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
- FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
- ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- DEALINGS IN THE SOFTWARE.
- */
- #ifndef BOOST_OUTCOME_VALUE_STORAGE_HPP
- #define BOOST_OUTCOME_VALUE_STORAGE_HPP
- #include "../config.hpp"
- #include <cassert>
- BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN
- namespace detail
- {
- template <class T, bool nothrow> struct strong_swap_impl
- {
- constexpr strong_swap_impl(bool &allgood, T &a, T &b)
- {
- allgood = true;
- using std::swap;
- swap(a, b);
- }
- };
- template <class T, bool nothrow> struct strong_placement_impl
- {
- template <class F> constexpr strong_placement_impl(bool &allgood, T *a, T *b, F &&f)
- {
- allgood = true;
- new(a) T(static_cast<T &&>(*b));
- b->~T();
- f();
- }
- };
- #ifndef BOOST_NO_EXCEPTIONS
- template <class T> struct strong_swap_impl<T, false>
- {
- strong_swap_impl(bool &allgood, T &a, T &b)
- {
- allgood = true;
- T v(static_cast<T &&>(a));
- try
- {
- a = static_cast<T &&>(b);
- }
- catch(...)
- {
- // Try to put back a
- try
- {
- a = static_cast<T &&>(v);
- // fall through as all good
- }
- catch(...)
- {
- // failed to completely restore
- allgood = false;
- // throw away second exception
- }
- throw; // rethrow original exception
- }
- // b has been moved to a, try to move v to b
- try
- {
- b = static_cast<T &&>(v);
- }
- catch(...)
- {
- // Try to restore a to b, and v to a
- try
- {
- b = static_cast<T &&>(a);
- a = static_cast<T &&>(v);
- // fall through as all good
- }
- catch(...)
- {
- // failed to completely restore
- allgood = false;
- // throw away second exception
- }
- throw; // rethrow original exception
- }
- }
- };
- template <class T> struct strong_placement_impl<T, false>
- {
- template <class F> strong_placement_impl(bool &allgood, T *a, T *b, F &&f)
- {
- new(a) T(static_cast<T &&>(*b));
- try
- {
- b->~T();
- f();
- }
- catch(...)
- {
- // Try to put back a, but only if we are still good
- if(allgood)
- {
- try
- {
- new(b) T(static_cast<T &&>(*a));
- // fall through as all good
- }
- catch(...)
- {
- // failed to completely restore
- allgood = false;
- // throw away second exception
- }
- throw; // rethrow original exception
- }
- }
- }
- };
- #endif
- } // namespace detail
- /*!
- */
- BOOST_OUTCOME_TEMPLATE(class T)
- BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_move_constructible<T>::value &&std::is_move_assignable<T>::value))
- constexpr inline void strong_swap(bool &allgood, T &a, T &b) noexcept(detail::is_nothrow_swappable<T>::value)
- {
- detail::strong_swap_impl<T, detail::is_nothrow_swappable<T>::value>(allgood, a, b);
- }
- /*!
- */
- BOOST_OUTCOME_TEMPLATE(class T, class F)
- BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_move_constructible<T>::value &&std::is_move_assignable<T>::value))
- constexpr inline void strong_placement(bool &allgood, T *a, T *b, F &&f) noexcept(std::is_nothrow_move_constructible<T>::value)
- {
- detail::strong_placement_impl<T, std::is_nothrow_move_constructible<T>::value>(allgood, a, b, static_cast<F &&>(f));
- }
- namespace detail
- {
- template <class T>
- constexpr
- #ifdef _MSC_VER
- __declspec(noreturn)
- #elif defined(__GNUC__) || defined(__clang__)
- __attribute__((noreturn))
- #endif
- void make_ub(T && /*unused*/)
- {
- assert(false); // NOLINT
- #if defined(__GNUC__) || defined(__clang__)
- __builtin_unreachable();
- #elif defined(_MSC_VER)
- __assume(0);
- #endif
- }
- /* Outcome v1 used a C bitfield whose values were tracked by compiler optimisers nicely,
- but that produces ICEs when used in constexpr.
- Outcome v2.0-v2.1 used a 32 bit integer and manually set and cleared bits. Unfortunately
- only GCC's optimiser tracks bit values during constant folding, and only per byte, and
- even then unreliably. https://wg21.link/P1886 "Error speed benchmarking" showed just how
- poorly clang and MSVC fails to optimise outcome-using code, if you manually set bits.
- Outcome v2.2 therefore uses an enum with fixed values, and constexpr manipulation functions
- to change the value to one of the enum's values. This is stupid to look at in source code,
- but it make clang's optimiser do the right thing, so it's worth it.
- */
- #define BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS 0
- enum class status : uint16_t
- {
- // WARNING: These bits are not tracked by abi-dumper, but changing them will break ABI!
- none = 0,
- have_value = (1U << 0U),
- have_error = (1U << 1U),
- have_exception = (2U << 1U),
- have_error_exception = (3U << 1U),
- // failed to complete a strong swap
- have_lost_consistency = (1U << 3U),
- have_value_lost_consistency = (1U << 0U) | (1U << 3U),
- have_error_lost_consistency = (1U << 1U) | (1U << 3U),
- have_exception_lost_consistency = (2U << 1U) | (1U << 3U),
- have_error_exception_lost_consistency = (3U << 1U) | (1U << 3U),
- // can errno be set from this error?
- have_error_is_errno = (1U << 4U),
- have_error_error_is_errno = (1U << 1U) | (1U << 4U),
- have_error_exception_error_is_errno = (3U << 1U) | (1U << 4U),
- have_error_lost_consistency_error_is_errno = (1U << 1U) | (1U << 3U) | (1U << 4U),
- have_error_exception_lost_consistency_error_is_errno = (3U << 1U) | (1U << 3U) | (1U << 4U),
- // value has been moved from
- have_moved_from = (1U << 5U)
- };
- struct status_bitfield_type
- {
- status status_value{status::none};
- uint16_t spare_storage_value{0}; // hooks::spare_storage()
- constexpr status_bitfield_type() = default;
- constexpr status_bitfield_type(status v) noexcept
- : status_value(v)
- {
- } // NOLINT
- constexpr status_bitfield_type(status v, uint16_t s) noexcept
- : status_value(v)
- , spare_storage_value(s)
- {
- }
- constexpr status_bitfield_type(const status_bitfield_type &) = default;
- constexpr status_bitfield_type(status_bitfield_type &&) = default;
- constexpr status_bitfield_type &operator=(const status_bitfield_type &) = default;
- constexpr status_bitfield_type &operator=(status_bitfield_type &&) = default;
- //~status_bitfield_type() = default; // Do NOT uncomment this, it breaks older clangs!
- constexpr bool have_value() const noexcept
- {
- #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
- return (status_value == status::have_value) //
- || (status_value == status::have_value_lost_consistency) //
- ;
- #else
- return (static_cast<uint16_t>(status_value) & static_cast<uint16_t>(status::have_value)) != 0;
- #endif
- }
- constexpr bool have_error() const noexcept
- {
- #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
- return (status_value == status::have_error) //
- || (status_value == status::have_error_exception) //
- || (status_value == status::have_error_lost_consistency) //
- || (status_value == status::have_error_exception_lost_consistency) //
- || (status_value == status::have_error_error_is_errno) //
- || (status_value == status::have_error_exception_error_is_errno) //
- || (status_value == status::have_error_lost_consistency_error_is_errno) //
- || (status_value == status::have_error_exception_lost_consistency_error_is_errno) //
- ;
- #else
- return (static_cast<uint16_t>(status_value) & static_cast<uint16_t>(status::have_error)) != 0;
- #endif
- }
- constexpr bool have_exception() const noexcept
- {
- #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
- return (status_value == status::have_exception) //
- || (status_value == status::have_error_exception) //
- || (status_value == status::have_exception_lost_consistency) //
- || (status_value == status::have_error_exception_lost_consistency) //
- || (status_value == status::have_error_exception_error_is_errno) //
- || (status_value == status::have_error_exception_lost_consistency_error_is_errno) //
- ;
- #else
- return (static_cast<uint16_t>(status_value) & static_cast<uint16_t>(status::have_exception)) != 0;
- #endif
- }
- constexpr bool have_lost_consistency() const noexcept
- {
- #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
- return (status_value == status::have_value_lost_consistency) //
- || (status_value == status::have_error_lost_consistency) //
- || (status_value == status::have_exception_lost_consistency) //
- || (status_value == status::have_error_lost_consistency_error_is_errno) //
- || (status_value == status::have_error_exception_lost_consistency_error_is_errno) //
- ;
- #else
- return (static_cast<uint16_t>(status_value) & static_cast<uint16_t>(status::have_lost_consistency)) != 0;
- #endif
- }
- constexpr bool have_error_is_errno() const noexcept
- {
- #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
- return (status_value == status::have_error_error_is_errno) //
- || (status_value == status::have_error_exception_error_is_errno) //
- || (status_value == status::have_error_lost_consistency_error_is_errno) //
- || (status_value == status::have_error_exception_lost_consistency_error_is_errno) //
- ;
- #else
- return (static_cast<uint16_t>(status_value) & static_cast<uint16_t>(status::have_error_is_errno)) != 0;
- #endif
- }
- constexpr bool have_moved_from() const noexcept
- {
- #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
- #error Fixme
- #else
- return (static_cast<uint16_t>(status_value) & static_cast<uint16_t>(status::have_moved_from)) != 0;
- #endif
- }
- constexpr status_bitfield_type &set_have_value(bool v) noexcept
- {
- #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
- switch(status_value)
- {
- case status::none:
- if(v)
- {
- status_value = status::have_value;
- }
- break;
- case status::have_value:
- if(!v)
- {
- status_value = status::none;
- }
- break;
- case status::have_error:
- if(v)
- {
- make_ub(*this);
- }
- break;
- case status::have_exception:
- if(v)
- {
- make_ub(*this);
- }
- break;
- case status::have_error_exception:
- if(v)
- {
- make_ub(*this);
- }
- break;
- case status::have_value_lost_consistency:
- if(!v)
- {
- status_value = status::none;
- }
- break;
- case status::have_error_lost_consistency:
- if(v)
- {
- make_ub(*this);
- }
- break;
- case status::have_exception_lost_consistency:
- if(v)
- {
- make_ub(*this);
- }
- break;
- case status::have_error_exception_lost_consistency:
- if(v)
- {
- make_ub(*this);
- }
- break;
- case status::have_error_error_is_errno:
- if(v)
- {
- make_ub(*this);
- }
- break;
- case status::have_error_exception_error_is_errno:
- if(v)
- {
- make_ub(*this);
- }
- break;
- case status::have_error_lost_consistency_error_is_errno:
- if(v)
- {
- make_ub(*this);
- }
- break;
- case status::have_error_exception_lost_consistency_error_is_errno:
- if(v)
- {
- make_ub(*this);
- }
- break;
- }
- #else
- status_value = static_cast<status>(v ? (static_cast<uint16_t>(status_value) | static_cast<uint16_t>(status::have_value)) :
- (static_cast<uint16_t>(status_value) & ~static_cast<uint16_t>(status::have_value)));
- #endif
- return *this;
- }
- constexpr status_bitfield_type &set_have_error(bool v) noexcept
- {
- #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
- switch(status_value)
- {
- case status::none:
- if(v)
- {
- status_value = status::have_error;
- }
- break;
- case status::have_value:
- if(v)
- {
- make_ub(*this);
- }
- break;
- case status::have_error:
- if(!v)
- {
- status_value = status::none;
- }
- break;
- case status::have_exception:
- if(v)
- {
- status_value = status::have_error_exception;
- }
- break;
- case status::have_error_exception:
- if(!v)
- {
- status_value = status::have_exception;
- }
- break;
- case status::have_value_lost_consistency:
- if(v)
- {
- make_ub(*this);
- }
- break;
- case status::have_error_lost_consistency:
- if(!v)
- {
- status_value = status::none;
- }
- break;
- case status::have_exception_lost_consistency:
- if(v)
- {
- status_value = status::have_error_exception_lost_consistency;
- }
- break;
- case status::have_error_exception_lost_consistency:
- if(!v)
- {
- status_value = status::have_exception_lost_consistency;
- }
- break;
- case status::have_error_error_is_errno:
- if(!v)
- {
- status_value = status::none;
- }
- break;
- case status::have_error_exception_error_is_errno:
- if(!v)
- {
- status_value = status::have_exception;
- }
- break;
- case status::have_error_lost_consistency_error_is_errno:
- if(!v)
- {
- status_value = status::none;
- }
- break;
- case status::have_error_exception_lost_consistency_error_is_errno:
- if(!v)
- {
- status_value = status::have_exception_lost_consistency;
- }
- break;
- }
- #else
- status_value = static_cast<status>(v ? (static_cast<uint16_t>(status_value) | static_cast<uint16_t>(status::have_error)) :
- (static_cast<uint16_t>(status_value) & ~static_cast<uint16_t>(status::have_error)));
- #endif
- return *this;
- }
- constexpr status_bitfield_type &set_have_exception(bool v) noexcept
- {
- #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
- switch(status_value)
- {
- case status::none:
- if(v)
- {
- status_value = status::have_exception;
- }
- break;
- case status::have_value:
- if(v)
- {
- make_ub(*this);
- }
- break;
- case status::have_error:
- if(v)
- {
- status_value = status::have_error_exception;
- }
- break;
- case status::have_exception:
- if(!v)
- {
- status_value = status::none;
- }
- break;
- case status::have_error_exception:
- if(!v)
- {
- status_value = status::have_error;
- }
- break;
- case status::have_value_lost_consistency:
- if(v)
- {
- make_ub(*this);
- }
- break;
- case status::have_error_lost_consistency:
- if(v)
- {
- status_value = status::have_error_exception_lost_consistency;
- }
- break;
- case status::have_exception_lost_consistency:
- if(!v)
- {
- status_value = status::none;
- }
- break;
- case status::have_error_exception_lost_consistency:
- if(!v)
- {
- status_value = status::have_error_lost_consistency;
- }
- break;
- case status::have_error_error_is_errno:
- if(v)
- {
- status_value = status::have_error_exception_error_is_errno;
- }
- break;
- case status::have_error_exception_error_is_errno:
- if(!v)
- {
- status_value = status::have_error_error_is_errno;
- }
- break;
- case status::have_error_lost_consistency_error_is_errno:
- if(v)
- {
- status_value = status::have_error_exception_lost_consistency_error_is_errno;
- }
- break;
- case status::have_error_exception_lost_consistency_error_is_errno:
- if(!v)
- {
- status_value = status::have_error_lost_consistency_error_is_errno;
- }
- break;
- }
- #else
- status_value = static_cast<status>(v ? (static_cast<uint16_t>(status_value) | static_cast<uint16_t>(status::have_exception)) :
- (static_cast<uint16_t>(status_value) & ~static_cast<uint16_t>(status::have_exception)));
- #endif
- return *this;
- }
- constexpr status_bitfield_type &set_have_error_is_errno(bool v) noexcept
- {
- #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
- switch(status_value)
- {
- case status::none:
- make_ub(*this);
- break;
- case status::have_value:
- make_ub(*this);
- break;
- case status::have_error:
- if(v)
- {
- status_value = status::have_error_error_is_errno;
- }
- break;
- case status::have_exception:
- make_ub(*this);
- break;
- case status::have_error_exception:
- if(v)
- {
- status_value = status::have_error_exception_error_is_errno;
- }
- break;
- case status::have_value_lost_consistency:
- make_ub(*this);
- break;
- case status::have_error_lost_consistency:
- if(v)
- {
- status_value = status::have_error_lost_consistency_error_is_errno;
- }
- break;
- case status::have_exception_lost_consistency:
- make_ub(*this);
- break;
- case status::have_error_exception_lost_consistency:
- if(v)
- {
- status_value = status::have_error_exception_lost_consistency_error_is_errno;
- }
- break;
- case status::have_error_error_is_errno:
- if(!v)
- {
- status_value = status::have_error;
- }
- break;
- case status::have_error_exception_error_is_errno:
- if(!v)
- {
- status_value = status::have_error_exception;
- }
- break;
- case status::have_error_lost_consistency_error_is_errno:
- if(!v)
- {
- status_value = status::have_error_lost_consistency;
- }
- break;
- case status::have_error_exception_lost_consistency_error_is_errno:
- if(!v)
- {
- status_value = status::have_error_exception_lost_consistency;
- }
- break;
- }
- #else
- status_value = static_cast<status>(v ? (static_cast<uint16_t>(status_value) | static_cast<uint16_t>(status::have_error_is_errno)) :
- (static_cast<uint16_t>(status_value) & ~static_cast<uint16_t>(status::have_error_is_errno)));
- #endif
- return *this;
- }
- constexpr status_bitfield_type &set_have_lost_consistency(bool v) noexcept
- {
- #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
- switch(status_value)
- {
- case status::none:
- if(v)
- {
- make_ub(*this);
- }
- break;
- case status::have_value:
- if(v)
- {
- status_value = status::have_value_lost_consistency;
- }
- break;
- case status::have_error:
- if(v)
- {
- status_value = status::have_error_lost_consistency;
- }
- break;
- case status::have_exception:
- if(v)
- {
- status_value = status::have_exception_lost_consistency;
- }
- break;
- case status::have_error_exception:
- if(v)
- {
- status_value = status::have_error_exception_lost_consistency;
- }
- break;
- case status::have_value_lost_consistency:
- if(!v)
- {
- status_value = status::have_value;
- }
- break;
- case status::have_error_lost_consistency:
- if(!v)
- {
- status_value = status::have_error;
- }
- break;
- case status::have_exception_lost_consistency:
- if(!v)
- {
- status_value = status::have_exception;
- }
- break;
- case status::have_error_exception_lost_consistency:
- if(!v)
- {
- status_value = status::have_error_exception;
- }
- break;
- case status::have_error_error_is_errno:
- if(v)
- {
- status_value = status::have_error_lost_consistency_error_is_errno;
- }
- break;
- case status::have_error_exception_error_is_errno:
- if(v)
- {
- status_value = status::have_error_exception_lost_consistency_error_is_errno;
- }
- break;
- case status::have_error_lost_consistency_error_is_errno:
- if(!v)
- {
- status_value = status::have_error_exception_error_is_errno;
- }
- break;
- case status::have_error_exception_lost_consistency_error_is_errno:
- if(!v)
- {
- status_value = status::have_error_exception_error_is_errno;
- }
- break;
- }
- #else
- status_value = static_cast<status>(v ? (static_cast<uint16_t>(status_value) | static_cast<uint16_t>(status::have_lost_consistency)) :
- (static_cast<uint16_t>(status_value) & ~static_cast<uint16_t>(status::have_lost_consistency)));
- #endif
- return *this;
- }
- constexpr status_bitfield_type &set_have_moved_from(bool v) noexcept
- {
- #if BOOST_OUTCOME_USE_CONSTEXPR_ENUM_STATUS
- #error Fixme
- #else
- status_value = static_cast<status>(v ? (static_cast<uint16_t>(status_value) | static_cast<uint16_t>(status::have_moved_from)) :
- (static_cast<uint16_t>(status_value) & ~static_cast<uint16_t>(status::have_moved_from)));
- #endif
- return *this;
- }
- };
- #if !defined(NDEBUG)
- // Check is trivial in all ways except default constructibility
- static_assert(sizeof(status_bitfield_type) == 4, "status_bitfield_type is not sized 4 bytes!");
- static_assert(std::is_trivially_copyable<status_bitfield_type>::value, "status_bitfield_type is not trivially copyable!");
- static_assert(std::is_trivially_assignable<status_bitfield_type, status_bitfield_type>::value, "status_bitfield_type is not trivially assignable!");
- static_assert(std::is_trivially_destructible<status_bitfield_type>::value, "status_bitfield_type is not trivially destructible!");
- static_assert(std::is_trivially_copy_constructible<status_bitfield_type>::value, "status_bitfield_type is not trivially copy constructible!");
- static_assert(std::is_trivially_move_constructible<status_bitfield_type>::value, "status_bitfield_type is not trivially move constructible!");
- static_assert(std::is_trivially_copy_assignable<status_bitfield_type>::value, "status_bitfield_type is not trivially copy assignable!");
- static_assert(std::is_trivially_move_assignable<status_bitfield_type>::value, "status_bitfield_type is not trivially move assignable!");
- // Also check is standard layout
- static_assert(std::is_standard_layout<status_bitfield_type>::value, "status_bitfield_type is not a standard layout type!");
- #endif
- template <class State> constexpr inline void _set_error_is_errno(State & /*unused*/) {}
- #ifdef _MSC_VER
- #pragma warning(push)
- #pragma warning(disable : 4127) // conditional expression is constant
- #pragma warning(disable : 4624) // destructor was implicitly defined as deleted
- #endif
- // Used if both T and E are trivial
- template <class T, class E> struct value_storage_trivial
- {
- using value_type = T;
- using error_type = E;
- // Disable in place construction if they are the same type
- struct disable_in_place_value_type
- {
- };
- struct disable_in_place_error_type
- {
- };
- using _value_type = std::conditional_t<std::is_same<value_type, error_type>::value, disable_in_place_value_type, value_type>;
- using _error_type = std::conditional_t<std::is_same<value_type, error_type>::value, disable_in_place_error_type, error_type>;
- using _value_type_ = devoid<value_type>;
- using _error_type_ = devoid<error_type>;
- union {
- empty_type _empty;
- _value_type_ _value;
- _error_type_ _error;
- };
- status_bitfield_type _status;
- constexpr value_storage_trivial() noexcept
- : _empty{}
- {
- }
- value_storage_trivial(const value_storage_trivial &) = default; // NOLINT
- value_storage_trivial(value_storage_trivial &&) = default; // NOLINT
- value_storage_trivial &operator=(const value_storage_trivial &) = default; // NOLINT
- value_storage_trivial &operator=(value_storage_trivial &&) = default; // NOLINT
- ~value_storage_trivial() = default;
- constexpr explicit value_storage_trivial(status_bitfield_type status)
- : _empty()
- , _status(status)
- {
- }
- template <class... Args>
- constexpr explicit value_storage_trivial(in_place_type_t<_value_type> /*unused*/,
- Args &&... args) noexcept(detail::is_nothrow_constructible<_value_type_, Args...>)
- : _value(static_cast<Args &&>(args)...)
- , _status(status::have_value)
- {
- }
- template <class U, class... Args>
- constexpr value_storage_trivial(in_place_type_t<_value_type> /*unused*/, std::initializer_list<U> il,
- Args &&... args) noexcept(detail::is_nothrow_constructible<_value_type_, std::initializer_list<U>, Args...>)
- : _value(il, static_cast<Args &&>(args)...)
- , _status(status::have_value)
- {
- }
- template <class... Args>
- constexpr explicit value_storage_trivial(in_place_type_t<_error_type> /*unused*/,
- Args &&... args) noexcept(detail::is_nothrow_constructible<_error_type_, Args...>)
- : _error(static_cast<Args &&>(args)...)
- , _status(status::have_error)
- {
- _set_error_is_errno(*this);
- }
- template <class U, class... Args>
- constexpr value_storage_trivial(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...>)
- : _error(il, static_cast<Args &&>(args)...)
- , _status(status::have_error)
- {
- _set_error_is_errno(*this);
- }
- struct nonvoid_converting_constructor_tag
- {
- };
- template <class U, class V>
- static constexpr bool enable_nonvoid_converting_constructor =
- !(std::is_same<std::decay_t<U>, value_type>::value && std::is_same<std::decay_t<V>, error_type>::value) //
- && detail::is_constructible<value_type, U> && detail::is_constructible<error_type, V>;
- BOOST_OUTCOME_TEMPLATE(class U, class V)
- BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_nonvoid_converting_constructor<U, V>))
- constexpr explicit value_storage_trivial(const value_storage_trivial<U, V> &o, nonvoid_converting_constructor_tag /*unused*/ = {}) noexcept(
- detail::is_nothrow_constructible<_value_type_, U> &&detail::is_nothrow_constructible<_error_type_, V>)
- : value_storage_trivial(o._status.have_value() ?
- value_storage_trivial(in_place_type<value_type>, o._value) :
- (o._status.have_error() ? value_storage_trivial(in_place_type<error_type>, o._error) : value_storage_trivial())) // NOLINT
- {
- _status = o._status;
- }
- BOOST_OUTCOME_TEMPLATE(class U, class V)
- BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_nonvoid_converting_constructor<U, V>))
- constexpr explicit value_storage_trivial(value_storage_trivial<U, V> &&o, nonvoid_converting_constructor_tag /*unused*/ = {}) noexcept(
- detail::is_nothrow_constructible<_value_type_, U> &&detail::is_nothrow_constructible<_error_type_, V>)
- : value_storage_trivial(
- o._status.have_value() ?
- value_storage_trivial(in_place_type<value_type>, static_cast<U &&>(o._value)) :
- (o._status.have_error() ? value_storage_trivial(in_place_type<error_type>, static_cast<V &&>(o._error)) : value_storage_trivial())) // NOLINT
- {
- _status = o._status;
- }
- struct void_value_converting_constructor_tag
- {
- };
- template <class V>
- static constexpr bool enable_void_value_converting_constructor =
- std::is_default_constructible<value_type>::value &&detail::is_constructible<error_type, V>;
- BOOST_OUTCOME_TEMPLATE(class V)
- BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_void_value_converting_constructor<V>))
- constexpr explicit value_storage_trivial(const value_storage_trivial<void, V> &o, void_value_converting_constructor_tag /*unused*/ = {}) noexcept(
- std::is_nothrow_default_constructible<_value_type_>::value &&detail::is_nothrow_constructible<_error_type_, V>)
- : value_storage_trivial(o._status.have_value() ?
- value_storage_trivial(in_place_type<value_type>) :
- (o._status.have_error() ? value_storage_trivial(in_place_type<error_type>, o._error) : value_storage_trivial())) // NOLINT
- {
- _status = o._status;
- }
- BOOST_OUTCOME_TEMPLATE(class V)
- BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_void_value_converting_constructor<V>))
- constexpr explicit value_storage_trivial(value_storage_trivial<void, V> &&o, void_value_converting_constructor_tag /*unused*/ = {}) noexcept(
- std::is_nothrow_default_constructible<_value_type_>::value &&detail::is_nothrow_constructible<_error_type_, V>)
- : value_storage_trivial(
- o._status.have_value() ?
- value_storage_trivial(in_place_type<value_type>) :
- (o._status.have_error() ? value_storage_trivial(in_place_type<error_type>, static_cast<V &&>(o._error)) : value_storage_trivial())) // NOLINT
- {
- _status = o._status;
- }
- struct void_error_converting_constructor_tag
- {
- };
- template <class U>
- static constexpr bool enable_void_error_converting_constructor =
- std::is_default_constructible<error_type>::value &&detail::is_constructible<value_type, U>;
- BOOST_OUTCOME_TEMPLATE(class U)
- BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_void_error_converting_constructor<U>))
- constexpr explicit value_storage_trivial(const value_storage_trivial<U, void> &o, void_error_converting_constructor_tag /*unused*/ = {}) noexcept(
- detail::is_nothrow_constructible<_value_type_, U> &&std::is_nothrow_default_constructible<_error_type_>::value)
- : value_storage_trivial(o._status.have_value() ?
- value_storage_trivial(in_place_type<value_type>, o._value) :
- (o._status.have_error() ? value_storage_trivial(in_place_type<error_type>) : value_storage_trivial())) // NOLINT
- {
- _status = o._status;
- }
- BOOST_OUTCOME_TEMPLATE(class U)
- BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_void_error_converting_constructor<U>))
- constexpr explicit value_storage_trivial(value_storage_trivial<U, void> &&o, void_error_converting_constructor_tag /*unused*/ = {}) noexcept(
- detail::is_nothrow_constructible<_value_type_, U> &&std::is_nothrow_default_constructible<_error_type_>::value)
- : value_storage_trivial(o._status.have_value() ?
- value_storage_trivial(in_place_type<value_type>, static_cast<U &&>(o._value)) :
- (o._status.have_error() ? value_storage_trivial(in_place_type<error_type>) : value_storage_trivial())) // NOLINT
- {
- _status = o._status;
- }
- constexpr void swap(value_storage_trivial &o) noexcept
- {
- // storage is trivial, so just use assignment
- auto temp = static_cast<value_storage_trivial &&>(*this);
- *this = static_cast<value_storage_trivial &&>(o);
- o = static_cast<value_storage_trivial &&>(temp);
- }
- };
- // Used if T is non-trivial
- template <class T, class E> struct value_storage_nontrivial
- {
- using value_type = T;
- using error_type = E;
- struct disable_in_place_value_type
- {
- };
- struct disable_in_place_error_type
- {
- };
- using _value_type = std::conditional_t<std::is_same<value_type, error_type>::value, disable_in_place_value_type, value_type>;
- using _error_type = std::conditional_t<std::is_same<value_type, error_type>::value, disable_in_place_error_type, error_type>;
- using _value_type_ = devoid<value_type>;
- using _error_type_ = devoid<error_type>;
- union {
- empty_type _empty1;
- _value_type_ _value;
- };
- status_bitfield_type _status;
- union {
- empty_type _empty2;
- _error_type_ _error;
- };
- value_storage_nontrivial() noexcept
- : _empty1{}
- , _empty2{}
- {
- }
- value_storage_nontrivial &operator=(const value_storage_nontrivial &) = default; // if reaches here, copy assignment is trivial
- value_storage_nontrivial &operator=(value_storage_nontrivial &&) = default; // NOLINT if reaches here, move assignment is trivial
- value_storage_nontrivial(value_storage_nontrivial &&o) noexcept(
- std::is_nothrow_move_constructible<_value_type_>::value &&std::is_nothrow_move_constructible<_error_type_>::value) // NOLINT
- {
- if(o._status.have_value())
- {
- new(&_value) _value_type_(static_cast<_value_type_ &&>(o._value)); // NOLINT
- }
- else if(o._status.have_error())
- {
- new(&_error) _error_type_(static_cast<_error_type_ &&>(o._error)); // NOLINT
- }
- _status = o._status;
- o._status.set_have_moved_from(true);
- }
- value_storage_nontrivial(const value_storage_nontrivial &o) noexcept(
- std::is_nothrow_copy_constructible<_value_type_>::value &&std::is_nothrow_copy_constructible<_error_type_>::value)
- {
- if(o._status.have_value())
- {
- new(&_value) _value_type_(o._value); // NOLINT
- }
- else if(o._status.have_error())
- {
- new(&_error) _error_type_(o._error); // NOLINT
- }
- _status = o._status;
- }
- explicit value_storage_nontrivial(status_bitfield_type status)
- : _empty1()
- , _status(status)
- , _empty2()
- {
- }
- template <class... Args>
- explicit value_storage_nontrivial(in_place_type_t<_value_type> /*unused*/,
- Args &&... args) noexcept(detail::is_nothrow_constructible<_value_type_, Args...>)
- : _value(static_cast<Args &&>(args)...) // NOLINT
- , _status(status::have_value)
- {
- }
- template <class U, class... Args>
- value_storage_nontrivial(in_place_type_t<_value_type> /*unused*/, std::initializer_list<U> il,
- Args &&... args) noexcept(detail::is_nothrow_constructible<_value_type_, std::initializer_list<U>, Args...>)
- : _value(il, static_cast<Args &&>(args)...)
- , _status(status::have_value)
- {
- }
- template <class... Args>
- explicit value_storage_nontrivial(in_place_type_t<_error_type> /*unused*/,
- Args &&... args) noexcept(detail::is_nothrow_constructible<_error_type_, Args...>)
- : _status(status::have_error)
- , _error(static_cast<Args &&>(args)...) // NOLINT
- {
- _set_error_is_errno(*this);
- }
- template <class U, class... Args>
- value_storage_nontrivial(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...>)
- : _status(status::have_error)
- , _error(il, static_cast<Args &&>(args)...)
- {
- _set_error_is_errno(*this);
- }
- struct nonvoid_converting_constructor_tag
- {
- };
- template <class U, class V>
- static constexpr bool enable_nonvoid_converting_constructor =
- !(std::is_same<std::decay_t<U>, value_type>::value && std::is_same<std::decay_t<V>, error_type>::value) //
- && detail::is_constructible<value_type, U> && detail::is_constructible<error_type, V>;
- BOOST_OUTCOME_TEMPLATE(class U, class V)
- BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_nonvoid_converting_constructor<U, V>))
- constexpr explicit value_storage_nontrivial(const value_storage_trivial<U, V> &o, nonvoid_converting_constructor_tag /*unused*/ = {}) noexcept(
- detail::is_nothrow_constructible<_value_type_, U> &&detail::is_nothrow_constructible<_error_type_, V>)
- : value_storage_nontrivial(o._status.have_value() ?
- value_storage_nontrivial(in_place_type<value_type>, o._value) :
- (o._status.have_error() ? value_storage_nontrivial(in_place_type<error_type>, o._error) : value_storage_nontrivial()))
- {
- _status = o._status;
- }
- BOOST_OUTCOME_TEMPLATE(class U, class V)
- BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_nonvoid_converting_constructor<U, V>))
- constexpr explicit value_storage_nontrivial(value_storage_trivial<U, V> &&o, nonvoid_converting_constructor_tag /*unused*/ = {}) noexcept(
- detail::is_nothrow_constructible<_value_type_, U> &&detail::is_nothrow_constructible<_error_type_, V>)
- : value_storage_nontrivial(
- o._status.have_value() ?
- value_storage_nontrivial(in_place_type<value_type>, static_cast<U &&>(o._value)) :
- (o._status.have_error() ? value_storage_nontrivial(in_place_type<error_type>, static_cast<V &&>(o._error)) : value_storage_nontrivial()))
- {
- _status = o._status;
- }
- BOOST_OUTCOME_TEMPLATE(class U, class V)
- BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_nonvoid_converting_constructor<U, V>))
- constexpr explicit value_storage_nontrivial(const value_storage_nontrivial<U, V> &o, nonvoid_converting_constructor_tag /*unused*/ = {}) noexcept(
- detail::is_nothrow_constructible<_value_type_, U> &&detail::is_nothrow_constructible<_error_type_, V>)
- : value_storage_nontrivial(o._status.have_value() ?
- value_storage_nontrivial(in_place_type<value_type>, o._value) :
- (o._status.have_error() ? value_storage_nontrivial(in_place_type<error_type>, o._error) : value_storage_nontrivial()))
- {
- _status = o._status;
- }
- BOOST_OUTCOME_TEMPLATE(class U, class V)
- BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_nonvoid_converting_constructor<U, V>))
- constexpr explicit value_storage_nontrivial(value_storage_nontrivial<U, V> &&o, nonvoid_converting_constructor_tag /*unused*/ = {}) noexcept(
- detail::is_nothrow_constructible<_value_type_, U> &&detail::is_nothrow_constructible<_error_type_, V>)
- : value_storage_nontrivial(
- o._status.have_value() ?
- value_storage_nontrivial(in_place_type<value_type>, static_cast<U &&>(o._value)) :
- (o._status.have_error() ? value_storage_nontrivial(in_place_type<error_type>, static_cast<V &&>(o._error)) : value_storage_nontrivial()))
- {
- _status = o._status;
- }
- struct void_value_converting_constructor_tag
- {
- };
- template <class V>
- static constexpr bool enable_void_value_converting_constructor =
- std::is_default_constructible<value_type>::value &&detail::is_constructible<error_type, V>;
- BOOST_OUTCOME_TEMPLATE(class V)
- BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_void_value_converting_constructor<V>))
- explicit value_storage_nontrivial(const value_storage_trivial<void, V> &o, void_value_converting_constructor_tag /*unused*/ = {}) noexcept(
- std::is_nothrow_default_constructible<_value_type_>::value &&detail::is_nothrow_constructible<_error_type_, V>)
- {
- if(o._status.have_value())
- {
- new(&_value) _value_type_(); // NOLINT
- }
- else if(o._status.have_error())
- {
- new(&_error) _error_type_(o._error); // NOLINT
- }
- _status = o._status;
- }
- BOOST_OUTCOME_TEMPLATE(class V)
- BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_void_value_converting_constructor<V>))
- explicit value_storage_nontrivial(value_storage_trivial<void, V> &&o, void_value_converting_constructor_tag /*unused*/ = {}) noexcept(
- std::is_nothrow_default_constructible<_value_type_>::value &&detail::is_nothrow_constructible<_error_type_, V>)
- {
- if(o._status.have_value())
- {
- new(&_value) _value_type_(); // NOLINT
- }
- else if(o._status.have_error())
- {
- new(&_error) _error_type_(static_cast<_error_type_ &&>(o._error)); // NOLINT
- }
- _status = o._status;
- o._status.set_have_moved_from(true);
- }
- struct void_error_converting_constructor_tag
- {
- };
- template <class U>
- static constexpr bool enable_void_error_converting_constructor =
- std::is_default_constructible<error_type>::value &&detail::is_constructible<value_type, U>;
- BOOST_OUTCOME_TEMPLATE(class U)
- BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_void_error_converting_constructor<U>))
- explicit value_storage_nontrivial(const value_storage_trivial<U, void> &o, void_error_converting_constructor_tag /*unused*/ = {}) noexcept(
- detail::is_nothrow_constructible<_value_type_, U> &&std::is_nothrow_default_constructible<_error_type_>::value)
- {
- if(o._status.have_value())
- {
- new(&_value) _value_type_(o._value); // NOLINT
- }
- else if(o._status.have_error())
- {
- new(&_error) _error_type_(); // NOLINT
- }
- _status = o._status;
- }
- BOOST_OUTCOME_TEMPLATE(class U)
- BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(enable_void_error_converting_constructor<U>))
- explicit value_storage_nontrivial(value_storage_trivial<U, void> &&o, void_error_converting_constructor_tag /*unused*/ = {}) noexcept(
- detail::is_nothrow_constructible<_value_type_, U> &&std::is_nothrow_default_constructible<_error_type_>::value)
- {
- if(o._status.have_value())
- {
- new(&_value) _value_type_(static_cast<_value_type_ &&>(o._value)); // NOLINT
- }
- else if(o._status.have_error())
- {
- new(&_error) _error_type_(); // NOLINT
- }
- _status = o._status;
- o._status.set_have_moved_from(true);
- }
- ~value_storage_nontrivial() noexcept(std::is_nothrow_destructible<_value_type_>::value &&std::is_nothrow_destructible<_error_type_>::value)
- {
- if(this->_status.have_value())
- {
- if(!trait::is_move_bitcopying<value_type>::value || !this->_status.have_moved_from())
- {
- this->_value.~_value_type_(); // NOLINT
- }
- this->_status.set_have_value(false);
- }
- else if(this->_status.have_error())
- {
- if(!trait::is_move_bitcopying<error_type>::value || !this->_status.have_moved_from())
- {
- this->_error.~_error_type_(); // NOLINT
- }
- this->_status.set_have_error(false);
- }
- }
- constexpr void
- swap(value_storage_nontrivial &o) noexcept(detail::is_nothrow_swappable<_value_type_>::value &&detail::is_nothrow_swappable<_error_type_>::value)
- {
- using std::swap;
- // empty/empty
- if(!_status.have_value() && !o._status.have_value() && !_status.have_error() && !o._status.have_error())
- {
- swap(_status, o._status);
- return;
- }
- // value/value
- if(_status.have_value() && o._status.have_value())
- {
- struct _
- {
- status_bitfield_type &a, &b;
- bool all_good{false};
- ~_()
- {
- if(!all_good)
- {
- // We lost one of the values
- a.set_have_lost_consistency(true);
- b.set_have_lost_consistency(true);
- }
- }
- } _{_status, o._status};
- strong_swap(_.all_good, _value, o._value);
- swap(_status, o._status);
- return;
- }
- // error/error
- if(_status.have_error() && o._status.have_error())
- {
- struct _
- {
- status_bitfield_type &a, &b;
- bool all_good{false};
- ~_()
- {
- if(!all_good)
- {
- // We lost one of the values
- a.set_have_lost_consistency(true);
- b.set_have_lost_consistency(true);
- }
- }
- } _{_status, o._status};
- strong_swap(_.all_good, _error, o._error);
- swap(_status, o._status);
- return;
- }
- // Could be value/empty, error/empty, etc
- if(_status.have_value() && !o._status.have_error())
- {
- // Move construct me into other
- new(&o._value) _value_type_(static_cast<_value_type_ &&>(_value)); // NOLINT
- if(!trait::is_move_bitcopying<value_type>::value)
- {
- this->_value.~value_type(); // NOLINT
- }
- swap(_status, o._status);
- return;
- }
- if(o._status.have_value() && !_status.have_error())
- {
- // Move construct other into me
- new(&_value) _value_type_(static_cast<_value_type_ &&>(o._value)); // NOLINT
- if(!trait::is_move_bitcopying<value_type>::value)
- {
- o._value.~value_type(); // NOLINT
- }
- swap(_status, o._status);
- return;
- }
- if(_status.have_error() && !o._status.have_value())
- {
- // Move construct me into other
- new(&o._error) _error_type_(static_cast<_error_type_ &&>(_error)); // NOLINT
- if(!trait::is_move_bitcopying<error_type>::value)
- {
- this->_error.~error_type(); // NOLINT
- }
- swap(_status, o._status);
- return;
- }
- if(o._status.have_error() && !_status.have_value())
- {
- // Move construct other into me
- new(&_error) _error_type_(static_cast<_error_type_ &&>(o._error)); // NOLINT
- if(!trait::is_move_bitcopying<error_type>::value)
- {
- o._error.~error_type(); // NOLINT
- }
- swap(_status, o._status);
- return;
- }
- // It can now only be value/error, or error/value
- struct _
- {
- status_bitfield_type &a, &b;
- _value_type_ *value, *o_value;
- _error_type_ *error, *o_error;
- bool all_good{true};
- ~_()
- {
- if(!all_good)
- {
- // We lost one of the values
- a.set_have_lost_consistency(true);
- b.set_have_lost_consistency(true);
- }
- }
- } _{_status, o._status, &_value, &o._value, &_error, &o._error};
- if(_status.have_value() && o._status.have_error())
- {
- strong_placement(_.all_good, _.o_value, _.value, [&_] { //
- strong_placement(_.all_good, _.error, _.o_error, [&_] { //
- swap(_.a, _.b); //
- });
- });
- return;
- }
- if(_status.have_error() && o._status.have_value())
- {
- strong_placement(_.all_good, _.o_error, _.error, [&_] { //
- strong_placement(_.all_good, _.value, _.o_value, [&_] { //
- swap(_.a, _.b); //
- });
- });
- return;
- }
- // Should never reach here
- make_ub(_value);
- }
- };
- template <class Base> struct value_storage_delete_copy_constructor : Base // NOLINT
- {
- using Base::Base;
- using value_type = typename Base::value_type;
- using error_type = typename Base::error_type;
- value_storage_delete_copy_constructor() = default;
- value_storage_delete_copy_constructor(const value_storage_delete_copy_constructor &) = delete;
- value_storage_delete_copy_constructor(value_storage_delete_copy_constructor &&) = default; // NOLINT
- };
- template <class Base> struct value_storage_delete_copy_assignment : Base // NOLINT
- {
- using Base::Base;
- using value_type = typename Base::value_type;
- using error_type = typename Base::error_type;
- value_storage_delete_copy_assignment() = default;
- value_storage_delete_copy_assignment(const value_storage_delete_copy_assignment &) = default;
- value_storage_delete_copy_assignment(value_storage_delete_copy_assignment &&) = default; // NOLINT
- value_storage_delete_copy_assignment &operator=(const value_storage_delete_copy_assignment &o) = delete;
- value_storage_delete_copy_assignment &operator=(value_storage_delete_copy_assignment &&o) = default; // NOLINT
- };
- template <class Base> struct value_storage_delete_move_assignment : Base // NOLINT
- {
- using Base::Base;
- using value_type = typename Base::value_type;
- using error_type = typename Base::error_type;
- value_storage_delete_move_assignment() = default;
- value_storage_delete_move_assignment(const value_storage_delete_move_assignment &) = default;
- value_storage_delete_move_assignment(value_storage_delete_move_assignment &&) = default; // NOLINT
- value_storage_delete_move_assignment &operator=(const value_storage_delete_move_assignment &o) = default;
- value_storage_delete_move_assignment &operator=(value_storage_delete_move_assignment &&o) = delete;
- };
- template <class Base> struct value_storage_delete_move_constructor : Base // NOLINT
- {
- using Base::Base;
- using value_type = typename Base::value_type;
- using error_type = typename Base::error_type;
- value_storage_delete_move_constructor() = default;
- value_storage_delete_move_constructor(const value_storage_delete_move_constructor &) = default;
- value_storage_delete_move_constructor(value_storage_delete_move_constructor &&) = delete;
- };
- template <class Base> struct value_storage_nontrivial_move_assignment : Base // NOLINT
- {
- using Base::Base;
- using value_type = typename Base::value_type;
- using error_type = typename Base::error_type;
- value_storage_nontrivial_move_assignment() = default;
- value_storage_nontrivial_move_assignment(const value_storage_nontrivial_move_assignment &) = default;
- value_storage_nontrivial_move_assignment(value_storage_nontrivial_move_assignment &&) = default; // NOLINT
- value_storage_nontrivial_move_assignment &operator=(const value_storage_nontrivial_move_assignment &o) = default;
- value_storage_nontrivial_move_assignment &operator=(value_storage_nontrivial_move_assignment &&o) noexcept(
- std::is_nothrow_move_assignable<value_type>::value &&std::is_nothrow_move_assignable<error_type>::value) // NOLINT
- {
- using _value_type_ = typename Base::_value_type_;
- using _error_type_ = typename Base::_error_type_;
- if(!this->_status.have_value() && !this->_status.have_error() && !o._status.have_value() && !o._status.have_error())
- {
- this->_status = o._status;
- o._status.set_have_moved_from(true);
- return *this;
- }
- if(this->_status.have_value() && o._status.have_value())
- {
- this->_value = static_cast<_value_type_&&>(o._value); // NOLINT
- this->_status = o._status;
- o._status.set_have_moved_from(true);
- return *this;
- }
- if(this->_status.have_error() && o._status.have_error())
- {
- this->_error = static_cast<_error_type_&&>(o._error); // NOLINT
- this->_status = o._status;
- o._status.set_have_moved_from(true);
- return *this;
- }
- if(this->_status.have_value() && !o._status.have_value() && !o._status.have_error())
- {
- if(!trait::is_move_bitcopying<value_type>::value || this->_status.have_moved_from())
- {
- this->_value.~_value_type_(); // NOLINT
- }
- this->_status = o._status;
- o._status.set_have_moved_from(true);
- return *this;
- }
- if(!this->_status.have_value() && !this->_status.have_error() && o._status.have_value())
- {
- new(&this->_value) _value_type_(static_cast<_value_type_&&>(o._value)); // NOLINT
- this->_status = o._status;
- o._status.set_have_moved_from(true);
- return *this;
- }
- if(this->_status.have_error() && !o._status.have_value() && !o._status.have_error())
- {
- if(!trait::is_move_bitcopying<error_type>::value || this->_status.have_moved_from())
- {
- this->_error.~_error_type_(); // NOLINT
- }
- this->_status = o._status;
- o._status.set_have_moved_from(true);
- return *this;
- }
- if(!this->_status.have_value() && !this->_status.have_error() && o._status.have_error())
- {
- new(&this->_error) _error_type_(static_cast<_error_type_&&>(o._error)); // NOLINT
- this->_status = o._status;
- o._status.set_have_moved_from(true);
- return *this;
- }
- if(this->_status.have_value() && o._status.have_error())
- {
- if(!trait::is_move_bitcopying<value_type>::value || this->_status.have_moved_from())
- {
- this->_value.~_value_type_(); // NOLINT
- }
- new(&this->_error) _error_type_(static_cast<_error_type_&&>(o._error)); // NOLINT
- this->_status = o._status;
- o._status.set_have_moved_from(true);
- return *this;
- }
- if(this->_status.have_error() && o._status.have_value())
- {
- if(!trait::is_move_bitcopying<error_type>::value || this->_status.have_moved_from())
- {
- this->_error.~_error_type_(); // NOLINT
- }
- new(&this->_value) _value_type_(static_cast<_value_type_&&>(o._value)); // NOLINT
- this->_status = o._status;
- o._status.set_have_moved_from(true);
- return *this;
- }
- // Should never reach here
- make_ub(this->_value);
- }
- };
- template <class Base> struct value_storage_nontrivial_copy_assignment : Base // NOLINT
- {
- using Base::Base;
- using value_type = typename Base::value_type;
- using error_type = typename Base::error_type;
- value_storage_nontrivial_copy_assignment() = default;
- value_storage_nontrivial_copy_assignment(const value_storage_nontrivial_copy_assignment &) = default;
- value_storage_nontrivial_copy_assignment(value_storage_nontrivial_copy_assignment &&) = default; // NOLINT
- value_storage_nontrivial_copy_assignment &operator=(value_storage_nontrivial_copy_assignment &&o) = default; // NOLINT
- value_storage_nontrivial_copy_assignment &operator=(const value_storage_nontrivial_copy_assignment &o) noexcept(
- std::is_nothrow_copy_assignable<value_type>::value &&std::is_nothrow_copy_assignable<error_type>::value)
- {
- using _value_type_ = typename Base::_value_type_;
- using _error_type_ = typename Base::_error_type_;
- if(!this->_status.have_value() && !this->_status.have_error() && !o._status.have_value()
- && !o._status.have_error())
- {
- this->_status = o._status;
- return *this;
- }
- if(this->_status.have_value() && o._status.have_value())
- {
- this->_value = o._value; // NOLINT
- this->_status = o._status;
- return *this;
- }
- if(this->_status.have_error() && o._status.have_error())
- {
- this->_error = o._error; // NOLINT
- this->_status = o._status;
- return *this;
- }
- if(this->_status.have_value() && !o._status.have_value() && !o._status.have_error())
- {
- if(!trait::is_move_bitcopying<value_type>::value || this->_status.have_moved_from())
- {
- this->_value.~_value_type_(); // NOLINT
- }
- this->_status = o._status;
- return *this;
- }
- if(!this->_status.have_value() && !this->_status.have_error() && o._status.have_value())
- {
- new(&this->_value) _value_type_(o._value); // NOLINT
- this->_status = o._status;
- return *this;
- }
- if(this->_status.have_error() && !o._status.have_value() && !o._status.have_error())
- {
- if(!trait::is_move_bitcopying<error_type>::value || this->_status.have_moved_from())
- {
- this->_error.~_error_type_(); // NOLINT
- }
- this->_status = o._status;
- return *this;
- }
- if(!this->_status.have_value() && !this->_status.have_error() && o._status.have_error())
- {
- new(&this->_error) _error_type_(o._error); // NOLINT
- this->_status = o._status;
- return *this;
- }
- if(this->_status.have_value() && o._status.have_error())
- {
- if(!trait::is_move_bitcopying<value_type>::value || this->_status.have_moved_from())
- {
- this->_value.~_value_type_(); // NOLINT
- }
- new(&this->_error) _error_type_(o._error); // NOLINT
- this->_status = o._status;
- return *this;
- }
- if(this->_status.have_error() && o._status.have_value())
- {
- if(!trait::is_move_bitcopying<error_type>::value || this->_status.have_moved_from())
- {
- this->_error.~_error_type_(); // NOLINT
- }
- new(&this->_value) _value_type_(o._value); // NOLINT
- this->_status = o._status;
- return *this;
- }
- // Should never reach here
- make_ub(this->_value);
- }
- };
- #ifdef _MSC_VER
- #pragma warning(pop)
- #endif
- // is_trivially_copyable is true even if type is not copyable, so handle that here
- template <class T> struct is_storage_trivial
- {
- static constexpr bool value = std::is_void<T>::value || (std::is_trivially_copy_constructible<T>::value && std::is_trivially_copyable<T>::value);
- };
- // work around libstdc++ 7 bug
- template <> struct is_storage_trivial<void>
- {
- static constexpr bool value = true;
- };
- template <> struct is_storage_trivial<const void>
- {
- static constexpr bool value = true;
- };
- template <class T, class E>
- using value_storage_select_trivality =
- std::conditional_t<is_storage_trivial<T>::value && is_storage_trivial<E>::value, value_storage_trivial<T, E>, value_storage_nontrivial<T, E>>;
- template <class T, class E>
- using value_storage_select_move_constructor =
- std::conditional_t<std::is_move_constructible<devoid<T>>::value && std::is_move_constructible<devoid<E>>::value, value_storage_select_trivality<T, E>,
- value_storage_delete_move_constructor<value_storage_select_trivality<T, E>>>;
- template <class T, class E>
- using value_storage_select_copy_constructor =
- std::conditional_t<std::is_copy_constructible<devoid<T>>::value && std::is_copy_constructible<devoid<E>>::value, value_storage_select_move_constructor<T, E>,
- value_storage_delete_copy_constructor<value_storage_select_move_constructor<T, E>>>;
- template <class T, class E>
- using value_storage_select_move_assignment =
- std::conditional_t<std::is_trivially_move_assignable<devoid<T>>::value && std::is_trivially_move_assignable<devoid<E>>::value,
- value_storage_select_copy_constructor<T, E>,
- std::conditional_t<std::is_move_assignable<devoid<T>>::value && std::is_move_assignable<devoid<E>>::value,
- value_storage_nontrivial_move_assignment<value_storage_select_copy_constructor<T, E>>,
- value_storage_delete_copy_assignment<value_storage_select_copy_constructor<T, E>>>>;
- template <class T, class E>
- using value_storage_select_copy_assignment =
- std::conditional_t<std::is_trivially_copy_assignable<devoid<T>>::value && std::is_trivially_copy_assignable<devoid<E>>::value,
- value_storage_select_move_assignment<T, E>,
- std::conditional_t<std::is_copy_assignable<devoid<T>>::value && std::is_copy_assignable<devoid<E>>::value,
- value_storage_nontrivial_copy_assignment<value_storage_select_move_assignment<T, E>>,
- value_storage_delete_copy_assignment<value_storage_select_move_assignment<T, E>>>>;
- template <class T, class E> using value_storage_select_impl = value_storage_select_copy_assignment<T, E>;
- #ifndef NDEBUG
- // Check is trivial in all ways except default constructibility
- // static_assert(std::is_trivial<value_storage_select_impl<int, long>>::value, "value_storage_select_impl<int, long> is not trivial!");
- // static_assert(std::is_trivially_default_constructible<value_storage_select_impl<int, long>>::value, "value_storage_select_impl<int, long> is not trivially
- // default constructible!");
- static_assert(std::is_trivially_copyable<value_storage_select_impl<int, long>>::value, "value_storage_select_impl<int, long> is not trivially copyable!");
- static_assert(std::is_trivially_assignable<value_storage_select_impl<int, long>, value_storage_select_impl<int, long>>::value,
- "value_storage_select_impl<int, long> is not trivially assignable!");
- static_assert(std::is_trivially_destructible<value_storage_select_impl<int, long>>::value,
- "value_storage_select_impl<int, long> is not trivially destructible!");
- static_assert(std::is_trivially_copy_constructible<value_storage_select_impl<int, long>>::value,
- "value_storage_select_impl<int, long> is not trivially copy constructible!");
- static_assert(std::is_trivially_move_constructible<value_storage_select_impl<int, long>>::value,
- "value_storage_select_impl<int, long> is not trivially move constructible!");
- static_assert(std::is_trivially_copy_assignable<value_storage_select_impl<int, long>>::value,
- "value_storage_select_impl<int, long> is not trivially copy assignable!");
- static_assert(std::is_trivially_move_assignable<value_storage_select_impl<int, long>>::value,
- "value_storage_select_impl<int, long> is not trivially move assignable!");
- // Also check is standard layout
- 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!");
- #endif
- } // namespace detail
- BOOST_OUTCOME_V2_NAMESPACE_END
- #endif
|