variant.hpp 70 KB

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