pack_options.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2013-2013
  4. //
  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)
  8. //
  9. // See http://www.boost.org/libs/intrusive for documentation.
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. #ifndef BHO_INTRUSIVE_PACK_OPTIONS_HPP
  13. #define BHO_INTRUSIVE_PACK_OPTIONS_HPP
  14. #include <asio2/bho/intrusive/detail/config_begin.hpp>
  15. #if defined(BHO_HAS_PRAGMA_ONCE)
  16. # pragma once
  17. #endif
  18. #include <cstddef>
  19. namespace bho {
  20. namespace intrusive {
  21. #ifndef BHO_INTRUSIVE_DOXYGEN_INVOKED
  22. #if !defined(BHO_INTRUSIVE_VARIADIC_TEMPLATES)
  23. template<class Prev, class Next>
  24. struct do_pack
  25. {
  26. //Use "pack" member template to pack options
  27. typedef typename Next::template pack<Prev> type;
  28. };
  29. template<class Prev>
  30. struct do_pack<Prev, void>
  31. {
  32. //Avoid packing "void" to shorten template names
  33. typedef Prev type;
  34. };
  35. template
  36. < class DefaultOptions
  37. , class O1 = void
  38. , class O2 = void
  39. , class O3 = void
  40. , class O4 = void
  41. , class O5 = void
  42. , class O6 = void
  43. , class O7 = void
  44. , class O8 = void
  45. , class O9 = void
  46. , class O10 = void
  47. , class O11 = void
  48. >
  49. struct pack_options
  50. {
  51. // join options
  52. typedef
  53. typename do_pack
  54. < typename do_pack
  55. < typename do_pack
  56. < typename do_pack
  57. < typename do_pack
  58. < typename do_pack
  59. < typename do_pack
  60. < typename do_pack
  61. < typename do_pack
  62. < typename do_pack
  63. < typename do_pack
  64. < DefaultOptions
  65. , O1
  66. >::type
  67. , O2
  68. >::type
  69. , O3
  70. >::type
  71. , O4
  72. >::type
  73. , O5
  74. >::type
  75. , O6
  76. >::type
  77. , O7
  78. >::type
  79. , O8
  80. >::type
  81. , O9
  82. >::type
  83. , O10
  84. >::type
  85. , O11
  86. >::type
  87. type;
  88. };
  89. #else
  90. //index_tuple
  91. template<std::size_t... Indexes>
  92. struct index_tuple{};
  93. //build_number_seq
  94. template<std::size_t Num, typename Tuple = index_tuple<> >
  95. struct build_number_seq;
  96. template<std::size_t Num, std::size_t... Indexes>
  97. struct build_number_seq<Num, index_tuple<Indexes...> >
  98. : build_number_seq<Num - 1, index_tuple<Indexes..., sizeof...(Indexes)> >
  99. {};
  100. template<std::size_t... Indexes>
  101. struct build_number_seq<0, index_tuple<Indexes...> >
  102. { typedef index_tuple<Indexes...> type; };
  103. template<class ...Types>
  104. struct typelist
  105. {};
  106. //invert_typelist
  107. template<class T>
  108. struct invert_typelist;
  109. template<std::size_t I, typename Tuple>
  110. struct typelist_element;
  111. template<std::size_t I, typename Head, typename... Tail>
  112. struct typelist_element<I, typelist<Head, Tail...> >
  113. {
  114. typedef typename typelist_element<I-1, typelist<Tail...> >::type type;
  115. };
  116. template<typename Head, typename... Tail>
  117. struct typelist_element<0, typelist<Head, Tail...> >
  118. {
  119. typedef Head type;
  120. };
  121. template<std::size_t ...Ints, class ...Types>
  122. typelist<typename typelist_element<(sizeof...(Types) - 1) - Ints, typelist<Types...> >::type...>
  123. inverted_typelist(index_tuple<Ints...>, typelist<Types...>)
  124. {
  125. return typelist<typename typelist_element<(sizeof...(Types) - 1) - Ints, typelist<Types...> >::type...>();
  126. }
  127. //sizeof_typelist
  128. template<class Typelist>
  129. struct sizeof_typelist;
  130. template<class ...Types>
  131. struct sizeof_typelist< typelist<Types...> >
  132. {
  133. static const std::size_t value = sizeof...(Types);
  134. };
  135. //invert_typelist_impl
  136. template<class Typelist, class Indexes>
  137. struct invert_typelist_impl;
  138. template<class Typelist, std::size_t ...Ints>
  139. struct invert_typelist_impl< Typelist, index_tuple<Ints...> >
  140. {
  141. static const std::size_t last_idx = sizeof_typelist<Typelist>::value - 1;
  142. typedef typelist
  143. <typename typelist_element<last_idx - Ints, Typelist>::type...> type;
  144. };
  145. template<class Typelist, std::size_t Int>
  146. struct invert_typelist_impl< Typelist, index_tuple<Int> >
  147. {
  148. typedef Typelist type;
  149. };
  150. template<class Typelist>
  151. struct invert_typelist_impl< Typelist, index_tuple<> >
  152. {
  153. typedef Typelist type;
  154. };
  155. //invert_typelist
  156. template<class Typelist>
  157. struct invert_typelist;
  158. template<class ...Types>
  159. struct invert_typelist< typelist<Types...> >
  160. {
  161. typedef typelist<Types...> typelist_t;
  162. typedef typename build_number_seq<sizeof...(Types)>::type indexes_t;
  163. typedef typename invert_typelist_impl<typelist_t, indexes_t>::type type;
  164. };
  165. //Do pack
  166. template<class Typelist>
  167. struct do_pack;
  168. template<>
  169. struct do_pack<typelist<> >;
  170. template<class Prev>
  171. struct do_pack<typelist<Prev> >
  172. {
  173. typedef Prev type;
  174. };
  175. template<class Prev, class Last>
  176. struct do_pack<typelist<Prev, Last> >
  177. {
  178. typedef typename Prev::template pack<Last> type;
  179. };
  180. template<class ...Others>
  181. struct do_pack<typelist<void, Others...> >
  182. {
  183. typedef typename do_pack<typelist<Others...> >::type type;
  184. };
  185. template<class Prev, class ...Others>
  186. struct do_pack<typelist<Prev, Others...> >
  187. {
  188. typedef typename Prev::template pack
  189. <typename do_pack<typelist<Others...> >::type> type;
  190. };
  191. template<class DefaultOptions, class ...Options>
  192. struct pack_options
  193. {
  194. typedef typelist<DefaultOptions, Options...> typelist_t;
  195. typedef typename invert_typelist<typelist_t>::type inverted_typelist;
  196. typedef typename do_pack<inverted_typelist>::type type;
  197. };
  198. #endif //!defined(BHO_INTRUSIVE_VARIADIC_TEMPLATES)
  199. #define BHO_INTRUSIVE_OPTION_TYPE(OPTION_NAME, TYPE, TYPEDEF_EXPR, TYPEDEF_NAME) \
  200. template< class TYPE> \
  201. struct OPTION_NAME \
  202. { \
  203. template<class Base> \
  204. struct pack : Base \
  205. { \
  206. typedef TYPEDEF_EXPR TYPEDEF_NAME; \
  207. }; \
  208. }; \
  209. //
  210. #define BHO_INTRUSIVE_OPTION_CONSTANT(OPTION_NAME, TYPE, VALUE, CONSTANT_NAME) \
  211. template< TYPE VALUE> \
  212. struct OPTION_NAME \
  213. { \
  214. static const TYPE value = VALUE; \
  215. \
  216. template<class Base> \
  217. struct pack : Base \
  218. { \
  219. static const TYPE CONSTANT_NAME = VALUE; \
  220. }; \
  221. }; \
  222. //
  223. #else //#ifndef BHO_INTRUSIVE_DOXYGEN_INVOKED
  224. //! This class is a utility that takes:
  225. //! - a default options class defining initial static constant
  226. //! and typedefs
  227. //! - several options defined with BHO_INTRUSIVE_OPTION_CONSTANT and
  228. //! BHO_INTRUSIVE_OPTION_TYPE
  229. //!
  230. //! and packs them together in a new type that defines all options as
  231. //! member typedefs or static constant values. Given options of form:
  232. //!
  233. //! \code
  234. //! BHO_INTRUSIVE_OPTION_TYPE(my_pointer, VoidPointer, VoidPointer, my_pointer_type)
  235. //! BHO_INTRUSIVE_OPTION_CONSTANT(incremental, bool, Enabled, is_incremental)
  236. //! \endcode
  237. //!
  238. //! the following expression
  239. //!
  240. //! \code
  241. //!
  242. //! struct default_options
  243. //! {
  244. //! typedef long int_type;
  245. //! static const int int_constant = -1;
  246. //! };
  247. //!
  248. //! pack_options< default_options, my_pointer<void*>, incremental<true> >::type
  249. //! \endcode
  250. //!
  251. //! will create a type that will contain the following typedefs/constants
  252. //!
  253. //! \code
  254. //! struct unspecified_type
  255. //! {
  256. //! //Default options
  257. //! typedef long int_type;
  258. //! static const int int_constant = -1;
  259. //!
  260. //! //Packed options (will ovewrite any default option)
  261. //! typedef void* my_pointer_type;
  262. //! static const bool is_incremental = true;
  263. //! };
  264. //! \endcode
  265. //!
  266. //! If an option is specified in the default options argument and later
  267. //! redefined as an option, the last definition will prevail.
  268. template<class DefaultOptions, class ...Options>
  269. struct pack_options
  270. {
  271. typedef unspecified_type type;
  272. };
  273. //! Defines an option class of name OPTION_NAME that can be used to specify a type
  274. //! of type TYPE...
  275. //!
  276. //! \code
  277. //! struct OPTION_NAME<class TYPE>
  278. //! { unspecified_content };
  279. //! \endcode
  280. //!
  281. //! ...that after being combined with
  282. //! <code>bho::intrusive::pack_options</code>,
  283. //! will typedef TYPE as a typedef of name TYPEDEF_NAME. Example:
  284. //!
  285. //! \code
  286. //! //[includes and namespaces omitted for brevity]
  287. //!
  288. //! //This macro will create the following class:
  289. //! // template<class VoidPointer>
  290. //! // struct my_pointer
  291. //! // { unspecified_content };
  292. //! BHO_INTRUSIVE_OPTION_TYPE(my_pointer, VoidPointer, bho::remove_pointer<VoidPointer>::type, my_pointer_type)
  293. //!
  294. //! struct empty_default{};
  295. //!
  296. //! typedef pack_options< empty_default, typename my_pointer<void*> >::type::my_pointer_type type;
  297. //!
  298. //! BHO_STATIC_ASSERT(( std::is_same<type, void>::value ));
  299. //!
  300. //! \endcode
  301. #define BHO_INTRUSIVE_OPTION_TYPE(OPTION_NAME, TYPE, TYPEDEF_EXPR, TYPEDEF_NAME)
  302. //! Defines an option class of name OPTION_NAME that can be used to specify a constant
  303. //! of type TYPE with value VALUE...
  304. //!
  305. //! \code
  306. //! struct OPTION_NAME<TYPE VALUE>
  307. //! { unspecified_content };
  308. //! \endcode
  309. //!
  310. //! ...that after being combined with
  311. //! <code>bho::intrusive::pack_options</code>,
  312. //! will contain a CONSTANT_NAME static constant of value VALUE. Example:
  313. //!
  314. //! \code
  315. //! //[includes and namespaces omitted for brevity]
  316. //!
  317. //! //This macro will create the following class:
  318. //! // template<bool Enabled>
  319. //! // struct incremental
  320. //! // { unspecified_content };
  321. //! BHO_INTRUSIVE_OPTION_CONSTANT(incremental, bool, Enabled, is_incremental)
  322. //!
  323. //! struct empty_default{};
  324. //!
  325. //! const bool is_incremental = pack_options< empty_default, incremental<true> >::type::is_incremental;
  326. //!
  327. //! BHO_STATIC_ASSERT(( is_incremental == true ));
  328. //!
  329. //! \endcode
  330. #define BHO_INTRUSIVE_OPTION_CONSTANT(OPTION_NAME, TYPE, VALUE, CONSTANT_NAME)
  331. #endif //#ifndef BHO_INTRUSIVE_DOXYGEN_INVOKED
  332. } //namespace intrusive {
  333. } //namespace bho {
  334. #include <asio2/bho/intrusive/detail/config_end.hpp>
  335. #endif //#ifndef BHO_INTRUSIVE_PACK_OPTIONS_HPP