movable_int.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2006. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. ///////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_CONTAINER_TEST_MOVABLE_INT_HEADER
  11. #define BOOST_CONTAINER_TEST_MOVABLE_INT_HEADER
  12. #include <boost/container/detail/config_begin.hpp>
  13. #include <boost/container/detail/workaround.hpp>
  14. #include <boost/move/utility_core.hpp>
  15. #include <ostream>
  16. #include <climits>
  17. #include <boost/assert.hpp>
  18. namespace boost {
  19. namespace container {
  20. namespace test {
  21. template<class T>
  22. struct is_copyable;
  23. template<>
  24. struct is_copyable<int>
  25. {
  26. static const bool value = true;
  27. };
  28. class movable_int
  29. {
  30. BOOST_MOVABLE_BUT_NOT_COPYABLE(movable_int)
  31. public:
  32. static unsigned int count;
  33. movable_int()
  34. : m_int(0)
  35. { ++count; }
  36. explicit movable_int(int a)
  37. : m_int(a)
  38. {
  39. //Disallow INT_MIN
  40. BOOST_ASSERT(this->m_int != INT_MIN);
  41. ++count;
  42. }
  43. movable_int(BOOST_RV_REF(movable_int) mmi)
  44. : m_int(mmi.m_int)
  45. { mmi.m_int = 0; ++count; }
  46. movable_int & operator= (BOOST_RV_REF(movable_int) mmi)
  47. { this->m_int = mmi.m_int; mmi.m_int = 0; return *this; }
  48. movable_int & operator= (int i)
  49. { this->m_int = i; BOOST_ASSERT(this->m_int != INT_MIN); return *this; }
  50. ~movable_int()
  51. {
  52. //Double destructor called
  53. BOOST_ASSERT(this->m_int != INT_MIN);
  54. this->m_int = INT_MIN;
  55. --count;
  56. }
  57. friend bool operator ==(const movable_int &l, const movable_int &r)
  58. { return l.m_int == r.m_int; }
  59. friend bool operator !=(const movable_int &l, const movable_int &r)
  60. { return l.m_int != r.m_int; }
  61. friend bool operator <(const movable_int &l, const movable_int &r)
  62. { return l.m_int < r.m_int; }
  63. friend bool operator <=(const movable_int &l, const movable_int &r)
  64. { return l.m_int <= r.m_int; }
  65. friend bool operator >=(const movable_int &l, const movable_int &r)
  66. { return l.m_int >= r.m_int; }
  67. friend bool operator >(const movable_int &l, const movable_int &r)
  68. { return l.m_int > r.m_int; }
  69. int get_int() const
  70. { return m_int; }
  71. friend bool operator==(const movable_int &l, int r)
  72. { return l.get_int() == r; }
  73. friend bool operator==(int l, const movable_int &r)
  74. { return l == r.get_int(); }
  75. friend bool operator<(const movable_int &l, int r)
  76. { return l.get_int() < r; }
  77. friend bool operator<(int l, const movable_int &r)
  78. { return l < r.get_int(); }
  79. friend std::size_t hash_value(const movable_int &v)
  80. { return (std::size_t)v.get_int(); }
  81. private:
  82. int m_int;
  83. };
  84. unsigned int movable_int::count = 0;
  85. inline movable_int produce_movable_int()
  86. { return movable_int(); }
  87. template<class E, class T>
  88. std::basic_ostream<E, T> & operator<<
  89. (std::basic_ostream<E, T> & os, movable_int const & p)
  90. {
  91. os << p.get_int();
  92. return os;
  93. }
  94. template<>
  95. struct is_copyable<movable_int>
  96. {
  97. static const bool value = false;
  98. };
  99. class movable_and_copyable_int
  100. {
  101. BOOST_COPYABLE_AND_MOVABLE(movable_and_copyable_int)
  102. public:
  103. static unsigned int count;
  104. movable_and_copyable_int()
  105. : m_int(0)
  106. { ++count; }
  107. explicit movable_and_copyable_int(int a)
  108. : m_int(a)
  109. {
  110. //Disallow INT_MIN
  111. BOOST_ASSERT(this->m_int != INT_MIN);
  112. ++count;
  113. }
  114. movable_and_copyable_int(const movable_and_copyable_int& mmi)
  115. : m_int(mmi.m_int)
  116. { ++count; }
  117. movable_and_copyable_int(BOOST_RV_REF(movable_and_copyable_int) mmi)
  118. : m_int(mmi.m_int)
  119. { mmi.m_int = 0; ++count; }
  120. ~movable_and_copyable_int()
  121. {
  122. //Double destructor called
  123. BOOST_ASSERT(this->m_int != INT_MIN);
  124. this->m_int = INT_MIN;
  125. --count;
  126. }
  127. movable_and_copyable_int &operator= (BOOST_COPY_ASSIGN_REF(movable_and_copyable_int) mi)
  128. { this->m_int = mi.m_int; return *this; }
  129. movable_and_copyable_int & operator= (BOOST_RV_REF(movable_and_copyable_int) mmi)
  130. { this->m_int = mmi.m_int; mmi.m_int = 0; return *this; }
  131. movable_and_copyable_int & operator= (int i)
  132. { this->m_int = i; BOOST_ASSERT(this->m_int != INT_MIN); return *this; }
  133. friend bool operator ==(const movable_and_copyable_int &l, const movable_and_copyable_int &r)
  134. { return l.m_int == r.m_int; }
  135. friend bool operator !=(const movable_and_copyable_int &l, const movable_and_copyable_int &r)
  136. { return l.m_int != r.m_int; }
  137. friend bool operator <(const movable_and_copyable_int &l, const movable_and_copyable_int &r)
  138. { return l.m_int < r.m_int; }
  139. friend bool operator <=(const movable_and_copyable_int &l, const movable_and_copyable_int &r)
  140. { return l.m_int <= r.m_int; }
  141. friend bool operator >=(const movable_and_copyable_int &l, const movable_and_copyable_int &r)
  142. { return l.m_int >= r.m_int; }
  143. friend bool operator >(const movable_and_copyable_int &l, const movable_and_copyable_int &r)
  144. { return l.m_int > r.m_int; }
  145. int get_int() const
  146. { return m_int; }
  147. friend bool operator==(const movable_and_copyable_int &l, int r)
  148. { return l.get_int() == r; }
  149. friend bool operator==(int l, const movable_and_copyable_int &r)
  150. { return l == r.get_int(); }
  151. friend bool operator<(const movable_and_copyable_int &l, int r)
  152. { return l.get_int() < r; }
  153. friend bool operator<(int l, const movable_and_copyable_int &r)
  154. { return l < r.get_int(); }
  155. friend std::size_t hash_value(const movable_and_copyable_int &v)
  156. { return (std::size_t)v.get_int(); }
  157. private:
  158. int m_int;
  159. };
  160. unsigned int movable_and_copyable_int::count = 0;
  161. inline movable_and_copyable_int produce_movable_and_copyable_int()
  162. { return movable_and_copyable_int(); }
  163. template<class E, class T>
  164. std::basic_ostream<E, T> & operator<<
  165. (std::basic_ostream<E, T> & os, movable_and_copyable_int const & p)
  166. {
  167. os << p.get_int();
  168. return os;
  169. }
  170. template<>
  171. struct is_copyable<movable_and_copyable_int>
  172. {
  173. static const bool value = true;
  174. };
  175. class copyable_int
  176. {
  177. public:
  178. static unsigned int count;
  179. copyable_int()
  180. : m_int(0)
  181. { ++count; }
  182. explicit copyable_int(int a)
  183. : m_int(a)
  184. {
  185. //Disallow INT_MIN
  186. BOOST_ASSERT(this->m_int != INT_MIN);
  187. ++count;
  188. }
  189. copyable_int(const copyable_int& mmi)
  190. : m_int(mmi.m_int)
  191. { ++count; }
  192. copyable_int & operator= (int i)
  193. { this->m_int = i; BOOST_ASSERT(this->m_int != INT_MIN); return *this; }
  194. copyable_int & operator= (const copyable_int &ci)
  195. { this->m_int = ci.m_int; BOOST_ASSERT(this->m_int != INT_MIN); return *this; }
  196. ~copyable_int()
  197. {
  198. //Double destructor called
  199. BOOST_ASSERT(this->m_int != INT_MIN);
  200. this->m_int = INT_MIN;
  201. --count;
  202. }
  203. friend bool operator ==(const copyable_int &l, const copyable_int &r)
  204. { return l.m_int == r.m_int; }
  205. friend bool operator !=(const copyable_int &l, const copyable_int &r)
  206. { return l.m_int != r.m_int; }
  207. friend bool operator <(const copyable_int &l, const copyable_int &r)
  208. { return l.m_int < r.m_int; }
  209. friend bool operator <=(const copyable_int &l, const copyable_int &r)
  210. { return l.m_int <= r.m_int; }
  211. friend bool operator >=(const copyable_int &l, const copyable_int &r)
  212. { return l.m_int >= r.m_int; }
  213. friend bool operator >(const copyable_int &l, const copyable_int &r)
  214. { return l.m_int > r.m_int; }
  215. int get_int() const
  216. { return m_int; }
  217. friend bool operator==(const copyable_int &l, int r)
  218. { return l.get_int() == r; }
  219. friend bool operator==(int l, const copyable_int &r)
  220. { return l == r.get_int(); }
  221. friend bool operator<(const copyable_int &l, int r)
  222. { return l.get_int() < r; }
  223. friend bool operator<(int l, const copyable_int &r)
  224. { return l < r.get_int(); }
  225. friend std::size_t hash_value(const copyable_int &v)
  226. { return (std::size_t)v.get_int(); }
  227. private:
  228. int m_int;
  229. };
  230. unsigned int copyable_int::count = 0;
  231. inline copyable_int produce_copyable_int()
  232. { return copyable_int(); }
  233. template<class E, class T>
  234. std::basic_ostream<E, T> & operator<<
  235. (std::basic_ostream<E, T> & os, copyable_int const & p)
  236. {
  237. os << p.get_int();
  238. return os;
  239. }
  240. template<>
  241. struct is_copyable<copyable_int>
  242. {
  243. static const bool value = true;
  244. };
  245. class non_copymovable_int
  246. {
  247. non_copymovable_int(const non_copymovable_int& mmi);
  248. non_copymovable_int & operator= (const non_copymovable_int &mi);
  249. public:
  250. static unsigned int count;
  251. non_copymovable_int()
  252. : m_int(0)
  253. { ++count; }
  254. explicit non_copymovable_int(int a)
  255. : m_int(a)
  256. { ++count; }
  257. ~non_copymovable_int()
  258. { m_int = 0; --count; }
  259. bool operator ==(const non_copymovable_int &mi) const
  260. { return this->m_int == mi.m_int; }
  261. bool operator !=(const non_copymovable_int &mi) const
  262. { return this->m_int != mi.m_int; }
  263. bool operator <(const non_copymovable_int &mi) const
  264. { return this->m_int < mi.m_int; }
  265. bool operator <=(const non_copymovable_int &mi) const
  266. { return this->m_int <= mi.m_int; }
  267. bool operator >=(const non_copymovable_int &mi) const
  268. { return this->m_int >= mi.m_int; }
  269. bool operator >(const non_copymovable_int &mi) const
  270. { return this->m_int > mi.m_int; }
  271. int get_int() const
  272. { return m_int; }
  273. friend bool operator==(const non_copymovable_int &l, int r)
  274. { return l.get_int() == r; }
  275. friend bool operator==(int l, const non_copymovable_int &r)
  276. { return l == r.get_int(); }
  277. friend bool operator<(const non_copymovable_int &l, int r)
  278. { return l.get_int() < r; }
  279. friend bool operator<(int l, const non_copymovable_int &r)
  280. { return l < r.get_int(); }
  281. friend std::size_t hash_value(const non_copymovable_int &v)
  282. { return (std::size_t)v.get_int(); }
  283. private:
  284. int m_int;
  285. };
  286. unsigned int non_copymovable_int::count = 0;
  287. template<class T>
  288. struct life_count
  289. {
  290. static unsigned check(unsigned) { return true; }
  291. };
  292. template<>
  293. struct life_count< movable_int >
  294. {
  295. static unsigned check(unsigned c)
  296. { return c == movable_int::count; }
  297. };
  298. template<>
  299. struct life_count< copyable_int >
  300. {
  301. static unsigned check(unsigned c)
  302. { return c == copyable_int::count; }
  303. };
  304. template<>
  305. struct life_count< movable_and_copyable_int >
  306. {
  307. static unsigned check(unsigned c)
  308. { return c == movable_and_copyable_int::count; }
  309. };
  310. template<>
  311. struct life_count< non_copymovable_int >
  312. {
  313. static unsigned check(unsigned c)
  314. { return c == non_copymovable_int::count; }
  315. };
  316. } //namespace test {
  317. } //namespace container {
  318. } //namespace boost {
  319. #include <boost/container/detail/config_end.hpp>
  320. #endif //#ifndef BOOST_CONTAINER_TEST_MOVABLE_INT_HEADER