variant.hpp 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456
  1. //-----------------------------------------------------------------------------
  2. // boost variant/variant.hpp header file
  3. // See http://www.boost.org for updates, documentation, and revision history.
  4. //-----------------------------------------------------------------------------
  5. //
  6. // Copyright (c) 2002-2003 Eric Friedman, Itay Maman
  7. // Copyright (c) 2012-2021 Antony Polukhin
  8. //
  9. // Distributed under the Boost Software License, Version 1.0. (See
  10. // accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. // Thanks to Adam Romanek for providing patches for exception-disabled env.
  13. #ifndef BOOST_VARIANT_VARIANT_HPP
  14. #define BOOST_VARIANT_VARIANT_HPP
  15. #include <cstddef> // for std::size_t
  16. #include <new> // for placement new
  17. #include <boost/type_index.hpp>
  18. #include <boost/variant/detail/config.hpp>
  19. #include <boost/mpl/aux_/value_wknd.hpp>
  20. #include <boost/variant/variant_fwd.hpp>
  21. #include <boost/variant/detail/backup_holder.hpp>
  22. #include <boost/variant/detail/enable_recursive_fwd.hpp>
  23. #include <boost/variant/detail/forced_return.hpp>
  24. #include <boost/variant/detail/initializer.hpp>
  25. #include <boost/variant/detail/make_variant_list.hpp>
  26. #include <boost/variant/detail/over_sequence.hpp>
  27. #include <boost/variant/detail/visitation_impl.hpp>
  28. #include <boost/variant/detail/hash_variant.hpp>
  29. #include <boost/variant/detail/std_hash.hpp>
  30. #include <boost/variant/detail/move.hpp>
  31. #include <boost/detail/reference_content.hpp>
  32. #include <boost/blank.hpp>
  33. #include <boost/integer/common_factor_ct.hpp>
  34. #include <boost/static_assert.hpp>
  35. #include <boost/preprocessor/cat.hpp>
  36. #include <boost/preprocessor/repeat.hpp>
  37. #include <boost/type_traits/aligned_storage.hpp>
  38. #include <boost/type_traits/alignment_of.hpp>
  39. #include <boost/type_traits/add_const.hpp>
  40. #include <boost/type_traits/has_nothrow_constructor.hpp>
  41. #include <boost/type_traits/has_nothrow_copy.hpp>
  42. #include <boost/type_traits/is_nothrow_move_assignable.hpp>
  43. #include <boost/type_traits/is_nothrow_move_constructible.hpp>
  44. #include <boost/type_traits/is_const.hpp>
  45. #include <boost/type_traits/is_same.hpp>
  46. #include <boost/type_traits/is_rvalue_reference.hpp>
  47. #include <boost/type_traits/is_constructible.hpp>
  48. #include <boost/type_traits/add_lvalue_reference.hpp>
  49. #include <boost/type_traits/declval.hpp>
  50. #include <boost/core/no_exceptions_support.hpp>
  51. #include <boost/core/enable_if.hpp>
  52. #include <boost/variant/recursive_wrapper_fwd.hpp>
  53. #include <boost/variant/static_visitor.hpp>
  54. #include <boost/mpl/assert.hpp>
  55. #include <boost/mpl/begin_end.hpp>
  56. #include <boost/mpl/bool.hpp>
  57. #include <boost/mpl/deref.hpp>
  58. #include <boost/mpl/empty.hpp>
  59. #include <boost/mpl/eval_if.hpp>
  60. #include <boost/mpl/find_if.hpp>
  61. #include <boost/mpl/fold.hpp>
  62. #include <boost/mpl/front.hpp>
  63. #include <boost/mpl/identity.hpp>
  64. #include <boost/mpl/if.hpp>
  65. #include <boost/mpl/insert_range.hpp>
  66. #include <boost/mpl/int.hpp>
  67. #include <boost/mpl/is_sequence.hpp>
  68. #include <boost/mpl/iterator_range.hpp>
  69. #include <boost/mpl/iter_fold_if.hpp>
  70. #include <boost/mpl/list.hpp>
  71. #include <boost/mpl/logical.hpp>
  72. #include <boost/mpl/max_element.hpp>
  73. #include <boost/mpl/next.hpp>
  74. #include <boost/mpl/not.hpp>
  75. #include <boost/mpl/pair.hpp>
  76. #include <boost/mpl/protect.hpp>
  77. #include <boost/mpl/push_front.hpp>
  78. #include <boost/mpl/same_as.hpp>
  79. #include <boost/mpl/size_t.hpp>
  80. #include <boost/mpl/sizeof.hpp>
  81. #include <boost/mpl/transform.hpp>
  82. ///////////////////////////////////////////////////////////////////////////////
  83. // Implementation Macros:
  84. //
  85. // BOOST_VARIANT_VISITATION_UNROLLING_LIMIT
  86. // Defined in boost/variant/detail/visitation_impl.hpp.
  87. //
  88. // BOOST_VARIANT_MINIMIZE_SIZE
  89. // When #defined, implementation employs all known means to minimize the
  90. // size of variant obje cts. However, often unsuccessful due to alignment
  91. // issues, and potentially harmful to runtime speed, so not enabled by
  92. // default. (TODO: Investigate further.)
  93. #if defined(BOOST_VARIANT_MINIMIZE_SIZE)
  94. # include <climits> // for SCHAR_MAX
  95. # include <boost/mpl/eval_if.hpp>
  96. # include <boost/mpl/equal_to.hpp>
  97. # include <boost/mpl/identity.hpp>
  98. # include <boost/mpl/int.hpp>
  99. # include <boost/mpl/if.hpp>
  100. # include <boost/mpl/less.hpp>
  101. # include <boost/mpl/long.hpp>
  102. # include <boost/mpl/O1_size.hpp>
  103. #endif
  104. namespace boost {
  105. namespace detail { namespace variant {
  106. ///////////////////////////////////////////////////////////////////////////////
  107. // (detail) metafunction max_value
  108. //
  109. // Finds the maximum value of the unary metafunction F over Sequence.
  110. //
  111. template <typename Sequence, typename F>
  112. struct max_value
  113. {
  114. private: // helpers, for metafunction result (below)
  115. typedef typename mpl::transform1<Sequence, F>::type transformed_;
  116. typedef typename mpl::max_element<transformed_
  117. >::type max_it;
  118. public: // metafunction result
  119. typedef typename mpl::deref<max_it>::type
  120. type;
  121. };
  122. struct add_alignment
  123. {
  124. template <typename State, typename Item>
  125. struct apply
  126. : mpl::size_t<
  127. ::boost::integer::static_lcm<
  128. BOOST_MPL_AUX_VALUE_WKND(State)::value
  129. , ::boost::alignment_of<Item>::value
  130. >::value
  131. >
  132. {};
  133. };
  134. ///////////////////////////////////////////////////////////////////////////////
  135. // (detail) metafunction find_fallback_type
  136. //
  137. // Provides a fallback (i.e., nothrow default-constructible) type from the
  138. // specified sequence, or no_fallback_type if not found.
  139. //
  140. // This implementation is designed to prefer boost::blank over other potential
  141. // fallback types, regardless of its position in the specified sequence.
  142. //
  143. class no_fallback_type;
  144. struct find_fallback_type_pred
  145. {
  146. template <typename Iterator>
  147. struct apply
  148. {
  149. private:
  150. typedef typename mpl::deref<Iterator>::type t_;
  151. public:
  152. typedef mpl::not_< has_nothrow_constructor<t_> > type;
  153. };
  154. };
  155. template <typename Types>
  156. struct find_fallback_type
  157. {
  158. private: // helpers, for metafunction result (below)
  159. typedef typename mpl::end<Types>::type end_it;
  160. // [Find the first suitable fallback type...]
  161. typedef typename mpl::iter_fold_if<
  162. Types
  163. , mpl::int_<0>, mpl::protect< mpl::next<> >
  164. , mpl::protect< find_fallback_type_pred >
  165. >::type first_result_;
  166. typedef typename first_result_::first first_result_index;
  167. typedef typename first_result_::second first_result_it;
  168. // [...now search the rest of the sequence for boost::blank...]
  169. typedef typename mpl::iter_fold_if<
  170. mpl::iterator_range< first_result_it,end_it >
  171. , first_result_index, mpl::protect< mpl::next<> >
  172. , mpl::protect< mpl::not_same_as<boost::blank> >
  173. >::type second_result_;
  174. typedef typename second_result_::second second_result_it;
  175. public: // metafunction result
  176. // [...and return the results of the search:]
  177. typedef typename mpl::eval_if<
  178. is_same< second_result_it,end_it >
  179. , mpl::if_<
  180. is_same< first_result_it,end_it >
  181. , mpl::pair< no_fallback_type,no_fallback_type >
  182. , first_result_
  183. >
  184. , mpl::identity< second_result_ >
  185. >::type type;
  186. };
  187. #ifndef BOOST_NO_CXX11_NOEXCEPT
  188. ///////////////////////////////////////////////////////////////////////////////
  189. // (detail) metafunction is_variant_move_noexcept_constructible
  190. //
  191. // Returns true_type if all the types are nothrow move constructible.
  192. //
  193. template <class Types>
  194. struct is_variant_move_noexcept_constructible {
  195. typedef typename boost::mpl::find_if<
  196. Types, mpl::not_<boost::is_nothrow_move_constructible<boost::mpl::_1> >
  197. >::type iterator_t;
  198. typedef typename boost::mpl::end<Types>::type end_t;
  199. typedef typename boost::is_same<
  200. iterator_t, end_t
  201. >::type type;
  202. };
  203. ///////////////////////////////////////////////////////////////////////////////
  204. // (detail) metafunction is_variant_move_noexcept_assignable
  205. //
  206. // Returns true_type if all the types are nothrow move constructible.
  207. //
  208. template <class Types>
  209. struct is_variant_move_noexcept_assignable {
  210. typedef typename boost::mpl::find_if<
  211. Types, mpl::not_<boost::is_nothrow_move_assignable<boost::mpl::_1> >
  212. >::type iterator_t;
  213. typedef typename boost::mpl::end<Types>::type end_t;
  214. typedef typename boost::is_same<
  215. iterator_t, end_t
  216. >::type type;
  217. };
  218. #endif // BOOST_NO_CXX11_NOEXCEPT
  219. ///////////////////////////////////////////////////////////////////////////////
  220. // (detail) metafunction is_variant_constructible_from
  221. //
  222. // Derives from true_type if at least one variant's type is constructible from T.
  223. //
  224. template <class T1, class T2>
  225. struct is_constructible_ext:
  226. boost::mpl::or_<
  227. boost::is_constructible<
  228. T1,
  229. T2
  230. >,
  231. boost::is_constructible<
  232. T1,
  233. typename boost::add_lvalue_reference<T2>::type
  234. >
  235. >
  236. {};
  237. template <class T, class Types>
  238. struct is_variant_constructible_from:
  239. boost::mpl::not_< boost::is_same<
  240. typename boost::mpl::find_if<
  241. Types,
  242. is_constructible_ext<boost::mpl::_1, T>
  243. >::type,
  244. typename boost::mpl::end<Types>::type
  245. > >
  246. {};
  247. template <BOOST_VARIANT_ENUM_PARAMS(typename T), class Types>
  248. struct is_variant_constructible_from< boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Types >:
  249. boost::is_same<
  250. typename boost::mpl::find_if<
  251. typename boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>::recursive_enabled_types,
  252. mpl::not_< is_variant_constructible_from< boost::mpl::_1, Types> >
  253. >::type,
  254. typename boost::mpl::end< typename boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>::recursive_enabled_types >::type
  255. >
  256. {};
  257. template <BOOST_VARIANT_ENUM_PARAMS(typename T), class Types>
  258. struct is_variant_constructible_from< const boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>& , Types >:
  259. is_variant_constructible_from<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Types >
  260. {};
  261. template <BOOST_VARIANT_ENUM_PARAMS(typename T), class Types>
  262. struct is_variant_constructible_from< boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>& , Types >:
  263. is_variant_constructible_from<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Types >
  264. {};
  265. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  266. template <BOOST_VARIANT_ENUM_PARAMS(typename T), class Types>
  267. struct is_variant_constructible_from< boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>&& , Types >:
  268. is_variant_constructible_from<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Types >
  269. {};
  270. template <BOOST_VARIANT_ENUM_PARAMS(typename T), class Types>
  271. struct is_variant_constructible_from< boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const && , Types >:
  272. is_variant_constructible_from<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Types >
  273. {};
  274. #endif // #ifndef BOOST_NO_CXX11_RVALUE_REFERENCE
  275. ///////////////////////////////////////////////////////////////////////////////
  276. // (detail) metafunction make_storage
  277. //
  278. // Provides an aligned storage type capable of holding any of the types
  279. // specified in the given type-sequence.
  280. //
  281. template <typename Types, typename NeverUsesBackupFlag>
  282. struct make_storage
  283. {
  284. private: // helpers, for metafunction result (below)
  285. typedef typename mpl::eval_if<
  286. NeverUsesBackupFlag
  287. , mpl::identity< Types >
  288. , mpl::push_front<
  289. Types, backup_holder<void*>
  290. >
  291. >::type types;
  292. typedef typename max_value<
  293. types, mpl::sizeof_<mpl::_1>
  294. >::type max_size;
  295. #if !BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x0551))
  296. typedef typename mpl::fold<
  297. types
  298. , mpl::size_t<1>
  299. , add_alignment
  300. >::type max_alignment;
  301. #else // borland
  302. // temporary workaround -- use maximal alignment
  303. typedef mpl::size_t< -1 > max_alignment;
  304. #endif // borland workaround
  305. public: // metafunction result
  306. typedef ::boost::aligned_storage<
  307. BOOST_MPL_AUX_VALUE_WKND(max_size)::value
  308. , BOOST_MPL_AUX_VALUE_WKND(max_alignment)::value
  309. > type;
  310. };
  311. ///////////////////////////////////////////////////////////////////////////////
  312. // (detail) class destroyer
  313. //
  314. // Internal visitor that destroys the value it visits.
  315. //
  316. struct destroyer
  317. : public static_visitor<>
  318. {
  319. public: // visitor interfaces
  320. template <typename T>
  321. void internal_visit(T& operand, int) const BOOST_NOEXCEPT
  322. {
  323. operand.~T(); // must be noexcept
  324. #if BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x0551)) || \
  325. BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
  326. (void)operand; // suppresses warnings
  327. #endif
  328. }
  329. };
  330. ///////////////////////////////////////////////////////////////////////////////
  331. // (detail) class template known_get
  332. //
  333. // Visitor that returns a reference to content of the specified type.
  334. //
  335. // Precondition: visited variant MUST contain logical content of type T.
  336. //
  337. template <typename T>
  338. class known_get
  339. : public static_visitor<T&>
  340. {
  341. public: // visitor interface
  342. T& operator()(T& operand) const BOOST_NOEXCEPT
  343. {
  344. return operand;
  345. }
  346. template <typename U>
  347. T& operator()(U&) const
  348. {
  349. // logical error to be here: see precondition above
  350. return ::boost::detail::variant::forced_return< T& >();
  351. }
  352. };
  353. ///////////////////////////////////////////////////////////////////////////////
  354. // (detail) class copy_into
  355. //
  356. // Internal visitor that copies the value it visits into the given buffer.
  357. //
  358. class copy_into
  359. : public static_visitor<>
  360. {
  361. private: // representation
  362. void* storage_;
  363. public: // structors
  364. explicit copy_into(void* storage) BOOST_NOEXCEPT
  365. : storage_(storage)
  366. {
  367. }
  368. public: // internal visitor interface
  369. template <typename T>
  370. void internal_visit(boost::detail::variant::backup_holder<T>& operand, long) const
  371. {
  372. new(storage_) T( operand.get() );
  373. }
  374. template <typename T>
  375. void internal_visit(const boost::detail::variant::backup_holder<T>& operand, long) const
  376. {
  377. new(storage_) T( operand.get() );
  378. }
  379. template <typename T>
  380. void internal_visit(const T& operand, int) const
  381. {
  382. new(storage_) T(operand);
  383. }
  384. };
  385. ///////////////////////////////////////////////////////////////////////////////
  386. // (detail) class move_into
  387. //
  388. // Internal visitor that moves the value it visits into the given buffer.
  389. //
  390. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  391. class move_into
  392. : public static_visitor<>
  393. {
  394. private: // representation
  395. void* storage_;
  396. public: // structors
  397. explicit move_into(void* storage) BOOST_NOEXCEPT
  398. : storage_(storage)
  399. {
  400. }
  401. public: // internal visitor interface
  402. template <typename T>
  403. void internal_visit(boost::detail::variant::backup_holder<T>& operand, long) const
  404. {
  405. new(storage_) T( ::boost::detail::variant::move(operand.get()) );
  406. }
  407. template <typename T>
  408. void internal_visit(T& operand, int) const BOOST_NOEXCEPT_IF(BOOST_NOEXCEPT_EXPR(T(boost::declval<T>())))
  409. {
  410. new(storage_) T(::boost::detail::variant::move(operand));
  411. }
  412. };
  413. #endif
  414. ///////////////////////////////////////////////////////////////////////////////
  415. // (detail) class assign_storage
  416. //
  417. // Internal visitor that assigns the given storage (which must be a
  418. // constructed value of the same type) to the value it visits.
  419. //
  420. struct assign_storage
  421. : public static_visitor<>
  422. {
  423. private: // representation
  424. const void* rhs_storage_;
  425. public: // structors
  426. explicit assign_storage(const void* rhs_storage) BOOST_NOEXCEPT
  427. : rhs_storage_(rhs_storage)
  428. {
  429. }
  430. public: // internal visitor interfaces
  431. template <typename T>
  432. void internal_visit(backup_holder<T>& lhs_content, long) const
  433. {
  434. lhs_content.get()
  435. = static_cast< const backup_holder<T>* >(rhs_storage_)->get();
  436. }
  437. template <typename T>
  438. void internal_visit(const backup_holder<T>& lhs_content, long) const
  439. {
  440. lhs_content.get()
  441. = static_cast< const backup_holder<T>* >(rhs_storage_)->get();
  442. }
  443. template <typename T>
  444. void internal_visit(T& lhs_content, int) const
  445. {
  446. // NOTE TO USER :
  447. // Compile error here indicates one of variant's bounded types does
  448. // not meet the requirements of the Assignable concept. Thus,
  449. // variant is not Assignable.
  450. //
  451. // Hint: Are any of the bounded types const-qualified or references?
  452. //
  453. lhs_content = *static_cast< const T* >(rhs_storage_);
  454. }
  455. };
  456. ///////////////////////////////////////////////////////////////////////////////
  457. // (detail) class move_storage
  458. //
  459. // Internal visitor that moves the given storage (which must be a
  460. // constructed value of the same type) to the value it visits.
  461. //
  462. struct move_storage
  463. : public static_visitor<>
  464. {
  465. private: // representation
  466. void* rhs_storage_;
  467. public: // structors
  468. explicit move_storage(void* rhs_storage) BOOST_NOEXCEPT
  469. : rhs_storage_(rhs_storage)
  470. {
  471. }
  472. public: // internal visitor interfaces
  473. template <typename T>
  474. void internal_visit(backup_holder<T>& lhs_content, long) const
  475. {
  476. lhs_content.get()
  477. = ::boost::detail::variant::move(static_cast<backup_holder<T>* >(rhs_storage_)->get());
  478. }
  479. template <typename T>
  480. void internal_visit(const backup_holder<T>& lhs_content, long) const
  481. {
  482. lhs_content.get()
  483. = ::boost::detail::variant::move(static_cast<backup_holder<T>* >(rhs_storage_)->get());
  484. }
  485. template <typename T>
  486. void internal_visit(T& lhs_content, int) const
  487. {
  488. // NOTE TO USER :
  489. // Compile error here indicates one of variant's bounded types does
  490. // not meet the requirements of the Assignable concept. Thus,
  491. // variant is not Assignable.
  492. //
  493. // Hint: Are any of the bounded types const-qualified or references?
  494. //
  495. lhs_content = ::boost::detail::variant::move(*static_cast<T* >(rhs_storage_));
  496. }
  497. };
  498. ///////////////////////////////////////////////////////////////////////////////
  499. // (detail) class direct_assigner
  500. //
  501. // Generic static visitor that: if and only if the visited value is of the
  502. // specified type, assigns the given value to the visited value and returns
  503. // true; else returns false.
  504. //
  505. template <typename T>
  506. class direct_assigner
  507. : public static_visitor<bool>
  508. {
  509. private: // representation
  510. const T& rhs_;
  511. public: // structors
  512. explicit direct_assigner(const T& rhs) BOOST_NOEXCEPT
  513. : rhs_(rhs)
  514. {
  515. }
  516. public: // visitor interface
  517. bool operator()(T& lhs)
  518. {
  519. lhs = rhs_;
  520. return true;
  521. }
  522. template <typename U>
  523. bool operator()(U&) BOOST_NOEXCEPT
  524. {
  525. return false;
  526. }
  527. #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
  528. private:
  529. // silence MSVC warning C4512: assignment operator could not be generated
  530. direct_assigner& operator= (direct_assigner const&);
  531. #endif
  532. };
  533. ///////////////////////////////////////////////////////////////////////////////
  534. // (detail) class direct_mover
  535. //
  536. // Generic static visitor that: if and only if the visited value is of the
  537. // specified type, move assigns the given value to the visited value and returns
  538. // true; else returns false.
  539. //
  540. template <typename T>
  541. class direct_mover
  542. : public static_visitor<bool>
  543. {
  544. private: // representation
  545. T& rhs_;
  546. public: // structors
  547. explicit direct_mover(T& rhs) BOOST_NOEXCEPT
  548. : rhs_(rhs)
  549. {
  550. }
  551. public: // visitor interface
  552. bool operator()(T& lhs)
  553. {
  554. lhs = ::boost::detail::variant::move(rhs_);
  555. return true;
  556. }
  557. template <typename U>
  558. bool operator()(U&) BOOST_NOEXCEPT
  559. {
  560. return false;
  561. }
  562. #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
  563. private:
  564. // silence MSVC warning C4512: assignment operator could not be generated
  565. direct_mover& operator= (direct_mover const&);
  566. #endif
  567. };
  568. ///////////////////////////////////////////////////////////////////////////////
  569. // (detail) class backup_assigner
  570. //
  571. // Internal visitor that "assigns" the given value to the visited value,
  572. // using backup to recover if the destroy-copy sequence fails.
  573. //
  574. // NOTE: This needs to be a friend of variant, as it needs access to
  575. // indicate_which, indicate_backup_which, etc.
  576. //
  577. template <typename Variant>
  578. class backup_assigner
  579. : public static_visitor<>
  580. {
  581. private: // representation
  582. Variant& lhs_;
  583. int rhs_which_;
  584. const void* rhs_content_;
  585. void (*copy_rhs_content_)(void*, const void*);
  586. public: // structors
  587. template<class RhsT>
  588. backup_assigner(Variant& lhs, int rhs_which, const RhsT& rhs_content)
  589. : lhs_(lhs)
  590. , rhs_which_(rhs_which)
  591. , rhs_content_(&rhs_content)
  592. , copy_rhs_content_(&construct_impl<RhsT>)
  593. {
  594. }
  595. private: // helpers, for visitor interface (below)
  596. template<class RhsT>
  597. static void construct_impl(void* addr, const void* obj)
  598. {
  599. new(addr) RhsT(*static_cast<const RhsT*>(obj));
  600. }
  601. template <typename LhsT>
  602. void backup_assign_impl(
  603. backup_holder<LhsT>& lhs_content
  604. , mpl::false_ // is_nothrow_move_constructible
  605. , long
  606. )
  607. {
  608. // Move lhs content to backup...
  609. backup_holder<LhsT> backup_lhs_content(0);
  610. backup_lhs_content.swap(lhs_content); // nothrow
  611. // ...destroy lhs content...
  612. lhs_content.~backup_holder<LhsT>(); // nothrow
  613. BOOST_TRY
  614. {
  615. // ...and attempt to copy rhs content into lhs storage:
  616. copy_rhs_content_(lhs_.storage_.address(), rhs_content_);
  617. }
  618. BOOST_CATCH (...)
  619. {
  620. // In case of failure, copy backup pointer to lhs storage...
  621. new(lhs_.storage_.address())
  622. backup_holder<LhsT>( 0 ); // nothrow
  623. static_cast<backup_holder<LhsT>* >(lhs_.storage_.address())
  624. ->swap(backup_lhs_content); // nothrow
  625. // ...and rethrow:
  626. BOOST_RETHROW;
  627. }
  628. BOOST_CATCH_END
  629. // In case of success, indicate new content type:
  630. lhs_.indicate_which(rhs_which_); // nothrow
  631. }
  632. template <typename LhsT>
  633. void backup_assign_impl(
  634. LhsT& lhs_content
  635. , mpl::true_ // is_nothrow_move_constructible
  636. , int
  637. )
  638. {
  639. // Move lhs content to backup...
  640. LhsT backup_lhs_content(
  641. ::boost::detail::variant::move(lhs_content)
  642. ); // nothrow
  643. // ...destroy lhs content...
  644. lhs_content.~LhsT(); // nothrow
  645. BOOST_TRY
  646. {
  647. // ...and attempt to copy rhs content into lhs storage:
  648. copy_rhs_content_(lhs_.storage_.address(), rhs_content_);
  649. }
  650. BOOST_CATCH (...)
  651. {
  652. // In case of failure, restore backup content to lhs storage...
  653. new(lhs_.storage_.address())
  654. LhsT(
  655. ::boost::detail::variant::move(backup_lhs_content)
  656. ); // nothrow
  657. // ...and rethrow:
  658. BOOST_RETHROW;
  659. }
  660. BOOST_CATCH_END
  661. // In case of success, indicate new content type:
  662. lhs_.indicate_which(rhs_which_); // nothrow
  663. }
  664. template <typename LhsT>
  665. void backup_assign_impl(
  666. LhsT& lhs_content
  667. , mpl::false_ // is_nothrow_move_constructible
  668. , int
  669. )
  670. {
  671. // Backup lhs content...
  672. LhsT* backup_lhs_ptr = new LhsT(lhs_content);
  673. // ...destroy lhs content...
  674. lhs_content.~LhsT(); // nothrow
  675. BOOST_TRY
  676. {
  677. // ...and attempt to copy rhs content into lhs storage:
  678. copy_rhs_content_(lhs_.storage_.address(), rhs_content_);
  679. }
  680. BOOST_CATCH (...)
  681. {
  682. // In case of failure, copy backup pointer to lhs storage...
  683. new(lhs_.storage_.address())
  684. backup_holder<LhsT>( backup_lhs_ptr ); // nothrow
  685. // ...indicate now using backup...
  686. lhs_.indicate_backup_which( lhs_.which() ); // nothrow
  687. // ...and rethrow:
  688. BOOST_RETHROW;
  689. }
  690. BOOST_CATCH_END
  691. // In case of success, indicate new content type...
  692. lhs_.indicate_which(rhs_which_); // nothrow
  693. // ...and delete backup:
  694. delete backup_lhs_ptr; // nothrow
  695. }
  696. public: // visitor interface
  697. template <typename LhsT>
  698. void internal_visit(LhsT& lhs_content, int)
  699. {
  700. typedef typename is_nothrow_move_constructible<LhsT>::type
  701. nothrow_move;
  702. backup_assign_impl( lhs_content, nothrow_move(), 1L);
  703. }
  704. #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
  705. private:
  706. // silence MSVC warning C4512: assignment operator could not be generated
  707. backup_assigner& operator= (backup_assigner const&);
  708. #endif
  709. };
  710. ///////////////////////////////////////////////////////////////////////////////
  711. // (detail) class swap_with
  712. //
  713. // Visitor that swaps visited value with content of given variant.
  714. //
  715. // Precondition: Given variant MUST have same logical type as visited value.
  716. //
  717. template <typename Variant>
  718. struct swap_with
  719. : public static_visitor<>
  720. {
  721. private: // representation
  722. Variant& toswap_;
  723. public: // structors
  724. explicit swap_with(Variant& toswap) BOOST_NOEXCEPT
  725. : toswap_(toswap)
  726. {
  727. }
  728. public: // internal visitor interfaces
  729. template <typename T>
  730. void operator()(T& operand) const
  731. {
  732. // Since the precondition ensures types are same, get T...
  733. known_get<T> getter;
  734. T& other = toswap_.apply_visitor(getter);
  735. // ...and swap:
  736. ::boost::detail::variant::move_swap( operand, other );
  737. }
  738. private:
  739. swap_with& operator=(const swap_with&);
  740. };
  741. ///////////////////////////////////////////////////////////////////////////////
  742. // (detail) class reflect
  743. //
  744. // Generic static visitor that performs a typeid on the value it visits.
  745. //
  746. class reflect
  747. : public static_visitor<const boost::typeindex::type_info&>
  748. {
  749. public: // visitor interfaces
  750. template <typename T>
  751. const boost::typeindex::type_info& operator()(const T&) const BOOST_NOEXCEPT
  752. {
  753. return boost::typeindex::type_id<T>().type_info();
  754. }
  755. };
  756. ///////////////////////////////////////////////////////////////////////////////
  757. // (detail) class comparer
  758. //
  759. // Generic static visitor that compares the content of the given lhs variant
  760. // with the visited rhs content using Comp.
  761. //
  762. // Precondition: lhs.which() == rhs.which()
  763. //
  764. template <typename Variant, typename Comp>
  765. class comparer
  766. : public static_visitor<bool>
  767. {
  768. private: // representation
  769. const Variant& lhs_;
  770. public: // structors
  771. explicit comparer(const Variant& lhs) BOOST_NOEXCEPT
  772. : lhs_(lhs)
  773. {
  774. }
  775. public: // visitor interfaces
  776. template <typename T>
  777. bool operator()(T& rhs_content) const
  778. {
  779. // Since the precondition ensures lhs and rhs types are same, get T...
  780. known_get<T> getter;
  781. const T& lhs_content = lhs_.apply_visitor(getter);
  782. // ...and compare lhs and rhs contents:
  783. return Comp()(lhs_content, rhs_content);
  784. }
  785. private:
  786. comparer& operator=(const comparer&);
  787. };
  788. ///////////////////////////////////////////////////////////////////////////////
  789. // (detail) class equal_comp
  790. //
  791. // Generic function object compares lhs with rhs using operator==.
  792. //
  793. struct equal_comp
  794. {
  795. template <typename T>
  796. bool operator()(const T& lhs, const T& rhs) const
  797. {
  798. return lhs == rhs;
  799. }
  800. };
  801. ///////////////////////////////////////////////////////////////////////////////
  802. // (detail) class less_comp
  803. //
  804. // Generic function object compares lhs with rhs using operator<.
  805. //
  806. struct less_comp
  807. {
  808. template <typename T>
  809. bool operator()(const T& lhs, const T& rhs) const
  810. {
  811. return lhs < rhs;
  812. }
  813. };
  814. ///////////////////////////////////////////////////////////////////////////////
  815. // (detail) class template invoke_visitor
  816. //
  817. // Internal visitor that invokes the given visitor using:
  818. // * for wrappers (e.g., recursive_wrapper), the wrapper's held value.
  819. // * for all other values, the value itself.
  820. //
  821. template <typename Visitor, bool MoveSemantics>
  822. class invoke_visitor
  823. {
  824. private: // representation
  825. Visitor& visitor_;
  826. public: // visitor typedefs
  827. typedef typename Visitor::result_type
  828. result_type;
  829. public: // structors
  830. explicit invoke_visitor(Visitor& visitor) BOOST_NOEXCEPT
  831. : visitor_(visitor)
  832. {
  833. }
  834. public: // internal visitor interfaces
  835. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  836. //using workaround with is_same<T, T> to prenvent compilation error, because we need to use T in enable_if to make SFINAE work
  837. template <typename T>
  838. typename enable_if_c<MoveSemantics && is_same<T, T>::value, result_type>::type internal_visit(T&& operand, int)
  839. {
  840. return visitor_(::boost::move(operand));
  841. }
  842. //using workaround with is_same<T, T> to prenvent compilation error, because we need to use T in enable_if to make SFINAE work
  843. template <typename T>
  844. typename disable_if_c<MoveSemantics && is_same<T, T>::value, result_type>::type internal_visit(T&& operand, int)
  845. {
  846. return visitor_(operand);
  847. }
  848. #else
  849. template <typename T>
  850. result_type internal_visit(T& operand, int)
  851. {
  852. return visitor_(operand);
  853. }
  854. # if BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x0564))
  855. template <typename T>
  856. result_type internal_visit(const T& operand, int)
  857. {
  858. return visitor_(operand);
  859. }
  860. # endif //BORLAND
  861. #endif //RVALUE REFERENCES
  862. public: // internal visitor interfaces, cont.
  863. template <typename T>
  864. result_type internal_visit(boost::recursive_wrapper<T>& operand, long)
  865. {
  866. return internal_visit( operand.get(), 1L );
  867. }
  868. template <typename T>
  869. result_type internal_visit(const boost::recursive_wrapper<T>& operand, long)
  870. {
  871. return internal_visit( operand.get(), 1L );
  872. }
  873. template <typename T>
  874. result_type internal_visit(boost::detail::reference_content<T>& operand, long)
  875. {
  876. return internal_visit( operand.get(), 1L );
  877. }
  878. template <typename T>
  879. result_type internal_visit(const boost::detail::reference_content<T>& operand, long)
  880. {
  881. return internal_visit( operand.get(), 1L );
  882. }
  883. template <typename T>
  884. result_type internal_visit(boost::detail::variant::backup_holder<T>& operand, long)
  885. {
  886. return internal_visit( operand.get(), 1L );
  887. }
  888. template <typename T>
  889. result_type internal_visit(const boost::detail::variant::backup_holder<T>& operand, long)
  890. {
  891. return internal_visit( operand.get(), 1L );
  892. }
  893. #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
  894. private:
  895. // silence MSVC warning C4512: assignment operator could not be generated
  896. invoke_visitor& operator= (invoke_visitor const&);
  897. #endif
  898. };
  899. }} // namespace detail::variant
  900. ///////////////////////////////////////////////////////////////////////////////
  901. // class template variant (concept inspired by Andrei Alexandrescu)
  902. //
  903. // See docs and boost/variant/variant_fwd.hpp for more information.
  904. //
  905. template <
  906. typename T0_
  907. , BOOST_VARIANT_ENUM_SHIFTED_PARAMS(typename T)
  908. >
  909. class variant
  910. {
  911. private: // helpers, for typedefs (below)
  912. typedef variant wknd_self_t;
  913. struct is_recursive_
  914. : detail::variant::is_recursive_flag<T0_>
  915. {
  916. };
  917. typedef typename mpl::eval_if<
  918. is_recursive_
  919. , T0_
  920. , mpl::identity< T0_ >
  921. >::type unwrapped_T0_;
  922. struct is_sequence_based_
  923. : detail::variant::is_over_sequence<unwrapped_T0_>
  924. {
  925. };
  926. #if !defined(BOOST_VARIANT_NO_TYPE_SEQUENCE_SUPPORT)
  927. private: // helpers, for typedefs (below)
  928. typedef typename mpl::eval_if<
  929. is_sequence_based_
  930. , unwrapped_T0_ // over_sequence<...>::type
  931. , detail::variant::make_variant_list<
  932. unwrapped_T0_
  933. , BOOST_VARIANT_ENUM_SHIFTED_PARAMS(T)
  934. >
  935. >::type specified_types;
  936. BOOST_STATIC_ASSERT((
  937. ::boost::mpl::not_< mpl::empty<specified_types> >::value
  938. ));
  939. public: // public typedefs
  940. typedef typename mpl::eval_if<
  941. is_recursive_
  942. , mpl::transform<
  943. specified_types
  944. , mpl::protect<
  945. detail::variant::quoted_enable_recursive<wknd_self_t>
  946. >
  947. >
  948. , mpl::identity< specified_types >
  949. >::type recursive_enabled_types; // used by is_variant_constructible_from<> trait
  950. typedef typename mpl::transform<
  951. recursive_enabled_types
  952. , unwrap_recursive<mpl::_1>
  953. >::type types;
  954. private: // internal typedefs
  955. typedef typename mpl::transform<
  956. recursive_enabled_types
  957. , mpl::protect< detail::make_reference_content<> >
  958. >::type internal_types;
  959. typedef typename mpl::front<
  960. internal_types
  961. >::type internal_T0;
  962. #else // defined(BOOST_VARIANT_NO_TYPE_SEQUENCE_SUPPORT)
  963. private: // helpers, for typedefs (below)
  964. typedef unwrapped_T0_ T0;
  965. #define BOOST_VARIANT_AUX_ENABLE_RECURSIVE_TYPEDEFS(z,N,_) \
  966. typedef typename mpl::eval_if< \
  967. is_recursive_ \
  968. , detail::variant::enable_recursive< \
  969. BOOST_PP_CAT(T,N) \
  970. , wknd_self_t \
  971. > \
  972. , mpl::identity< BOOST_PP_CAT(T,N) > \
  973. >::type BOOST_PP_CAT(recursive_enabled_T,N); \
  974. /**/
  975. BOOST_PP_REPEAT(
  976. BOOST_VARIANT_LIMIT_TYPES
  977. , BOOST_VARIANT_AUX_ENABLE_RECURSIVE_TYPEDEFS
  978. , _
  979. )
  980. #undef BOOST_VARIANT_AUX_ENABLE_RECURSIVE_TYPEDEFS
  981. #define BOOST_VARIANT_AUX_UNWRAP_RECURSIVE_TYPEDEFS(z,N,_) \
  982. typedef typename unwrap_recursive< \
  983. BOOST_PP_CAT(recursive_enabled_T,N) \
  984. >::type BOOST_PP_CAT(public_T,N); \
  985. /**/
  986. BOOST_PP_REPEAT(
  987. BOOST_VARIANT_LIMIT_TYPES
  988. , BOOST_VARIANT_AUX_UNWRAP_RECURSIVE_TYPEDEFS
  989. , _
  990. )
  991. #undef BOOST_VARIANT_AUX_UNWRAP_RECURSIVE_TYPEDEFS
  992. public: // public typedefs
  993. typedef typename detail::variant::make_variant_list<
  994. BOOST_VARIANT_ENUM_PARAMS(public_T)
  995. >::type types;
  996. private: // helpers, for internal typedefs (below)
  997. #define BOOST_VARIANT_AUX_MAKE_REFERENCE_CONTENT_TYPEDEFS(z,N,_) \
  998. typedef detail::make_reference_content< \
  999. BOOST_PP_CAT(recursive_enabled_T,N) \
  1000. >::type BOOST_PP_CAT(internal_T,N); \
  1001. /**/
  1002. BOOST_PP_REPEAT(
  1003. BOOST_VARIANT_LIMIT_TYPES
  1004. , BOOST_VARIANT_AUX_MAKE_REFERENCE_CONTENT_TYPEDEFS
  1005. , _
  1006. )
  1007. #undef BOOST_VARIANT_AUX_MAKE_REFERENCE_CONTENT_TYPEDEFS
  1008. private: // internal typedefs
  1009. typedef typename detail::variant::make_variant_list<
  1010. BOOST_VARIANT_ENUM_PARAMS(internal_T)
  1011. >::type internal_types;
  1012. private: // static precondition assertions
  1013. // NOTE TO USER :
  1014. // variant< type-sequence > syntax is not supported on this compiler!
  1015. //
  1016. BOOST_MPL_ASSERT_NOT(( is_sequence_based_ ));
  1017. #endif // BOOST_VARIANT_NO_TYPE_SEQUENCE_SUPPORT workaround
  1018. private: // helpers, for representation (below)
  1019. typedef typename detail::variant::find_fallback_type<
  1020. internal_types
  1021. >::type fallback_type_result_;
  1022. typedef typename fallback_type_result_::first
  1023. fallback_type_index_;
  1024. typedef typename fallback_type_result_::second
  1025. fallback_type_;
  1026. struct has_fallback_type_
  1027. : mpl::not_<
  1028. is_same< fallback_type_, detail::variant::no_fallback_type >
  1029. >
  1030. {
  1031. };
  1032. typedef has_fallback_type_
  1033. never_uses_backup_flag;
  1034. typedef typename detail::variant::make_storage<
  1035. internal_types, never_uses_backup_flag
  1036. >::type storage_t;
  1037. #ifndef BOOST_NO_CXX11_NOEXCEPT
  1038. typedef typename detail::variant::is_variant_move_noexcept_constructible<
  1039. internal_types
  1040. > variant_move_noexcept_constructible;
  1041. typedef typename detail::variant::is_variant_move_noexcept_assignable<
  1042. internal_types
  1043. > variant_move_noexcept_assignable;
  1044. #endif
  1045. private: // helpers, for representation (below)
  1046. // which_ on:
  1047. // * [0, size<internal_types>) indicates stack content
  1048. // * [-size<internal_types>, 0) indicates pointer to heap backup
  1049. // if which_ >= 0:
  1050. // * then which() -> which_
  1051. // * else which() -> -(which_ + 1)
  1052. #if !defined(BOOST_VARIANT_MINIMIZE_SIZE)
  1053. typedef int which_t;
  1054. #else // defined(BOOST_VARIANT_MINIMIZE_SIZE)
  1055. // [if O1_size available, then attempt which_t size optimization...]
  1056. // [select signed char if fewer than SCHAR_MAX types, else signed int:]
  1057. typedef typename mpl::eval_if<
  1058. mpl::equal_to< mpl::O1_size<internal_types>, mpl::long_<-1> >
  1059. , mpl::identity< int >
  1060. , mpl::if_<
  1061. mpl::less< mpl::O1_size<internal_types>, mpl::int_<SCHAR_MAX> >
  1062. , signed char
  1063. , int
  1064. >
  1065. >::type which_t;
  1066. #endif // BOOST_VARIANT_MINIMIZE_SIZE switch
  1067. // representation -- private when possible
  1068. #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  1069. private:
  1070. #else
  1071. public:
  1072. #endif
  1073. which_t which_;
  1074. storage_t storage_;
  1075. void indicate_which(int which_arg) BOOST_NOEXCEPT
  1076. {
  1077. which_ = static_cast<which_t>( which_arg );
  1078. }
  1079. void indicate_backup_which(int which_arg) BOOST_NOEXCEPT
  1080. {
  1081. which_ = static_cast<which_t>( -(which_arg + 1) );
  1082. }
  1083. private: // helpers, for queries (below)
  1084. bool using_backup() const BOOST_NOEXCEPT
  1085. {
  1086. return which_ < 0;
  1087. }
  1088. public: // queries
  1089. int which() const BOOST_NOEXCEPT
  1090. {
  1091. // If using heap backup...
  1092. if (using_backup())
  1093. // ...then return adjusted which_:
  1094. return -(which_ + 1);
  1095. // Otherwise, return which_ directly:
  1096. return which_;
  1097. }
  1098. private: // helpers, for structors (below)
  1099. struct initializer
  1100. : BOOST_VARIANT_AUX_INITIALIZER_T(
  1101. recursive_enabled_types, recursive_enabled_T
  1102. )
  1103. {
  1104. };
  1105. void destroy_content() BOOST_NOEXCEPT
  1106. {
  1107. detail::variant::destroyer visitor;
  1108. this->internal_apply_visitor(visitor);
  1109. }
  1110. public: // structors
  1111. ~variant() BOOST_NOEXCEPT
  1112. {
  1113. destroy_content();
  1114. }
  1115. variant()
  1116. #if !(defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x5130))
  1117. BOOST_NOEXCEPT_IF(boost::has_nothrow_constructor<internal_T0>::value)
  1118. #endif
  1119. {
  1120. #ifdef _MSC_VER
  1121. #pragma warning( push )
  1122. // behavior change: an object of POD type constructed with an initializer of the form () will be default-initialized
  1123. #pragma warning( disable : 4345 )
  1124. #endif
  1125. // NOTE TO USER :
  1126. // Compile error from here indicates that the first bound
  1127. // type is not default-constructible, and so variant cannot
  1128. // support its own default-construction.
  1129. //
  1130. new( storage_.address() ) internal_T0();
  1131. indicate_which(0); // zero is the index of the first bounded type
  1132. #ifdef _MSC_VER
  1133. #pragma warning( pop )
  1134. #endif
  1135. }
  1136. private: // helpers, for structors, cont. (below)
  1137. class convert_copy_into
  1138. : public static_visitor<int>
  1139. {
  1140. private: // representation
  1141. void* storage_;
  1142. public: // structors
  1143. explicit convert_copy_into(void* storage) BOOST_NOEXCEPT
  1144. : storage_(storage)
  1145. {
  1146. }
  1147. public: // internal visitor interfaces (below)
  1148. template <typename T>
  1149. int internal_visit(T& operand, int) const
  1150. {
  1151. // NOTE TO USER :
  1152. // Compile error here indicates one of the source variant's types
  1153. // cannot be unambiguously converted to the destination variant's
  1154. // types (or that no conversion exists).
  1155. //
  1156. return initializer::initialize(storage_, operand);
  1157. }
  1158. # if BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x0564))
  1159. template <typename T>
  1160. result_type internal_visit(const T& operand, int) const
  1161. {
  1162. return initializer::initialize(storage_, operand);
  1163. }
  1164. # endif
  1165. template <typename T>
  1166. int internal_visit(boost::detail::reference_content<T>& operand, long) const
  1167. {
  1168. return internal_visit( operand.get(), 1L );
  1169. }
  1170. template <typename T>
  1171. int internal_visit(const boost::detail::reference_content<T>& operand, long) const
  1172. {
  1173. return internal_visit( operand.get(), 1L );
  1174. }
  1175. template <typename T>
  1176. int internal_visit(boost::detail::variant::backup_holder<T>& operand, long) const
  1177. {
  1178. return internal_visit( operand.get(), 1L );
  1179. }
  1180. template <typename T>
  1181. int internal_visit(const boost::detail::variant::backup_holder<T>& operand, long) const
  1182. {
  1183. return internal_visit( operand.get(), 1L );
  1184. }
  1185. template <typename T>
  1186. int internal_visit(boost::recursive_wrapper<T>& operand, long) const
  1187. {
  1188. return internal_visit( operand.get(), 1L );
  1189. }
  1190. template <typename T>
  1191. int internal_visit(const boost::recursive_wrapper<T>& operand, long) const
  1192. {
  1193. return internal_visit( operand.get(), 1L );
  1194. }
  1195. };
  1196. friend class convert_copy_into;
  1197. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  1198. class convert_move_into
  1199. : public static_visitor<int>
  1200. {
  1201. private: // representation
  1202. void* storage_;
  1203. public: // structors
  1204. explicit convert_move_into(void* storage) BOOST_NOEXCEPT
  1205. : storage_(storage)
  1206. {
  1207. }
  1208. public: // internal visitor interfaces (below)
  1209. template <typename T>
  1210. int internal_visit(T& operand, int) const
  1211. {
  1212. // NOTE TO USER :
  1213. // Compile error here indicates one of the source variant's types
  1214. // cannot be unambiguously converted to the destination variant's
  1215. // types (or that no conversion exists).
  1216. //
  1217. return initializer::initialize(storage_, detail::variant::move(operand) );
  1218. }
  1219. template <typename T>
  1220. int internal_visit(boost::detail::reference_content<T>& operand, long) const
  1221. {
  1222. return internal_visit( operand.get(), 1L );
  1223. }
  1224. template <typename T>
  1225. int internal_visit(const boost::detail::reference_content<T>& operand, long) const
  1226. {
  1227. return internal_visit( operand.get(), 1L );
  1228. }
  1229. template <typename T>
  1230. int internal_visit(boost::detail::variant::backup_holder<T>& operand, long) const
  1231. {
  1232. return internal_visit( operand.get(), 1L );
  1233. }
  1234. template <typename T>
  1235. int internal_visit(const boost::detail::variant::backup_holder<T>& operand, long) const
  1236. {
  1237. return internal_visit( operand.get(), 1L );
  1238. }
  1239. template <typename T>
  1240. int internal_visit(boost::recursive_wrapper<T>& operand, long) const
  1241. {
  1242. return internal_visit( operand.get(), 1L );
  1243. }
  1244. template <typename T>
  1245. int internal_visit(const boost::recursive_wrapper<T>& operand, long) const
  1246. {
  1247. return internal_visit( operand.get(), 1L );
  1248. }
  1249. };
  1250. friend class convert_move_into;
  1251. #endif
  1252. private: // helpers, for structors, below
  1253. template <typename T>
  1254. void convert_construct(
  1255. T& operand
  1256. , int
  1257. , mpl::false_ = mpl::false_() // is_foreign_variant
  1258. )
  1259. {
  1260. // NOTE TO USER :
  1261. // Compile error here indicates that the given type is not
  1262. // unambiguously convertible to one of the variant's types
  1263. // (or that no conversion exists).
  1264. //
  1265. indicate_which(
  1266. initializer::initialize(
  1267. storage_.address()
  1268. , operand
  1269. )
  1270. );
  1271. }
  1272. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  1273. template <typename T>
  1274. typename boost::enable_if<boost::is_rvalue_reference<T&&> >::type convert_construct(
  1275. T&& operand
  1276. , int
  1277. , mpl::false_ = mpl::false_() // is_foreign_variant
  1278. )
  1279. {
  1280. // NOTE TO USER :
  1281. // Compile error here indicates that the given type is not
  1282. // unambiguously convertible to one of the variant's types
  1283. // (or that no conversion exists).
  1284. //
  1285. indicate_which(
  1286. initializer::initialize(
  1287. storage_.address()
  1288. , detail::variant::move(operand)
  1289. )
  1290. );
  1291. }
  1292. #endif
  1293. template <typename Variant>
  1294. void convert_construct(
  1295. Variant& operand
  1296. , long
  1297. , mpl::true_// is_foreign_variant
  1298. )
  1299. {
  1300. convert_copy_into visitor(storage_.address());
  1301. indicate_which(
  1302. operand.internal_apply_visitor(visitor)
  1303. );
  1304. }
  1305. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  1306. template <typename Variant>
  1307. typename boost::enable_if<boost::is_rvalue_reference<Variant&&> >::type convert_construct(
  1308. Variant&& operand
  1309. , long
  1310. , mpl::true_// is_foreign_variant
  1311. )
  1312. {
  1313. convert_move_into visitor(storage_.address());
  1314. indicate_which(
  1315. operand.internal_apply_visitor(visitor)
  1316. );
  1317. }
  1318. #endif
  1319. template <typename Variant>
  1320. void convert_construct_variant(Variant& operand)
  1321. {
  1322. // [Determine if the given variant is itself a bounded type, or if its
  1323. // content needs to be converted (i.e., it is a 'foreign' variant):]
  1324. //
  1325. typedef typename mpl::find_if<
  1326. types
  1327. , is_same<
  1328. add_const<mpl::_1>
  1329. , const Variant
  1330. >
  1331. >::type found_it;
  1332. typedef typename mpl::end<types>::type not_found;
  1333. typedef typename is_same<
  1334. found_it, not_found
  1335. >::type is_foreign_variant;
  1336. // Convert construct from operand:
  1337. convert_construct(
  1338. operand, 1L
  1339. , is_foreign_variant()
  1340. );
  1341. }
  1342. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  1343. template <typename Variant>
  1344. typename boost::enable_if<boost::is_rvalue_reference<Variant&&> >::type convert_construct_variant(Variant&& operand)
  1345. {
  1346. // [Determine if the given variant is itself a bounded type, or if its
  1347. // content needs to be converted (i.e., it is a 'foreign' variant):]
  1348. //
  1349. typedef typename mpl::find_if<
  1350. types
  1351. , is_same<
  1352. add_const<mpl::_1>
  1353. , const Variant
  1354. >
  1355. >::type found_it;
  1356. typedef typename mpl::end<types>::type not_found;
  1357. typedef typename is_same<
  1358. found_it, not_found
  1359. >::type is_foreign_variant;
  1360. // Convert move construct from operand:
  1361. convert_construct(
  1362. detail::variant::move(operand), 1L
  1363. , is_foreign_variant()
  1364. );
  1365. }
  1366. #endif
  1367. template <BOOST_VARIANT_ENUM_PARAMS(typename U)>
  1368. typename boost::enable_if<mpl::or_<
  1369. boost::is_same<boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>, variant>,
  1370. boost::detail::variant::is_variant_constructible_from<boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>&, internal_types>
  1371. > >::type convert_construct(
  1372. boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>& operand
  1373. , long
  1374. )
  1375. {
  1376. convert_construct_variant(operand);
  1377. }
  1378. template <BOOST_VARIANT_ENUM_PARAMS(typename U)>
  1379. typename boost::enable_if<mpl::or_<
  1380. boost::is_same<boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>, variant>,
  1381. boost::detail::variant::is_variant_constructible_from<const boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>&, internal_types>
  1382. > >::type convert_construct(
  1383. const boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>& operand
  1384. , long
  1385. )
  1386. {
  1387. convert_construct_variant(operand);
  1388. }
  1389. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  1390. template <BOOST_VARIANT_ENUM_PARAMS(typename U)>
  1391. typename boost::enable_if<mpl::or_<
  1392. boost::is_same<boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>, variant>,
  1393. boost::detail::variant::is_variant_constructible_from<boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>&&, internal_types>
  1394. > >::type convert_construct(
  1395. boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>&& operand
  1396. , long
  1397. )
  1398. {
  1399. convert_construct_variant( detail::variant::move(operand) );
  1400. }
  1401. #endif
  1402. public: // structors, cont.
  1403. template <typename T>
  1404. variant(const T& operand,
  1405. typename boost::enable_if<mpl::or_<
  1406. mpl::and_<
  1407. mpl::not_< boost::is_same<T, variant> >,
  1408. boost::detail::variant::is_variant_constructible_from<const T&, internal_types>
  1409. >,
  1410. boost::is_same<T, boost::recursive_variant_> >,
  1411. bool >::type = true)
  1412. {
  1413. convert_construct(operand, 1L);
  1414. }
  1415. template <typename T>
  1416. variant(
  1417. T& operand
  1418. , typename boost::enable_if<mpl::or_<
  1419. mpl::and_<
  1420. mpl::not_< is_const<T> >,
  1421. mpl::not_< boost::is_same<T, variant> >,
  1422. boost::detail::variant::is_variant_constructible_from<T&, internal_types>
  1423. >,
  1424. boost::is_same<T, boost::recursive_variant_> >,
  1425. bool >::type = true
  1426. )
  1427. {
  1428. convert_construct(operand, 1L);
  1429. }
  1430. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  1431. template <class T>
  1432. variant(T&& operand,
  1433. typename boost::enable_if<mpl::or_<
  1434. mpl::and_<
  1435. boost::is_rvalue_reference<T&&>,
  1436. mpl::not_< boost::is_const<T> >,
  1437. mpl::not_< boost::is_same<T, variant> >,
  1438. boost::detail::variant::is_variant_constructible_from<T&&, internal_types>
  1439. >,
  1440. boost::is_same<T, boost::recursive_variant_> >,
  1441. bool >::type = true)
  1442. {
  1443. convert_construct( detail::variant::move(operand), 1L);
  1444. }
  1445. #endif
  1446. public: // structors, cont.
  1447. // [MSVC6 requires copy constructor appear after template constructors]
  1448. variant(const variant& operand)
  1449. {
  1450. // Copy the value of operand into *this...
  1451. detail::variant::copy_into visitor( storage_.address() );
  1452. operand.internal_apply_visitor(visitor);
  1453. // ...and activate the *this's primary storage on success:
  1454. indicate_which(operand.which());
  1455. }
  1456. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  1457. variant(variant&& operand) BOOST_NOEXCEPT_IF(variant_move_noexcept_constructible::type::value)
  1458. {
  1459. // Move the value of operand into *this...
  1460. detail::variant::move_into visitor( storage_.address() );
  1461. operand.internal_apply_visitor(visitor);
  1462. // ...and activate the *this's primary storage on success:
  1463. indicate_which(operand.which());
  1464. }
  1465. #endif
  1466. private: // helpers, for modifiers (below)
  1467. # if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  1468. template <typename Variant>
  1469. friend class detail::variant::backup_assigner;
  1470. # endif
  1471. // class assigner
  1472. //
  1473. // Internal visitor that "assigns" the visited value to the given variant
  1474. // by appropriate destruction and copy-construction.
  1475. //
  1476. class assigner
  1477. : public static_visitor<>
  1478. {
  1479. protected: // representation
  1480. variant& lhs_;
  1481. const int rhs_which_;
  1482. public: // structors
  1483. assigner(variant& lhs, int rhs_which) BOOST_NOEXCEPT
  1484. : lhs_(lhs)
  1485. , rhs_which_(rhs_which)
  1486. {
  1487. }
  1488. protected: // helpers, for internal visitor interface (below)
  1489. template <typename RhsT, typename B1, typename B2>
  1490. void assign_impl(
  1491. const RhsT& rhs_content
  1492. , mpl::true_ // has_nothrow_copy
  1493. , B1 // is_nothrow_move_constructible
  1494. , B2 // has_fallback_type
  1495. ) const BOOST_NOEXCEPT
  1496. {
  1497. // Destroy lhs's content...
  1498. lhs_.destroy_content(); // nothrow
  1499. // ...copy rhs content into lhs's storage...
  1500. new(lhs_.storage_.address())
  1501. RhsT( rhs_content ); // nothrow
  1502. // ...and indicate new content type:
  1503. lhs_.indicate_which(rhs_which_); // nothrow
  1504. }
  1505. template <typename RhsT, typename B>
  1506. void assign_impl(
  1507. const RhsT& rhs_content
  1508. , mpl::false_ // has_nothrow_copy
  1509. , mpl::true_ // is_nothrow_move_constructible
  1510. , B // has_fallback_type
  1511. ) const
  1512. {
  1513. // Attempt to make a temporary copy (so as to move it below)...
  1514. RhsT temp(rhs_content);
  1515. // ...and upon success destroy lhs's content...
  1516. lhs_.destroy_content(); // nothrow
  1517. // ...move the temporary copy into lhs's storage...
  1518. new(lhs_.storage_.address())
  1519. RhsT( detail::variant::move(temp) ); // nothrow
  1520. // ...and indicate new content type:
  1521. lhs_.indicate_which(rhs_which_); // nothrow
  1522. }
  1523. void construct_fallback() const BOOST_NOEXCEPT {
  1524. // In case of failure, default-construct fallback type in lhs's storage...
  1525. new (lhs_.storage_.address())
  1526. fallback_type_; // nothrow
  1527. // ...indicate construction of fallback type...
  1528. lhs_.indicate_which(
  1529. BOOST_MPL_AUX_VALUE_WKND(fallback_type_index_)::value
  1530. ); // nothrow
  1531. }
  1532. template <typename RhsT>
  1533. void assign_impl(
  1534. const RhsT& rhs_content
  1535. , mpl::false_ // has_nothrow_copy
  1536. , mpl::false_ // is_nothrow_move_constructible
  1537. , mpl::true_ // has_fallback_type
  1538. ) const
  1539. {
  1540. // Destroy lhs's content...
  1541. lhs_.destroy_content(); // nothrow
  1542. BOOST_TRY
  1543. {
  1544. // ...and attempt to copy rhs's content into lhs's storage:
  1545. new(lhs_.storage_.address())
  1546. RhsT( rhs_content );
  1547. }
  1548. BOOST_CATCH (...)
  1549. {
  1550. construct_fallback();
  1551. // ...and rethrow:
  1552. BOOST_RETHROW;
  1553. }
  1554. BOOST_CATCH_END
  1555. // In the event of success, indicate new content type:
  1556. lhs_.indicate_which(rhs_which_); // nothrow
  1557. }
  1558. template <typename RhsT>
  1559. void assign_impl(
  1560. const RhsT& rhs_content
  1561. , mpl::false_ // has_nothrow_copy
  1562. , mpl::false_ // is_nothrow_move_constructible
  1563. , mpl::false_ // has_fallback_type
  1564. ) const
  1565. {
  1566. detail::variant::backup_assigner<wknd_self_t>
  1567. visitor(lhs_, rhs_which_, rhs_content);
  1568. lhs_.internal_apply_visitor(visitor);
  1569. }
  1570. public: // internal visitor interfaces
  1571. template <typename RhsT>
  1572. void internal_visit(const RhsT& rhs_content, int) const
  1573. {
  1574. typedef typename has_nothrow_copy<RhsT>::type
  1575. nothrow_copy;
  1576. typedef typename mpl::or_< // reduces compile-time
  1577. nothrow_copy
  1578. , is_nothrow_move_constructible<RhsT>
  1579. >::type nothrow_move_constructor;
  1580. assign_impl(
  1581. rhs_content
  1582. , nothrow_copy()
  1583. , nothrow_move_constructor()
  1584. , has_fallback_type_()
  1585. );
  1586. }
  1587. #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
  1588. private:
  1589. // silence MSVC warning C4512: assignment operator could not be generated
  1590. assigner& operator= (assigner const&);
  1591. #endif
  1592. };
  1593. friend class assigner;
  1594. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  1595. // class move_assigner
  1596. //
  1597. // Internal visitor that "move assigns" the visited value to the given variant
  1598. // by appropriate destruction and move-construction.
  1599. //
  1600. class move_assigner
  1601. : public assigner
  1602. {
  1603. public: // structors
  1604. move_assigner(variant& lhs, int rhs_which) BOOST_NOEXCEPT
  1605. : assigner(lhs, rhs_which)
  1606. {
  1607. }
  1608. private: // helpers, for internal visitor interface (below)
  1609. template <typename RhsT, typename B2>
  1610. void assign_impl(
  1611. RhsT& rhs_content
  1612. , mpl::true_ // has_nothrow_copy
  1613. , mpl::false_ // is_nothrow_move_constructible
  1614. , B2 // has_fallback_type
  1615. ) const BOOST_NOEXCEPT
  1616. {
  1617. assigner::assign_impl(rhs_content, mpl::true_(), mpl::false_(), B2());
  1618. }
  1619. template <typename RhsT, typename B, typename B2>
  1620. void assign_impl(
  1621. RhsT& rhs_content
  1622. , B // has_nothrow_copy
  1623. , mpl::true_ // is_nothrow_move_constructible
  1624. , B2 // has_fallback_type
  1625. ) const BOOST_NOEXCEPT
  1626. {
  1627. // ...destroy lhs's content...
  1628. assigner::lhs_.destroy_content(); // nothrow
  1629. // ...move the rhs_content into lhs's storage...
  1630. new(assigner::lhs_.storage_.address())
  1631. RhsT( detail::variant::move(rhs_content) ); // nothrow
  1632. // ...and indicate new content type:
  1633. assigner::lhs_.indicate_which(assigner::rhs_which_); // nothrow
  1634. }
  1635. template <typename RhsT>
  1636. void assign_impl(
  1637. RhsT& rhs_content
  1638. , mpl::false_ // has_nothrow_copy
  1639. , mpl::false_ // is_nothrow_move_constructible
  1640. , mpl::true_ // has_fallback_type
  1641. ) const
  1642. {
  1643. // Destroy lhs's content...
  1644. assigner::lhs_.destroy_content(); // nothrow
  1645. BOOST_TRY
  1646. {
  1647. // ...and attempt to copy rhs's content into lhs's storage:
  1648. new(assigner::lhs_.storage_.address())
  1649. RhsT( detail::variant::move(rhs_content) );
  1650. }
  1651. BOOST_CATCH (...)
  1652. {
  1653. assigner::construct_fallback();
  1654. // ...and rethrow:
  1655. BOOST_RETHROW;
  1656. }
  1657. BOOST_CATCH_END
  1658. // In the event of success, indicate new content type:
  1659. assigner::lhs_.indicate_which(assigner::rhs_which_); // nothrow
  1660. }
  1661. template <typename RhsT>
  1662. void assign_impl(
  1663. RhsT& rhs_content
  1664. , mpl::false_ // has_nothrow_copy
  1665. , mpl::false_ // is_nothrow_move_constructible
  1666. , mpl::false_ // has_fallback_type
  1667. ) const
  1668. {
  1669. assigner::assign_impl(rhs_content, mpl::false_(), mpl::false_(), mpl::false_());
  1670. }
  1671. public: // internal visitor interfaces
  1672. template <typename RhsT>
  1673. void internal_visit(RhsT& rhs_content, int) const
  1674. {
  1675. typedef typename is_nothrow_move_constructible<RhsT>::type
  1676. nothrow_move_constructor;
  1677. typedef typename mpl::or_< // reduces compile-time
  1678. nothrow_move_constructor
  1679. , has_nothrow_copy<RhsT>
  1680. >::type nothrow_copy;
  1681. assign_impl(
  1682. rhs_content
  1683. , nothrow_copy()
  1684. , nothrow_move_constructor()
  1685. , has_fallback_type_()
  1686. );
  1687. }
  1688. #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
  1689. private:
  1690. // silence MSVC warning C4512: assignment operator could not be generated
  1691. move_assigner& operator= (move_assigner const&);
  1692. #endif
  1693. };
  1694. friend class move_assigner;
  1695. #endif // BOOST_NO_CXX11_RVALUE_REFERENCES
  1696. void variant_assign(const variant& rhs)
  1697. {
  1698. // If the contained types are EXACTLY the same...
  1699. if (which_ == rhs.which_)
  1700. {
  1701. // ...then assign rhs's storage to lhs's content:
  1702. detail::variant::assign_storage visitor(rhs.storage_.address());
  1703. this->internal_apply_visitor(visitor);
  1704. }
  1705. else
  1706. {
  1707. // Otherwise, perform general (copy-based) variant assignment:
  1708. assigner visitor(*this, rhs.which());
  1709. rhs.internal_apply_visitor(visitor);
  1710. }
  1711. }
  1712. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  1713. void variant_assign(variant&& rhs)
  1714. {
  1715. // If the contained types are EXACTLY the same...
  1716. if (which_ == rhs.which_)
  1717. {
  1718. // ...then move rhs's storage to lhs's content:
  1719. detail::variant::move_storage visitor(rhs.storage_.address());
  1720. this->internal_apply_visitor(visitor);
  1721. }
  1722. else
  1723. {
  1724. // Otherwise, perform general (move-based) variant assignment:
  1725. move_assigner visitor(*this, rhs.which());
  1726. rhs.internal_apply_visitor(visitor);
  1727. }
  1728. }
  1729. #endif // BOOST_NO_CXX11_RVALUE_REFERENCES
  1730. private: // helpers, for modifiers (below)
  1731. template <typename T>
  1732. void assign(const T& rhs)
  1733. {
  1734. // If direct T-to-T assignment is not possible...
  1735. detail::variant::direct_assigner<T> direct_assign(rhs);
  1736. if (this->apply_visitor(direct_assign) == false)
  1737. {
  1738. // ...then convert rhs to variant and assign:
  1739. //
  1740. // While potentially inefficient, the following construction of a
  1741. // variant allows T as any type convertible to one of the bounded
  1742. // types without excessive code redundancy.
  1743. //
  1744. variant temp(rhs);
  1745. variant_assign( detail::variant::move(temp) );
  1746. }
  1747. }
  1748. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  1749. template <typename T>
  1750. void move_assign(T&& rhs)
  1751. {
  1752. // If direct T-to-T move assignment is not possible...
  1753. detail::variant::direct_mover<T> direct_move(rhs);
  1754. if (this->apply_visitor(direct_move) == false)
  1755. {
  1756. // ...then convert rhs to variant and assign:
  1757. //
  1758. // While potentially inefficient, the following construction of a
  1759. // variant allows T as any type convertible to one of the bounded
  1760. // types without excessive code redundancy.
  1761. //
  1762. variant temp( detail::variant::move(rhs) );
  1763. variant_assign( detail::variant::move(temp) );
  1764. }
  1765. }
  1766. #endif // BOOST_NO_CXX11_RVALUE_REFERENCES
  1767. public: // modifiers
  1768. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  1769. template <class T>
  1770. typename boost::enable_if<
  1771. boost::mpl::and_<
  1772. boost::is_rvalue_reference<T&&>,
  1773. mpl::not_< boost::is_const<T> >,
  1774. boost::detail::variant::is_variant_constructible_from<T&&, internal_types>
  1775. >,
  1776. variant&
  1777. >::type operator=(T&& rhs)
  1778. {
  1779. move_assign( detail::variant::move(rhs) );
  1780. return *this;
  1781. }
  1782. #endif // BOOST_NO_CXX11_RVALUE_REFERENCES
  1783. template <typename T>
  1784. typename boost::enable_if<
  1785. mpl::or_<
  1786. boost::is_same<T, variant>,
  1787. boost::detail::variant::is_variant_constructible_from<const T&, internal_types>
  1788. >,
  1789. variant&
  1790. >::type operator=(const T& rhs)
  1791. {
  1792. assign(rhs);
  1793. return *this;
  1794. }
  1795. // [MSVC6 requires copy assign appear after templated operator=]
  1796. variant& operator=(const variant& rhs)
  1797. {
  1798. variant_assign(rhs);
  1799. return *this;
  1800. }
  1801. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  1802. variant& operator=(variant&& rhs)
  1803. #if !defined(__GNUC__) || (__GNUC__ != 4) || (__GNUC_MINOR__ > 6) || defined(__clang__)
  1804. BOOST_NOEXCEPT_IF(variant_move_noexcept_constructible::type::value && variant_move_noexcept_assignable::type::value)
  1805. #endif
  1806. {
  1807. variant_assign( detail::variant::move(rhs) );
  1808. return *this;
  1809. }
  1810. #endif // BOOST_NO_CXX11_RVALUE_REFERENCES
  1811. void swap(variant& rhs)
  1812. {
  1813. // If the contained types are the same...
  1814. if (which() == rhs.which())
  1815. {
  1816. // ...then swap the values directly:
  1817. detail::variant::swap_with<variant> visitor(rhs);
  1818. this->apply_visitor(visitor);
  1819. }
  1820. else
  1821. {
  1822. // ...otherwise, perform general variant swap:
  1823. variant tmp( detail::variant::move(rhs) );
  1824. rhs = detail::variant::move(*this);
  1825. *this = detail::variant::move(tmp);
  1826. }
  1827. }
  1828. public: // queries
  1829. //
  1830. // NOTE: member which() defined above.
  1831. //
  1832. bool empty() const BOOST_NOEXCEPT
  1833. {
  1834. return false;
  1835. }
  1836. const boost::typeindex::type_info& type() const
  1837. {
  1838. detail::variant::reflect visitor;
  1839. return this->apply_visitor(visitor);
  1840. }
  1841. public: // prevent comparison with foreign types
  1842. template <typename U>
  1843. void operator==(const U&) const
  1844. {
  1845. BOOST_STATIC_ASSERT( false && sizeof(U) );
  1846. }
  1847. template <typename U>
  1848. void operator<(const U&) const
  1849. {
  1850. BOOST_STATIC_ASSERT( false && sizeof(U) );
  1851. }
  1852. template <typename U>
  1853. void operator!=(const U&) const
  1854. {
  1855. BOOST_STATIC_ASSERT( false && sizeof(U) );
  1856. }
  1857. template <typename U>
  1858. void operator>(const U&) const
  1859. {
  1860. BOOST_STATIC_ASSERT( false && sizeof(U) );
  1861. }
  1862. template <typename U>
  1863. void operator<=(const U&) const
  1864. {
  1865. BOOST_STATIC_ASSERT( false && sizeof(U) );
  1866. }
  1867. template <typename U>
  1868. void operator>=(const U&) const
  1869. {
  1870. BOOST_STATIC_ASSERT( false && sizeof(U) );
  1871. }
  1872. public: // comparison operators
  1873. // [MSVC6 requires these operators appear after template operators]
  1874. bool operator==(const variant& rhs) const
  1875. {
  1876. if (this->which() != rhs.which())
  1877. return false;
  1878. detail::variant::comparer<
  1879. variant, detail::variant::equal_comp
  1880. > visitor(*this);
  1881. return rhs.apply_visitor(visitor);
  1882. }
  1883. bool operator<(const variant& rhs) const
  1884. {
  1885. //
  1886. // Dirk Schreib suggested this collating order.
  1887. //
  1888. if (this->which() != rhs.which())
  1889. return this->which() < rhs.which();
  1890. detail::variant::comparer<
  1891. variant, detail::variant::less_comp
  1892. > visitor(*this);
  1893. return rhs.apply_visitor(visitor);
  1894. }
  1895. ///////////////////////////////////////////////////////////////////////////////
  1896. // comparison operators != > <= >=
  1897. inline bool operator!=(const variant& rhs) const
  1898. {
  1899. return !(*this == rhs);
  1900. }
  1901. inline bool operator>(const variant& rhs) const
  1902. {
  1903. return rhs < *this;
  1904. }
  1905. inline bool operator<=(const variant& rhs) const
  1906. {
  1907. return !(*this > rhs);
  1908. }
  1909. inline bool operator>=(const variant& rhs) const
  1910. {
  1911. return !(*this < rhs);
  1912. }
  1913. // helpers, for visitation support (below) -- private when possible
  1914. #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  1915. template < BOOST_VARIANT_ENUM_PARAMS(typename U) >
  1916. friend class variant;
  1917. private:
  1918. #else// defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  1919. public:
  1920. #endif// !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  1921. template <typename Visitor, typename VoidPtrCV>
  1922. BOOST_FORCEINLINE static typename Visitor::result_type
  1923. internal_apply_visitor_impl(
  1924. int internal_which
  1925. , int logical_which
  1926. , Visitor& visitor
  1927. , VoidPtrCV storage
  1928. )
  1929. {
  1930. typedef mpl::int_<0> first_which;
  1931. typedef typename mpl::begin<internal_types>::type first_it;
  1932. typedef typename mpl::end<internal_types>::type last_it;
  1933. typedef detail::variant::visitation_impl_step<
  1934. first_it, last_it
  1935. > first_step;
  1936. return detail::variant::visitation_impl(
  1937. internal_which, logical_which
  1938. , visitor, storage, mpl::false_()
  1939. , never_uses_backup_flag()
  1940. , static_cast<first_which*>(0), static_cast<first_step*>(0)
  1941. );
  1942. }
  1943. template <typename Visitor>
  1944. BOOST_FORCEINLINE typename Visitor::result_type
  1945. internal_apply_visitor(Visitor& visitor)
  1946. {
  1947. return internal_apply_visitor_impl(
  1948. which_, which(), visitor, storage_.address()
  1949. );
  1950. }
  1951. template <typename Visitor>
  1952. BOOST_FORCEINLINE typename Visitor::result_type
  1953. internal_apply_visitor(Visitor& visitor) const
  1954. {
  1955. return internal_apply_visitor_impl(
  1956. which_, which(), visitor, storage_.address()
  1957. );
  1958. }
  1959. public: // visitation support
  1960. #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
  1961. template <typename Visitor>
  1962. typename Visitor::result_type
  1963. apply_visitor(Visitor& visitor) &&
  1964. {
  1965. detail::variant::invoke_visitor<Visitor, true> invoker(visitor);
  1966. return this->internal_apply_visitor(invoker);
  1967. }
  1968. template <typename Visitor>
  1969. typename Visitor::result_type
  1970. apply_visitor(Visitor& visitor) const&&
  1971. {
  1972. detail::variant::invoke_visitor<Visitor, true> invoker(visitor);
  1973. return this->internal_apply_visitor(invoker);
  1974. }
  1975. #endif
  1976. template <typename Visitor>
  1977. typename Visitor::result_type
  1978. apply_visitor(Visitor& visitor)
  1979. #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
  1980. &
  1981. #endif
  1982. {
  1983. detail::variant::invoke_visitor<Visitor, false> invoker(visitor);
  1984. return this->internal_apply_visitor(invoker);
  1985. }
  1986. template <typename Visitor>
  1987. typename Visitor::result_type
  1988. apply_visitor(Visitor& visitor) const
  1989. #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
  1990. &
  1991. #endif
  1992. {
  1993. detail::variant::invoke_visitor<Visitor, false> invoker(visitor);
  1994. return this->internal_apply_visitor(invoker);
  1995. }
  1996. }; // class variant
  1997. ///////////////////////////////////////////////////////////////////////////////
  1998. // metafunction make_variant_over
  1999. //
  2000. // See docs and boost/variant/variant_fwd.hpp for more information.
  2001. //
  2002. template <typename Types>
  2003. struct make_variant_over
  2004. {
  2005. private: // precondition assertions
  2006. BOOST_STATIC_ASSERT(( ::boost::mpl::is_sequence<Types>::value ));
  2007. typedef typename boost::mpl::insert_range<
  2008. boost::mpl::list<>
  2009. , boost::mpl::end< boost::mpl::list<> >::type
  2010. , Types
  2011. >::type copied_sequence_t;
  2012. public: // metafunction result
  2013. typedef variant<
  2014. detail::variant::over_sequence<copied_sequence_t>
  2015. > type;
  2016. };
  2017. ///////////////////////////////////////////////////////////////////////////////
  2018. // function template swap
  2019. //
  2020. // Swaps two variants of the same type (i.e., identical specification).
  2021. //
  2022. template < BOOST_VARIANT_ENUM_PARAMS(typename T) >
  2023. inline void swap(
  2024. variant< BOOST_VARIANT_ENUM_PARAMS(T) >& lhs
  2025. , variant< BOOST_VARIANT_ENUM_PARAMS(T) >& rhs
  2026. )
  2027. {
  2028. lhs.swap(rhs);
  2029. }
  2030. } // namespace boost
  2031. // implementation additions
  2032. #if !defined(BOOST_NO_IOSTREAM)
  2033. #include <boost/variant/detail/variant_io.hpp>
  2034. #endif // BOOST_NO_IOSTREAM
  2035. #endif // BOOST_VARIANT_VARIANT_HPP