testsuite_rvalref.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // -*- C++ -*-
  2. // Testing utilities for the rvalue reference.
  3. //
  4. // Copyright (C) 2005-2022 Free Software Foundation, Inc.
  5. //
  6. // This file is part of the GNU ISO C++ Library. This library is free
  7. // software; you can redistribute it and/or modify it under the
  8. // terms of the GNU General Public License as published by the
  9. // Free Software Foundation; either version 3, or (at your option)
  10. // any later version.
  11. //
  12. // This library is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License along
  18. // with this library; see the file COPYING3. If not see
  19. // <http://www.gnu.org/licenses/>.
  20. //
  21. #ifndef _GLIBCXX_TESTSUITE_RVALREF_H
  22. #define _GLIBCXX_TESTSUITE_RVALREF_H 1
  23. #include <testsuite_hooks.h>
  24. #include <bits/functional_hash.h>
  25. namespace __gnu_test
  26. {
  27. // This class is designed to test libstdc++'s template-based rvalue
  28. // reference support. It should fail at compile-time if there is an
  29. // attempt to copy it.
  30. struct rvalstruct
  31. {
  32. int val;
  33. bool valid;
  34. rvalstruct() : val(0), valid(true)
  35. { }
  36. rvalstruct(int inval) : val(inval), valid(true)
  37. { }
  38. rvalstruct&
  39. operator=(int newval)
  40. {
  41. val = newval;
  42. valid = true;
  43. return *this;
  44. }
  45. rvalstruct(const rvalstruct&) = delete;
  46. rvalstruct(rvalstruct&& in)
  47. {
  48. VERIFY( in.valid == true );
  49. val = in.val;
  50. in.valid = false;
  51. valid = true;
  52. }
  53. rvalstruct&
  54. operator=(const rvalstruct&) = delete;
  55. rvalstruct&
  56. operator=(rvalstruct&& in)
  57. {
  58. VERIFY( this != &in );
  59. VERIFY( in.valid == true );
  60. val = in.val;
  61. in.valid = false;
  62. valid = true;
  63. return *this;
  64. }
  65. };
  66. inline bool
  67. operator==(const rvalstruct& lhs, const rvalstruct& rhs)
  68. { return lhs.val == rhs.val; }
  69. inline bool
  70. operator<(const rvalstruct& lhs, const rvalstruct& rhs)
  71. { return lhs.val < rhs.val; }
  72. void
  73. swap(rvalstruct& lhs, rvalstruct& rhs)
  74. {
  75. VERIFY( lhs.valid && rhs.valid );
  76. int temp = lhs.val;
  77. lhs.val = rhs.val;
  78. rhs.val = temp;
  79. }
  80. // This is a moveable class which copies how many times it is copied.
  81. // This is mainly of use in the containers, where the an element inserted
  82. // into a container has to be copied once to get there, but we want to check
  83. // nothing else is copied.
  84. struct copycounter
  85. {
  86. static int copycount;
  87. int val;
  88. bool valid;
  89. copycounter() : val(0), valid(true)
  90. { }
  91. copycounter(int inval) : val(inval), valid(true)
  92. { }
  93. copycounter(const copycounter& in) : val(in.val), valid(true)
  94. {
  95. VERIFY( in.valid == true );
  96. ++copycount;
  97. }
  98. copycounter(copycounter&& in) noexcept
  99. {
  100. VERIFY( in.valid == true );
  101. val = in.val;
  102. in.valid = false;
  103. valid = true;
  104. }
  105. copycounter&
  106. operator=(int newval)
  107. {
  108. val = newval;
  109. valid = true;
  110. return *this;
  111. }
  112. bool
  113. operator=(const copycounter& in)
  114. {
  115. VERIFY( in.valid == true );
  116. ++copycount;
  117. val = in.val;
  118. valid = true;
  119. return true;
  120. }
  121. copycounter&
  122. operator=(copycounter&& in)
  123. {
  124. VERIFY(in.valid == true);
  125. val = in.val;
  126. in.valid = false;
  127. valid = true;
  128. return *this;
  129. }
  130. ~copycounter() noexcept
  131. { valid = false; }
  132. };
  133. int copycounter::copycount = 0;
  134. inline bool
  135. operator==(const copycounter& lhs, const copycounter& rhs)
  136. { return lhs.val == rhs.val; }
  137. inline bool
  138. operator<(const copycounter& lhs, const copycounter& rhs)
  139. { return lhs.val < rhs.val; }
  140. inline void
  141. swap(copycounter& lhs, copycounter& rhs)
  142. {
  143. VERIFY( lhs.valid && rhs.valid );
  144. int temp = lhs.val;
  145. lhs.val = rhs.val;
  146. rhs.val = temp;
  147. }
  148. // In the occasion of libstdc++/48038.
  149. struct rvalstruct_compare_by_value
  150. {
  151. int val;
  152. bool ok;
  153. rvalstruct_compare_by_value(int v)
  154. : val(v), ok(true) { }
  155. rvalstruct_compare_by_value(const rvalstruct_compare_by_value& rh)
  156. : val(rh.val), ok(rh.ok)
  157. {
  158. VERIFY(rh.ok);
  159. }
  160. rvalstruct_compare_by_value&
  161. operator=(const rvalstruct_compare_by_value& rh)
  162. {
  163. VERIFY( rh.ok );
  164. val = rh.val;
  165. ok = rh.ok;
  166. return *this;
  167. }
  168. rvalstruct_compare_by_value(rvalstruct_compare_by_value&& rh)
  169. : val(rh.val), ok(rh.ok)
  170. {
  171. VERIFY( rh.ok );
  172. rh.ok = false;
  173. }
  174. rvalstruct_compare_by_value&
  175. operator=(rvalstruct_compare_by_value&& rh)
  176. {
  177. VERIFY( rh.ok );
  178. val = rh.val;
  179. ok = rh.ok;
  180. rh.ok = false;
  181. return *this;
  182. }
  183. };
  184. inline bool
  185. operator<(rvalstruct_compare_by_value lh,
  186. rvalstruct_compare_by_value rh)
  187. {
  188. VERIFY( rh.ok );
  189. VERIFY( lh.ok );
  190. return lh.val < rh.val;
  191. }
  192. inline bool
  193. order(rvalstruct_compare_by_value lh,
  194. rvalstruct_compare_by_value rh)
  195. {
  196. VERIFY( rh.ok );
  197. VERIFY( lh.ok );
  198. return lh.val < rh.val;
  199. }
  200. struct throwing_move_constructor
  201. {
  202. throwing_move_constructor() = default;
  203. throwing_move_constructor(throwing_move_constructor&&)
  204. { throw 1; }
  205. throwing_move_constructor(const throwing_move_constructor&) = default;
  206. throwing_move_constructor&
  207. operator=(const throwing_move_constructor&) = default;
  208. };
  209. } // namespace __gnu_test
  210. namespace std
  211. {
  212. /// std::hash specialization for __gnu_test::rvalstruct.
  213. template<>
  214. struct hash<__gnu_test::rvalstruct>
  215. {
  216. typedef size_t result_type;
  217. typedef __gnu_test::rvalstruct argument_type;
  218. size_t
  219. operator()(const __gnu_test::rvalstruct& __rvs) const
  220. { return __rvs.val; }
  221. };
  222. }
  223. #endif // _GLIBCXX_TESTSUITE_TR1_H