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)
27 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
29 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
31 template<class Prev, class Next>
34 //Use "pack" member template to pack options
35 typedef typename Next::template pack<Prev> type;
39 struct do_pack<Prev, void>
41 //Avoid packing "void" to shorten template names
46 < class DefaultOptions
102 template<std::size_t... Indexes>
103 struct index_tuple{};
106 template<std::size_t Num, typename Tuple = index_tuple<> >
107 struct build_number_seq;
109 template<std::size_t Num, std::size_t... Indexes>
110 struct build_number_seq<Num, index_tuple<Indexes...> >
111 : build_number_seq<Num - 1, index_tuple<Indexes..., sizeof...(Indexes)> >
114 template<std::size_t... Indexes>
115 struct build_number_seq<0, index_tuple<Indexes...> >
116 { typedef index_tuple<Indexes...> type; };
118 template<class ...Types>
124 struct invert_typelist;
126 template<std::size_t I, typename Tuple>
127 struct typelist_element;
129 template<std::size_t I, typename Head, typename... Tail>
130 struct typelist_element<I, typelist<Head, Tail...> >
132 typedef typename typelist_element<I-1, typelist<Tail...> >::type type;
135 template<typename Head, typename... Tail>
136 struct typelist_element<0, typelist<Head, Tail...> >
141 template<std::size_t ...Ints, class ...Types>
142 typelist<typename typelist_element<(sizeof...(Types) - 1) - Ints, typelist<Types...> >::type...>
143 inverted_typelist(index_tuple<Ints...>, typelist<Types...>)
145 return typelist<typename typelist_element<(sizeof...(Types) - 1) - Ints, typelist<Types...> >::type...>();
149 template<class Typelist>
150 struct sizeof_typelist;
152 template<class ...Types>
153 struct sizeof_typelist< typelist<Types...> >
155 static const std::size_t value = sizeof...(Types);
158 //invert_typelist_impl
159 template<class Typelist, class Indexes>
160 struct invert_typelist_impl;
163 template<class Typelist, std::size_t ...Ints>
164 struct invert_typelist_impl< Typelist, index_tuple<Ints...> >
166 static const std::size_t last_idx = sizeof_typelist<Typelist>::value - 1;
168 <typename typelist_element<last_idx - Ints, Typelist>::type...> type;
171 template<class Typelist, std::size_t Int>
172 struct invert_typelist_impl< Typelist, index_tuple<Int> >
174 typedef Typelist type;
177 template<class Typelist>
178 struct invert_typelist_impl< Typelist, index_tuple<> >
180 typedef Typelist type;
184 template<class Typelist>
185 struct invert_typelist;
187 template<class ...Types>
188 struct invert_typelist< typelist<Types...> >
190 typedef typelist<Types...> typelist_t;
191 typedef typename build_number_seq<sizeof...(Types)>::type indexes_t;
192 typedef typename invert_typelist_impl<typelist_t, indexes_t>::type type;
196 template<class Typelist>
200 struct do_pack<typelist<> >;
203 struct do_pack<typelist<Prev> >
208 template<class Prev, class Last>
209 struct do_pack<typelist<Prev, Last> >
211 typedef typename Prev::template pack<Last> type;
214 template<class ...Others>
215 struct do_pack<typelist<void, Others...> >
217 typedef typename do_pack<typelist<Others...> >::type type;
220 template<class Prev, class ...Others>
221 struct do_pack<typelist<Prev, Others...> >
223 typedef typename Prev::template pack
224 <typename do_pack<typelist<Others...> >::type> type;
228 template<class DefaultOptions, class ...Options>
231 typedef typelist<DefaultOptions, Options...> typelist_t;
232 typedef typename invert_typelist<typelist_t>::type inverted_typelist;
233 typedef typename do_pack<inverted_typelist>::type type;
236 #endif //!defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
238 #define BOOST_INTRUSIVE_OPTION_TYPE(OPTION_NAME, TYPE, TYPEDEF_EXPR, TYPEDEF_NAME) \
239 template< class TYPE> \
242 template<class Base> \
245 typedef TYPEDEF_EXPR TYPEDEF_NAME; \
250 #define BOOST_INTRUSIVE_OPTION_CONSTANT(OPTION_NAME, TYPE, VALUE, CONSTANT_NAME) \
251 template< TYPE VALUE> \
254 template<class Base> \
257 static const TYPE CONSTANT_NAME = VALUE; \
262 #else //#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
264 //! This class is a utility that takes:
265 //! - a default options class defining initial static constant
267 //! - several options defined with BOOST_INTRUSIVE_OPTION_CONSTANT and
268 //! BOOST_INTRUSIVE_OPTION_TYPE
270 //! and packs them together in a new type that defines all options as
271 //! member typedefs or static constant values. Given options of form:
274 //! BOOST_INTRUSIVE_OPTION_TYPE(my_pointer, VoidPointer, VoidPointer, my_pointer_type)
275 //! BOOST_INTRUSIVE_OPTION_CONSTANT(incremental, bool, Enabled, is_incremental)
278 //! the following expression
282 //! struct default_options
284 //! typedef long int_type;
285 //! static const int int_constant = -1;
288 //! pack_options< default_options, my_pointer<void*>, incremental<true> >::type
291 //! will create a type that will contain the following typedefs/constants
294 //! struct unspecified_type
296 //! //Default options
297 //! typedef long int_type;
298 //! static const int int_constant = -1;
300 //! //Packed options (will ovewrite any default option)
301 //! typedef void* my_pointer_type;
302 //! static const bool is_incremental = true;
306 //! If an option is specified in the default options argument and later
307 //! redefined as an option, the last definition will prevail.
308 template<class DefaultOptions, class ...Options>
311 typedef unspecified_type type;
314 //! Defines an option class of name OPTION_NAME that can be used to specify a type
318 //! struct OPTION_NAME<class TYPE>
319 //! { unspecified_content };
322 //! ...that after being combined with
323 //! <code>boost::intrusive::pack_options</code>,
324 //! will typedef TYPE as a typedef of name TYPEDEF_NAME. Example:
327 //! //[includes and namespaces omitted for brevity]
329 //! //This macro will create the following class:
330 //! // template<class VoidPointer>
331 //! // struct my_pointer
332 //! // { unspecified_content };
333 //! BOOST_INTRUSIVE_OPTION_TYPE(my_pointer, VoidPointer, boost::remove_pointer<VoidPointer>::type, my_pointer_type)
335 //! struct empty_default{};
337 //! typedef pack_options< empty_default, typename my_pointer<void*> >::type::my_pointer_type type;
339 //! BOOST_STATIC_ASSERT(( boost::is_same<type, void>::value ));
342 #define BOOST_INTRUSIVE_OPTION_TYPE(OPTION_NAME, TYPE, TYPEDEF_EXPR, TYPEDEF_NAME)
344 //! Defines an option class of name OPTION_NAME that can be used to specify a constant
345 //! of type TYPE with value VALUE...
348 //! struct OPTION_NAME<TYPE VALUE>
349 //! { unspecified_content };
352 //! ...that after being combined with
353 //! <code>boost::intrusive::pack_options</code>,
354 //! will contain a CONSTANT_NAME static constant of value VALUE. Example:
357 //! //[includes and namespaces omitted for brevity]
359 //! //This macro will create the following class:
360 //! // template<bool Enabled>
361 //! // struct incremental
362 //! // { unspecified_content };
363 //! BOOST_INTRUSIVE_OPTION_CONSTANT(incremental, bool, Enabled, is_incremental)
365 //! struct empty_default{};
367 //! const bool is_incremental = pack_options< empty_default, incremental<true> >::type::is_incremental;
369 //! BOOST_STATIC_ASSERT(( is_incremental == true ));
372 #define BOOST_INTRUSIVE_OPTION_CONSTANT(OPTION_NAME, TYPE, VALUE, CONSTANT_NAME)
374 #endif //#ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
377 } //namespace intrusive {
378 } //namespace boost {
380 #include <boost/intrusive/detail/config_end.hpp>
382 #endif //#ifndef BOOST_INTRUSIVE_PACK_OPTIONS_HPP