mpl.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2006-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 BOOST_INTRUSIVE_DETAIL_MPL_HPP
  13. #define BOOST_INTRUSIVE_DETAIL_MPL_HPP
  14. #include <boost/intrusive/detail/config_begin.hpp>
  15. #include <cstddef>
  16. namespace boost {
  17. namespace intrusive {
  18. namespace detail {
  19. typedef char one;
  20. struct two {one _[2];};
  21. template< bool C_ >
  22. struct bool_
  23. {
  24. static const bool value = C_;
  25. };
  26. typedef bool_<true> true_;
  27. typedef bool_<false> false_;
  28. typedef true_ true_type;
  29. typedef false_ false_type;
  30. typedef char yes_type;
  31. struct no_type
  32. {
  33. char padding[8];
  34. };
  35. template <bool B, class T = void>
  36. struct enable_if_c {
  37. typedef T type;
  38. };
  39. template <class T>
  40. struct enable_if_c<false, T> {};
  41. template <class Cond, class T = void>
  42. struct enable_if : public enable_if_c<Cond::value, T>{};
  43. template<class F, class Param>
  44. struct apply
  45. {
  46. typedef typename F::template apply<Param>::type type;
  47. };
  48. template <class T, class U>
  49. class is_convertible
  50. {
  51. typedef char true_t;
  52. class false_t { char dummy[2]; };
  53. static true_t dispatch(U);
  54. static false_t dispatch(...);
  55. static const T &trigger();
  56. public:
  57. static const bool value = sizeof(dispatch(trigger())) == sizeof(true_t);
  58. };
  59. template<
  60. bool C
  61. , typename T1
  62. , typename T2
  63. >
  64. struct if_c
  65. {
  66. typedef T1 type;
  67. };
  68. template<
  69. typename T1
  70. , typename T2
  71. >
  72. struct if_c<false,T1,T2>
  73. {
  74. typedef T2 type;
  75. };
  76. template<
  77. typename C
  78. , typename T1
  79. , typename T2
  80. >
  81. struct if_
  82. {
  83. typedef typename if_c<0 != C::value, T1, T2>::type type;
  84. };
  85. template<
  86. bool C
  87. , typename F1
  88. , typename F2
  89. >
  90. struct eval_if_c
  91. : if_c<C,F1,F2>::type
  92. {};
  93. template<
  94. typename C
  95. , typename T1
  96. , typename T2
  97. >
  98. struct eval_if
  99. : if_<C,T1,T2>::type
  100. {};
  101. // identity is an extension: it is not part of the standard.
  102. template <class T>
  103. struct identity
  104. {
  105. typedef T type;
  106. };
  107. #if defined(BOOST_MSVC) || defined(__BORLANDC_)
  108. #define BOOST_INTRUSIVE_TT_DECL __cdecl
  109. #else
  110. #define BOOST_INTRUSIVE_TT_DECL
  111. #endif
  112. #if defined(_MSC_EXTENSIONS) && !defined(__BORLAND__) && !defined(_WIN64) && !defined(UNDER_CE)
  113. #define BOOST_INTRUSIVE_TT_TEST_MSC_FUNC_SIGS
  114. #endif
  115. template <typename T>
  116. struct is_unary_or_binary_function_impl
  117. { static const bool value = false; };
  118. // see boost ticket #4094
  119. // avoid duplicate definitions of is_unary_or_binary_function_impl
  120. #ifndef BOOST_INTRUSIVE_TT_TEST_MSC_FUNC_SIGS
  121. template <typename R>
  122. struct is_unary_or_binary_function_impl<R (*)()>
  123. { static const bool value = true; };
  124. template <typename R>
  125. struct is_unary_or_binary_function_impl<R (*)(...)>
  126. { static const bool value = true; };
  127. #else // BOOST_INTRUSIVE_TT_TEST_MSC_FUNC_SIGS
  128. template <typename R>
  129. struct is_unary_or_binary_function_impl<R (__stdcall*)()>
  130. { static const bool value = true; };
  131. #ifndef _MANAGED
  132. template <typename R>
  133. struct is_unary_or_binary_function_impl<R (__fastcall*)()>
  134. { static const bool value = true; };
  135. #endif
  136. template <typename R>
  137. struct is_unary_or_binary_function_impl<R (__cdecl*)()>
  138. { static const bool value = true; };
  139. template <typename R>
  140. struct is_unary_or_binary_function_impl<R (__cdecl*)(...)>
  141. { static const bool value = true; };
  142. #endif
  143. // see boost ticket #4094
  144. // avoid duplicate definitions of is_unary_or_binary_function_impl
  145. #ifndef BOOST_INTRUSIVE_TT_TEST_MSC_FUNC_SIGS
  146. template <typename R, class T0>
  147. struct is_unary_or_binary_function_impl<R (*)(T0)>
  148. { static const bool value = true; };
  149. template <typename R, class T0>
  150. struct is_unary_or_binary_function_impl<R (*)(T0...)>
  151. { static const bool value = true; };
  152. #else // BOOST_INTRUSIVE_TT_TEST_MSC_FUNC_SIGS
  153. template <typename R, class T0>
  154. struct is_unary_or_binary_function_impl<R (__stdcall*)(T0)>
  155. { static const bool value = true; };
  156. #ifndef _MANAGED
  157. template <typename R, class T0>
  158. struct is_unary_or_binary_function_impl<R (__fastcall*)(T0)>
  159. { static const bool value = true; };
  160. #endif
  161. template <typename R, class T0>
  162. struct is_unary_or_binary_function_impl<R (__cdecl*)(T0)>
  163. { static const bool value = true; };
  164. template <typename R, class T0>
  165. struct is_unary_or_binary_function_impl<R (__cdecl*)(T0...)>
  166. { static const bool value = true; };
  167. #endif
  168. // see boost ticket #4094
  169. // avoid duplicate definitions of is_unary_or_binary_function_impl
  170. #ifndef BOOST_INTRUSIVE_TT_TEST_MSC_FUNC_SIGS
  171. template <typename R, class T0, class T1>
  172. struct is_unary_or_binary_function_impl<R (*)(T0, T1)>
  173. { static const bool value = true; };
  174. template <typename R, class T0, class T1>
  175. struct is_unary_or_binary_function_impl<R (*)(T0, T1...)>
  176. { static const bool value = true; };
  177. #else // BOOST_INTRUSIVE_TT_TEST_MSC_FUNC_SIGS
  178. template <typename R, class T0, class T1>
  179. struct is_unary_or_binary_function_impl<R (__stdcall*)(T0, T1)>
  180. { static const bool value = true; };
  181. #ifndef _MANAGED
  182. template <typename R, class T0, class T1>
  183. struct is_unary_or_binary_function_impl<R (__fastcall*)(T0, T1)>
  184. { static const bool value = true; };
  185. #endif
  186. template <typename R, class T0, class T1>
  187. struct is_unary_or_binary_function_impl<R (__cdecl*)(T0, T1)>
  188. { static const bool value = true; };
  189. template <typename R, class T0, class T1>
  190. struct is_unary_or_binary_function_impl<R (__cdecl*)(T0, T1...)>
  191. { static const bool value = true; };
  192. #endif
  193. template <typename T>
  194. struct is_unary_or_binary_function_impl<T&>
  195. { static const bool value = false; };
  196. template<typename T>
  197. struct is_unary_or_binary_function
  198. { static const bool value = is_unary_or_binary_function_impl<T>::value; };
  199. //boost::alignment_of yields to 10K lines of preprocessed code, so we
  200. //need an alternative
  201. template <typename T> struct alignment_of;
  202. template <typename T>
  203. struct alignment_of_hack
  204. {
  205. char c;
  206. T t;
  207. alignment_of_hack();
  208. };
  209. template <unsigned A, unsigned S>
  210. struct alignment_logic
  211. {
  212. static const std::size_t value = A < S ? A : S;
  213. };
  214. template< typename T >
  215. struct alignment_of
  216. {
  217. static const std::size_t value = alignment_logic
  218. < sizeof(alignment_of_hack<T>) - sizeof(T)
  219. , sizeof(T)
  220. >::value;
  221. };
  222. template <typename T, typename U>
  223. struct is_same
  224. {
  225. static const bool value = false;
  226. };
  227. template <typename T>
  228. struct is_same<T, T>
  229. {
  230. static const bool value = true;
  231. };
  232. template<typename T>
  233. struct add_const
  234. { typedef const T type; };
  235. template<typename T>
  236. struct remove_const
  237. { typedef T type; };
  238. template<typename T>
  239. struct remove_const<const T>
  240. { typedef T type; };
  241. template<typename T>
  242. struct remove_cv
  243. { typedef T type; };
  244. template<typename T>
  245. struct remove_cv<const T>
  246. { typedef T type; };
  247. template<typename T>
  248. struct remove_cv<const volatile T>
  249. { typedef T type; };
  250. template<typename T>
  251. struct remove_cv<volatile T>
  252. { typedef T type; };
  253. template<class T>
  254. struct remove_reference
  255. {
  256. typedef T type;
  257. };
  258. template<class T>
  259. struct remove_reference<T&>
  260. {
  261. typedef T type;
  262. };
  263. template<class Class>
  264. class is_empty_class
  265. {
  266. template <typename T>
  267. struct empty_helper_t1 : public T
  268. {
  269. empty_helper_t1();
  270. int i[256];
  271. };
  272. struct empty_helper_t2
  273. { int i[256]; };
  274. public:
  275. static const bool value = sizeof(empty_helper_t1<Class>) == sizeof(empty_helper_t2);
  276. };
  277. template<std::size_t S>
  278. struct ls_zeros
  279. {
  280. static const std::size_t value = (S & std::size_t(1)) ? 0 : (1 + ls_zeros<(S>>1u)>::value);
  281. };
  282. template<>
  283. struct ls_zeros<0>
  284. {
  285. static const std::size_t value = 0;
  286. };
  287. template<>
  288. struct ls_zeros<1>
  289. {
  290. static const std::size_t value = 0;
  291. };
  292. } //namespace detail
  293. } //namespace intrusive
  294. } //namespace boost
  295. #include <boost/intrusive/detail/config_end.hpp>
  296. #endif //BOOST_INTRUSIVE_DETAIL_MPL_HPP