1 //-----------------------------------------------------------------------------
2 // boost variant/detail/initializer.hpp header file
3 // See http://www.boost.org for updates, documentation, and revision history.
4 //-----------------------------------------------------------------------------
6 // Copyright (c) 2002-2003
7 // Eric Friedman, Itay Maman
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)
13 #ifndef BOOST_VARIANT_DETAIL_INITIALIZER_HPP
14 #define BOOST_VARIANT_DETAIL_INITIALIZER_HPP
16 #include <new> // for placement new
18 #include <boost/config.hpp>
20 #include <boost/call_traits.hpp>
21 #include <boost/detail/reference_content.hpp>
22 #include <boost/variant/recursive_wrapper_fwd.hpp>
23 #include <boost/variant/detail/move.hpp>
25 #if !defined(BOOST_NO_USING_DECLARATION_OVERLOADS_FROM_TYPENAME_BASE)
26 # include <boost/mpl/aux_/value_wknd.hpp>
27 # include <boost/mpl/int.hpp>
28 # include <boost/mpl/iter_fold.hpp>
29 # include <boost/mpl/next.hpp>
30 # include <boost/mpl/deref.hpp>
31 # include <boost/mpl/pair.hpp>
32 # include <boost/mpl/protect.hpp>
34 # include <boost/variant/variant_fwd.hpp>
35 # include <boost/preprocessor/cat.hpp>
36 # include <boost/preprocessor/enum.hpp>
37 # include <boost/preprocessor/repeat.hpp>
41 namespace detail { namespace variant {
43 ///////////////////////////////////////////////////////////////////////////////
44 // (detail) support to simulate standard overload resolution rules
46 // The below initializers allows variant to follow standard overload
47 // resolution rules over the specified set of bounded types.
49 // On compilers where using declarations in class templates can correctly
50 // avoid name hiding, use an optimal solution based on the variant's typelist.
52 // Otherwise, use a preprocessor workaround based on knowledge of the fixed
53 // size of the variant's psuedo-variadic template parameter list.
56 #if !defined(BOOST_NO_USING_DECLARATION_OVERLOADS_FROM_TYPENAME_BASE)
58 // (detail) quoted metafunction make_initializer_node
60 // Exposes a pair whose first type is a node in the initializer hierarchy.
62 struct make_initializer_node
64 template <typename BaseIndexPair, typename Iterator>
67 private: // helpers, for metafunction result (below)
69 typedef typename BaseIndexPair::first
71 typedef typename BaseIndexPair::second
74 class initializer_node
77 private: // helpers, for static functions (below)
79 typedef typename mpl::deref<Iterator>::type
81 typedef typename unwrap_recursive<recursive_enabled_T>::type
84 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
85 typedef boost::is_reference<public_T>
86 is_reference_content_t;
88 typedef typename boost::mpl::if_<is_reference_content_t, public_T, const public_T& >::type
91 template <class T> struct disable_overload{};
93 typedef typename boost::mpl::if_<is_reference_content_t, disable_overload<public_T>, public_T&& >::type
96 typedef typename call_traits<public_T>::param_type
100 public: // static functions
102 using base::initialize;
104 static int initialize(void* dest, param_T operand)
106 typedef typename boost::detail::make_reference_content<
110 new(dest) internal_T(operand);
111 return BOOST_MPL_AUX_VALUE_WKND(index)::value; // which
114 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
115 static int initialize(void* dest, param2_T operand)
117 // This assert must newer trigger, because all the reference contents are
118 // handled by the initilize(void* dest, param_T operand) function above
119 BOOST_ASSERT(!is_reference_content_t::value);
121 typedef typename boost::mpl::if_<is_reference_content_t, param2_T, recursive_enabled_T>::type value_T;
122 new(dest) value_T( boost::detail::variant::move(operand) );
123 return BOOST_MPL_AUX_VALUE_WKND(index)::value; // which
128 friend class initializer_node;
130 public: // metafunction result
134 , typename mpl::next< index >::type
140 // (detail) class initializer_root
142 // Every level of the initializer hierarchy must expose the name
143 // "initialize," so initializer_root provides a dummy function:
145 class initializer_root
147 public: // static functions
149 static void initialize();
153 #else // defined(BOOST_NO_USING_DECLARATION_OVERLOADS_FROM_TYPENAME_BASE)
156 #define BOOST_VARIANT_AUX_PP_INITIALIZER_TEMPLATE_PARAMS \
157 BOOST_VARIANT_ENUM_PARAMS(typename recursive_enabled_T) \
161 #define BOOST_VARIANT_AUX_PP_INITIALIZER_DEFINE_PARAM_T(N) \
162 typedef typename unwrap_recursive< \
163 BOOST_PP_CAT(recursive_enabled_T,N) \
164 >::type BOOST_PP_CAT(public_T,N); \
165 typedef typename call_traits< \
166 BOOST_PP_CAT(public_T,N) \
167 >::param_type BOOST_PP_CAT(param_T,N); \
170 template < BOOST_VARIANT_ENUM_PARAMS(typename recursive_enabled_T) >
171 struct preprocessor_list_initializer
173 public: // static functions
175 #define BOOST_VARIANT_AUX_PP_INITIALIZE_FUNCTION(z,N,_) \
176 typedef typename unwrap_recursive< \
177 BOOST_PP_CAT(recursive_enabled_T,N) \
178 >::type BOOST_PP_CAT(public_T,N); \
179 typedef typename call_traits< \
180 BOOST_PP_CAT(public_T,N) \
181 >::param_type BOOST_PP_CAT(param_T,N); \
182 static int initialize( \
184 , BOOST_PP_CAT(param_T,N) operand \
187 typedef typename boost::detail::make_reference_content< \
188 BOOST_PP_CAT(recursive_enabled_T,N) \
189 >::type internal_T; \
191 new(dest) internal_T(operand); \
192 return (N); /*which*/ \
197 BOOST_VARIANT_LIMIT_TYPES
198 , BOOST_VARIANT_AUX_PP_INITIALIZE_FUNCTION
202 #undef BOOST_VARIANT_AUX_PP_INITIALIZE_FUNCTION
206 #endif // BOOST_NO_USING_DECLARATION_OVERLOADS_FROM_TYPENAME_BASE workaround
208 }} // namespace detail::variant
211 ///////////////////////////////////////////////////////////////////////////////
212 // macro BOOST_VARIANT_AUX_INITIALIZER_T
214 // Given both the variant's typelist and a basename for forming the list of
215 // bounded types (i.e., T becomes T1, T2, etc.), exposes the initializer
216 // most appropriate to the current compiler.
219 #if !defined(BOOST_NO_USING_DECLARATION_OVERLOADS_FROM_TYPENAME_BASE)
221 #define BOOST_VARIANT_AUX_INITIALIZER_T( mpl_seq, typename_base ) \
222 ::boost::mpl::iter_fold< \
224 , ::boost::mpl::pair< \
225 ::boost::detail::variant::initializer_root \
226 , ::boost::mpl::int_<0> \
228 , ::boost::mpl::protect< \
229 ::boost::detail::variant::make_initializer_node \
234 #else // defined(BOOST_NO_USING_DECLARATION_OVERLOADS_FROM_TYPENAME_BASE)
237 #define BOOST_VARIANT_AUX_PP_INITIALIZER_TEMPLATE_ARGS(typename_base) \
238 BOOST_VARIANT_ENUM_PARAMS(typename_base) \
241 #define BOOST_VARIANT_AUX_INITIALIZER_T( mpl_seq, typename_base ) \
242 ::boost::detail::variant::preprocessor_list_initializer< \
243 BOOST_VARIANT_ENUM_PARAMS(typename_base) \
247 #endif // BOOST_NO_USING_DECLARATION_OVERLOADS_FROM_TYPENAME_BASE workaround
249 #endif // BOOST_VARIANT_DETAIL_INITIALIZER_HPP