1 /////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2013-2013
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // See http://www.boost.org/libs/intrusive for documentation.
11 /////////////////////////////////////////////////////////////////////////////
13 #ifndef BOOST_INTRUSIVE_PACK_OPTIONS_HPP
14 #define BOOST_INTRUSIVE_PACK_OPTIONS_HPP
16 #include <boost/intrusive/detail/config_begin.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
25 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
27 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
29 template<class Prev, class Next>
32 //Use "pack" member template to pack options
33 typedef typename Next::template pack<Prev> type;
37 struct do_pack<Prev, void>
39 //Avoid packing "void" to shorten template names
44 < class DefaultOptions
100 template<int... Indexes>
101 struct index_tuple{};
104 template<std::size_t Num, typename Tuple = index_tuple<> >
105 struct build_number_seq;
107 template<std::size_t Num, int... Indexes>
108 struct build_number_seq<Num, index_tuple<Indexes...> >
109 : build_number_seq<Num - 1, index_tuple<Indexes..., sizeof...(Indexes)> >
112 template<int... Indexes>
113 struct build_number_seq<0, index_tuple<Indexes...> >
114 { typedef index_tuple<Indexes...> type; };
116 template<class ...Types>
122 struct invert_typelist;
124 template<int I, typename Tuple>
125 struct typelist_element;
127 template<int I, typename Head, typename... Tail>
128 struct typelist_element<I, typelist<Head, Tail...> >
130 typedef typename typelist_element<I-1, typelist<Tail...> >::type type;
133 template<typename Head, typename... Tail>
134 struct typelist_element<0, typelist<Head, Tail...> >
139 template<int ...Ints, class ...Types>
140 typelist<typename typelist_element<(sizeof...(Types) - 1) - Ints, typelist<Types...> >::type...>
141 inverted_typelist(index_tuple<Ints...>, typelist<Types...>)
143 return typelist<typename typelist_element<(sizeof...(Types) - 1) - Ints, typelist<Types...> >::type...>();
147 template<class Typelist>
148 struct sizeof_typelist;
150 template<class ...Types>
151 struct sizeof_typelist< typelist<Types...> >
153 static const std::size_t value = sizeof...(Types);
156 //invert_typelist_impl
157 template<class Typelist, class Indexes>
158 struct invert_typelist_impl;
161 template<class Typelist, int ...Ints>
162 struct invert_typelist_impl< Typelist, index_tuple<Ints...> >
164 static const std::size_t last_idx = sizeof_typelist<Typelist>::value - 1;
166 <typename typelist_element<last_idx - Ints, Typelist>::type...> type;
169 template<class Typelist, int Int>
170 struct invert_typelist_impl< Typelist, index_tuple<Int> >
172 typedef Typelist type;
175 template<class Typelist>
176 struct invert_typelist_impl< Typelist, index_tuple<> >
178 typedef Typelist type;
182 template<class Typelist>
183 struct invert_typelist;
185 template<class ...Types>
186 struct invert_typelist< typelist<Types...> >
188 typedef typelist<Types...> typelist_t;
189 typedef typename build_number_seq<sizeof...(Types)>::type indexes_t;
190 typedef typename invert_typelist_impl<typelist_t, indexes_t>::type type;
194 template<class Typelist>
198 struct do_pack<typelist<> >;
201 struct do_pack<typelist<Prev> >
206 template<class Prev, class Last>
207 struct do_pack<typelist<Prev, Last> >
209 typedef typename Prev::template pack<Last> type;
212 template<class Prev, class ...Others>
213 struct do_pack<typelist<Prev, Others...> >
215 typedef typename Prev::template pack
216 <typename do_pack<typelist<Others...> >::type> type;
220 template<class DefaultOptions, class ...Options>
223 typedef typelist<DefaultOptions, Options...> typelist_t;
224 typedef typename invert_typelist<typelist_t>::type inverted_typelist;
225 typedef typename do_pack<inverted_typelist>::type type;
228 #endif //!defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
230 #define BOOST_INTRUSIVE_OPTION_TYPE(OPTION_NAME, TYPE, TYPEDEF_EXPR, TYPEDEF_NAME) \
231 template< class TYPE> \
234 template<class Base> \
237 typedef TYPEDEF_EXPR TYPEDEF_NAME; \
242 #define BOOST_INTRUSIVE_OPTION_CONSTANT(OPTION_NAME, TYPE, VALUE, CONSTANT_NAME) \
243 template< TYPE VALUE> \
246 template<class Base> \
249 static const TYPE CONSTANT_NAME = VALUE; \
254 #else //#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
256 //! This class is a utility that takes:
257 //! - a default options class defining initial static constant
259 //! - several options defined with BOOST_INTRUSIVE_OPTION_CONSTANT and
260 //! BOOST_INTRUSIVE_OPTION_TYPE
262 //! and packs them together in a new type that defines all options as
263 //! member typedefs or static constant values. Given options of form:
266 //! BOOST_INTRUSIVE_OPTION_TYPE(my_pointer, VoidPointer, VoidPointer, my_pointer_type)
267 //! BOOST_INTRUSIVE_OPTION_CONSTANT(incremental, bool, Enabled, is_incremental)
270 //! the following expression
274 //! struct default_options
276 //! typedef long int_type;
277 //! static const int int_constant = -1;
280 //! pack_options< default_options, my_pointer<void*>, incremental<true> >::type
283 //! will create a type that will contain the following typedefs/constants
286 //! struct unspecified_type
288 //! //Default options
289 //! typedef long int_type;
290 //! static const int int_constant = -1;
292 //! //Packed options (will ovewrite any default option)
293 //! typedef void* my_pointer_type;
294 //! static const bool is_incremental = true;
298 //! If an option is specified in the default options argument and later
299 //! redefined as an option, the last definition will prevail.
300 template<class DefaultOptions, class ...Options>
303 typedef unspecified_type type;
306 //! Defines an option class of name OPTION_NAME that can be used to specify a type
310 //! struct OPTION_NAME<class TYPE>
311 //! { unspecified_content };
314 //! ...that after being combined with
315 //! <code>boost::intrusive::pack_options</code>,
316 //! will typedef TYPE as a typedef of name TYPEDEF_NAME. Example:
319 //! //[includes and namespaces omitted for brevity]
321 //! //This macro will create the following class:
322 //! // template<class VoidPointer>
323 //! // struct my_pointer
324 //! // { unspecified_content };
325 //! BOOST_INTRUSIVE_OPTION_TYPE(my_pointer, VoidPointer, boost::remove_pointer<VoidPointer>::type, my_pointer_type)
327 //! struct empty_default{};
329 //! typedef pack_options< empty_default, typename my_pointer<void*> >::type::my_pointer_type type;
331 //! BOOST_STATIC_ASSERT(( boost::is_same<type, void>::value ));
334 #define BOOST_INTRUSIVE_OPTION_TYPE(OPTION_NAME, TYPE, TYPEDEF_EXPR, TYPEDEF_NAME)
336 //! Defines an option class of name OPTION_NAME that can be used to specify a constant
337 //! of type TYPE with value VALUE...
340 //! struct OPTION_NAME<TYPE VALUE>
341 //! { unspecified_content };
344 //! ...that after being combined with
345 //! <code>boost::intrusive::pack_options</code>,
346 //! will contain a CONSTANT_NAME static constant of value VALUE. Example:
349 //! //[includes and namespaces omitted for brevity]
351 //! //This macro will create the following class:
352 //! // template<bool Enabled>
353 //! // struct incremental
354 //! // { unspecified_content };
355 //! BOOST_INTRUSIVE_OPTION_CONSTANT(incremental, bool, Enabled, is_incremental)
357 //! struct empty_default{};
359 //! const bool is_incremental = pack_options< empty_default, incremental<true> >::type::is_incremental;
361 //! BOOST_STATIC_ASSERT(( is_incremental == true ));
364 #define BOOST_INTRUSIVE_OPTION_CONSTANT(OPTION_NAME, TYPE, VALUE, CONSTANT_NAME)
366 #endif //#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
369 } //namespace intrusive {
370 } //namespace boost {
372 #include <boost/intrusive/detail/config_end.hpp>
374 #endif //#ifndef BOOST_INTRUSIVE_PACK_OPTIONS_HPP