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 ...Others>
213 struct do_pack<typelist<void, Others...> >
215 typedef typename do_pack<typelist<Others...> >::type type;
218 template<class Prev, class ...Others>
219 struct do_pack<typelist<Prev, Others...> >
221 typedef typename Prev::template pack
222 <typename do_pack<typelist<Others...> >::type> type;
226 template<class DefaultOptions, class ...Options>
229 typedef typelist<DefaultOptions, Options...> typelist_t;
230 typedef typename invert_typelist<typelist_t>::type inverted_typelist;
231 typedef typename do_pack<inverted_typelist>::type type;
234 #endif //!defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
236 #define BOOST_INTRUSIVE_OPTION_TYPE(OPTION_NAME, TYPE, TYPEDEF_EXPR, TYPEDEF_NAME) \
237 template< class TYPE> \
240 template<class Base> \
243 typedef TYPEDEF_EXPR TYPEDEF_NAME; \
248 #define BOOST_INTRUSIVE_OPTION_CONSTANT(OPTION_NAME, TYPE, VALUE, CONSTANT_NAME) \
249 template< TYPE VALUE> \
252 template<class Base> \
255 static const TYPE CONSTANT_NAME = VALUE; \
260 #else //#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
262 //! This class is a utility that takes:
263 //! - a default options class defining initial static constant
265 //! - several options defined with BOOST_INTRUSIVE_OPTION_CONSTANT and
266 //! BOOST_INTRUSIVE_OPTION_TYPE
268 //! and packs them together in a new type that defines all options as
269 //! member typedefs or static constant values. Given options of form:
272 //! BOOST_INTRUSIVE_OPTION_TYPE(my_pointer, VoidPointer, VoidPointer, my_pointer_type)
273 //! BOOST_INTRUSIVE_OPTION_CONSTANT(incremental, bool, Enabled, is_incremental)
276 //! the following expression
280 //! struct default_options
282 //! typedef long int_type;
283 //! static const int int_constant = -1;
286 //! pack_options< default_options, my_pointer<void*>, incremental<true> >::type
289 //! will create a type that will contain the following typedefs/constants
292 //! struct unspecified_type
294 //! //Default options
295 //! typedef long int_type;
296 //! static const int int_constant = -1;
298 //! //Packed options (will ovewrite any default option)
299 //! typedef void* my_pointer_type;
300 //! static const bool is_incremental = true;
304 //! If an option is specified in the default options argument and later
305 //! redefined as an option, the last definition will prevail.
306 template<class DefaultOptions, class ...Options>
309 typedef unspecified_type type;
312 //! Defines an option class of name OPTION_NAME that can be used to specify a type
316 //! struct OPTION_NAME<class TYPE>
317 //! { unspecified_content };
320 //! ...that after being combined with
321 //! <code>boost::intrusive::pack_options</code>,
322 //! will typedef TYPE as a typedef of name TYPEDEF_NAME. Example:
325 //! //[includes and namespaces omitted for brevity]
327 //! //This macro will create the following class:
328 //! // template<class VoidPointer>
329 //! // struct my_pointer
330 //! // { unspecified_content };
331 //! BOOST_INTRUSIVE_OPTION_TYPE(my_pointer, VoidPointer, boost::remove_pointer<VoidPointer>::type, my_pointer_type)
333 //! struct empty_default{};
335 //! typedef pack_options< empty_default, typename my_pointer<void*> >::type::my_pointer_type type;
337 //! BOOST_STATIC_ASSERT(( boost::is_same<type, void>::value ));
340 #define BOOST_INTRUSIVE_OPTION_TYPE(OPTION_NAME, TYPE, TYPEDEF_EXPR, TYPEDEF_NAME)
342 //! Defines an option class of name OPTION_NAME that can be used to specify a constant
343 //! of type TYPE with value VALUE...
346 //! struct OPTION_NAME<TYPE VALUE>
347 //! { unspecified_content };
350 //! ...that after being combined with
351 //! <code>boost::intrusive::pack_options</code>,
352 //! will contain a CONSTANT_NAME static constant of value VALUE. Example:
355 //! //[includes and namespaces omitted for brevity]
357 //! //This macro will create the following class:
358 //! // template<bool Enabled>
359 //! // struct incremental
360 //! // { unspecified_content };
361 //! BOOST_INTRUSIVE_OPTION_CONSTANT(incremental, bool, Enabled, is_incremental)
363 //! struct empty_default{};
365 //! const bool is_incremental = pack_options< empty_default, incremental<true> >::type::is_incremental;
367 //! BOOST_STATIC_ASSERT(( is_incremental == true ));
370 #define BOOST_INTRUSIVE_OPTION_CONSTANT(OPTION_NAME, TYPE, VALUE, CONSTANT_NAME)
372 #endif //#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
375 } //namespace intrusive {
376 } //namespace boost {
378 #include <boost/intrusive/detail/config_end.hpp>
380 #endif //#ifndef BOOST_INTRUSIVE_PACK_OPTIONS_HPP