variant.hpp 67 KB

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