ob_compressed_pair.hpp 16 KB

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