sl@0: //----------------------------------------------------------------------------- sl@0: // boost variant/variant.hpp header file sl@0: // See http://www.boost.org for updates, documentation, and revision history. sl@0: //----------------------------------------------------------------------------- sl@0: // sl@0: // Copyright (c) 2002-2003 sl@0: // Eric Friedman, Itay Maman sl@0: // sl@0: // Distributed under the Boost Software License, Version 1.0. (See sl@0: // accompanying file LICENSE_1_0.txt or copy at sl@0: // http://www.boost.org/LICENSE_1_0.txt) sl@0: /* sl@0: * © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved. sl@0: */ sl@0: #ifndef BOOST_VARIANT_VARIANT_HPP sl@0: #define BOOST_VARIANT_VARIANT_HPP sl@0: sl@0: #include // for std::size_t sl@0: #include // for placement new sl@0: #ifdef __SYMBIAN32__ sl@0: #include sl@0: #endif sl@0: #include // for typeid, std::type_info sl@0: sl@0: #include "boost/variant/detail/config.hpp" sl@0: #include "boost/mpl/aux_/config/eti.hpp" sl@0: #include "boost/mpl/aux_/value_wknd.hpp" sl@0: sl@0: #include "boost/variant/variant_fwd.hpp" sl@0: #include "boost/variant/detail/backup_holder.hpp" sl@0: #include "boost/variant/detail/enable_recursive_fwd.hpp" sl@0: #include "boost/variant/detail/forced_return.hpp" sl@0: #include "boost/variant/detail/initializer.hpp" sl@0: #include "boost/variant/detail/make_variant_list.hpp" sl@0: #include "boost/variant/detail/over_sequence.hpp" sl@0: #include "boost/variant/detail/visitation_impl.hpp" sl@0: sl@0: #include "boost/variant/detail/generic_result_type.hpp" sl@0: #include "boost/variant/detail/has_nothrow_move.hpp" sl@0: #include "boost/variant/detail/move.hpp" sl@0: sl@0: #include "boost/detail/reference_content.hpp" sl@0: #include "boost/aligned_storage.hpp" sl@0: #include "boost/blank.hpp" sl@0: #include "boost/static_assert.hpp" sl@0: #include "boost/preprocessor/cat.hpp" sl@0: #include "boost/preprocessor/repeat.hpp" sl@0: #include "boost/type_traits/alignment_of.hpp" sl@0: #include "boost/type_traits/add_const.hpp" sl@0: #include "boost/type_traits/has_nothrow_constructor.hpp" sl@0: #include "boost/type_traits/has_nothrow_copy.hpp" sl@0: #include "boost/type_traits/is_const.hpp" sl@0: #include "boost/type_traits/is_same.hpp" sl@0: #include "boost/utility/enable_if.hpp" sl@0: #include "boost/variant/recursive_wrapper_fwd.hpp" sl@0: #include "boost/variant/static_visitor.hpp" sl@0: sl@0: #include "boost/mpl/eval_if.hpp" sl@0: #include "boost/mpl/begin_end.hpp" sl@0: #include "boost/mpl/bool.hpp" sl@0: #include "boost/mpl/empty.hpp" sl@0: #include "boost/mpl/find_if.hpp" sl@0: #include "boost/mpl/front.hpp" sl@0: #include "boost/mpl/identity.hpp" sl@0: #include "boost/mpl/if.hpp" sl@0: #include "boost/mpl/int.hpp" sl@0: #include "boost/mpl/is_sequence.hpp" sl@0: #include "boost/mpl/iterator_range.hpp" sl@0: #include "boost/mpl/iter_fold_if.hpp" sl@0: #include "boost/mpl/logical.hpp" sl@0: #include "boost/mpl/max_element.hpp" sl@0: #include "boost/mpl/next.hpp" sl@0: #include "boost/mpl/deref.hpp" sl@0: #include "boost/mpl/pair.hpp" sl@0: #include "boost/mpl/protect.hpp" sl@0: #include "boost/mpl/push_front.hpp" sl@0: #include "boost/mpl/same_as.hpp" sl@0: #include "boost/mpl/size_t.hpp" sl@0: #include "boost/mpl/sizeof.hpp" sl@0: #include "boost/mpl/transform.hpp" sl@0: #include "boost/mpl/assert.hpp" sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // Implementation Macros: sl@0: // sl@0: // BOOST_VARIANT_VISITATION_UNROLLING_LIMIT sl@0: // Defined in boost/variant/detail/visitation_impl.hpp. sl@0: // sl@0: // BOOST_VARIANT_MINIMIZE_SIZE sl@0: // When #defined, implementation employs all known means to minimize the sl@0: // size of variant obje cts. However, often unsuccessful due to alignment sl@0: // issues, and potentially harmful to runtime speed, so not enabled by sl@0: // default. (TODO: Investigate further.) sl@0: sl@0: #if defined(BOOST_VARIANT_MINIMIZE_SIZE) sl@0: # include // for SCHAR_MAX sl@0: # include "boost/mpl/eval_if.hpp" sl@0: # include "boost/mpl/equal_to.hpp" sl@0: # include "boost/mpl/identity.hpp" sl@0: # include "boost/mpl/int.hpp" sl@0: # include "boost/mpl/if.hpp" sl@0: # include "boost/mpl/less.hpp" sl@0: # include "boost/mpl/long.hpp" sl@0: # include "boost/mpl/O1_size.hpp" sl@0: #endif sl@0: sl@0: sl@0: namespace boost { sl@0: sl@0: namespace detail { namespace variant { sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // (detail) metafunction max_value sl@0: // sl@0: // Finds the maximum value of the unary metafunction F over Sequence. sl@0: // sl@0: template sl@0: struct max_value sl@0: { sl@0: private: // helpers, for metafunction result (below) sl@0: sl@0: typedef typename mpl::transform1::type transformed_; sl@0: typedef typename mpl::max_element::type max_it; sl@0: sl@0: public: // metafunction result sl@0: sl@0: typedef typename mpl::deref::type sl@0: type; sl@0: sl@0: }; sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // (detail) metafunction find_fallback_type sl@0: // sl@0: // Provides a fallback (i.e., nothrow default-constructible) type from the sl@0: // specified sequence, or no_fallback_type if not found. sl@0: // sl@0: // This implementation is designed to prefer boost::blank over other potential sl@0: // fallback types, regardless of its position in the specified sequence. sl@0: // sl@0: sl@0: class no_fallback_type; sl@0: sl@0: struct find_fallback_type_pred sl@0: { sl@0: template sl@0: struct apply sl@0: { sl@0: private: sl@0: typedef typename mpl::deref::type t_; sl@0: sl@0: public: sl@0: typedef mpl::not_< has_nothrow_constructor > type; sl@0: }; sl@0: }; sl@0: sl@0: template sl@0: struct find_fallback_type sl@0: { sl@0: private: // helpers, for metafunction result (below) sl@0: sl@0: typedef typename mpl::end::type end_it; sl@0: sl@0: // [Find the first suitable fallback type...] sl@0: sl@0: typedef typename mpl::iter_fold_if< sl@0: Types sl@0: , mpl::int_<0>, mpl::protect< mpl::next<> > sl@0: , mpl::protect< find_fallback_type_pred > sl@0: >::type first_result_; sl@0: sl@0: typedef typename first_result_::first first_result_index; sl@0: typedef typename first_result_::second first_result_it; sl@0: sl@0: // [...now search the rest of the sequence for boost::blank...] sl@0: sl@0: typedef typename mpl::iter_fold_if< sl@0: mpl::iterator_range< first_result_it,end_it > sl@0: , first_result_index, mpl::protect< mpl::next<> > sl@0: , mpl::protect< mpl::not_same_as > sl@0: >::type second_result_; sl@0: sl@0: typedef typename second_result_::second second_result_it; sl@0: sl@0: public: // metafunction result sl@0: sl@0: // [...and return the results of the search:] sl@0: typedef typename mpl::eval_if< sl@0: is_same< second_result_it,end_it > sl@0: , mpl::if_< sl@0: is_same< first_result_it,end_it > sl@0: , mpl::pair< no_fallback_type,no_fallback_type > sl@0: , first_result_ sl@0: > sl@0: , mpl::identity< second_result_ > sl@0: >::type type; sl@0: sl@0: }; sl@0: sl@0: #if defined(BOOST_MPL_CFG_MSVC_60_ETI_BUG) sl@0: sl@0: template<> sl@0: struct find_fallback_type sl@0: { sl@0: typedef mpl::pair< no_fallback_type,no_fallback_type > type; sl@0: }; sl@0: sl@0: #endif // BOOST_MPL_CFG_MSVC_60_ETI_BUG workaround sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // (detail) metafunction make_storage sl@0: // sl@0: // Provides an aligned storage type capable of holding any of the types sl@0: // specified in the given type-sequence. sl@0: // sl@0: sl@0: template sl@0: struct make_storage sl@0: { sl@0: private: // helpers, for metafunction result (below) sl@0: sl@0: typedef typename mpl::eval_if< sl@0: NeverUsesBackupFlag sl@0: , mpl::identity< Types > sl@0: , mpl::push_front< sl@0: Types, backup_holder sl@0: > sl@0: >::type types; sl@0: sl@0: typedef typename max_value< sl@0: types, mpl::sizeof_ sl@0: >::type max_size; sl@0: sl@0: #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0551)) sl@0: sl@0: typedef typename max_value< sl@0: types, alignment_of sl@0: >::type max_alignment; sl@0: sl@0: #else // borland sl@0: sl@0: // temporary workaround -- use maximal alignment sl@0: typedef mpl::size_t< -1 > max_alignment; sl@0: sl@0: #endif // borland workaround sl@0: sl@0: public: // metafunction result sl@0: sl@0: #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1300) sl@0: sl@0: typedef ::boost::aligned_storage< sl@0: BOOST_MPL_AUX_VALUE_WKND(max_size)::value sl@0: , BOOST_MPL_AUX_VALUE_WKND(max_alignment)::value sl@0: > type; sl@0: sl@0: #else // MSVC7 and below sl@0: sl@0: BOOST_STATIC_CONSTANT(std::size_t, msvc_max_size_c = max_size::value); sl@0: BOOST_STATIC_CONSTANT(std::size_t, msvc_max_alignment_c = max_alignment::value); sl@0: sl@0: typedef ::boost::aligned_storage< sl@0: msvc_max_size_c sl@0: , msvc_max_alignment_c sl@0: > type; sl@0: sl@0: #endif // MSVC workaround sl@0: sl@0: }; sl@0: sl@0: #if defined(BOOST_MPL_CFG_MSVC_60_ETI_BUG) sl@0: sl@0: template<> sl@0: struct make_storage sl@0: { sl@0: typedef int type; sl@0: }; sl@0: sl@0: #endif // BOOST_MPL_CFG_MSVC_60_ETI_BUG workaround sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // (detail) class destroyer sl@0: // sl@0: // Internal visitor that destroys the value it visits. sl@0: // sl@0: struct destroyer sl@0: : public static_visitor<> sl@0: { sl@0: public: // visitor interfaces sl@0: sl@0: template sl@0: BOOST_VARIANT_AUX_RETURN_VOID_TYPE sl@0: internal_visit(T& operand, int) const sl@0: { sl@0: operand.~T(); sl@0: sl@0: #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0551)) sl@0: operand; // suppresses warnings sl@0: #endif sl@0: sl@0: BOOST_VARIANT_AUX_RETURN_VOID; sl@0: } sl@0: sl@0: }; sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // (detail) class template known_get sl@0: // sl@0: // Visitor that returns a reference to content of the specified type. sl@0: // sl@0: // Precondition: visited variant MUST contain logical content of type T. sl@0: // sl@0: template sl@0: class known_get sl@0: : public static_visitor sl@0: { sl@0: sl@0: #if !BOOST_WORKAROUND(BOOST_MSVC, < 1300) sl@0: sl@0: public: // visitor interface sl@0: sl@0: T& operator()(T& operand) const sl@0: { sl@0: return operand; sl@0: } sl@0: sl@0: template sl@0: T& operator()(U&) const sl@0: { sl@0: // logical error to be here: see precondition above sl@0: BOOST_ASSERT(false); sl@0: return ::boost::detail::variant::forced_return< T& >(); sl@0: } sl@0: sl@0: #else // MSVC6 sl@0: sl@0: private: // helpers, for visitor interface (below) sl@0: sl@0: T& execute(T& operand, mpl::true_) const sl@0: { sl@0: return operand; sl@0: } sl@0: sl@0: template sl@0: T& execute(U& operand, mpl::false_) const sl@0: { sl@0: // logical error to be here: see precondition above sl@0: BOOST_ASSERT(false); sl@0: return ::boost::detail::variant::forced_return< T& >(); sl@0: } sl@0: sl@0: public: // visitor interface sl@0: sl@0: template sl@0: T& operator()(U& operand) const sl@0: { sl@0: typedef typename is_same< U,T >::type sl@0: U_is_T; sl@0: sl@0: return execute(operand, U_is_T()); sl@0: } sl@0: sl@0: #endif // MSVC6 workaround sl@0: sl@0: }; sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // (detail) class copy_into sl@0: // sl@0: // Internal visitor that copies the value it visits into the given buffer. sl@0: // sl@0: class copy_into sl@0: : public static_visitor<> sl@0: { sl@0: private: // representation sl@0: sl@0: void* storage_; sl@0: sl@0: public: // structors sl@0: sl@0: explicit copy_into(void* storage) sl@0: : storage_(storage) sl@0: { sl@0: } sl@0: sl@0: public: // internal visitor interface sl@0: sl@0: template sl@0: BOOST_VARIANT_AUX_RETURN_VOID_TYPE sl@0: internal_visit(boost::detail::variant::backup_holder& operand, long) const sl@0: { sl@0: new(storage_) T( operand.get() ); sl@0: BOOST_VARIANT_AUX_RETURN_VOID; sl@0: } sl@0: sl@0: template sl@0: BOOST_VARIANT_AUX_RETURN_VOID_TYPE sl@0: internal_visit(const boost::detail::variant::backup_holder& operand, long) const sl@0: { sl@0: new(storage_) T( operand.get() ); sl@0: BOOST_VARIANT_AUX_RETURN_VOID; sl@0: } sl@0: sl@0: template sl@0: BOOST_VARIANT_AUX_RETURN_VOID_TYPE sl@0: internal_visit(const T& operand, int) const sl@0: { sl@0: new(storage_) T(operand); sl@0: BOOST_VARIANT_AUX_RETURN_VOID; sl@0: } sl@0: sl@0: }; sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // (detail) class assign_storage sl@0: // sl@0: // Internal visitor that assigns the given storage (which must be a sl@0: // constructed value of the same type) to the value it visits. sl@0: // sl@0: struct assign_storage sl@0: : public static_visitor<> sl@0: { sl@0: private: // representation sl@0: sl@0: const void* rhs_storage_; sl@0: sl@0: public: // structors sl@0: sl@0: explicit assign_storage(const void* rhs_storage) sl@0: : rhs_storage_(rhs_storage) sl@0: { sl@0: } sl@0: sl@0: public: // internal visitor interfaces sl@0: sl@0: template sl@0: BOOST_VARIANT_AUX_RETURN_VOID_TYPE sl@0: internal_visit(backup_holder& lhs_content, long) const sl@0: { sl@0: lhs_content.get() sl@0: = static_cast< const backup_holder* >(rhs_storage_)->get(); sl@0: BOOST_VARIANT_AUX_RETURN_VOID; sl@0: } sl@0: sl@0: template sl@0: BOOST_VARIANT_AUX_RETURN_VOID_TYPE sl@0: internal_visit(const backup_holder& lhs_content, long) const sl@0: { sl@0: lhs_content.get() sl@0: = static_cast< const backup_holder* >(rhs_storage_)->get(); sl@0: BOOST_VARIANT_AUX_RETURN_VOID; sl@0: } sl@0: sl@0: template sl@0: BOOST_VARIANT_AUX_RETURN_VOID_TYPE sl@0: internal_visit(T& lhs_content, int) const sl@0: { sl@0: // NOTE TO USER : sl@0: // Compile error here indicates one of variant's bounded types does sl@0: // not meet the requirements of the Assignable concept. Thus, sl@0: // variant is not Assignable. sl@0: // sl@0: // Hint: Are any of the bounded types const-qualified or references? sl@0: // sl@0: lhs_content = *static_cast< const T* >(rhs_storage_); sl@0: BOOST_VARIANT_AUX_RETURN_VOID; sl@0: } sl@0: sl@0: }; sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // (detail) class direct_assigner sl@0: // sl@0: // Generic static visitor that: if and only if the visited value is of the sl@0: // specified type, assigns the given value to the visited value and returns sl@0: // true; else returns false. sl@0: // sl@0: template sl@0: class direct_assigner sl@0: : public static_visitor sl@0: { sl@0: private: // representation sl@0: sl@0: T& rhs_; sl@0: sl@0: public: // structors sl@0: sl@0: explicit direct_assigner(T& rhs) sl@0: : rhs_(rhs) sl@0: { sl@0: } sl@0: sl@0: #if !BOOST_WORKAROUND(BOOST_MSVC, < 1300) sl@0: sl@0: public: // visitor interface sl@0: sl@0: bool operator()(T& lhs) sl@0: { sl@0: lhs = rhs_; sl@0: return true; sl@0: } sl@0: sl@0: template sl@0: bool operator()(U&) sl@0: { sl@0: return false; sl@0: } sl@0: sl@0: #else // MSVC6 sl@0: sl@0: private: // helpers, for visitor interface (below) sl@0: sl@0: bool execute(T& lhs, mpl::true_) sl@0: { sl@0: lhs = rhs_; sl@0: return true; sl@0: } sl@0: sl@0: template sl@0: bool execute(U&, mpl::false_) sl@0: { sl@0: return false; sl@0: } sl@0: sl@0: public: // visitor interface sl@0: sl@0: template sl@0: bool operator()(U& lhs) sl@0: { sl@0: typedef typename is_same::type U_is_T; sl@0: return execute(lhs, U_is_T()); sl@0: } sl@0: sl@0: #endif // MSVC6 workaround sl@0: sl@0: }; sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // (detail) class backup_assigner sl@0: // sl@0: // Internal visitor that "assigns" the given value to the visited value, sl@0: // using backup to recover if the destroy-copy sequence fails. sl@0: // sl@0: // NOTE: This needs to be a friend of variant, as it needs access to sl@0: // indicate_which, indicate_backup_which, etc. sl@0: // sl@0: template sl@0: class backup_assigner sl@0: : public static_visitor<> sl@0: { sl@0: private: // representation sl@0: sl@0: Variant& lhs_; sl@0: int rhs_which_; sl@0: const RhsT& rhs_content_; sl@0: sl@0: public: // structors sl@0: sl@0: backup_assigner(Variant& lhs, int rhs_which, const RhsT& rhs_content) sl@0: : lhs_(lhs) sl@0: , rhs_which_(rhs_which) sl@0: , rhs_content_(rhs_content) sl@0: { sl@0: } sl@0: sl@0: private: // helpers, for visitor interface (below) sl@0: sl@0: template sl@0: void backup_assign_impl( sl@0: LhsT& lhs_content sl@0: , mpl::true_// has_nothrow_move sl@0: ) sl@0: { sl@0: // Move lhs content to backup... sl@0: LhsT backup_lhs_content( sl@0: ::boost::detail::variant::move(lhs_content) sl@0: ); // nothrow sl@0: sl@0: // ...destroy lhs content... sl@0: lhs_content.~LhsT(); // nothrow sl@0: sl@0: try sl@0: { sl@0: // ...and attempt to copy rhs content into lhs storage: sl@0: new(lhs_.storage_.address()) RhsT(rhs_content_); sl@0: } sl@0: catch (...) sl@0: { sl@0: // In case of failure, restore backup content to lhs storage... sl@0: new(lhs_.storage_.address()) sl@0: LhsT( sl@0: ::boost::detail::variant::move(backup_lhs_content) sl@0: ); // nothrow sl@0: sl@0: // ...and rethrow: sl@0: throw; sl@0: } sl@0: sl@0: // In case of success, indicate new content type: sl@0: lhs_.indicate_which(rhs_which_); // nothrow sl@0: } sl@0: sl@0: template sl@0: void backup_assign_impl( sl@0: LhsT& lhs_content sl@0: , mpl::false_// has_nothrow_move sl@0: ) sl@0: { sl@0: // Backup lhs content... sl@0: LhsT* backup_lhs_ptr = new LhsT(lhs_content); sl@0: sl@0: // ...destroy lhs content... sl@0: lhs_content.~LhsT(); // nothrow sl@0: sl@0: try sl@0: { sl@0: // ...and attempt to copy rhs content into lhs storage: sl@0: new(lhs_.storage_.address()) RhsT(rhs_content_); sl@0: } sl@0: catch (...) sl@0: { sl@0: // In case of failure, copy backup pointer to lhs storage... sl@0: new(lhs_.storage_.address()) sl@0: backup_holder( backup_lhs_ptr ); // nothrow sl@0: sl@0: // ...indicate now using backup... sl@0: lhs_.indicate_backup_which( lhs_.which() ); // nothrow sl@0: sl@0: // ...and rethrow: sl@0: throw; sl@0: } sl@0: sl@0: // In case of success, indicate new content type... sl@0: lhs_.indicate_which(rhs_which_); // nothrow sl@0: sl@0: // ...and delete backup: sl@0: delete backup_lhs_ptr; // nothrow sl@0: } sl@0: sl@0: public: // visitor interface sl@0: sl@0: template sl@0: BOOST_VARIANT_AUX_RETURN_VOID_TYPE sl@0: internal_visit(LhsT& lhs_content, int) sl@0: { sl@0: typedef typename has_nothrow_move_constructor::type sl@0: nothrow_move; sl@0: sl@0: backup_assign_impl( lhs_content, nothrow_move() ); sl@0: sl@0: BOOST_VARIANT_AUX_RETURN_VOID; sl@0: } sl@0: sl@0: }; sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // (detail) class swap_with sl@0: // sl@0: // Visitor that swaps visited value with content of given variant. sl@0: // sl@0: // Precondition: Given variant MUST have same logical type as visited value. sl@0: // sl@0: template sl@0: struct swap_with sl@0: : public static_visitor<> sl@0: { sl@0: private: // representation sl@0: sl@0: Variant& toswap_; sl@0: sl@0: public: // structors sl@0: sl@0: explicit swap_with(Variant& toswap) sl@0: : toswap_(toswap) sl@0: { sl@0: } sl@0: sl@0: public: // internal visitor interfaces sl@0: sl@0: template sl@0: void operator()(T& operand) const sl@0: { sl@0: // Since the precondition ensures types are same, get T... sl@0: known_get getter; sl@0: T& other = toswap_.apply_visitor(getter); sl@0: sl@0: // ...and swap: sl@0: ::boost::detail::variant::move_swap( operand, other ); sl@0: } sl@0: sl@0: }; sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // (detail) class reflect sl@0: // sl@0: // Generic static visitor that performs a typeid on the value it visits. sl@0: // sl@0: class reflect sl@0: : public static_visitor sl@0: { sl@0: public: // visitor interfaces sl@0: sl@0: template sl@0: const std::type_info& operator()(const T&) const sl@0: { sl@0: return typeid(T); sl@0: } sl@0: sl@0: }; sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // (detail) class comparer sl@0: // sl@0: // Generic static visitor that compares the content of the given lhs variant sl@0: // with the visited rhs content using Comp. sl@0: // sl@0: // Precondition: lhs.which() == rhs.which() sl@0: // sl@0: template sl@0: class comparer sl@0: : public static_visitor sl@0: { sl@0: private: // representation sl@0: sl@0: const Variant& lhs_; sl@0: sl@0: public: // structors sl@0: sl@0: explicit comparer(const Variant& lhs) sl@0: : lhs_(lhs) sl@0: { sl@0: } sl@0: sl@0: public: // visitor interfaces sl@0: sl@0: template sl@0: bool operator()(const T& rhs_content) const sl@0: { sl@0: // Since the precondition ensures lhs and rhs types are same, get T... sl@0: known_get getter; sl@0: const T& lhs_content = lhs_.apply_visitor(getter); sl@0: sl@0: // ...and compare lhs and rhs contents: sl@0: return Comp()(lhs_content, rhs_content); sl@0: } sl@0: sl@0: }; sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // (detail) class equal_comp sl@0: // sl@0: // Generic function object compares lhs with rhs using operator==. sl@0: // sl@0: struct equal_comp sl@0: { sl@0: template sl@0: bool operator()(const T& lhs, const T& rhs) const sl@0: { sl@0: return lhs == rhs; sl@0: } sl@0: }; sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // (detail) class less_comp sl@0: // sl@0: // Generic function object compares lhs with rhs using operator<. sl@0: // sl@0: struct less_comp sl@0: { sl@0: template sl@0: bool operator()(const T& lhs, const T& rhs) const sl@0: { sl@0: return lhs < rhs; sl@0: } sl@0: }; sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // (detail) class template invoke_visitor sl@0: // sl@0: // Internal visitor that invokes the given visitor using: sl@0: // * for wrappers (e.g., recursive_wrapper), the wrapper's held value. sl@0: // * for all other values, the value itself. sl@0: // sl@0: template sl@0: class invoke_visitor sl@0: { sl@0: private: // representation sl@0: sl@0: Visitor& visitor_; sl@0: sl@0: public: // visitor typedefs sl@0: sl@0: typedef typename Visitor::result_type sl@0: result_type; sl@0: sl@0: public: // structors sl@0: sl@0: explicit invoke_visitor(Visitor& visitor) sl@0: : visitor_(visitor) sl@0: { sl@0: } sl@0: sl@0: #if !defined(BOOST_NO_VOID_RETURNS) sl@0: sl@0: public: // internal visitor interfaces sl@0: sl@0: template sl@0: result_type internal_visit(T& operand, int) sl@0: { sl@0: return visitor_(operand); sl@0: } sl@0: sl@0: # if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0564)) sl@0: template sl@0: result_type internal_visit(const T& operand, int) sl@0: { sl@0: return visitor_(operand); sl@0: } sl@0: # endif sl@0: sl@0: #else // defined(BOOST_NO_VOID_RETURNS) sl@0: sl@0: private: // helpers, for internal visitor interfaces (below) sl@0: sl@0: template sl@0: BOOST_VARIANT_AUX_GENERIC_RESULT_TYPE(result_type) sl@0: visit_impl(T& operand, mpl::false_) sl@0: { sl@0: return visitor_(operand); sl@0: } sl@0: sl@0: template sl@0: BOOST_VARIANT_AUX_RETURN_VOID_TYPE sl@0: visit_impl(T& operand, mpl::true_) sl@0: { sl@0: visitor_(operand); sl@0: BOOST_VARIANT_AUX_RETURN_VOID; sl@0: } sl@0: sl@0: public: // internal visitor interfaces sl@0: sl@0: template sl@0: BOOST_VARIANT_AUX_GENERIC_RESULT_TYPE(result_type) sl@0: internal_visit(T& operand, int) sl@0: { sl@0: typedef typename is_same::type sl@0: has_void_result_type; sl@0: sl@0: return visit_impl(operand, has_void_result_type()); sl@0: } sl@0: sl@0: #endif // BOOST_NO_VOID_RETURNS) workaround sl@0: sl@0: public: // internal visitor interfaces, cont. sl@0: sl@0: template sl@0: BOOST_VARIANT_AUX_GENERIC_RESULT_TYPE(result_type) sl@0: internal_visit(boost::recursive_wrapper& operand, long) sl@0: { sl@0: return internal_visit( operand.get(), 1L ); sl@0: } sl@0: sl@0: template sl@0: BOOST_VARIANT_AUX_GENERIC_RESULT_TYPE(result_type) sl@0: internal_visit(const boost::recursive_wrapper& operand, long) sl@0: { sl@0: return internal_visit( operand.get(), 1L ); sl@0: } sl@0: sl@0: template sl@0: BOOST_VARIANT_AUX_GENERIC_RESULT_TYPE(result_type) sl@0: internal_visit(boost::detail::reference_content& operand, long) sl@0: { sl@0: return internal_visit( operand.get(), 1L ); sl@0: } sl@0: sl@0: template sl@0: BOOST_VARIANT_AUX_GENERIC_RESULT_TYPE(result_type) sl@0: internal_visit(const boost::detail::reference_content& operand, long) sl@0: { sl@0: return internal_visit( operand.get(), 1L ); sl@0: } sl@0: sl@0: template sl@0: BOOST_VARIANT_AUX_GENERIC_RESULT_TYPE(result_type) sl@0: internal_visit(boost::detail::variant::backup_holder& operand, long) sl@0: { sl@0: return internal_visit( operand.get(), 1L ); sl@0: } sl@0: sl@0: template sl@0: BOOST_VARIANT_AUX_GENERIC_RESULT_TYPE(result_type) sl@0: internal_visit(const boost::detail::variant::backup_holder& operand, long) sl@0: { sl@0: return internal_visit( operand.get(), 1L ); sl@0: } sl@0: sl@0: }; sl@0: sl@0: }} // namespace detail::variant sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // class template variant (concept inspired by Andrei Alexandrescu) sl@0: // sl@0: // See docs and boost/variant/variant_fwd.hpp for more information. sl@0: // sl@0: template < sl@0: typename T0_ sl@0: , BOOST_VARIANT_ENUM_SHIFTED_PARAMS(typename T) sl@0: > sl@0: class variant sl@0: { sl@0: private: // helpers, for typedefs (below) sl@0: sl@0: typedef variant wknd_self_t; sl@0: sl@0: struct is_recursive_ sl@0: : detail::variant::is_recursive_flag sl@0: { sl@0: }; sl@0: sl@0: typedef typename mpl::eval_if< sl@0: is_recursive_ sl@0: , T0_ sl@0: , mpl::identity< T0_ > sl@0: >::type unwrapped_T0_; sl@0: sl@0: struct is_sequence_based_ sl@0: : detail::variant::is_over_sequence sl@0: { sl@0: }; sl@0: sl@0: #if !defined(BOOST_VARIANT_NO_TYPE_SEQUENCE_SUPPORT) sl@0: sl@0: private: // helpers, for typedefs (below) sl@0: sl@0: typedef typename mpl::eval_if< sl@0: is_sequence_based_ sl@0: , unwrapped_T0_ // over_sequence<...>::type sl@0: , detail::variant::make_variant_list< sl@0: unwrapped_T0_ sl@0: , BOOST_VARIANT_ENUM_SHIFTED_PARAMS(T) sl@0: > sl@0: >::type specified_types; sl@0: sl@0: BOOST_STATIC_ASSERT(( sl@0: ::boost::mpl::not_< mpl::empty >::value sl@0: )); sl@0: sl@0: typedef typename mpl::eval_if< sl@0: is_recursive_ sl@0: , mpl::transform< sl@0: specified_types sl@0: , mpl::protect< sl@0: detail::variant::quoted_enable_recursive sl@0: > sl@0: > sl@0: , mpl::identity< specified_types > sl@0: >::type recursive_enabled_types; sl@0: sl@0: public: // public typedefs sl@0: sl@0: typedef typename mpl::transform< sl@0: recursive_enabled_types sl@0: , unwrap_recursive sl@0: >::type types; sl@0: sl@0: private: // internal typedefs sl@0: sl@0: typedef typename mpl::transform< sl@0: recursive_enabled_types sl@0: , mpl::protect< detail::make_reference_content<> > sl@0: >::type internal_types; sl@0: sl@0: typedef typename mpl::front< sl@0: internal_types sl@0: >::type internal_T0; sl@0: sl@0: #else // defined(BOOST_VARIANT_NO_TYPE_SEQUENCE_SUPPORT) sl@0: sl@0: private: // helpers, for typedefs (below) sl@0: sl@0: typedef unwrapped_T0_ T0; sl@0: sl@0: #define BOOST_VARIANT_AUX_ENABLE_RECURSIVE_TYPEDEFS(z,N,_) \ sl@0: typedef typename mpl::eval_if< \ sl@0: is_recursive_ \ sl@0: , detail::variant::enable_recursive< \ sl@0: BOOST_PP_CAT(T,N) \ sl@0: , wknd_self_t \ sl@0: > \ sl@0: , mpl::identity< BOOST_PP_CAT(T,N) > \ sl@0: >::type BOOST_PP_CAT(recursive_enabled_T,N); \ sl@0: /**/ sl@0: sl@0: BOOST_PP_REPEAT( sl@0: BOOST_VARIANT_LIMIT_TYPES sl@0: , BOOST_VARIANT_AUX_ENABLE_RECURSIVE_TYPEDEFS sl@0: , _ sl@0: ) sl@0: sl@0: #undef BOOST_VARIANT_AUX_ENABLE_RECURSIVE_TYPEDEFS sl@0: sl@0: #define BOOST_VARIANT_AUX_UNWRAP_RECURSIVE_TYPEDEFS(z,N,_) \ sl@0: typedef typename unwrap_recursive< \ sl@0: BOOST_PP_CAT(recursive_enabled_T,N) \ sl@0: >::type BOOST_PP_CAT(public_T,N); \ sl@0: /**/ sl@0: sl@0: BOOST_PP_REPEAT( sl@0: BOOST_VARIANT_LIMIT_TYPES sl@0: , BOOST_VARIANT_AUX_UNWRAP_RECURSIVE_TYPEDEFS sl@0: , _ sl@0: ) sl@0: sl@0: #undef BOOST_VARIANT_AUX_UNWRAP_RECURSIVE_TYPEDEFS sl@0: sl@0: public: // public typedefs sl@0: sl@0: typedef typename detail::variant::make_variant_list< sl@0: BOOST_VARIANT_ENUM_PARAMS(public_T) sl@0: >::type types; sl@0: sl@0: private: // helpers, for internal typedefs (below) sl@0: sl@0: #define BOOST_VARIANT_AUX_MAKE_REFERENCE_CONTENT_TYPEDEFS(z,N,_) \ sl@0: typedef detail::make_reference_content< \ sl@0: BOOST_PP_CAT(recursive_enabled_T,N) \ sl@0: >::type BOOST_PP_CAT(internal_T,N); \ sl@0: /**/ sl@0: sl@0: BOOST_PP_REPEAT( sl@0: BOOST_VARIANT_LIMIT_TYPES sl@0: , BOOST_VARIANT_AUX_MAKE_REFERENCE_CONTENT_TYPEDEFS sl@0: , _ sl@0: ) sl@0: sl@0: #undef BOOST_VARIANT_AUX_MAKE_REFERENCE_CONTENT_TYPEDEFS sl@0: sl@0: private: // internal typedefs sl@0: sl@0: typedef typename detail::variant::make_variant_list< sl@0: BOOST_VARIANT_ENUM_PARAMS(internal_T) sl@0: >::type internal_types; sl@0: sl@0: private: // static precondition assertions sl@0: sl@0: // NOTE TO USER : sl@0: // variant< type-sequence > syntax is not supported on this compiler! sl@0: // sl@0: BOOST_MPL_ASSERT_NOT(( is_sequence_based_ )); sl@0: sl@0: #endif // BOOST_VARIANT_NO_TYPE_SEQUENCE_SUPPORT workaround sl@0: sl@0: private: // helpers, for representation (below) sl@0: sl@0: typedef typename detail::variant::find_fallback_type< sl@0: internal_types sl@0: >::type fallback_type_result_; sl@0: sl@0: typedef typename fallback_type_result_::first sl@0: fallback_type_index_; sl@0: typedef typename fallback_type_result_::second sl@0: fallback_type_; sl@0: sl@0: struct has_fallback_type_ sl@0: : mpl::not_< sl@0: is_same< fallback_type_, detail::variant::no_fallback_type > sl@0: > sl@0: { sl@0: }; sl@0: sl@0: typedef has_fallback_type_ sl@0: never_uses_backup_flag; sl@0: sl@0: typedef typename detail::variant::make_storage< sl@0: internal_types, never_uses_backup_flag sl@0: >::type storage_t; sl@0: sl@0: private: // helpers, for representation (below) sl@0: sl@0: // which_ on: sl@0: // * [0, size) indicates stack content sl@0: // * [-size, 0) indicates pointer to heap backup sl@0: // if which_ >= 0: sl@0: // * then which() -> which_ sl@0: // * else which() -> -(which_ + 1) sl@0: sl@0: #if !defined(BOOST_VARIANT_MINIMIZE_SIZE) sl@0: sl@0: typedef int which_t; sl@0: sl@0: #else // defined(BOOST_VARIANT_MINIMIZE_SIZE) sl@0: sl@0: // [if O1_size available, then attempt which_t size optimization...] sl@0: // [select signed char if fewer than SCHAR_MAX types, else signed int:] sl@0: typedef typename mpl::eval_if< sl@0: mpl::equal_to< mpl::O1_size, mpl::long_<-1> > sl@0: , mpl::identity< int > sl@0: , mpl::if_< sl@0: mpl::less< mpl::O1_size, mpl::int_ > sl@0: , signed char sl@0: , int sl@0: > sl@0: >::type which_t; sl@0: sl@0: #endif // BOOST_VARIANT_MINIMIZE_SIZE switch sl@0: sl@0: // representation -- private when possible sl@0: #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) sl@0: private: sl@0: #else sl@0: public: sl@0: #endif sl@0: sl@0: which_t which_; sl@0: storage_t storage_; sl@0: sl@0: void indicate_which(int which) sl@0: { sl@0: which_ = static_cast( which ); sl@0: } sl@0: sl@0: void indicate_backup_which(int which) sl@0: { sl@0: which_ = static_cast( -(which + 1) ); sl@0: } sl@0: sl@0: private: // helpers, for queries (below) sl@0: sl@0: bool using_backup() const sl@0: { sl@0: return which_ < 0; sl@0: } sl@0: sl@0: public: // queries sl@0: sl@0: int which() const sl@0: { sl@0: // If using heap backup... sl@0: if (using_backup()) sl@0: // ...then return adjusted which_: sl@0: return -(which_ + 1); sl@0: sl@0: // Otherwise, return which_ directly: sl@0: return which_; sl@0: } sl@0: sl@0: private: // helpers, for structors (below) sl@0: sl@0: struct initializer sl@0: : BOOST_VARIANT_AUX_INITIALIZER_T( sl@0: recursive_enabled_types, recursive_enabled_T sl@0: ) sl@0: { sl@0: }; sl@0: sl@0: void destroy_content() sl@0: { sl@0: detail::variant::destroyer visitor; sl@0: this->internal_apply_visitor(visitor); sl@0: } sl@0: sl@0: public: // structors sl@0: sl@0: ~variant() sl@0: { sl@0: destroy_content(); sl@0: } sl@0: sl@0: variant() sl@0: { sl@0: // NOTE TO USER : sl@0: // Compile error from here indicates that the first bound sl@0: // type is not default-constructible, and so variant cannot sl@0: // support its own default-construction. sl@0: // sl@0: new( storage_.address() ) internal_T0(); sl@0: indicate_which(0); // zero is the index of the first bounded type sl@0: } sl@0: sl@0: private: // helpers, for structors, cont. (below) sl@0: sl@0: class convert_copy_into sl@0: : public static_visitor sl@0: { sl@0: private: // representation sl@0: sl@0: void* storage_; sl@0: sl@0: public: // structors sl@0: sl@0: explicit convert_copy_into(void* storage) sl@0: : storage_(storage) sl@0: { sl@0: } sl@0: sl@0: public: // internal visitor interfaces (below) sl@0: sl@0: template sl@0: int internal_visit(T& operand, int) const sl@0: { sl@0: // NOTE TO USER : sl@0: // Compile error here indicates one of the source variant's types sl@0: // cannot be unambiguously converted to the destination variant's sl@0: // types (or that no conversion exists). sl@0: // sl@0: return initializer::initialize(storage_, operand); sl@0: } sl@0: sl@0: # if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0564)) sl@0: template sl@0: result_type internal_visit(const T& operand, int) const sl@0: { sl@0: return initializer::initialize(storage_, operand); sl@0: } sl@0: # endif sl@0: sl@0: template sl@0: int internal_visit(boost::detail::reference_content& operand, long) const sl@0: { sl@0: return internal_visit( operand.get(), 1L ); sl@0: } sl@0: sl@0: template sl@0: int internal_visit(const boost::detail::reference_content& operand, long) const sl@0: { sl@0: return internal_visit( operand.get(), 1L ); sl@0: } sl@0: sl@0: template sl@0: int internal_visit(boost::detail::variant::backup_holder& operand, long) const sl@0: { sl@0: return internal_visit( operand.get(), 1L ); sl@0: } sl@0: sl@0: template sl@0: int internal_visit(const boost::detail::variant::backup_holder& operand, long) const sl@0: { sl@0: return internal_visit( operand.get(), 1L ); sl@0: } sl@0: sl@0: template sl@0: int internal_visit(boost::recursive_wrapper& operand, long) const sl@0: { sl@0: return internal_visit( operand.get(), 1L ); sl@0: } sl@0: sl@0: template sl@0: int internal_visit(const boost::recursive_wrapper& operand, long) const sl@0: { sl@0: return internal_visit( operand.get(), 1L ); sl@0: } sl@0: sl@0: }; sl@0: sl@0: friend class convert_copy_into; sl@0: sl@0: private: // helpers, for structors, below sl@0: sl@0: template sl@0: void convert_construct( sl@0: T& operand sl@0: , int sl@0: , mpl::false_ = mpl::false_() // is_foreign_variant sl@0: ) sl@0: { sl@0: // NOTE TO USER : sl@0: // Compile error here indicates that the given type is not sl@0: // unambiguously convertible to one of the variant's types sl@0: // (or that no conversion exists). sl@0: // sl@0: indicate_which( sl@0: initializer::initialize( sl@0: storage_.address() sl@0: , operand sl@0: ) sl@0: ); sl@0: } sl@0: sl@0: template sl@0: void convert_construct( sl@0: Variant& operand sl@0: , long sl@0: , mpl::true_// is_foreign_variant sl@0: ) sl@0: { sl@0: convert_copy_into visitor(storage_.address()); sl@0: indicate_which( sl@0: operand.internal_apply_visitor(visitor) sl@0: ); sl@0: } sl@0: sl@0: template sl@0: void convert_construct_variant(Variant& operand) sl@0: { sl@0: // [Determine if the given variant is itself a bounded type, or if its sl@0: // content needs to be converted (i.e., it is a 'foreign' variant):] sl@0: // sl@0: sl@0: typedef typename mpl::find_if< sl@0: types sl@0: , is_same< sl@0: add_const sl@0: , const Variant sl@0: > sl@0: >::type found_it; sl@0: sl@0: typedef typename mpl::end::type not_found; sl@0: typedef typename is_same< sl@0: found_it, not_found sl@0: >::type is_foreign_variant; sl@0: sl@0: // Convert construct from operand: sl@0: convert_construct( sl@0: operand, 1L sl@0: , is_foreign_variant() sl@0: ); sl@0: } sl@0: sl@0: template sl@0: void convert_construct( sl@0: boost::variant& operand sl@0: , long sl@0: ) sl@0: { sl@0: convert_construct_variant(operand); sl@0: } sl@0: sl@0: template sl@0: void convert_construct( sl@0: const boost::variant& operand sl@0: , long sl@0: ) sl@0: { sl@0: convert_construct_variant(operand); sl@0: } sl@0: sl@0: public: // structors, cont. sl@0: sl@0: #if !defined(BOOST_VARIANT_AUX_BROKEN_CONSTRUCTOR_TEMPLATE_ORDERING) sl@0: sl@0: template sl@0: variant(const T& operand) sl@0: { sl@0: convert_construct(operand, 1L); sl@0: } sl@0: sl@0: template sl@0: variant(T& operand) sl@0: { sl@0: convert_construct(operand, 1L); sl@0: } sl@0: sl@0: #elif defined(BOOST_VARIANT_AUX_HAS_CONSTRUCTOR_TEMPLATE_ORDERING_SFINAE_WKND) sl@0: sl@0: // For compilers that cannot distinguish between T& and const T& in sl@0: // template constructors, but do fully support SFINAE, we can workaround: sl@0: sl@0: template sl@0: variant(const T& operand) sl@0: { sl@0: convert_construct(operand, 1L); sl@0: } sl@0: sl@0: template sl@0: variant( sl@0: T& operand sl@0: , typename enable_if< sl@0: mpl::not_< is_const > sl@0: , void sl@0: >::type* = 0 sl@0: ) sl@0: { sl@0: convert_construct(operand, 1L); sl@0: } sl@0: sl@0: #else // !defined(BOOST_VARIANT_AUX_HAS_CONSTRUCTOR_TEMPLATE_ORDERING_SFINAE_WKND) sl@0: sl@0: // For compilers that cannot distinguish between T& and const T& in sl@0: // template constructors, and do NOT support SFINAE, we can't workaround: sl@0: sl@0: template sl@0: variant(const T& operand) sl@0: { sl@0: convert_construct(operand, 1L); sl@0: } sl@0: sl@0: #endif // BOOST_VARIANT_AUX_BROKEN_CONSTRUCTOR_TEMPLATE_ORDERING workarounds sl@0: sl@0: public: // structors, cont. sl@0: sl@0: // [MSVC6 requires copy constructor appear after template constructors] sl@0: variant(const variant& operand) sl@0: { sl@0: // Copy the value of operand into *this... sl@0: detail::variant::copy_into visitor( storage_.address() ); sl@0: operand.internal_apply_visitor(visitor); sl@0: sl@0: // ...and activate the *this's primary storage on success: sl@0: indicate_which(operand.which()); sl@0: } sl@0: sl@0: private: // helpers, for modifiers (below) sl@0: sl@0: # if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) sl@0: template sl@0: friend class detail::variant::backup_assigner; sl@0: # endif sl@0: sl@0: // class assigner sl@0: // sl@0: // Internal visitor that "assigns" the visited value to the given variant sl@0: // by appropriate destruction and copy-construction. sl@0: // sl@0: sl@0: class assigner sl@0: : public static_visitor<> sl@0: { sl@0: private: // representation sl@0: sl@0: variant& lhs_; sl@0: int rhs_which_; sl@0: sl@0: public: // structors sl@0: sl@0: assigner(variant& lhs, int rhs_which) sl@0: : lhs_(lhs) sl@0: , rhs_which_(rhs_which) sl@0: { sl@0: } sl@0: sl@0: private: // helpers, for internal visitor interface (below) sl@0: sl@0: template sl@0: void assign_impl( sl@0: const RhsT& rhs_content sl@0: , mpl::true_// has_nothrow_copy sl@0: , B1// has_nothrow_move_constructor sl@0: , B2// has_fallback_type sl@0: ) sl@0: { sl@0: // Destroy lhs's content... sl@0: lhs_.destroy_content(); // nothrow sl@0: sl@0: // ...copy rhs content into lhs's storage... sl@0: new(lhs_.storage_.address()) sl@0: RhsT( rhs_content ); // nothrow sl@0: sl@0: // ...and indicate new content type: sl@0: lhs_.indicate_which(rhs_which_); // nothrow sl@0: } sl@0: sl@0: template sl@0: void assign_impl( sl@0: const RhsT& rhs_content sl@0: , mpl::false_// has_nothrow_copy sl@0: , mpl::true_// has_nothrow_move_constructor sl@0: , B// has_fallback_type sl@0: ) sl@0: { sl@0: // Attempt to make a temporary copy (so as to move it below)... sl@0: RhsT temp(rhs_content); sl@0: sl@0: // ...and upon success destroy lhs's content... sl@0: lhs_.destroy_content(); // nothrow sl@0: sl@0: // ...move the temporary copy into lhs's storage... sl@0: new(lhs_.storage_.address()) sl@0: RhsT( detail::variant::move(temp) ); // nothrow sl@0: sl@0: // ...and indicate new content type: sl@0: lhs_.indicate_which(rhs_which_); // nothrow sl@0: } sl@0: sl@0: template sl@0: void assign_impl( sl@0: const RhsT& rhs_content sl@0: , mpl::false_// has_nothrow_copy sl@0: , mpl::false_// has_nothrow_move_constructor sl@0: , mpl::true_// has_fallback_type sl@0: ) sl@0: { sl@0: // Destroy lhs's content... sl@0: lhs_.destroy_content(); // nothrow sl@0: sl@0: try sl@0: { sl@0: // ...and attempt to copy rhs's content into lhs's storage: sl@0: new(lhs_.storage_.address()) sl@0: RhsT( rhs_content ); sl@0: } sl@0: catch (...) sl@0: { sl@0: // In case of failure, default-construct fallback type in lhs's storage... sl@0: new (lhs_.storage_.address()) sl@0: fallback_type_; // nothrow sl@0: sl@0: // ...indicate construction of fallback type... sl@0: lhs_.indicate_which( sl@0: BOOST_MPL_AUX_VALUE_WKND(fallback_type_index_)::value sl@0: ); // nothrow sl@0: sl@0: // ...and rethrow: sl@0: throw; sl@0: } sl@0: sl@0: // In the event of success, indicate new content type: sl@0: lhs_.indicate_which(rhs_which_); // nothrow sl@0: } sl@0: sl@0: template sl@0: void assign_impl( sl@0: const RhsT& rhs_content sl@0: , mpl::false_// has_nothrow_copy sl@0: , mpl::false_// has_nothrow_move_constructor sl@0: , mpl::false_// has_fallback_type sl@0: ) sl@0: { sl@0: detail::variant::backup_assigner sl@0: visitor(lhs_, rhs_which_, rhs_content); sl@0: lhs_.internal_apply_visitor(visitor); sl@0: } sl@0: sl@0: public: // internal visitor interfaces sl@0: sl@0: template sl@0: BOOST_VARIANT_AUX_RETURN_VOID_TYPE sl@0: internal_visit(const RhsT& rhs_content, int) sl@0: { sl@0: typedef typename has_nothrow_copy::type sl@0: nothrow_copy; sl@0: typedef typename mpl::or_< // reduces compile-time sl@0: nothrow_copy sl@0: , detail::variant::has_nothrow_move_constructor sl@0: >::type nothrow_move_constructor; sl@0: sl@0: assign_impl( sl@0: rhs_content sl@0: , nothrow_copy() sl@0: , nothrow_move_constructor() sl@0: , has_fallback_type_() sl@0: ); sl@0: sl@0: BOOST_VARIANT_AUX_RETURN_VOID; sl@0: } sl@0: sl@0: }; sl@0: sl@0: friend class assigner; sl@0: sl@0: void variant_assign(const variant& rhs) sl@0: { sl@0: // If the contained types are EXACTLY the same... sl@0: if (which_ == rhs.which_) sl@0: { sl@0: // ...then assign rhs's storage to lhs's content: sl@0: detail::variant::assign_storage visitor(rhs.storage_.address()); sl@0: this->internal_apply_visitor(visitor); sl@0: } sl@0: else sl@0: { sl@0: // Otherwise, perform general (copy-based) variant assignment: sl@0: assigner visitor(*this, rhs.which()); sl@0: rhs.internal_apply_visitor(visitor); sl@0: } sl@0: } sl@0: sl@0: private: // helpers, for modifiers (below) sl@0: sl@0: template sl@0: void assign(const T& rhs) sl@0: { sl@0: // If direct T-to-T assignment is not possible... sl@0: detail::variant::direct_assigner direct_assign(rhs); sl@0: if (this->apply_visitor(direct_assign) == false) sl@0: { sl@0: // ...then convert rhs to variant and assign: sl@0: // sl@0: // While potentially inefficient, the following construction of a sl@0: // variant allows T as any type convertible to one of the bounded sl@0: // types without excessive code redundancy. sl@0: // sl@0: variant temp(rhs); sl@0: variant_assign( detail::variant::move(temp) ); sl@0: } sl@0: } sl@0: sl@0: public: // modifiers sl@0: sl@0: template sl@0: variant& operator=(const T& rhs) sl@0: { sl@0: assign(rhs); sl@0: return *this; sl@0: } sl@0: sl@0: // [MSVC6 requires copy assign appear after templated operator=] sl@0: variant& operator=(const variant& rhs) sl@0: { sl@0: variant_assign(rhs); sl@0: return *this; sl@0: } sl@0: sl@0: void swap(variant& rhs) sl@0: { sl@0: // If the contained types are the same... sl@0: if (which() == rhs.which()) sl@0: { sl@0: // ...then swap the values directly: sl@0: detail::variant::swap_with visitor(rhs); sl@0: this->apply_visitor(visitor); sl@0: } sl@0: else sl@0: { sl@0: // ...otherwise, perform general variant swap: sl@0: variant tmp( detail::variant::move(rhs) ); sl@0: rhs = detail::variant::move(*this); sl@0: *this = detail::variant::move(tmp); sl@0: } sl@0: } sl@0: sl@0: public: // queries sl@0: sl@0: // sl@0: // NOTE: member which() defined above. sl@0: // sl@0: sl@0: bool empty() const sl@0: { sl@0: return false; sl@0: } sl@0: sl@0: const std::type_info& type() const sl@0: { sl@0: detail::variant::reflect visitor; sl@0: return this->apply_visitor(visitor); sl@0: } sl@0: sl@0: public: // prevent comparison with foreign types sl@0: sl@0: #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1300) sl@0: sl@0: # define BOOST_VARIANT_AUX_FAIL_COMPARISON_RETURN_TYPE \ sl@0: void sl@0: sl@0: #else // MSVC7 sl@0: sl@0: // sl@0: // MSVC7 gives error about return types for above being different than sl@0: // the true comparison operator overloads: sl@0: // sl@0: sl@0: # define BOOST_VARIANT_AUX_FAIL_COMPARISON_RETURN_TYPE \ sl@0: bool sl@0: sl@0: #endif // MSVC7 workaround sl@0: sl@0: template sl@0: BOOST_VARIANT_AUX_FAIL_COMPARISON_RETURN_TYPE sl@0: operator==(const U&) const sl@0: { sl@0: BOOST_STATIC_ASSERT( false && sizeof(U) ); sl@0: } sl@0: sl@0: template sl@0: BOOST_VARIANT_AUX_FAIL_COMPARISON_RETURN_TYPE sl@0: operator<(const U&) const sl@0: { sl@0: BOOST_STATIC_ASSERT( false && sizeof(U) ); sl@0: } sl@0: sl@0: public: // comparison operators sl@0: sl@0: // [MSVC6 requires these operators appear after template operators] sl@0: sl@0: bool operator==(const variant& rhs) const sl@0: { sl@0: if (this->which() != rhs.which()) sl@0: return false; sl@0: sl@0: detail::variant::comparer< sl@0: variant, detail::variant::equal_comp sl@0: > visitor(*this); sl@0: return rhs.apply_visitor(visitor); sl@0: } sl@0: sl@0: bool operator<(const variant& rhs) const sl@0: { sl@0: // sl@0: // Dirk Schreib suggested this collating order. sl@0: // sl@0: sl@0: if (this->which() != rhs.which()) sl@0: return this->which() < rhs.which(); sl@0: sl@0: detail::variant::comparer< sl@0: variant, detail::variant::less_comp sl@0: > visitor(*this); sl@0: return rhs.apply_visitor(visitor); sl@0: } sl@0: sl@0: // helpers, for visitation support (below) -- private when possible sl@0: #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) sl@0: sl@0: template < BOOST_VARIANT_ENUM_PARAMS(typename U) > sl@0: friend class variant; sl@0: sl@0: private: sl@0: sl@0: #else// defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) sl@0: sl@0: public: sl@0: sl@0: #endif// !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) sl@0: sl@0: template sl@0: static sl@0: BOOST_VARIANT_AUX_GENERIC_RESULT_TYPE( sl@0: typename Visitor::result_type sl@0: ) sl@0: internal_apply_visitor_impl( sl@0: int internal_which sl@0: , int logical_which sl@0: , Visitor& visitor sl@0: , VoidPtrCV storage sl@0: ) sl@0: { sl@0: typedef mpl::int_<0> first_which; sl@0: typedef typename mpl::begin::type first_it; sl@0: typedef typename mpl::end::type last_it; sl@0: sl@0: typedef detail::variant::visitation_impl_step< sl@0: first_it, last_it sl@0: > first_step; sl@0: sl@0: return detail::variant::visitation_impl( sl@0: internal_which, logical_which sl@0: , visitor, storage, mpl::false_() sl@0: , never_uses_backup_flag() sl@0: , static_cast(0), static_cast(0) sl@0: ); sl@0: } sl@0: sl@0: template sl@0: BOOST_VARIANT_AUX_GENERIC_RESULT_TYPE( sl@0: typename Visitor::result_type sl@0: ) sl@0: internal_apply_visitor(Visitor& visitor) sl@0: { sl@0: return internal_apply_visitor_impl( sl@0: which_, which(), visitor, storage_.address() sl@0: ); sl@0: } sl@0: sl@0: template sl@0: BOOST_VARIANT_AUX_GENERIC_RESULT_TYPE( sl@0: typename Visitor::result_type sl@0: ) sl@0: internal_apply_visitor(Visitor& visitor) const sl@0: { sl@0: return internal_apply_visitor_impl( sl@0: which_, which(), visitor, storage_.address() sl@0: ); sl@0: } sl@0: sl@0: public: // visitation support sl@0: sl@0: template sl@0: BOOST_VARIANT_AUX_GENERIC_RESULT_TYPE( sl@0: typename Visitor::result_type sl@0: ) sl@0: apply_visitor(Visitor& visitor) sl@0: { sl@0: detail::variant::invoke_visitor invoker(visitor); sl@0: return this->internal_apply_visitor(invoker); sl@0: } sl@0: sl@0: template sl@0: BOOST_VARIANT_AUX_GENERIC_RESULT_TYPE( sl@0: typename Visitor::result_type sl@0: ) sl@0: apply_visitor(Visitor& visitor) const sl@0: { sl@0: detail::variant::invoke_visitor invoker(visitor); sl@0: return this->internal_apply_visitor(invoker); sl@0: } sl@0: sl@0: }; // class variant sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // metafunction make_variant_over sl@0: // sl@0: // See docs and boost/variant/variant_fwd.hpp for more information. sl@0: // sl@0: template sl@0: struct make_variant_over sl@0: { sl@0: private: // precondition assertions sl@0: sl@0: #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1300) sl@0: BOOST_STATIC_ASSERT(( ::boost::mpl::is_sequence::value )); sl@0: #endif sl@0: sl@0: public: // metafunction result sl@0: sl@0: typedef variant< sl@0: detail::variant::over_sequence< Types > sl@0: > type; sl@0: sl@0: }; sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // function template swap sl@0: // sl@0: // Swaps two variants of the same type (i.e., identical specification). sl@0: // sl@0: template < BOOST_VARIANT_ENUM_PARAMS(typename T) > sl@0: inline void swap( sl@0: variant< BOOST_VARIANT_ENUM_PARAMS(T) >& lhs sl@0: , variant< BOOST_VARIANT_ENUM_PARAMS(T) >& rhs sl@0: ) sl@0: { sl@0: lhs.swap(rhs); sl@0: } sl@0: sl@0: } // namespace boost sl@0: sl@0: // implementation additions sl@0: #include "boost/variant/detail/variant_io.hpp" sl@0: sl@0: #endif // BOOST_VARIANT_VARIANT_HPP