1 //-----------------------------------------------------------------------------
2 // boost variant/detail/visitation_impl.hpp header file
3 // See http://www.boost.org for updates, documentation, and revision history.
4 //-----------------------------------------------------------------------------
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_VISITATION_IMPL_HPP
14 #define BOOST_VARIANT_DETAIL_VISITATION_IMPL_HPP
16 #include <boost/config.hpp>
18 #include <boost/variant/detail/backup_holder.hpp>
19 #include <boost/variant/detail/cast_storage.hpp>
20 #include <boost/variant/detail/forced_return.hpp>
21 #include <boost/variant/variant_fwd.hpp> // for BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES
23 #include <boost/mpl/eval_if.hpp>
24 #include <boost/mpl/bool.hpp>
25 #include <boost/mpl/identity.hpp>
26 #include <boost/mpl/int.hpp>
27 #include <boost/mpl/next.hpp>
28 #include <boost/mpl/deref.hpp>
29 #include <boost/mpl/or.hpp>
30 #include <boost/preprocessor/cat.hpp>
31 #include <boost/preprocessor/inc.hpp>
32 #include <boost/preprocessor/repeat.hpp>
33 #include <boost/type_traits/is_same.hpp>
34 #include <boost/type_traits/has_nothrow_copy.hpp>
35 #include <boost/type_traits/is_nothrow_move_constructible.hpp>
37 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
38 # pragma warning (push)
39 # pragma warning (disable : 4702) //unreachable code
42 ///////////////////////////////////////////////////////////////////////////////
43 // BOOST_VARIANT_VISITATION_UNROLLING_LIMIT
45 // Unrolls variant's visitation mechanism to reduce template instantiation
46 // and potentially increase runtime performance. (TODO: Investigate further.)
48 #if !defined(BOOST_VARIANT_VISITATION_UNROLLING_LIMIT)
50 #ifndef BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES
51 # include <boost/mpl/limits/list.hpp>
52 # define BOOST_VARIANT_VISITATION_UNROLLING_LIMIT \
53 BOOST_MPL_LIMIT_LIST_SIZE
55 # define BOOST_VARIANT_VISITATION_UNROLLING_LIMIT \
56 BOOST_VARIANT_LIMIT_TYPES
62 namespace detail { namespace variant {
64 ///////////////////////////////////////////////////////////////////////////////
65 // (detail) class apply_visitor_unrolled
67 // Tag type indicates when visitation_impl is unrolled.
69 struct apply_visitor_unrolled {};
71 ///////////////////////////////////////////////////////////////////////////////
72 // (detail) class template visitation_impl_step
74 // "Never ending" iterator range facilitates visitation_impl unrolling.
78 template <typename Iter, typename LastIter>
79 struct visitation_impl_step
81 typedef typename mpl::deref<Iter>::type type;
83 typedef typename mpl::next<Iter>::type next_iter;
84 typedef visitation_impl_step<
89 template <typename LastIter>
90 struct visitation_impl_step< LastIter,LastIter >
92 typedef apply_visitor_unrolled type;
93 typedef visitation_impl_step next;
97 ///////////////////////////////////////////////////////////////////////////////
98 // (detail) function template visitation_impl_invoke
100 // Invokes the given visitor on the specified type in the given storage.
103 template <typename Visitor, typename VoidPtrCV, typename T>
104 inline typename Visitor::result_type
105 visitation_impl_invoke_impl(
106 int, Visitor& visitor, VoidPtrCV storage, T*
107 , mpl::true_// never_uses_backup
110 return visitor.internal_visit(
111 cast_storage<T>(storage), 1L
115 template <typename Visitor, typename VoidPtrCV, typename T>
116 inline typename Visitor::result_type
117 visitation_impl_invoke_impl(
118 int internal_which, Visitor& visitor, VoidPtrCV storage, T*
119 , mpl::false_// never_uses_backup
122 if (internal_which >= 0)
124 return visitor.internal_visit(
125 cast_storage<T>(storage), 1L
130 return visitor.internal_visit(
131 cast_storage< backup_holder<T> >(storage), 1L
136 template <typename Visitor, typename VoidPtrCV, typename T, typename NoBackupFlag>
137 inline typename Visitor::result_type
138 visitation_impl_invoke(
139 int internal_which, Visitor& visitor, VoidPtrCV storage, T* t
144 typedef typename mpl::or_<
146 , is_nothrow_move_constructible<T>
147 , has_nothrow_copy<T>
148 >::type never_uses_backup;
150 return (visitation_impl_invoke_impl)(
151 internal_which, visitor, storage, t
152 , never_uses_backup()
156 template <typename Visitor, typename VoidPtrCV, typename NBF>
157 inline typename Visitor::result_type
158 visitation_impl_invoke(int, Visitor&, VoidPtrCV, apply_visitor_unrolled*, NBF, long)
160 // should never be here at runtime!
161 typedef typename Visitor::result_type result_type;
162 return ::boost::detail::variant::forced_return< result_type >();
165 ///////////////////////////////////////////////////////////////////////////////
166 // (detail) function template visitation_impl
168 // Invokes the given visitor on the type in the given variant storage.
172 typename W, typename S
173 , typename Visitor, typename VPCV
176 inline typename Visitor::result_type
178 int, int, Visitor&, VPCV
179 , mpl::true_ // is_apply_visitor_unrolled
180 , NBF, W* = 0, S* = 0
183 // should never be here at runtime!
184 typedef typename Visitor::result_type result_type;
185 return ::boost::detail::variant::forced_return< result_type >();
189 typename Which, typename step0
190 , typename Visitor, typename VoidPtrCV
191 , typename NoBackupFlag
193 BOOST_FORCEINLINE typename Visitor::result_type
195 const int internal_which, const int logical_which
196 , Visitor& visitor, VoidPtrCV storage
197 , mpl::false_ // is_apply_visitor_unrolled
198 , NoBackupFlag no_backup_flag
199 , Which* = 0, step0* = 0
202 // Typedef apply_visitor_unrolled steps and associated types...
203 # define BOOST_VARIANT_AUX_APPLY_VISITOR_STEP_TYPEDEF(z, N, _) \
204 typedef typename BOOST_PP_CAT(step,N)::type BOOST_PP_CAT(T,N); \
205 typedef typename BOOST_PP_CAT(step,N)::next \
206 BOOST_PP_CAT(step, BOOST_PP_INC(N)); \
210 BOOST_VARIANT_VISITATION_UNROLLING_LIMIT
211 , BOOST_VARIANT_AUX_APPLY_VISITOR_STEP_TYPEDEF
215 # undef BOOST_VARIANT_AUX_APPLY_VISITOR_STEP_TYPEDEF
217 // ...switch on the target which-index value...
218 switch (logical_which)
221 // ...applying the appropriate case:
222 # define BOOST_VARIANT_AUX_APPLY_VISITOR_STEP_CASE(z, N, _) \
223 case (Which::value + (N)): \
224 return (visitation_impl_invoke)( \
225 internal_which, visitor, storage \
226 , static_cast<BOOST_PP_CAT(T,N)*>(0) \
227 , no_backup_flag, 1L \
232 BOOST_VARIANT_VISITATION_UNROLLING_LIMIT
233 , BOOST_VARIANT_AUX_APPLY_VISITOR_STEP_CASE
237 # undef BOOST_VARIANT_AUX_APPLY_VISITOR_STEP_CASE
242 // If not handled in this iteration, continue unrolling:
244 Which::value + (BOOST_VARIANT_VISITATION_UNROLLING_LIMIT)
247 typedef BOOST_PP_CAT(step, BOOST_VARIANT_VISITATION_UNROLLING_LIMIT)
250 typedef typename next_step::type next_type;
251 typedef typename is_same< next_type,apply_visitor_unrolled >::type
252 is_apply_visitor_unrolled;
254 return detail::variant::visitation_impl(
255 internal_which, logical_which
257 , is_apply_visitor_unrolled()
259 , static_cast<next_which*>(0), static_cast<next_step*>(0)
263 }} // namespace detail::variant
266 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
267 # pragma warning(pop)
270 #endif // BOOST_VARIANT_DETAIL_VISITATION_IMPL_HPP