variant.hpp 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453
  1. //-----------------------------------------------------------------------------
  2. // boost variant/variant.hpp header file
  3. // See http://www.boost.org for updates, documentation, and revision history.
  4. //-----------------------------------------------------------------------------
  5. //
  6. // Copyright (c) 2002-2003 Eric Friedman, Itay Maman
  7. // Copyright (c) 2012-2019 Antony Polukhin
  8. //
  9. // Distributed under the Boost Software License, Version 1.0. (See
  10. // accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. // Thanks to Adam Romanek for providing patches for exception-disabled env.
  13. #ifndef BOOST_VARIANT_VARIANT_HPP
  14. #define BOOST_VARIANT_VARIANT_HPP
  15. #include <cstddef> // for std::size_t
  16. #include <new> // for placement new
  17. #include <boost/type_index.hpp>
  18. #include <boost/variant/detail/config.hpp>
  19. #include <boost/mpl/aux_/value_wknd.hpp>
  20. #include <boost/variant/variant_fwd.hpp>
  21. #include <boost/variant/detail/backup_holder.hpp>
  22. #include <boost/variant/detail/enable_recursive_fwd.hpp>
  23. #include <boost/variant/detail/forced_return.hpp>
  24. #include <boost/variant/detail/initializer.hpp>
  25. #include <boost/variant/detail/make_variant_list.hpp>
  26. #include <boost/variant/detail/over_sequence.hpp>
  27. #include <boost/variant/detail/visitation_impl.hpp>
  28. #include <boost/variant/detail/hash_variant.hpp>
  29. #include <boost/variant/detail/std_hash.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. void internal_visit(T& operand, int) const BOOST_NOEXCEPT
  322. {
  323. operand.~T(); // must be noexcept
  324. #if BOOST_WORKAROUND(__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<T>(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(__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(__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_> > >::type* = 0)
  1411. {
  1412. convert_construct(operand, 1L);
  1413. }
  1414. template <typename T>
  1415. variant(
  1416. T& operand
  1417. , typename boost::enable_if<mpl::or_<
  1418. mpl::and_<
  1419. mpl::not_< is_const<T> >,
  1420. mpl::not_< boost::is_same<T, variant> >,
  1421. boost::detail::variant::is_variant_constructible_from<T&, internal_types>
  1422. >,
  1423. boost::is_same<T, boost::recursive_variant_> > >::type* = 0
  1424. )
  1425. {
  1426. convert_construct(operand, 1L);
  1427. }
  1428. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  1429. template <class T>
  1430. variant(T&& operand,
  1431. typename boost::enable_if<mpl::or_<
  1432. mpl::and_<
  1433. boost::is_rvalue_reference<T&&>,
  1434. mpl::not_< boost::is_const<T> >,
  1435. mpl::not_< boost::is_same<T, variant> >,
  1436. boost::detail::variant::is_variant_constructible_from<T&&, internal_types>
  1437. >,
  1438. boost::is_same<T, boost::recursive_variant_> > >::type* = 0)
  1439. {
  1440. convert_construct( detail::variant::move(operand), 1L);
  1441. }
  1442. #endif
  1443. public: // structors, cont.
  1444. // [MSVC6 requires copy constructor appear after template constructors]
  1445. variant(const variant& operand)
  1446. {
  1447. // Copy the value of operand into *this...
  1448. detail::variant::copy_into visitor( storage_.address() );
  1449. operand.internal_apply_visitor(visitor);
  1450. // ...and activate the *this's primary storage on success:
  1451. indicate_which(operand.which());
  1452. }
  1453. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  1454. variant(variant&& operand) BOOST_NOEXCEPT_IF(variant_move_noexcept_constructible::type::value)
  1455. {
  1456. // Move the value of operand into *this...
  1457. detail::variant::move_into visitor( storage_.address() );
  1458. operand.internal_apply_visitor(visitor);
  1459. // ...and activate the *this's primary storage on success:
  1460. indicate_which(operand.which());
  1461. }
  1462. #endif
  1463. private: // helpers, for modifiers (below)
  1464. # if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  1465. template <typename Variant>
  1466. friend class detail::variant::backup_assigner;
  1467. # endif
  1468. // class assigner
  1469. //
  1470. // Internal visitor that "assigns" the visited value to the given variant
  1471. // by appropriate destruction and copy-construction.
  1472. //
  1473. class assigner
  1474. : public static_visitor<>
  1475. {
  1476. protected: // representation
  1477. variant& lhs_;
  1478. const int rhs_which_;
  1479. public: // structors
  1480. assigner(variant& lhs, int rhs_which) BOOST_NOEXCEPT
  1481. : lhs_(lhs)
  1482. , rhs_which_(rhs_which)
  1483. {
  1484. }
  1485. protected: // helpers, for internal visitor interface (below)
  1486. template <typename RhsT, typename B1, typename B2>
  1487. void assign_impl(
  1488. const RhsT& rhs_content
  1489. , mpl::true_ // has_nothrow_copy
  1490. , B1 // is_nothrow_move_constructible
  1491. , B2 // has_fallback_type
  1492. ) const BOOST_NOEXCEPT
  1493. {
  1494. // Destroy lhs's content...
  1495. lhs_.destroy_content(); // nothrow
  1496. // ...copy rhs content into lhs's storage...
  1497. new(lhs_.storage_.address())
  1498. RhsT( rhs_content ); // nothrow
  1499. // ...and indicate new content type:
  1500. lhs_.indicate_which(rhs_which_); // nothrow
  1501. }
  1502. template <typename RhsT, typename B>
  1503. void assign_impl(
  1504. const RhsT& rhs_content
  1505. , mpl::false_ // has_nothrow_copy
  1506. , mpl::true_ // is_nothrow_move_constructible
  1507. , B // has_fallback_type
  1508. ) const
  1509. {
  1510. // Attempt to make a temporary copy (so as to move it below)...
  1511. RhsT temp(rhs_content);
  1512. // ...and upon success destroy lhs's content...
  1513. lhs_.destroy_content(); // nothrow
  1514. // ...move the temporary copy into lhs's storage...
  1515. new(lhs_.storage_.address())
  1516. RhsT( detail::variant::move(temp) ); // nothrow
  1517. // ...and indicate new content type:
  1518. lhs_.indicate_which(rhs_which_); // nothrow
  1519. }
  1520. void construct_fallback() const BOOST_NOEXCEPT {
  1521. // In case of failure, default-construct fallback type in lhs's storage...
  1522. new (lhs_.storage_.address())
  1523. fallback_type_; // nothrow
  1524. // ...indicate construction of fallback type...
  1525. lhs_.indicate_which(
  1526. BOOST_MPL_AUX_VALUE_WKND(fallback_type_index_)::value
  1527. ); // nothrow
  1528. }
  1529. template <typename RhsT>
  1530. void assign_impl(
  1531. const RhsT& rhs_content
  1532. , mpl::false_ // has_nothrow_copy
  1533. , mpl::false_ // is_nothrow_move_constructible
  1534. , mpl::true_ // has_fallback_type
  1535. ) const
  1536. {
  1537. // Destroy lhs's content...
  1538. lhs_.destroy_content(); // nothrow
  1539. BOOST_TRY
  1540. {
  1541. // ...and attempt to copy rhs's content into lhs's storage:
  1542. new(lhs_.storage_.address())
  1543. RhsT( rhs_content );
  1544. }
  1545. BOOST_CATCH (...)
  1546. {
  1547. construct_fallback();
  1548. // ...and rethrow:
  1549. BOOST_RETHROW;
  1550. }
  1551. BOOST_CATCH_END
  1552. // In the event of success, indicate new content type:
  1553. lhs_.indicate_which(rhs_which_); // nothrow
  1554. }
  1555. template <typename RhsT>
  1556. void assign_impl(
  1557. const RhsT& rhs_content
  1558. , mpl::false_ // has_nothrow_copy
  1559. , mpl::false_ // is_nothrow_move_constructible
  1560. , mpl::false_ // has_fallback_type
  1561. ) const
  1562. {
  1563. detail::variant::backup_assigner<wknd_self_t>
  1564. visitor(lhs_, rhs_which_, rhs_content);
  1565. lhs_.internal_apply_visitor(visitor);
  1566. }
  1567. public: // internal visitor interfaces
  1568. template <typename RhsT>
  1569. void internal_visit(const RhsT& rhs_content, int) const
  1570. {
  1571. typedef typename has_nothrow_copy<RhsT>::type
  1572. nothrow_copy;
  1573. typedef typename mpl::or_< // reduces compile-time
  1574. nothrow_copy
  1575. , is_nothrow_move_constructible<RhsT>
  1576. >::type nothrow_move_constructor;
  1577. assign_impl(
  1578. rhs_content
  1579. , nothrow_copy()
  1580. , nothrow_move_constructor()
  1581. , has_fallback_type_()
  1582. );
  1583. }
  1584. #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
  1585. private:
  1586. // silence MSVC warning C4512: assignment operator could not be generated
  1587. assigner& operator= (assigner const&);
  1588. #endif
  1589. };
  1590. friend class assigner;
  1591. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  1592. // class move_assigner
  1593. //
  1594. // Internal visitor that "move assigns" the visited value to the given variant
  1595. // by appropriate destruction and move-construction.
  1596. //
  1597. class move_assigner
  1598. : public assigner
  1599. {
  1600. public: // structors
  1601. move_assigner(variant& lhs, int rhs_which) BOOST_NOEXCEPT
  1602. : assigner(lhs, rhs_which)
  1603. {
  1604. }
  1605. private: // helpers, for internal visitor interface (below)
  1606. template <typename RhsT, typename B2>
  1607. void assign_impl(
  1608. RhsT& rhs_content
  1609. , mpl::true_ // has_nothrow_copy
  1610. , mpl::false_ // is_nothrow_move_constructible
  1611. , B2 // has_fallback_type
  1612. ) const BOOST_NOEXCEPT
  1613. {
  1614. assigner::assign_impl(rhs_content, mpl::true_(), mpl::false_(), B2());
  1615. }
  1616. template <typename RhsT, typename B, typename B2>
  1617. void assign_impl(
  1618. RhsT& rhs_content
  1619. , B // has_nothrow_copy
  1620. , mpl::true_ // is_nothrow_move_constructible
  1621. , B2 // has_fallback_type
  1622. ) const BOOST_NOEXCEPT
  1623. {
  1624. // ...destroy lhs's content...
  1625. assigner::lhs_.destroy_content(); // nothrow
  1626. // ...move the rhs_content into lhs's storage...
  1627. new(assigner::lhs_.storage_.address())
  1628. RhsT( detail::variant::move(rhs_content) ); // nothrow
  1629. // ...and indicate new content type:
  1630. assigner::lhs_.indicate_which(assigner::rhs_which_); // nothrow
  1631. }
  1632. template <typename RhsT>
  1633. void assign_impl(
  1634. RhsT& rhs_content
  1635. , mpl::false_ // has_nothrow_copy
  1636. , mpl::false_ // is_nothrow_move_constructible
  1637. , mpl::true_ // has_fallback_type
  1638. ) const
  1639. {
  1640. // Destroy lhs's content...
  1641. assigner::lhs_.destroy_content(); // nothrow
  1642. BOOST_TRY
  1643. {
  1644. // ...and attempt to copy rhs's content into lhs's storage:
  1645. new(assigner::lhs_.storage_.address())
  1646. RhsT( detail::variant::move(rhs_content) );
  1647. }
  1648. BOOST_CATCH (...)
  1649. {
  1650. assigner::construct_fallback();
  1651. // ...and rethrow:
  1652. BOOST_RETHROW;
  1653. }
  1654. BOOST_CATCH_END
  1655. // In the event of success, indicate new content type:
  1656. assigner::lhs_.indicate_which(assigner::rhs_which_); // nothrow
  1657. }
  1658. template <typename RhsT>
  1659. void assign_impl(
  1660. RhsT& rhs_content
  1661. , mpl::false_ // has_nothrow_copy
  1662. , mpl::false_ // is_nothrow_move_constructible
  1663. , mpl::false_ // has_fallback_type
  1664. ) const
  1665. {
  1666. assigner::assign_impl(rhs_content, mpl::false_(), mpl::false_(), mpl::false_());
  1667. }
  1668. public: // internal visitor interfaces
  1669. template <typename RhsT>
  1670. void internal_visit(RhsT& rhs_content, int) const
  1671. {
  1672. typedef typename is_nothrow_move_constructible<RhsT>::type
  1673. nothrow_move_constructor;
  1674. typedef typename mpl::or_< // reduces compile-time
  1675. nothrow_move_constructor
  1676. , has_nothrow_copy<RhsT>
  1677. >::type nothrow_copy;
  1678. assign_impl(
  1679. rhs_content
  1680. , nothrow_copy()
  1681. , nothrow_move_constructor()
  1682. , has_fallback_type_()
  1683. );
  1684. }
  1685. #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
  1686. private:
  1687. // silence MSVC warning C4512: assignment operator could not be generated
  1688. move_assigner& operator= (move_assigner const&);
  1689. #endif
  1690. };
  1691. friend class move_assigner;
  1692. #endif // BOOST_NO_CXX11_RVALUE_REFERENCES
  1693. void variant_assign(const variant& rhs)
  1694. {
  1695. // If the contained types are EXACTLY the same...
  1696. if (which_ == rhs.which_)
  1697. {
  1698. // ...then assign rhs's storage to lhs's content:
  1699. detail::variant::assign_storage visitor(rhs.storage_.address());
  1700. this->internal_apply_visitor(visitor);
  1701. }
  1702. else
  1703. {
  1704. // Otherwise, perform general (copy-based) variant assignment:
  1705. assigner visitor(*this, rhs.which());
  1706. rhs.internal_apply_visitor(visitor);
  1707. }
  1708. }
  1709. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  1710. void variant_assign(variant&& rhs)
  1711. {
  1712. // If the contained types are EXACTLY the same...
  1713. if (which_ == rhs.which_)
  1714. {
  1715. // ...then move rhs's storage to lhs's content:
  1716. detail::variant::move_storage visitor(rhs.storage_.address());
  1717. this->internal_apply_visitor(visitor);
  1718. }
  1719. else
  1720. {
  1721. // Otherwise, perform general (move-based) variant assignment:
  1722. move_assigner visitor(*this, rhs.which());
  1723. rhs.internal_apply_visitor(visitor);
  1724. }
  1725. }
  1726. #endif // BOOST_NO_CXX11_RVALUE_REFERENCES
  1727. private: // helpers, for modifiers (below)
  1728. template <typename T>
  1729. void assign(const T& rhs)
  1730. {
  1731. // If direct T-to-T assignment is not possible...
  1732. detail::variant::direct_assigner<T> direct_assign(rhs);
  1733. if (this->apply_visitor(direct_assign) == false)
  1734. {
  1735. // ...then convert rhs to variant and assign:
  1736. //
  1737. // While potentially inefficient, the following construction of a
  1738. // variant allows T as any type convertible to one of the bounded
  1739. // types without excessive code redundancy.
  1740. //
  1741. variant temp(rhs);
  1742. variant_assign( detail::variant::move(temp) );
  1743. }
  1744. }
  1745. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  1746. template <typename T>
  1747. void move_assign(T&& rhs)
  1748. {
  1749. // If direct T-to-T move assignment is not possible...
  1750. detail::variant::direct_mover<T> direct_move(rhs);
  1751. if (this->apply_visitor(direct_move) == false)
  1752. {
  1753. // ...then convert rhs to variant and assign:
  1754. //
  1755. // While potentially inefficient, the following construction of a
  1756. // variant allows T as any type convertible to one of the bounded
  1757. // types without excessive code redundancy.
  1758. //
  1759. variant temp( detail::variant::move(rhs) );
  1760. variant_assign( detail::variant::move(temp) );
  1761. }
  1762. }
  1763. #endif // BOOST_NO_CXX11_RVALUE_REFERENCES
  1764. public: // modifiers
  1765. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  1766. template <class T>
  1767. typename boost::enable_if<
  1768. boost::mpl::and_<
  1769. boost::is_rvalue_reference<T&&>,
  1770. mpl::not_< boost::is_const<T> >,
  1771. boost::detail::variant::is_variant_constructible_from<T&&, internal_types>
  1772. >,
  1773. variant&
  1774. >::type operator=(T&& rhs)
  1775. {
  1776. move_assign( detail::variant::move(rhs) );
  1777. return *this;
  1778. }
  1779. #endif // BOOST_NO_CXX11_RVALUE_REFERENCES
  1780. template <typename T>
  1781. typename boost::enable_if<
  1782. mpl::or_<
  1783. boost::is_same<T, variant>,
  1784. boost::detail::variant::is_variant_constructible_from<const T&, internal_types>
  1785. >,
  1786. variant&
  1787. >::type operator=(const T& rhs)
  1788. {
  1789. assign(rhs);
  1790. return *this;
  1791. }
  1792. // [MSVC6 requires copy assign appear after templated operator=]
  1793. variant& operator=(const variant& rhs)
  1794. {
  1795. variant_assign(rhs);
  1796. return *this;
  1797. }
  1798. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  1799. variant& operator=(variant&& rhs)
  1800. #if !defined(__GNUC__) || (__GNUC__ != 4) || (__GNUC_MINOR__ > 6) || defined(__clang__)
  1801. BOOST_NOEXCEPT_IF(variant_move_noexcept_constructible::type::value && variant_move_noexcept_assignable::type::value)
  1802. #endif
  1803. {
  1804. variant_assign( detail::variant::move(rhs) );
  1805. return *this;
  1806. }
  1807. #endif // BOOST_NO_CXX11_RVALUE_REFERENCES
  1808. void swap(variant& rhs)
  1809. {
  1810. // If the contained types are the same...
  1811. if (which() == rhs.which())
  1812. {
  1813. // ...then swap the values directly:
  1814. detail::variant::swap_with<variant> visitor(rhs);
  1815. this->apply_visitor(visitor);
  1816. }
  1817. else
  1818. {
  1819. // ...otherwise, perform general variant swap:
  1820. variant tmp( detail::variant::move(rhs) );
  1821. rhs = detail::variant::move(*this);
  1822. *this = detail::variant::move(tmp);
  1823. }
  1824. }
  1825. public: // queries
  1826. //
  1827. // NOTE: member which() defined above.
  1828. //
  1829. bool empty() const BOOST_NOEXCEPT
  1830. {
  1831. return false;
  1832. }
  1833. const boost::typeindex::type_info& type() const
  1834. {
  1835. detail::variant::reflect visitor;
  1836. return this->apply_visitor(visitor);
  1837. }
  1838. public: // prevent comparison with foreign types
  1839. template <typename U>
  1840. void operator==(const U&) const
  1841. {
  1842. BOOST_STATIC_ASSERT( false && sizeof(U) );
  1843. }
  1844. template <typename U>
  1845. void operator<(const U&) const
  1846. {
  1847. BOOST_STATIC_ASSERT( false && sizeof(U) );
  1848. }
  1849. template <typename U>
  1850. void operator!=(const U&) const
  1851. {
  1852. BOOST_STATIC_ASSERT( false && sizeof(U) );
  1853. }
  1854. template <typename U>
  1855. void operator>(const U&) const
  1856. {
  1857. BOOST_STATIC_ASSERT( false && sizeof(U) );
  1858. }
  1859. template <typename U>
  1860. void operator<=(const U&) const
  1861. {
  1862. BOOST_STATIC_ASSERT( false && sizeof(U) );
  1863. }
  1864. template <typename U>
  1865. void operator>=(const U&) const
  1866. {
  1867. BOOST_STATIC_ASSERT( false && sizeof(U) );
  1868. }
  1869. public: // comparison operators
  1870. // [MSVC6 requires these operators appear after template operators]
  1871. bool operator==(const variant& rhs) const
  1872. {
  1873. if (this->which() != rhs.which())
  1874. return false;
  1875. detail::variant::comparer<
  1876. variant, detail::variant::equal_comp
  1877. > visitor(*this);
  1878. return rhs.apply_visitor(visitor);
  1879. }
  1880. bool operator<(const variant& rhs) const
  1881. {
  1882. //
  1883. // Dirk Schreib suggested this collating order.
  1884. //
  1885. if (this->which() != rhs.which())
  1886. return this->which() < rhs.which();
  1887. detail::variant::comparer<
  1888. variant, detail::variant::less_comp
  1889. > visitor(*this);
  1890. return rhs.apply_visitor(visitor);
  1891. }
  1892. ///////////////////////////////////////////////////////////////////////////////
  1893. // comparison operators != > <= >=
  1894. inline bool operator!=(const variant& rhs) const
  1895. {
  1896. return !(*this == rhs);
  1897. }
  1898. inline bool operator>(const variant& rhs) const
  1899. {
  1900. return rhs < *this;
  1901. }
  1902. inline bool operator<=(const variant& rhs) const
  1903. {
  1904. return !(*this > rhs);
  1905. }
  1906. inline bool operator>=(const variant& rhs) const
  1907. {
  1908. return !(*this < rhs);
  1909. }
  1910. // helpers, for visitation support (below) -- private when possible
  1911. #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  1912. template < BOOST_VARIANT_ENUM_PARAMS(typename U) >
  1913. friend class variant;
  1914. private:
  1915. #else// defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  1916. public:
  1917. #endif// !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  1918. template <typename Visitor, typename VoidPtrCV>
  1919. BOOST_FORCEINLINE static typename Visitor::result_type
  1920. internal_apply_visitor_impl(
  1921. int internal_which
  1922. , int logical_which
  1923. , Visitor& visitor
  1924. , VoidPtrCV storage
  1925. )
  1926. {
  1927. typedef mpl::int_<0> first_which;
  1928. typedef typename mpl::begin<internal_types>::type first_it;
  1929. typedef typename mpl::end<internal_types>::type last_it;
  1930. typedef detail::variant::visitation_impl_step<
  1931. first_it, last_it
  1932. > first_step;
  1933. return detail::variant::visitation_impl(
  1934. internal_which, logical_which
  1935. , visitor, storage, mpl::false_()
  1936. , never_uses_backup_flag()
  1937. , static_cast<first_which*>(0), static_cast<first_step*>(0)
  1938. );
  1939. }
  1940. template <typename Visitor>
  1941. BOOST_FORCEINLINE typename Visitor::result_type
  1942. internal_apply_visitor(Visitor& visitor)
  1943. {
  1944. return internal_apply_visitor_impl(
  1945. which_, which(), visitor, storage_.address()
  1946. );
  1947. }
  1948. template <typename Visitor>
  1949. BOOST_FORCEINLINE typename Visitor::result_type
  1950. internal_apply_visitor(Visitor& visitor) const
  1951. {
  1952. return internal_apply_visitor_impl(
  1953. which_, which(), visitor, storage_.address()
  1954. );
  1955. }
  1956. public: // visitation support
  1957. #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
  1958. template <typename Visitor>
  1959. typename Visitor::result_type
  1960. apply_visitor(Visitor& visitor) &&
  1961. {
  1962. detail::variant::invoke_visitor<Visitor, true> invoker(visitor);
  1963. return this->internal_apply_visitor(invoker);
  1964. }
  1965. template <typename Visitor>
  1966. typename Visitor::result_type
  1967. apply_visitor(Visitor& visitor) const&&
  1968. {
  1969. detail::variant::invoke_visitor<Visitor, true> invoker(visitor);
  1970. return this->internal_apply_visitor(invoker);
  1971. }
  1972. #endif
  1973. template <typename Visitor>
  1974. typename Visitor::result_type
  1975. apply_visitor(Visitor& visitor)
  1976. #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
  1977. &
  1978. #endif
  1979. {
  1980. detail::variant::invoke_visitor<Visitor, false> invoker(visitor);
  1981. return this->internal_apply_visitor(invoker);
  1982. }
  1983. template <typename Visitor>
  1984. typename Visitor::result_type
  1985. apply_visitor(Visitor& visitor) const
  1986. #ifndef BOOST_NO_CXX11_REF_QUALIFIERS
  1987. &
  1988. #endif
  1989. {
  1990. detail::variant::invoke_visitor<Visitor, false> invoker(visitor);
  1991. return this->internal_apply_visitor(invoker);
  1992. }
  1993. }; // class variant
  1994. ///////////////////////////////////////////////////////////////////////////////
  1995. // metafunction make_variant_over
  1996. //
  1997. // See docs and boost/variant/variant_fwd.hpp for more information.
  1998. //
  1999. template <typename Types>
  2000. struct make_variant_over
  2001. {
  2002. private: // precondition assertions
  2003. BOOST_STATIC_ASSERT(( ::boost::mpl::is_sequence<Types>::value ));
  2004. typedef typename boost::mpl::insert_range<
  2005. boost::mpl::list<>
  2006. , boost::mpl::end< boost::mpl::list<> >::type
  2007. , Types
  2008. >::type copied_sequence_t;
  2009. public: // metafunction result
  2010. typedef variant<
  2011. detail::variant::over_sequence<copied_sequence_t>
  2012. > type;
  2013. };
  2014. ///////////////////////////////////////////////////////////////////////////////
  2015. // function template swap
  2016. //
  2017. // Swaps two variants of the same type (i.e., identical specification).
  2018. //
  2019. template < BOOST_VARIANT_ENUM_PARAMS(typename T) >
  2020. inline void swap(
  2021. variant< BOOST_VARIANT_ENUM_PARAMS(T) >& lhs
  2022. , variant< BOOST_VARIANT_ENUM_PARAMS(T) >& rhs
  2023. )
  2024. {
  2025. lhs.swap(rhs);
  2026. }
  2027. } // namespace boost
  2028. // implementation additions
  2029. #if !defined(BOOST_NO_IOSTREAM)
  2030. #include <boost/variant/detail/variant_io.hpp>
  2031. #endif // BOOST_NO_IOSTREAM
  2032. #endif // BOOST_VARIANT_VARIANT_HPP