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-2013 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 */