📄 variant.hpp
字号:
//-----------------------------------------------------------------------------// boost variant/variant.hpp header file// See http://www.boost.org for updates, documentation, and revision history.//-----------------------------------------------------------------------------//// Copyright (c) 2002-2003// Eric Friedman, Itay Maman//// Distributed under the Boost Software License, Version 1.0. (See// accompanying file LICENSE_1_0.txt or copy at// http://www.boost.org/LICENSE_1_0.txt)#ifndef BOOST_VARIANT_VARIANT_HPP#define BOOST_VARIANT_VARIANT_HPP#include <cstddef> // for std::size_t#include <new> // for placement new#include <typeinfo> // for typeid, std::type_info#include "boost/variant/detail/config.hpp"#include "boost/mpl/aux_/config/eti.hpp"#include "boost/mpl/aux_/value_wknd.hpp"#include "boost/variant/variant_fwd.hpp"#include "boost/variant/detail/backup_holder.hpp"#include "boost/variant/detail/enable_recursive_fwd.hpp"#include "boost/variant/detail/forced_return.hpp"#include "boost/variant/detail/initializer.hpp"#include "boost/variant/detail/make_variant_list.hpp"#include "boost/variant/detail/over_sequence.hpp"#include "boost/variant/detail/visitation_impl.hpp"#include "boost/variant/detail/generic_result_type.hpp"#include "boost/variant/detail/has_nothrow_move.hpp"#include "boost/variant/detail/move.hpp"#include "boost/detail/reference_content.hpp"#include "boost/aligned_storage.hpp"#include "boost/blank.hpp"#include "boost/static_assert.hpp"#include "boost/preprocessor/cat.hpp"#include "boost/preprocessor/repeat.hpp"#include "boost/type_traits/alignment_of.hpp"#include "boost/type_traits/add_const.hpp"#include "boost/type_traits/has_nothrow_constructor.hpp"#include "boost/type_traits/has_nothrow_copy.hpp"#include "boost/type_traits/is_const.hpp"#include "boost/type_traits/is_same.hpp"#include "boost/utility/enable_if.hpp"#include "boost/variant/recursive_wrapper_fwd.hpp"#include "boost/variant/static_visitor.hpp"#include "boost/mpl/eval_if.hpp"#include "boost/mpl/begin_end.hpp"#include "boost/mpl/bool.hpp"#include "boost/mpl/empty.hpp"#include "boost/mpl/find_if.hpp"#include "boost/mpl/front.hpp"#include "boost/mpl/identity.hpp"#include "boost/mpl/if.hpp"#include "boost/mpl/int.hpp"#include "boost/mpl/is_sequence.hpp"#include "boost/mpl/iterator_range.hpp"#include "boost/mpl/iter_fold_if.hpp"#include "boost/mpl/logical.hpp"#include "boost/mpl/max_element.hpp"#include "boost/mpl/next.hpp"#include "boost/mpl/deref.hpp"#include "boost/mpl/pair.hpp"#include "boost/mpl/protect.hpp"#include "boost/mpl/push_front.hpp"#include "boost/mpl/same_as.hpp"#include "boost/mpl/size_t.hpp"#include "boost/mpl/sizeof.hpp"#include "boost/mpl/transform.hpp"#include "boost/mpl/assert.hpp"///////////////////////////////////////////////////////////////////////////////// Implementation Macros://// BOOST_VARIANT_VISITATION_UNROLLING_LIMIT// Defined in boost/variant/detail/visitation_impl.hpp.//// BOOST_VARIANT_MINIMIZE_SIZE// When #defined, implementation employs all known means to minimize the// size of variant obje cts. However, often unsuccessful due to alignment// issues, and potentially harmful to runtime speed, so not enabled by// default. (TODO: Investigate further.)#if defined(BOOST_VARIANT_MINIMIZE_SIZE)# include <climits> // for SCHAR_MAX# include "boost/mpl/eval_if.hpp"# include "boost/mpl/equal_to.hpp"# include "boost/mpl/identity.hpp"# include "boost/mpl/int.hpp"# include "boost/mpl/if.hpp"# include "boost/mpl/less.hpp"# include "boost/mpl/long.hpp"# include "boost/mpl/O1_size.hpp"#endifnamespace boost {namespace detail { namespace variant {///////////////////////////////////////////////////////////////////////////////// (detail) metafunction max_value//// Finds the maximum value of the unary metafunction F over Sequence.//template <typename Sequence, typename F>struct max_value{private: // helpers, for metafunction result (below) typedef typename mpl::transform1<Sequence, F>::type transformed_; typedef typename mpl::max_element<transformed_ >::type max_it;public: // metafunction result typedef typename mpl::deref<max_it>::type type;};///////////////////////////////////////////////////////////////////////////////// (detail) metafunction find_fallback_type//// Provides a fallback (i.e., nothrow default-constructible) type from the// specified sequence, or no_fallback_type if not found.//// This implementation is designed to prefer boost::blank over other potential// fallback types, regardless of its position in the specified sequence.//class no_fallback_type;struct find_fallback_type_pred{ template <typename Iterator> struct apply { private: typedef typename mpl::deref<Iterator>::type t_; public: typedef mpl::not_< has_nothrow_constructor<t_> > type; };};template <typename Types>struct find_fallback_type{private: // helpers, for metafunction result (below) typedef typename mpl::end<Types>::type end_it; // [Find the first suitable fallback type...] typedef typename mpl::iter_fold_if< Types , mpl::int_<0>, mpl::protect< mpl::next<> > , mpl::protect< find_fallback_type_pred > >::type first_result_; typedef typename first_result_::first first_result_index; typedef typename first_result_::second first_result_it; // [...now search the rest of the sequence for boost::blank...] typedef typename mpl::iter_fold_if< mpl::iterator_range< first_result_it,end_it > , first_result_index, mpl::protect< mpl::next<> > , mpl::protect< mpl::not_same_as<boost::blank> > >::type second_result_; typedef typename second_result_::second second_result_it;public: // metafunction result // [...and return the results of the search:] typedef typename mpl::eval_if< is_same< second_result_it,end_it > , mpl::if_< is_same< first_result_it,end_it > , mpl::pair< no_fallback_type,no_fallback_type > , first_result_ > , mpl::identity< second_result_ > >::type type;};#if defined(BOOST_MPL_CFG_MSVC_60_ETI_BUG)template<>struct find_fallback_type<int>{ typedef mpl::pair< no_fallback_type,no_fallback_type > type;};#endif // BOOST_MPL_CFG_MSVC_60_ETI_BUG workaround///////////////////////////////////////////////////////////////////////////////// (detail) metafunction make_storage//// Provides an aligned storage type capable of holding any of the types// specified in the given type-sequence.//template <typename Types, typename NeverUsesBackupFlag>struct make_storage{private: // helpers, for metafunction result (below) typedef typename mpl::eval_if< NeverUsesBackupFlag , mpl::identity< Types > , mpl::push_front< Types, backup_holder<void*> > >::type types; typedef typename max_value< types, mpl::sizeof_<mpl::_1> >::type max_size;#if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0551)) typedef typename max_value< types, alignment_of<mpl::_1> >::type max_alignment;#else // borland // temporary workaround -- use maximal alignment typedef mpl::size_t< -1 > max_alignment;#endif // borland workaroundpublic: // metafunction result#if !BOOST_WORKAROUND(BOOST_MSVC, <= 1300) typedef ::boost::aligned_storage< BOOST_MPL_AUX_VALUE_WKND(max_size)::value , BOOST_MPL_AUX_VALUE_WKND(max_alignment)::value > type;#else // MSVC7 and below BOOST_STATIC_CONSTANT(std::size_t, msvc_max_size_c = max_size::value); BOOST_STATIC_CONSTANT(std::size_t, msvc_max_alignment_c = max_alignment::value); typedef ::boost::aligned_storage< msvc_max_size_c , msvc_max_alignment_c > type;#endif // MSVC workaround};#if defined(BOOST_MPL_CFG_MSVC_60_ETI_BUG)template<>struct make_storage<int,int>{ typedef int type;};#endif // BOOST_MPL_CFG_MSVC_60_ETI_BUG workaround///////////////////////////////////////////////////////////////////////////////// (detail) class destroyer//// Internal visitor that destroys the value it visits.//struct destroyer : public static_visitor<>{public: // visitor interfaces template <typename T> BOOST_VARIANT_AUX_RETURN_VOID_TYPE internal_visit(T& operand, int) const { operand.~T();#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0551)) operand; // suppresses warnings#endif BOOST_VARIANT_AUX_RETURN_VOID; }};///////////////////////////////////////////////////////////////////////////////// (detail) class template known_get//// Visitor that returns a reference to content of the specified type.//// Precondition: visited variant MUST contain logical content of type T.//template <typename T>class known_get : public static_visitor<T&>{#if !BOOST_WORKAROUND(BOOST_MSVC, <= 1200)public: // visitor interface T& operator()(T& operand) const { return operand; } template <typename U> T& operator()(U&) const { // logical error to be here: see precondition above BOOST_ASSERT(false); return ::boost::detail::variant::forced_return< T& >(); }#else // MSVC6private: // helpers, for visitor interface (below) T& execute(T& operand, mpl::true_) const { return operand; } template <typename U> T& execute(U& operand, mpl::false_) const { // logical error to be here: see precondition above BOOST_ASSERT(false); return ::boost::detail::variant::forced_return< T& >(); }public: // visitor interface template <typename U> T& operator()(U& operand) const { typedef typename is_same< U,T >::type U_is_T; return execute(operand, U_is_T()); }#endif // MSVC6 workaround};///////////////////////////////////////////////////////////////////////////////// (detail) class copy_into//// Internal visitor that copies the value it visits into the given buffer.//class copy_into : public static_visitor<>{private: // representation void* storage_;public: // structors explicit copy_into(void* storage) : storage_(storage) { }public: // internal visitor interface template <typename T> BOOST_VARIANT_AUX_RETURN_VOID_TYPE internal_visit(boost::detail::variant::backup_holder<T>& operand, long) const { new(storage_) T( operand.get() ); BOOST_VARIANT_AUX_RETURN_VOID; } template <typename T> BOOST_VARIANT_AUX_RETURN_VOID_TYPE internal_visit(const boost::detail::variant::backup_holder<T>& operand, long) const { new(storage_) T( operand.get() ); BOOST_VARIANT_AUX_RETURN_VOID; } template <typename T> BOOST_VARIANT_AUX_RETURN_VOID_TYPE internal_visit(const T& operand, int) const { new(storage_) T(operand); BOOST_VARIANT_AUX_RETURN_VOID; }};///////////////////////////////////////////////////////////////////////////////// (detail) class assign_storage//// Internal visitor that assigns the given storage (which must be a// constructed value of the same type) to the value it visits.//struct assign_storage : public static_visitor<>{private: // representation const void* rhs_storage_;public: // structors explicit assign_storage(const void* rhs_storage) : rhs_storage_(rhs_storage) { }public: // internal visitor interfaces template <typename T> BOOST_VARIANT_AUX_RETURN_VOID_TYPE internal_visit(backup_holder<T>& lhs_content, long) const { lhs_content.get() = static_cast< const backup_holder<T>* >(rhs_storage_)->get(); BOOST_VARIANT_AUX_RETURN_VOID; } template <typename T> BOOST_VARIANT_AUX_RETURN_VOID_TYPE internal_visit(const backup_holder<T>& lhs_content, long) const { lhs_content.get() = static_cast< const backup_holder<T>* >(rhs_storage_)->get(); BOOST_VARIANT_AUX_RETURN_VOID; } template <typename T> BOOST_VARIANT_AUX_RETURN_VOID_TYPE internal_visit(T& lhs_content, int) const { // NOTE TO USER : // Compile error here indicates one of variant's bounded types does // not meet the requirements of the Assignable concept. Thus, // variant is not Assignable. //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -