You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

496 lines
18 KiB

  1. /*
  2. Copyright 2005-2014 Intel Corporation. All Rights Reserved.
  3. This file is part of Threading Building Blocks.
  4. Threading Building Blocks is free software; you can redistribute it
  5. and/or modify it under the terms of the GNU General Public License
  6. version 2 as published by the Free Software Foundation.
  7. Threading Building Blocks is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  9. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with Threading Building Blocks; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. As a special exception, you may use this file as part of a free software
  15. library without restriction. Specifically, if other files instantiate
  16. templates or use macros or inline functions from this file, or you compile
  17. this file and link it with other files to produce an executable, this
  18. file does not by itself cause the resulting executable to be covered by
  19. the GNU General Public License. This exception does not however
  20. invalidate any other reasons why the executable file might be covered by
  21. the GNU General Public License.
  22. */
  23. #ifndef __TBB_tuple_H
  24. #define __TBB_tuple_H
  25. #include <utility>
  26. #include "../tbb_stddef.h"
  27. // build preprocessor variables for varying number of arguments
  28. // Need the leading comma so the empty __TBB_T_PACK will not cause a syntax error.
  29. #if __TBB_VARIADIC_MAX <= 5
  30. #define __TBB_T_PACK
  31. #define __TBB_U_PACK
  32. #define __TBB_TYPENAME_T_PACK
  33. #define __TBB_TYPENAME_U_PACK
  34. #define __TBB_NULL_TYPE_PACK
  35. #define __TBB_REF_T_PARAM_PACK
  36. #define __TBB_CONST_REF_T_PARAM_PACK
  37. #define __TBB_T_PARAM_LIST_PACK
  38. #define __TBB_CONST_NULL_REF_PACK
  39. //
  40. #elif __TBB_VARIADIC_MAX == 6
  41. #define __TBB_T_PACK ,__T5
  42. #define __TBB_U_PACK ,__U5
  43. #define __TBB_TYPENAME_T_PACK , typename __T5
  44. #define __TBB_TYPENAME_U_PACK , typename __U5
  45. #define __TBB_NULL_TYPE_PACK , null_type
  46. #define __TBB_REF_T_PARAM_PACK ,__T5& t5
  47. #define __TBB_CONST_REF_T_PARAM_PACK ,const __T5& t5
  48. #define __TBB_T_PARAM_LIST_PACK ,t5
  49. #define __TBB_CONST_NULL_REF_PACK , const null_type&
  50. //
  51. #elif __TBB_VARIADIC_MAX == 7
  52. #define __TBB_T_PACK ,__T5, __T6
  53. #define __TBB_U_PACK ,__U5, __U6
  54. #define __TBB_TYPENAME_T_PACK , typename __T5 , typename __T6
  55. #define __TBB_TYPENAME_U_PACK , typename __U5 , typename __U6
  56. #define __TBB_NULL_TYPE_PACK , null_type, null_type
  57. #define __TBB_REF_T_PARAM_PACK ,__T5& t5, __T6& t6
  58. #define __TBB_CONST_REF_T_PARAM_PACK ,const __T5& t5, const __T6& t6
  59. #define __TBB_T_PARAM_LIST_PACK ,t5 ,t6
  60. #define __TBB_CONST_NULL_REF_PACK , const null_type&, const null_type&
  61. //
  62. #elif __TBB_VARIADIC_MAX == 8
  63. #define __TBB_T_PACK ,__T5, __T6, __T7
  64. #define __TBB_U_PACK ,__U5, __U6, __U7
  65. #define __TBB_TYPENAME_T_PACK , typename __T5 , typename __T6, typename __T7
  66. #define __TBB_TYPENAME_U_PACK , typename __U5 , typename __U6, typename __U7
  67. #define __TBB_NULL_TYPE_PACK , null_type, null_type, null_type
  68. #define __TBB_REF_T_PARAM_PACK ,__T5& t5, __T6& t6, __T7& t7
  69. #define __TBB_CONST_REF_T_PARAM_PACK , const __T5& t5, const __T6& t6, const __T7& t7
  70. #define __TBB_T_PARAM_LIST_PACK ,t5 ,t6 ,t7
  71. #define __TBB_CONST_NULL_REF_PACK , const null_type&, const null_type&, const null_type&
  72. //
  73. #elif __TBB_VARIADIC_MAX == 9
  74. #define __TBB_T_PACK ,__T5, __T6, __T7, __T8
  75. #define __TBB_U_PACK ,__U5, __U6, __U7, __U8
  76. #define __TBB_TYPENAME_T_PACK , typename __T5, typename __T6, typename __T7, typename __T8
  77. #define __TBB_TYPENAME_U_PACK , typename __U5, typename __U6, typename __U7, typename __U8
  78. #define __TBB_NULL_TYPE_PACK , null_type, null_type, null_type, null_type
  79. #define __TBB_REF_T_PARAM_PACK ,__T5& t5, __T6& t6, __T7& t7, __T8& t8
  80. #define __TBB_CONST_REF_T_PARAM_PACK , const __T5& t5, const __T6& t6, const __T7& t7, const __T8& t8
  81. #define __TBB_T_PARAM_LIST_PACK ,t5 ,t6 ,t7 ,t8
  82. #define __TBB_CONST_NULL_REF_PACK , const null_type&, const null_type&, const null_type&, const null_type&
  83. //
  84. #elif __TBB_VARIADIC_MAX >= 10
  85. #define __TBB_T_PACK ,__T5, __T6, __T7, __T8, __T9
  86. #define __TBB_U_PACK ,__U5, __U6, __U7, __U8, __U9
  87. #define __TBB_TYPENAME_T_PACK , typename __T5, typename __T6, typename __T7, typename __T8, typename __T9
  88. #define __TBB_TYPENAME_U_PACK , typename __U5, typename __U6, typename __U7, typename __U8, typename __U9
  89. #define __TBB_NULL_TYPE_PACK , null_type, null_type, null_type, null_type, null_type
  90. #define __TBB_REF_T_PARAM_PACK ,__T5& t5, __T6& t6, __T7& t7, __T8& t8, __T9& t9
  91. #define __TBB_CONST_REF_T_PARAM_PACK , const __T5& t5, const __T6& t6, const __T7& t7, const __T8& t8, const __T9& t9
  92. #define __TBB_T_PARAM_LIST_PACK ,t5 ,t6 ,t7 ,t8 ,t9
  93. #define __TBB_CONST_NULL_REF_PACK , const null_type&, const null_type&, const null_type&, const null_type&, const null_type&
  94. #endif
  95. namespace tbb {
  96. namespace interface5 {
  97. namespace internal {
  98. struct null_type { };
  99. }
  100. using internal::null_type;
  101. // tuple forward declaration
  102. template <typename __T0=null_type, typename __T1=null_type, typename __T2=null_type,
  103. typename __T3=null_type, typename __T4=null_type
  104. #if __TBB_VARIADIC_MAX >= 6
  105. , typename __T5=null_type
  106. #if __TBB_VARIADIC_MAX >= 7
  107. , typename __T6=null_type
  108. #if __TBB_VARIADIC_MAX >= 8
  109. , typename __T7=null_type
  110. #if __TBB_VARIADIC_MAX >= 9
  111. , typename __T8=null_type
  112. #if __TBB_VARIADIC_MAX >= 10
  113. , typename __T9=null_type
  114. #endif
  115. #endif
  116. #endif
  117. #endif
  118. #endif
  119. >
  120. class tuple;
  121. namespace internal {
  122. // const null_type temp
  123. inline const null_type cnull() { return null_type(); }
  124. // cons forward declaration
  125. template <typename __HT, typename __TT> struct cons;
  126. // type of a component of the cons
  127. template<int __N, typename __T>
  128. struct component {
  129. typedef typename __T::tail_type next;
  130. typedef typename component<__N-1,next>::type type;
  131. };
  132. template<typename __T>
  133. struct component<0,__T> {
  134. typedef typename __T::head_type type;
  135. };
  136. template<>
  137. struct component<0,null_type> {
  138. typedef null_type type;
  139. };
  140. // const version of component
  141. template<int __N, typename __T>
  142. struct component<__N, const __T>
  143. {
  144. typedef typename __T::tail_type next;
  145. typedef const typename component<__N-1,next>::type type;
  146. };
  147. template<typename __T>
  148. struct component<0, const __T>
  149. {
  150. typedef const typename __T::head_type type;
  151. };
  152. // helper class for getting components of cons
  153. template< int __N>
  154. struct get_helper {
  155. template<typename __HT, typename __TT>
  156. inline static typename component<__N, cons<__HT,__TT> >::type& get(cons<__HT,__TT>& ti) {
  157. return get_helper<__N-1>::get(ti.tail);
  158. }
  159. template<typename __HT, typename __TT>
  160. inline static typename component<__N, cons<__HT,__TT> >::type const& get(const cons<__HT,__TT>& ti) {
  161. return get_helper<__N-1>::get(ti.tail);
  162. }
  163. };
  164. template<>
  165. struct get_helper<0> {
  166. template<typename __HT, typename __TT>
  167. inline static typename component<0, cons<__HT,__TT> >::type& get(cons<__HT,__TT>& ti) {
  168. return ti.head;
  169. }
  170. template<typename __HT, typename __TT>
  171. inline static typename component<0, cons<__HT,__TT> >::type const& get(const cons<__HT,__TT>& ti) {
  172. return ti.head;
  173. }
  174. };
  175. // traits adaptor
  176. template <typename __T0, typename __T1, typename __T2, typename __T3, typename __T4 __TBB_TYPENAME_T_PACK>
  177. struct tuple_traits {
  178. typedef cons <__T0, typename tuple_traits<__T1, __T2, __T3, __T4 __TBB_T_PACK , null_type>::U > U;
  179. };
  180. template <typename __T0>
  181. struct tuple_traits<__T0, null_type, null_type, null_type, null_type __TBB_NULL_TYPE_PACK > {
  182. typedef cons<__T0, null_type> U;
  183. };
  184. template<>
  185. struct tuple_traits<null_type, null_type, null_type, null_type, null_type __TBB_NULL_TYPE_PACK > {
  186. typedef null_type U;
  187. };
  188. // core cons defs
  189. template <typename __HT, typename __TT>
  190. struct cons{
  191. typedef __HT head_type;
  192. typedef __TT tail_type;
  193. head_type head;
  194. tail_type tail;
  195. static const int length = 1 + tail_type::length;
  196. // default constructors
  197. explicit cons() : head(), tail() { }
  198. // non-default constructors
  199. cons(head_type& h, const tail_type& t) : head(h), tail(t) { }
  200. template <typename __T0, typename __T1, typename __T2, typename __T3, typename __T4 __TBB_TYPENAME_T_PACK >
  201. cons(const __T0& t0, const __T1& t1, const __T2& t2, const __T3& t3, const __T4& t4 __TBB_CONST_REF_T_PARAM_PACK) :
  202. head(t0), tail(t1, t2, t3, t4 __TBB_T_PARAM_LIST_PACK, cnull()) { }
  203. template <typename __T0, typename __T1, typename __T2, typename __T3, typename __T4 __TBB_TYPENAME_T_PACK >
  204. cons(__T0& t0, __T1& t1, __T2& t2, __T3& t3, __T4& t4 __TBB_REF_T_PARAM_PACK) :
  205. head(t0), tail(t1, t2, t3, t4 __TBB_T_PARAM_LIST_PACK , cnull()) { }
  206. template <typename __HT1, typename __TT1>
  207. cons(const cons<__HT1,__TT1>& other) : head(other.head), tail(other.tail) { }
  208. cons& operator=(const cons& other) { head = other.head; tail = other.tail; return *this; }
  209. friend bool operator==(const cons& me, const cons& other) {
  210. return me.head == other.head && me.tail == other.tail;
  211. }
  212. friend bool operator<(const cons& me, const cons& other) {
  213. return me.head < other.head || (!(other.head < me.head) && me.tail < other.tail);
  214. }
  215. friend bool operator>(const cons& me, const cons& other) { return other<me; }
  216. friend bool operator!=(const cons& me, const cons& other) { return !(me==other); }
  217. friend bool operator>=(const cons& me, const cons& other) { return !(me<other); }
  218. friend bool operator<=(const cons& me, const cons& other) { return !(me>other); }
  219. template<typename __HT1, typename __TT1>
  220. friend bool operator==(const cons<__HT,__TT>& me, const cons<__HT1,__TT1>& other) {
  221. return me.head == other.head && me.tail == other.tail;
  222. }
  223. template<typename __HT1, typename __TT1>
  224. friend bool operator<(const cons<__HT,__TT>& me, const cons<__HT1,__TT1>& other) {
  225. return me.head < other.head || (!(other.head < me.head) && me.tail < other.tail);
  226. }
  227. template<typename __HT1, typename __TT1>
  228. friend bool operator>(const cons<__HT,__TT>& me, const cons<__HT1,__TT1>& other) { return other<me; }
  229. template<typename __HT1, typename __TT1>
  230. friend bool operator!=(const cons<__HT,__TT>& me, const cons<__HT1,__TT1>& other) { return !(me==other); }
  231. template<typename __HT1, typename __TT1>
  232. friend bool operator>=(const cons<__HT,__TT>& me, const cons<__HT1,__TT1>& other) { return !(me<other); }
  233. template<typename __HT1, typename __TT1>
  234. friend bool operator<=(const cons<__HT,__TT>& me, const cons<__HT1,__TT1>& other) { return !(me>other); }
  235. }; // cons
  236. template <typename __HT>
  237. struct cons<__HT,null_type> {
  238. typedef __HT head_type;
  239. typedef null_type tail_type;
  240. head_type head;
  241. static const int length = 1;
  242. // default constructor
  243. cons() : head() { /*std::cout << "default constructor 1\n";*/ }
  244. cons(const null_type&, const null_type&, const null_type&, const null_type&, const null_type& __TBB_CONST_NULL_REF_PACK) : head() { /*std::cout << "default constructor 2\n";*/ }
  245. // non-default constructor
  246. template<typename __T1>
  247. cons(__T1& t1, const null_type&, const null_type&, const null_type&, const null_type& __TBB_CONST_NULL_REF_PACK) : head(t1) { /*std::cout << "non-default a1, t1== " << t1 << "\n";*/}
  248. cons(head_type& h, const null_type& = null_type() ) : head(h) { }
  249. cons(const head_type& t0, const null_type&, const null_type&, const null_type&, const null_type& __TBB_CONST_NULL_REF_PACK) : head(t0) { }
  250. // converting constructor
  251. template<typename __HT1>
  252. cons(__HT1 h1, const null_type&, const null_type&, const null_type&, const null_type& __TBB_CONST_NULL_REF_PACK) : head(h1) { }
  253. // copy constructor
  254. template<typename __HT1>
  255. cons( const cons<__HT1, null_type>& other) : head(other.head) { }
  256. // assignment operator
  257. cons& operator=(const cons& other) { head = other.head; return *this; }
  258. friend bool operator==(const cons& me, const cons& other) { return me.head == other.head; }
  259. friend bool operator<(const cons& me, const cons& other) { return me.head < other.head; }
  260. friend bool operator>(const cons& me, const cons& other) { return other<me; }
  261. friend bool operator!=(const cons& me, const cons& other) {return !(me==other); }
  262. friend bool operator<=(const cons& me, const cons& other) {return !(me>other); }
  263. friend bool operator>=(const cons& me, const cons& other) {return !(me<other); }
  264. template<typename __HT1>
  265. friend bool operator==(const cons<__HT,null_type>& me, const cons<__HT1,null_type>& other) {
  266. return me.head == other.head;
  267. }
  268. template<typename __HT1>
  269. friend bool operator<(const cons<__HT,null_type>& me, const cons<__HT1,null_type>& other) {
  270. return me.head < other.head;
  271. }
  272. template<typename __HT1>
  273. friend bool operator>(const cons<__HT,null_type>& me, const cons<__HT1,null_type>& other) { return other<me; }
  274. template<typename __HT1>
  275. friend bool operator!=(const cons<__HT,null_type>& me, const cons<__HT1,null_type>& other) { return !(me==other); }
  276. template<typename __HT1>
  277. friend bool operator<=(const cons<__HT,null_type>& me, const cons<__HT1,null_type>& other) { return !(me>other); }
  278. template<typename __HT1>
  279. friend bool operator>=(const cons<__HT,null_type>& me, const cons<__HT1,null_type>& other) { return !(me<other); }
  280. }; // cons
  281. template <>
  282. struct cons<null_type,null_type> { typedef null_type tail_type; static const int length = 0; };
  283. // wrapper for default constructor
  284. template<typename __T>
  285. inline const __T wrap_dcons(__T*) { return __T(); }
  286. } // namespace internal
  287. // tuple definition
  288. template<typename __T0, typename __T1, typename __T2, typename __T3, typename __T4 __TBB_TYPENAME_T_PACK >
  289. class tuple : public internal::tuple_traits<__T0, __T1, __T2, __T3, __T4 __TBB_T_PACK >::U {
  290. // friends
  291. template <typename __T> friend class tuple_size;
  292. template<int __N, typename __T> friend struct tuple_element;
  293. // stl components
  294. typedef tuple<__T0,__T1,__T2,__T3,__T4 __TBB_T_PACK > value_type;
  295. typedef value_type *pointer;
  296. typedef const value_type *const_pointer;
  297. typedef value_type &reference;
  298. typedef const value_type &const_reference;
  299. typedef size_t size_type;
  300. typedef typename internal::tuple_traits<__T0,__T1,__T2,__T3, __T4 __TBB_T_PACK >::U my_cons;
  301. public:
  302. tuple(const __T0& t0=internal::wrap_dcons((__T0*)NULL)
  303. ,const __T1& t1=internal::wrap_dcons((__T1*)NULL)
  304. ,const __T2& t2=internal::wrap_dcons((__T2*)NULL)
  305. ,const __T3& t3=internal::wrap_dcons((__T3*)NULL)
  306. ,const __T4& t4=internal::wrap_dcons((__T4*)NULL)
  307. #if __TBB_VARIADIC_MAX >= 6
  308. ,const __T5& t5=internal::wrap_dcons((__T5*)NULL)
  309. #if __TBB_VARIADIC_MAX >= 7
  310. ,const __T6& t6=internal::wrap_dcons((__T6*)NULL)
  311. #if __TBB_VARIADIC_MAX >= 8
  312. ,const __T7& t7=internal::wrap_dcons((__T7*)NULL)
  313. #if __TBB_VARIADIC_MAX >= 9
  314. ,const __T8& t8=internal::wrap_dcons((__T8*)NULL)
  315. #if __TBB_VARIADIC_MAX >= 10
  316. ,const __T9& t9=internal::wrap_dcons((__T9*)NULL)
  317. #endif
  318. #endif
  319. #endif
  320. #endif
  321. #endif
  322. ) :
  323. my_cons(t0,t1,t2,t3,t4 __TBB_T_PARAM_LIST_PACK) { }
  324. template<int __N>
  325. struct internal_tuple_element {
  326. typedef typename internal::component<__N,my_cons>::type type;
  327. };
  328. template<int __N>
  329. typename internal_tuple_element<__N>::type& get() { return internal::get_helper<__N>::get(*this); }
  330. template<int __N>
  331. typename internal_tuple_element<__N>::type const& get() const { return internal::get_helper<__N>::get(*this); }
  332. template<typename __U1, typename __U2>
  333. tuple& operator=(const internal::cons<__U1,__U2>& other) {
  334. my_cons::operator=(other);
  335. return *this;
  336. }
  337. template<typename __U1, typename __U2>
  338. tuple& operator=(const std::pair<__U1,__U2>& other) {
  339. // __TBB_ASSERT(tuple_size<value_type>::value == 2, "Invalid size for pair to tuple assignment");
  340. this->head = other.first;
  341. this->tail.head = other.second;
  342. return *this;
  343. }
  344. friend bool operator==(const tuple& me, const tuple& other) {return static_cast<const my_cons &>(me)==(other);}
  345. friend bool operator<(const tuple& me, const tuple& other) {return static_cast<const my_cons &>(me)<(other);}
  346. friend bool operator>(const tuple& me, const tuple& other) {return static_cast<const my_cons &>(me)>(other);}
  347. friend bool operator!=(const tuple& me, const tuple& other) {return static_cast<const my_cons &>(me)!=(other);}
  348. friend bool operator>=(const tuple& me, const tuple& other) {return static_cast<const my_cons &>(me)>=(other);}
  349. friend bool operator<=(const tuple& me, const tuple& other) {return static_cast<const my_cons &>(me)<=(other);}
  350. }; // tuple
  351. // empty tuple
  352. template<>
  353. class tuple<null_type, null_type, null_type, null_type, null_type __TBB_NULL_TYPE_PACK > : public null_type {
  354. };
  355. // helper classes
  356. template < typename __T>
  357. class tuple_size {
  358. public:
  359. static const size_t value = 1 + tuple_size<typename __T::tail_type>::value;
  360. };
  361. template <>
  362. class tuple_size<tuple<> > {
  363. public:
  364. static const size_t value = 0;
  365. };
  366. template <>
  367. class tuple_size<null_type> {
  368. public:
  369. static const size_t value = 0;
  370. };
  371. template<int __N, typename __T>
  372. struct tuple_element {
  373. typedef typename internal::component<__N, typename __T::my_cons>::type type;
  374. };
  375. template<int __N, typename __T0, typename __T1, typename __T2, typename __T3, typename __T4 __TBB_TYPENAME_T_PACK >
  376. inline static typename tuple_element<__N,tuple<__T0,__T1,__T2,__T3,__T4 __TBB_T_PACK > >::type&
  377. get(tuple<__T0,__T1,__T2,__T3,__T4 __TBB_T_PACK >& t) { return internal::get_helper<__N>::get(t); }
  378. template<int __N, typename __T0, typename __T1, typename __T2, typename __T3, typename __T4 __TBB_TYPENAME_T_PACK >
  379. inline static typename tuple_element<__N,tuple<__T0,__T1,__T2,__T3,__T4 __TBB_T_PACK > >::type const&
  380. get(const tuple<__T0,__T1,__T2,__T3,__T4 __TBB_T_PACK >& t) { return internal::get_helper<__N>::get(t); }
  381. } // interface5
  382. } // tbb
  383. #if !__TBB_CPP11_TUPLE_PRESENT
  384. namespace tbb {
  385. namespace flow {
  386. using tbb::interface5::tuple;
  387. using tbb::interface5::tuple_size;
  388. using tbb::interface5::tuple_element;
  389. using tbb::interface5::get;
  390. }
  391. }
  392. #endif
  393. #undef __TBB_T_PACK
  394. #undef __TBB_U_PACK
  395. #undef __TBB_TYPENAME_T_PACK
  396. #undef __TBB_TYPENAME_U_PACK
  397. #undef __TBB_NULL_TYPE_PACK
  398. #undef __TBB_REF_T_PARAM_PACK
  399. #undef __TBB_CONST_REF_T_PARAM_PACK
  400. #undef __TBB_T_PARAM_LIST_PACK
  401. #undef __TBB_CONST_NULL_REF_PACK
  402. #endif /* __TBB_tuple_H */