conversion_test.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright David Abrahams, Vicente Botet, Ion Gaztanaga 2010-2012.
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See http://www.boost.org/libs/move for documentation.
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11. #include <boost/move/utility_core.hpp>
  12. #include <boost/move/detail/meta_utils.hpp>
  13. #include <cassert>
  14. #include <new>
  15. #include <boost/move/detail/move_helpers.hpp>
  16. enum ConstructionType { Copied, Moved, Other };
  17. class conversion_source
  18. {
  19. public:
  20. conversion_source(){}
  21. operator int() const { return 0; }
  22. };
  23. class conversion_target
  24. {
  25. ConstructionType c_type_;
  26. public:
  27. conversion_target(conversion_source)
  28. { c_type_ = Other; }
  29. conversion_target()
  30. { c_type_ = Other; }
  31. conversion_target(const conversion_target &)
  32. { c_type_ = Copied; }
  33. ConstructionType construction_type() const
  34. { return c_type_; }
  35. };
  36. class conversion_target_copymovable
  37. {
  38. ConstructionType c_type_;
  39. BOOST_COPYABLE_AND_MOVABLE(conversion_target_copymovable)
  40. public:
  41. conversion_target_copymovable()
  42. { c_type_ = Other; }
  43. conversion_target_copymovable(conversion_source)
  44. { c_type_ = Other; }
  45. conversion_target_copymovable(const conversion_target_copymovable &)
  46. { c_type_ = Copied; }
  47. conversion_target_copymovable(BOOST_RV_REF(conversion_target_copymovable) )
  48. { c_type_ = Moved; }
  49. conversion_target_copymovable &operator=(BOOST_RV_REF(conversion_target_copymovable) )
  50. { c_type_ = Moved; return *this; }
  51. conversion_target_copymovable &operator=(BOOST_COPY_ASSIGN_REF(conversion_target_copymovable) )
  52. { c_type_ = Copied; return *this; }
  53. ConstructionType construction_type() const
  54. { return c_type_; }
  55. };
  56. class conversion_target_movable
  57. {
  58. ConstructionType c_type_;
  59. BOOST_MOVABLE_BUT_NOT_COPYABLE(conversion_target_movable)
  60. public:
  61. conversion_target_movable()
  62. { c_type_ = Other; }
  63. conversion_target_movable(conversion_source)
  64. { c_type_ = Other; }
  65. conversion_target_movable(BOOST_RV_REF(conversion_target_movable) )
  66. { c_type_ = Moved; }
  67. conversion_target_movable &operator=(BOOST_RV_REF(conversion_target_movable) )
  68. { c_type_ = Moved; return *this; }
  69. ConstructionType construction_type() const
  70. { return c_type_; }
  71. };
  72. template<class T>
  73. class container
  74. {
  75. T *storage_;
  76. public:
  77. struct const_iterator{};
  78. struct iterator : const_iterator{};
  79. container()
  80. : storage_(0)
  81. {}
  82. ~container()
  83. { delete storage_; }
  84. container(const container &c)
  85. : storage_(c.storage_ ? new T(*c.storage_) : 0)
  86. {}
  87. container &operator=(const container &c)
  88. {
  89. if(storage_){
  90. delete storage_;
  91. storage_ = 0;
  92. }
  93. storage_ = c.storage_ ? new T(*c.storage_) : 0;
  94. return *this;
  95. }
  96. BOOST_MOVE_CONVERSION_AWARE_CATCH(push_back, T, void, priv_push_back)
  97. BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, T, iterator, priv_insert, const_iterator, const_iterator)
  98. template <class Iterator>
  99. iterator insert(Iterator, Iterator){ return iterator(); }
  100. ConstructionType construction_type() const
  101. { return construction_type_impl
  102. (typename ::boost::move_detail::integral_constant<bool, ::boost::move_detail::is_class_or_union<T>::value>());
  103. }
  104. ConstructionType construction_type_impl(::boost::move_detail::true_type) const
  105. { return storage_->construction_type(); }
  106. ConstructionType construction_type_impl(::boost::move_detail::false_type) const
  107. { return Copied; }
  108. iterator begin() const { return iterator(); }
  109. private:
  110. template<class U>
  111. void priv_construct(BOOST_MOVE_CATCH_FWD(U) x)
  112. {
  113. if(storage_){
  114. delete storage_;
  115. storage_ = 0;
  116. }
  117. storage_ = new T(::boost::forward<U>(x));
  118. }
  119. template<class U>
  120. void priv_push_back(BOOST_MOVE_CATCH_FWD(U) x)
  121. { priv_construct(::boost::forward<U>(x)); }
  122. template<class U>
  123. iterator priv_insert(const_iterator, BOOST_MOVE_CATCH_FWD(U) x)
  124. { priv_construct(::boost::forward<U>(x)); return iterator(); }
  125. };
  126. class recursive_container
  127. {
  128. BOOST_COPYABLE_AND_MOVABLE(recursive_container)
  129. public:
  130. recursive_container()
  131. {}
  132. recursive_container(const recursive_container &c)
  133. : container_(c.container_)
  134. {}
  135. recursive_container(BOOST_RV_REF(recursive_container) c)
  136. : container_(::boost::move(c.container_))
  137. {}
  138. recursive_container & operator =(BOOST_COPY_ASSIGN_REF(recursive_container) c)
  139. {
  140. container_= c.container_;
  141. return *this;
  142. }
  143. recursive_container & operator =(BOOST_RV_REF(recursive_container) c)
  144. {
  145. container_= ::boost::move(c.container_);
  146. return *this;
  147. }
  148. container<recursive_container> container_;
  149. friend bool operator< (const recursive_container &a, const recursive_container &b)
  150. { return &a < &b; }
  151. };
  152. int main()
  153. {
  154. conversion_target_movable a;
  155. conversion_target_movable b(::boost::move(a));
  156. {
  157. container<conversion_target> c;
  158. {
  159. conversion_target x;
  160. c.push_back(x);
  161. assert(c.construction_type() == Copied);
  162. c.insert(c.begin(), x);
  163. assert(c.construction_type() == Copied);
  164. }
  165. {
  166. const conversion_target x;
  167. c.push_back(x);
  168. assert(c.construction_type() == Copied);
  169. c.insert(c.begin(), x);
  170. assert(c.construction_type() == Copied);
  171. }
  172. {
  173. c.push_back(conversion_target());
  174. assert(c.construction_type() == Copied);
  175. c.insert(c.begin(), conversion_target());
  176. assert(c.construction_type() == Copied);
  177. }
  178. {
  179. conversion_source x;
  180. c.push_back(x);
  181. assert(c.construction_type() == Copied);
  182. c.insert(c.begin(), x);
  183. assert(c.construction_type() == Copied);
  184. }
  185. {
  186. const conversion_source x;
  187. c.push_back(x);
  188. assert(c.construction_type() == Copied);
  189. c.insert(c.begin(), x);
  190. assert(c.construction_type() == Copied);
  191. }
  192. {
  193. c.push_back(conversion_source());
  194. assert(c.construction_type() == Copied);
  195. c.insert(c.begin(), conversion_source());
  196. assert(c.construction_type() == Copied);
  197. }
  198. }
  199. {
  200. container<conversion_target_copymovable> c;
  201. {
  202. conversion_target_copymovable x;
  203. c.push_back(x);
  204. assert(c.construction_type() == Copied);
  205. c.insert(c.begin(), x);
  206. assert(c.construction_type() == Copied);
  207. }
  208. {
  209. const conversion_target_copymovable x;
  210. c.push_back(x);
  211. assert(c.construction_type() == Copied);
  212. c.insert(c.begin(), x);
  213. assert(c.construction_type() == Copied);
  214. }
  215. {
  216. c.push_back(conversion_target_copymovable());
  217. assert(c.construction_type() == Moved);
  218. c.insert(c.begin(), conversion_target_copymovable());
  219. assert(c.construction_type() == Moved);
  220. }
  221. {
  222. conversion_source x;
  223. c.push_back(x);
  224. assert(c.construction_type() == Moved);
  225. c.insert(c.begin(), x);
  226. assert(c.construction_type() == Moved);
  227. }
  228. {
  229. const conversion_source x;
  230. c.push_back(x);
  231. assert(c.construction_type() == Moved);
  232. c.insert(c.begin(), x);
  233. assert(c.construction_type() == Moved);
  234. }
  235. {
  236. c.push_back(conversion_source());
  237. assert(c.construction_type() == Moved);
  238. c.insert(c.begin(), conversion_source());
  239. assert(c.construction_type() == Moved);
  240. }
  241. }
  242. {
  243. container<conversion_target_movable> c;
  244. //This should not compile
  245. //{
  246. // conversion_target_movable x;
  247. // c.push_back(x);
  248. // assert(c.construction_type() == Copied);
  249. //}
  250. //{
  251. // const conversion_target_movable x;
  252. // c.push_back(x);
  253. // assert(c.construction_type() == Copied);
  254. //}
  255. {
  256. c.push_back(conversion_target_movable());
  257. assert(c.construction_type() == Moved);
  258. c.insert(c.begin(), conversion_target_movable());
  259. assert(c.construction_type() == Moved);
  260. }
  261. {
  262. conversion_source x;
  263. c.push_back(x);
  264. assert(c.construction_type() == Moved);
  265. c.insert(c.begin(), x);
  266. assert(c.construction_type() == Moved);
  267. }
  268. {
  269. const conversion_source x;
  270. c.push_back(x);
  271. assert(c.construction_type() == Moved);
  272. c.insert(c.begin(), x);
  273. assert(c.construction_type() == Moved);
  274. }
  275. {
  276. c.push_back(conversion_source());
  277. assert(c.construction_type() == Moved);
  278. c.insert(c.begin(), conversion_source());
  279. assert(c.construction_type() == Moved);
  280. }
  281. }
  282. {
  283. container<int> c;
  284. {
  285. int x = 0;
  286. c.push_back(x);
  287. assert(c.construction_type() == Copied);
  288. c.insert(c.begin(), c.construction_type());
  289. assert(c.construction_type() == Copied);
  290. }
  291. {
  292. const int x = 0;
  293. c.push_back(x);
  294. assert(c.construction_type() == Copied);
  295. c.insert(c.begin(), x);
  296. assert(c.construction_type() == Copied);
  297. }
  298. {
  299. c.push_back(int(0));
  300. assert(c.construction_type() == Copied);
  301. c.insert(c.begin(), int(0));
  302. assert(c.construction_type() == Copied);
  303. }
  304. {
  305. conversion_source x;
  306. c.push_back(x);
  307. assert(c.construction_type() == Copied);
  308. c.insert(c.begin(), x);
  309. assert(c.construction_type() == Copied);
  310. }
  311. {
  312. const conversion_source x;
  313. c.push_back(x);
  314. assert(c.construction_type() == Copied);
  315. c.insert(c.begin(), x);
  316. assert(c.construction_type() == Copied);
  317. }
  318. {
  319. c.push_back(conversion_source());
  320. assert(c.construction_type() == Copied);
  321. c.insert(c.begin(), conversion_source());
  322. assert(c.construction_type() == Copied);
  323. }
  324. //c.insert(c.begin(), c.begin());
  325. }
  326. {
  327. container<int> c;
  328. {
  329. int x = 0;
  330. c.push_back(x);
  331. assert(c.construction_type() == Copied);
  332. c.insert(c.begin(), c.construction_type());
  333. assert(c.construction_type() == Copied);
  334. }
  335. {
  336. const int x = 0;
  337. c.push_back(x);
  338. assert(c.construction_type() == Copied);
  339. c.insert(c.begin(), x);
  340. assert(c.construction_type() == Copied);
  341. }
  342. {
  343. c.push_back(int(0));
  344. assert(c.construction_type() == Copied);
  345. c.insert(c.begin(), int(0));
  346. assert(c.construction_type() == Copied);
  347. }
  348. {
  349. conversion_source x;
  350. c.push_back(x);
  351. assert(c.construction_type() == Copied);
  352. c.insert(c.begin(), x);
  353. assert(c.construction_type() == Copied);
  354. }
  355. {
  356. const conversion_source x;
  357. c.push_back(x);
  358. assert(c.construction_type() == Copied);
  359. c.insert(c.begin(), x);
  360. assert(c.construction_type() == Copied);
  361. }
  362. {
  363. c.push_back(conversion_source());
  364. assert(c.construction_type() == Copied);
  365. c.insert(c.begin(), conversion_source());
  366. assert(c.construction_type() == Copied);
  367. }
  368. c.insert(c.begin(), c.begin());
  369. }
  370. {
  371. recursive_container c;
  372. recursive_container internal;
  373. c.container_.insert(c.container_.begin(), recursive_container());
  374. c.container_.insert(c.container_.begin(), internal);
  375. c.container_.insert(c.container_.begin(), c.container_.begin());
  376. }
  377. return 0;
  378. }