variant.hpp 74 KB

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