vartempl_support.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* Copyright 2003-2013 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/multi_index for library home page.
  7. */
  8. #ifndef BOOST_MULTI_INDEX_DETAIL_VARTEMPL_SUPPORT_HPP
  9. #define BOOST_MULTI_INDEX_DETAIL_VARTEMPL_SUPPORT_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. /* Utilities for emulation of variadic template functions. Variadic packs are
  14. * replaced by lists of BOOST_MULTI_INDEX_LIMIT_VARTEMPL_ARGS parameters:
  15. *
  16. * - typename... Args --> BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK
  17. * - Args&&... args --> BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK
  18. * - std::forward<Args>(args)... --> BOOST_MULTI_INDEX_FORWARD_PARAM_PACK
  19. *
  20. * Forwarding emulated with Boost.Move. A template functions foo_imp
  21. * defined in such way accepts *exactly* BOOST_MULTI_INDEX_LIMIT_VARTEMPL_ARGS
  22. * arguments: variable number of arguments is emulated by providing a set of
  23. * overloads foo forwarding to foo_impl with
  24. *
  25. * BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL
  26. * BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL_EXTRA_ARG (initial extra arg)
  27. *
  28. * which fill the extra args with boost::multi_index::detail::noarg's.
  29. * boost::multi_index::detail::vartempl_placement_new works the opposite
  30. * way: it acceps a full a pointer x to Value and a
  31. * BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK and forwards to
  32. * new(x) Value(args) where args is the argument pack after discarding
  33. * noarg's.
  34. *
  35. * Emulation decays to the real thing when the compiler supports variadic
  36. * templates and move semantics natively.
  37. */
  38. #include <boost/config.hpp>
  39. #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)||\
  40. defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  41. #include <boost/move/core.hpp>
  42. #include <boost/move/utility.hpp>
  43. #include <boost/preprocessor/arithmetic/add.hpp>
  44. #include <boost/preprocessor/arithmetic/sub.hpp>
  45. #include <boost/preprocessor/cat.hpp>
  46. #include <boost/preprocessor/control/if.hpp>
  47. #include <boost/preprocessor/facilities/empty.hpp>
  48. #include <boost/preprocessor/facilities/intercept.hpp>
  49. #include <boost/preprocessor/logical/and.hpp>
  50. #include <boost/preprocessor/punctuation/comma.hpp>
  51. #include <boost/preprocessor/punctuation/comma_if.hpp>
  52. #include <boost/preprocessor/repetition/enum.hpp>
  53. #include <boost/preprocessor/repetition/enum_params.hpp>
  54. #include <boost/preprocessor/repetition/repeat_from_to.hpp>
  55. #include <boost/preprocessor/seq/elem.hpp>
  56. #if !defined(BOOST_MULTI_INDEX_LIMIT_VARTEMPL_ARGS)
  57. #define BOOST_MULTI_INDEX_LIMIT_VARTEMPL_ARGS 5
  58. #endif
  59. #define BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK \
  60. BOOST_PP_ENUM_PARAMS( \
  61. BOOST_MULTI_INDEX_LIMIT_VARTEMPL_ARGS,typename T)
  62. #define BOOST_MULTI_INDEX_VARTEMPL_ARG(z,n,_) \
  63. BOOST_FWD_REF(BOOST_PP_CAT(T,n)) BOOST_PP_CAT(t,n)
  64. #define BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK \
  65. BOOST_PP_ENUM( \
  66. BOOST_MULTI_INDEX_LIMIT_VARTEMPL_ARGS, \
  67. BOOST_MULTI_INDEX_VARTEMPL_ARG,~)
  68. #define BOOST_MULTI_INDEX_VARTEMPL_FORWARD_ARG(z,n,_) \
  69. boost::forward<BOOST_PP_CAT(T,n)>(BOOST_PP_CAT(t,n))
  70. #define BOOST_MULTI_INDEX_FORWARD_PARAM_PACK \
  71. BOOST_PP_ENUM( \
  72. BOOST_MULTI_INDEX_LIMIT_VARTEMPL_ARGS, \
  73. BOOST_MULTI_INDEX_VARTEMPL_FORWARD_ARG,~)
  74. namespace boost{namespace multi_index{namespace detail{
  75. struct noarg{};
  76. }}}
  77. /* call vartempl function without args */
  78. #define BOOST_MULTI_INDEX_NULL_PARAM_PACK \
  79. BOOST_PP_ENUM_PARAMS( \
  80. BOOST_MULTI_INDEX_LIMIT_VARTEMPL_ARGS, \
  81. boost::multi_index::detail::noarg() BOOST_PP_INTERCEPT)
  82. #define BOOST_MULTI_INDEX_TEMPLATE_N(n) \
  83. template<BOOST_PP_ENUM_PARAMS(n,typename T)>
  84. #define BOOST_MULTI_INDEX_TEMPLATE_0(n)
  85. #define BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL_AUX(z,n,data) \
  86. BOOST_PP_IF(n, \
  87. BOOST_MULTI_INDEX_TEMPLATE_N, \
  88. BOOST_MULTI_INDEX_TEMPLATE_0)(n) \
  89. BOOST_PP_SEQ_ELEM(0,data) /* ret */ \
  90. BOOST_PP_SEQ_ELEM(1,data) /* name_from */ ( \
  91. BOOST_PP_ENUM(n,BOOST_MULTI_INDEX_VARTEMPL_ARG,~)) \
  92. { \
  93. return BOOST_PP_SEQ_ELEM(2,data) /* name_to */ ( \
  94. BOOST_PP_ENUM(n,BOOST_MULTI_INDEX_VARTEMPL_FORWARD_ARG,~) \
  95. BOOST_PP_COMMA_IF( \
  96. BOOST_PP_AND( \
  97. n,BOOST_PP_SUB(BOOST_MULTI_INDEX_LIMIT_VARTEMPL_ARGS,n))) \
  98. BOOST_PP_ENUM_PARAMS( \
  99. BOOST_PP_SUB(BOOST_MULTI_INDEX_LIMIT_VARTEMPL_ARGS,n), \
  100. boost::multi_index::detail::noarg() BOOST_PP_INTERCEPT) \
  101. ); \
  102. }
  103. #define BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL( \
  104. ret,name_from,name_to) \
  105. BOOST_PP_REPEAT_FROM_TO( \
  106. 0,BOOST_PP_ADD(BOOST_MULTI_INDEX_LIMIT_VARTEMPL_ARGS,1), \
  107. BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL_AUX, \
  108. (ret)(name_from)(name_to))
  109. #define BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL_EXTRA_ARG_AUX( \
  110. z,n,data) \
  111. BOOST_PP_IF(n, \
  112. BOOST_MULTI_INDEX_TEMPLATE_N, \
  113. BOOST_MULTI_INDEX_TEMPLATE_0)(n) \
  114. BOOST_PP_SEQ_ELEM(0,data) /* ret */ \
  115. BOOST_PP_SEQ_ELEM(1,data) /* name_from */ ( \
  116. BOOST_PP_SEQ_ELEM(3,data) BOOST_PP_SEQ_ELEM(4,data) /* extra arg */\
  117. BOOST_PP_COMMA_IF(n) \
  118. BOOST_PP_ENUM(n,BOOST_MULTI_INDEX_VARTEMPL_ARG,~)) \
  119. { \
  120. return BOOST_PP_SEQ_ELEM(2,data) /* name_to */ ( \
  121. BOOST_PP_SEQ_ELEM(4,data) /* extra_arg_name */ \
  122. BOOST_PP_COMMA_IF(n) \
  123. BOOST_PP_ENUM(n,BOOST_MULTI_INDEX_VARTEMPL_FORWARD_ARG,~) \
  124. BOOST_PP_COMMA_IF( \
  125. BOOST_PP_SUB(BOOST_MULTI_INDEX_LIMIT_VARTEMPL_ARGS,n)) \
  126. BOOST_PP_ENUM_PARAMS( \
  127. BOOST_PP_SUB(BOOST_MULTI_INDEX_LIMIT_VARTEMPL_ARGS,n), \
  128. boost::multi_index::detail::noarg() BOOST_PP_INTERCEPT) \
  129. ); \
  130. }
  131. #define BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL_EXTRA_ARG( \
  132. ret,name_from,name_to,extra_arg_type,extra_arg_name) \
  133. BOOST_PP_REPEAT_FROM_TO( \
  134. 0,BOOST_PP_ADD(BOOST_MULTI_INDEX_LIMIT_VARTEMPL_ARGS,1), \
  135. BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL_EXTRA_ARG_AUX, \
  136. (ret)(name_from)(name_to)(extra_arg_type)(extra_arg_name))
  137. namespace boost{
  138. namespace multi_index{
  139. namespace detail{
  140. #define BOOST_MULTI_INDEX_VARTEMPL_TO_PLACEMENT_NEW_AUX(z,n,name) \
  141. template< \
  142. typename Value \
  143. BOOST_PP_COMMA_IF(n) \
  144. BOOST_PP_ENUM_PARAMS(n,typename T) \
  145. > \
  146. Value* name( \
  147. Value* x \
  148. BOOST_PP_COMMA_IF(n) \
  149. BOOST_PP_ENUM(n,BOOST_MULTI_INDEX_VARTEMPL_ARG,~) \
  150. BOOST_PP_COMMA_IF( \
  151. BOOST_PP_SUB(BOOST_MULTI_INDEX_LIMIT_VARTEMPL_ARGS,n)) \
  152. BOOST_PP_ENUM_PARAMS( \
  153. BOOST_PP_SUB(BOOST_MULTI_INDEX_LIMIT_VARTEMPL_ARGS,n), \
  154. BOOST_FWD_REF(noarg) BOOST_PP_INTERCEPT)) \
  155. { \
  156. return new(x) Value( \
  157. BOOST_PP_ENUM(n,BOOST_MULTI_INDEX_VARTEMPL_FORWARD_ARG,~)); \
  158. }
  159. #define BOOST_MULTI_INDEX_VARTEMPL_TO_PLACEMENT_NEW(name) \
  160. BOOST_PP_REPEAT_FROM_TO( \
  161. 0,BOOST_PP_ADD(BOOST_MULTI_INDEX_LIMIT_VARTEMPL_ARGS,1), \
  162. BOOST_MULTI_INDEX_VARTEMPL_TO_PLACEMENT_NEW_AUX, \
  163. name)
  164. BOOST_MULTI_INDEX_VARTEMPL_TO_PLACEMENT_NEW(vartempl_placement_new)
  165. #undef BOOST_MULTI_INDEX_VARTEMPL_TO_PLACEMENT_NEW_AUX
  166. #undef BOOST_MULTI_INDEX_VARTEMPL_TO_PLACEMENT_NEW
  167. } /* namespace multi_index::detail */
  168. } /* namespace multi_index */
  169. } /* namespace boost */
  170. #else
  171. /* native variadic templates support */
  172. #include <utility>
  173. #define BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK typename... Args
  174. #define BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK Args&&... args
  175. #define BOOST_MULTI_INDEX_FORWARD_PARAM_PACK std::forward<Args>(args)...
  176. #define BOOST_MULTI_INDEX_NULL_PARAM_PACK
  177. #define BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL( \
  178. ret,name_from,name_to) \
  179. template<typename... Args> ret name_from(Args&&... args) \
  180. { \
  181. return name_to(std::forward<Args>(args)...); \
  182. }
  183. #define BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL_EXTRA_ARG( \
  184. ret,name_from,name_to,extra_arg_type,extra_arg_name) \
  185. template<typename... Args> ret name_from( \
  186. extra_arg_type extra_arg_name,Args&&... args) \
  187. { \
  188. return name_to(extra_arg_name,std::forward<Args>(args)...); \
  189. }
  190. namespace boost{
  191. namespace multi_index{
  192. namespace detail{
  193. template<typename Value,typename... Args>
  194. Value* vartempl_placement_new(Value*x,Args&&... args)
  195. {
  196. return new(x) Value(std::forward<Args>(args)...);
  197. }
  198. } /* namespace multi_index::detail */
  199. } /* namespace multi_index */
  200. } /* namespace boost */
  201. #endif
  202. #endif