variant.hpp 69 KB

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