variant.hpp 69 KB

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