ob_compressed_pair.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. // (C) Copyright Steve Cleary, Beman Dawes, Howard Hinnant & John Maddock 2000.
  2. // Use, modification and distribution are subject to the Boost Software License,
  3. // Version 1.0. (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/utility for most recent version including documentation.
  7. // see libs/utility/compressed_pair.hpp
  8. //
  9. /* Release notes:
  10. 20 Jan 2001:
  11. Fixed obvious bugs (David Abrahams)
  12. 07 Oct 2000:
  13. Added better single argument constructor support.
  14. 03 Oct 2000:
  15. Added VC6 support (JM).
  16. 23rd July 2000:
  17. Additional comments added. (JM)
  18. Jan 2000:
  19. Original version: this version crippled for use with crippled compilers
  20. - John Maddock Jan 2000.
  21. */
  22. #ifndef BOOST_OB_COMPRESSED_PAIR_HPP
  23. #define BOOST_OB_COMPRESSED_PAIR_HPP
  24. #include <algorithm>
  25. #ifndef BOOST_OBJECT_TYPE_TRAITS_HPP
  26. #include <boost/type_traits/object_traits.hpp>
  27. #endif
  28. #ifndef BOOST_SAME_TRAITS_HPP
  29. #include <boost/type_traits/same_traits.hpp>
  30. #endif
  31. #ifndef BOOST_CALL_TRAITS_HPP
  32. #include <boost/call_traits.hpp>
  33. #endif
  34. namespace boost
  35. {
  36. #ifdef BOOST_MSVC6_MEMBER_TEMPLATES
  37. //
  38. // use member templates to emulate
  39. // partial specialisation. Note that due to
  40. // problems with overload resolution with VC6
  41. // each of the compressed_pair versions that follow
  42. // have one template single-argument constructor
  43. // in place of two specific constructors:
  44. //
  45. template <class T1, class T2>
  46. class compressed_pair;
  47. namespace detail{
  48. template <class A, class T1, class T2>
  49. struct best_conversion_traits
  50. {
  51. typedef char one;
  52. typedef char (&two)[2];
  53. static A a;
  54. static one test(T1);
  55. static two test(T2);
  56. enum { value = sizeof(test(a)) };
  57. };
  58. template <int>
  59. struct init_one;
  60. template <>
  61. struct init_one<1>
  62. {
  63. template <class A, class T1, class T2>
  64. static void init(const A& a, T1* p1, T2*)
  65. {
  66. *p1 = a;
  67. }
  68. };
  69. template <>
  70. struct init_one<2>
  71. {
  72. template <class A, class T1, class T2>
  73. static void init(const A& a, T1*, T2* p2)
  74. {
  75. *p2 = a;
  76. }
  77. };
  78. // T1 != T2, both non-empty
  79. template <class T1, class T2>
  80. class compressed_pair_0
  81. {
  82. private:
  83. T1 _first;
  84. T2 _second;
  85. public:
  86. typedef T1 first_type;
  87. typedef T2 second_type;
  88. typedef typename call_traits<first_type>::param_type first_param_type;
  89. typedef typename call_traits<second_type>::param_type second_param_type;
  90. typedef typename call_traits<first_type>::reference first_reference;
  91. typedef typename call_traits<second_type>::reference second_reference;
  92. typedef typename call_traits<first_type>::const_reference first_const_reference;
  93. typedef typename call_traits<second_type>::const_reference second_const_reference;
  94. compressed_pair_0() : _first(), _second() {}
  95. compressed_pair_0(first_param_type x, second_param_type y) : _first(x), _second(y) {}
  96. template <class A>
  97. explicit compressed_pair_0(const A& val)
  98. {
  99. init_one<best_conversion_traits<A, T1, T2>::value>::init(val, &_first, &_second);
  100. }
  101. compressed_pair_0(const ::boost::compressed_pair<T1,T2>& x)
  102. : _first(x.first()), _second(x.second()) {}
  103. #if 0
  104. compressed_pair_0& operator=(const compressed_pair_0& x) {
  105. cout << "assigning compressed pair 0" << endl;
  106. _first = x._first;
  107. _second = x._second;
  108. cout << "finished assigning compressed pair 0" << endl;
  109. return *this;
  110. }
  111. #endif
  112. first_reference first() { return _first; }
  113. first_const_reference first() const { return _first; }
  114. second_reference second() { return _second; }
  115. second_const_reference second() const { return _second; }
  116. void swap(compressed_pair_0& y)
  117. {
  118. using std::swap;
  119. swap(_first, y._first);
  120. swap(_second, y._second);
  121. }
  122. };
  123. // T1 != T2, T2 empty
  124. template <class T1, class T2>
  125. class compressed_pair_1 : T2
  126. {
  127. private:
  128. T1 _first;
  129. public:
  130. typedef T1 first_type;
  131. typedef T2 second_type;
  132. typedef typename call_traits<first_type>::param_type first_param_type;
  133. typedef typename call_traits<second_type>::param_type second_param_type;
  134. typedef typename call_traits<first_type>::reference first_reference;
  135. typedef typename call_traits<second_type>::reference second_reference;
  136. typedef typename call_traits<first_type>::const_reference first_const_reference;
  137. typedef typename call_traits<second_type>::const_reference second_const_reference;
  138. compressed_pair_1() : T2(), _first() {}
  139. compressed_pair_1(first_param_type x, second_param_type y) : T2(y), _first(x) {}
  140. template <class A>
  141. explicit compressed_pair_1(const A& val)
  142. {
  143. init_one<best_conversion_traits<A, T1, T2>::value>::init(val, &_first, static_cast<T2*>(this));
  144. }
  145. compressed_pair_1(const ::boost::compressed_pair<T1,T2>& x)
  146. : T2(x.second()), _first(x.first()) {}
  147. first_reference first() { return _first; }
  148. first_const_reference first() const { return _first; }
  149. second_reference second() { return *this; }
  150. second_const_reference second() const { return *this; }
  151. void swap(compressed_pair_1& y)
  152. {
  153. // no need to swap empty base class:
  154. using std::swap;
  155. swap(_first, y._first);
  156. }
  157. };
  158. // T1 != T2, T1 empty
  159. template <class T1, class T2>
  160. class compressed_pair_2 : T1
  161. {
  162. private:
  163. T2 _second;
  164. public:
  165. typedef T1 first_type;
  166. typedef T2 second_type;
  167. typedef typename call_traits<first_type>::param_type first_param_type;
  168. typedef typename call_traits<second_type>::param_type second_param_type;
  169. typedef typename call_traits<first_type>::reference first_reference;
  170. typedef typename call_traits<second_type>::reference second_reference;
  171. typedef typename call_traits<first_type>::const_reference first_const_reference;
  172. typedef typename call_traits<second_type>::const_reference second_const_reference;
  173. compressed_pair_2() : T1(), _second() {}
  174. compressed_pair_2(first_param_type x, second_param_type y) : T1(x), _second(y) {}
  175. template <class A>
  176. explicit compressed_pair_2(const A& val)
  177. {
  178. init_one<best_conversion_traits<A, T1, T2>::value>::init(val, static_cast<T1*>(this), &_second);
  179. }
  180. compressed_pair_2(const ::boost::compressed_pair<T1,T2>& x)
  181. : T1(x.first()), _second(x.second()) {}
  182. #if 0
  183. compressed_pair_2& operator=(const compressed_pair_2& x) {
  184. cout << "assigning compressed pair 2" << endl;
  185. T1::operator=(x);
  186. _second = x._second;
  187. cout << "finished assigning compressed pair 2" << endl;
  188. return *this;
  189. }
  190. #endif
  191. first_reference first() { return *this; }
  192. first_const_reference first() const { return *this; }
  193. second_reference second() { return _second; }
  194. second_const_reference second() const { return _second; }
  195. void swap(compressed_pair_2& y)
  196. {
  197. // no need to swap empty base class:
  198. using std::swap;
  199. swap(_second, y._second);
  200. }
  201. };
  202. // T1 != T2, both empty
  203. template <class T1, class T2>
  204. class compressed_pair_3 : T1, T2
  205. {
  206. public:
  207. typedef T1 first_type;
  208. typedef T2 second_type;
  209. typedef typename call_traits<first_type>::param_type first_param_type;
  210. typedef typename call_traits<second_type>::param_type second_param_type;
  211. typedef typename call_traits<first_type>::reference first_reference;
  212. typedef typename call_traits<second_type>::reference second_reference;
  213. typedef typename call_traits<first_type>::const_reference first_const_reference;
  214. typedef typename call_traits<second_type>::const_reference second_const_reference;
  215. compressed_pair_3() : T1(), T2() {}
  216. compressed_pair_3(first_param_type x, second_param_type y) : T1(x), T2(y) {}
  217. template <class A>
  218. explicit compressed_pair_3(const A& val)
  219. {
  220. init_one<best_conversion_traits<A, T1, T2>::value>::init(val, static_cast<T1*>(this), static_cast<T2*>(this));
  221. }
  222. compressed_pair_3(const ::boost::compressed_pair<T1,T2>& x)
  223. : T1(x.first()), T2(x.second()) {}
  224. first_reference first() { return *this; }
  225. first_const_reference first() const { return *this; }
  226. second_reference second() { return *this; }
  227. second_const_reference second() const { return *this; }
  228. void swap(compressed_pair_3& y)
  229. {
  230. // no need to swap empty base classes:
  231. }
  232. };
  233. // T1 == T2, and empty
  234. template <class T1, class T2>
  235. class compressed_pair_4 : T1
  236. {
  237. public:
  238. typedef T1 first_type;
  239. typedef T2 second_type;
  240. typedef typename call_traits<first_type>::param_type first_param_type;
  241. typedef typename call_traits<second_type>::param_type second_param_type;
  242. typedef typename call_traits<first_type>::reference first_reference;
  243. typedef typename call_traits<second_type>::reference second_reference;
  244. typedef typename call_traits<first_type>::const_reference first_const_reference;
  245. typedef typename call_traits<second_type>::const_reference second_const_reference;
  246. compressed_pair_4() : T1() {}
  247. compressed_pair_4(first_param_type x, second_param_type y) : T1(x), m_second(y) {}
  248. // only one single argument constructor since T1 == T2
  249. explicit compressed_pair_4(first_param_type x) : T1(x), m_second(x) {}
  250. compressed_pair_4(const ::boost::compressed_pair<T1,T2>& x)
  251. : T1(x.first()), m_second(x.second()) {}
  252. first_reference first() { return *this; }
  253. first_const_reference first() const { return *this; }
  254. second_reference second() { return m_second; }
  255. second_const_reference second() const { return m_second; }
  256. void swap(compressed_pair_4& y)
  257. {
  258. // no need to swap empty base classes:
  259. }
  260. private:
  261. T2 m_second;
  262. };
  263. // T1 == T2, not empty
  264. template <class T1, class T2>
  265. class compressed_pair_5
  266. {
  267. private:
  268. T1 _first;
  269. T2 _second;
  270. public:
  271. typedef T1 first_type;
  272. typedef T2 second_type;
  273. typedef typename call_traits<first_type>::param_type first_param_type;
  274. typedef typename call_traits<second_type>::param_type second_param_type;
  275. typedef typename call_traits<first_type>::reference first_reference;
  276. typedef typename call_traits<second_type>::reference second_reference;
  277. typedef typename call_traits<first_type>::const_reference first_const_reference;
  278. typedef typename call_traits<second_type>::const_reference second_const_reference;
  279. compressed_pair_5() : _first(), _second() {}
  280. compressed_pair_5(first_param_type x, second_param_type y) : _first(x), _second(y) {}
  281. // only one single argument constructor since T1 == T2
  282. explicit compressed_pair_5(first_param_type x) : _first(x), _second(x) {}
  283. compressed_pair_5(const ::boost::compressed_pair<T1,T2>& c)
  284. : _first(c.first()), _second(c.second()) {}
  285. first_reference first() { return _first; }
  286. first_const_reference first() const { return _first; }
  287. second_reference second() { return _second; }
  288. second_const_reference second() const { return _second; }
  289. void swap(compressed_pair_5& y)
  290. {
  291. using std::swap;
  292. swap(_first, y._first);
  293. swap(_second, y._second);
  294. }
  295. };
  296. template <bool e1, bool e2, bool same>
  297. struct compressed_pair_chooser
  298. {
  299. template <class T1, class T2>
  300. struct rebind
  301. {
  302. typedef compressed_pair_0<T1, T2> type;
  303. };
  304. };
  305. template <>
  306. struct compressed_pair_chooser<false, true, false>
  307. {
  308. template <class T1, class T2>
  309. struct rebind
  310. {
  311. typedef compressed_pair_1<T1, T2> type;
  312. };
  313. };
  314. template <>
  315. struct compressed_pair_chooser<true, false, false>
  316. {
  317. template <class T1, class T2>
  318. struct rebind
  319. {
  320. typedef compressed_pair_2<T1, T2> type;
  321. };
  322. };
  323. template <>
  324. struct compressed_pair_chooser<true, true, false>
  325. {
  326. template <class T1, class T2>
  327. struct rebind
  328. {
  329. typedef compressed_pair_3<T1, T2> type;
  330. };
  331. };
  332. template <>
  333. struct compressed_pair_chooser<true, true, true>
  334. {
  335. template <class T1, class T2>
  336. struct rebind
  337. {
  338. typedef compressed_pair_4<T1, T2> type;
  339. };
  340. };
  341. template <>
  342. struct compressed_pair_chooser<false, false, true>
  343. {
  344. template <class T1, class T2>
  345. struct rebind
  346. {
  347. typedef compressed_pair_5<T1, T2> type;
  348. };
  349. };
  350. template <class T1, class T2>
  351. struct compressed_pair_traits
  352. {
  353. private:
  354. typedef compressed_pair_chooser<is_empty<T1>::value, is_empty<T2>::value, is_same<T1,T2>::value> chooser;
  355. typedef typename chooser::template rebind<T1, T2> bound_type;
  356. public:
  357. typedef typename bound_type::type type;
  358. };
  359. } // namespace detail
  360. template <class T1, class T2>
  361. class compressed_pair : public detail::compressed_pair_traits<T1, T2>::type
  362. {
  363. private:
  364. typedef typename detail::compressed_pair_traits<T1, T2>::type base_type;
  365. public:
  366. typedef T1 first_type;
  367. typedef T2 second_type;
  368. typedef typename call_traits<first_type>::param_type first_param_type;
  369. typedef typename call_traits<second_type>::param_type second_param_type;
  370. typedef typename call_traits<first_type>::reference first_reference;
  371. typedef typename call_traits<second_type>::reference second_reference;
  372. typedef typename call_traits<first_type>::const_reference first_const_reference;
  373. typedef typename call_traits<second_type>::const_reference second_const_reference;
  374. compressed_pair() : base_type() {}
  375. compressed_pair(first_param_type x, second_param_type y) : base_type(x, y) {}
  376. template <class A>
  377. explicit compressed_pair(const A& x) : base_type(x){}
  378. first_reference first() { return base_type::first(); }
  379. first_const_reference first() const { return base_type::first(); }
  380. second_reference second() { return base_type::second(); }
  381. second_const_reference second() const { return base_type::second(); }
  382. };
  383. template <class T1, class T2>
  384. inline void swap(compressed_pair<T1, T2>& x, compressed_pair<T1, T2>& y)
  385. {
  386. x.swap(y);
  387. }
  388. #else
  389. // no partial specialisation, no member templates:
  390. template <class T1, class T2>
  391. class compressed_pair
  392. {
  393. private:
  394. T1 _first;
  395. T2 _second;
  396. public:
  397. typedef T1 first_type;
  398. typedef T2 second_type;
  399. typedef typename call_traits<first_type>::param_type first_param_type;
  400. typedef typename call_traits<second_type>::param_type second_param_type;
  401. typedef typename call_traits<first_type>::reference first_reference;
  402. typedef typename call_traits<second_type>::reference second_reference;
  403. typedef typename call_traits<first_type>::const_reference first_const_reference;
  404. typedef typename call_traits<second_type>::const_reference second_const_reference;
  405. compressed_pair() : _first(), _second() {}
  406. compressed_pair(first_param_type x, second_param_type y) : _first(x), _second(y) {}
  407. explicit compressed_pair(first_param_type x) : _first(x), _second() {}
  408. // can't define this in case T1 == T2:
  409. // explicit compressed_pair(second_param_type y) : _first(), _second(y) {}
  410. first_reference first() { return _first; }
  411. first_const_reference first() const { return _first; }
  412. second_reference second() { return _second; }
  413. second_const_reference second() const { return _second; }
  414. void swap(compressed_pair& y)
  415. {
  416. using std::swap;
  417. swap(_first, y._first);
  418. swap(_second, y._second);
  419. }
  420. };
  421. template <class T1, class T2>
  422. inline void swap(compressed_pair<T1, T2>& x, compressed_pair<T1, T2>& y)
  423. {
  424. x.swap(y);
  425. }
  426. #endif
  427. } // boost
  428. #endif // BOOST_OB_COMPRESSED_PAIR_HPP