Ticket #3051: variant.hpp

File variant.hpp, 49.8 KB (added by anonymous, 13 years ago)

Modified variant.hpp

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