]>
Commit | Line | Data |
---|---|---|
f5783e34 CJ |
1 | // -*- C++ -*- |
2 | // Testing utilities for the rvalue reference. | |
3 | // | |
8d9254fc | 4 | // Copyright (C) 2005-2020 Free Software Foundation, Inc. |
f5783e34 CJ |
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 | |
748086b7 | 9 | // Free Software Foundation; either version 3, or (at your option) |
f5783e34 CJ |
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 | |
748086b7 JJ |
18 | // with this library; see the file COPYING3. If not see |
19 | // <http://www.gnu.org/licenses/>. | |
f5783e34 | 20 | // |
f5783e34 CJ |
21 | |
22 | #ifndef _GLIBCXX_TESTSUITE_RVALREF_H | |
23 | #define _GLIBCXX_TESTSUITE_RVALREF_H 1 | |
24 | ||
25 | #include <testsuite_hooks.h> | |
60c5236e | 26 | #include <bits/functional_hash.h> |
f5783e34 CJ |
27 | |
28 | namespace __gnu_test | |
29 | { | |
fb7342fd PC |
30 | // This class is designed to test libstdc++'s template-based rvalue |
31 | // reference support. It should fail at compile-time if there is an | |
32 | // attempt to copy it. | |
33 | struct rvalstruct | |
f5783e34 | 34 | { |
f5783e34 CJ |
35 | int val; |
36 | bool valid; | |
37 | ||
fb7342fd | 38 | rvalstruct() : val(0), valid(true) |
f5783e34 CJ |
39 | { } |
40 | ||
41 | rvalstruct(int inval) : val(inval), valid(true) | |
42 | { } | |
fb7342fd | 43 | |
f5783e34 CJ |
44 | rvalstruct& |
45 | operator=(int newval) | |
f92ab29f | 46 | { |
fb7342fd | 47 | val = newval; |
f5783e34 | 48 | valid = true; |
7919bb2f | 49 | return *this; |
f5783e34 CJ |
50 | } |
51 | ||
fb7342fd PC |
52 | rvalstruct(const rvalstruct&) = delete; |
53 | ||
f5783e34 | 54 | rvalstruct(rvalstruct&& in) |
60c5236e | 55 | { |
60c5236e | 56 | VERIFY( in.valid == true ); |
f5783e34 CJ |
57 | val = in.val; |
58 | in.valid = false; | |
59 | valid = true; | |
60 | } | |
61 | ||
fb7342fd PC |
62 | rvalstruct& |
63 | operator=(const rvalstruct&) = delete; | |
64 | ||
f5783e34 CJ |
65 | rvalstruct& |
66 | operator=(rvalstruct&& in) | |
60c5236e | 67 | { |
848ca96f | 68 | VERIFY( this != &in ); |
60c5236e | 69 | VERIFY( in.valid == true ); |
f5783e34 CJ |
70 | val = in.val; |
71 | in.valid = false; | |
72 | valid = true; | |
73 | return *this; | |
74 | } | |
75 | }; | |
76 | ||
f92ab29f | 77 | inline bool |
f5783e34 CJ |
78 | operator==(const rvalstruct& lhs, const rvalstruct& rhs) |
79 | { return lhs.val == rhs.val; } | |
80 | ||
fb7342fd | 81 | inline bool |
f5783e34 CJ |
82 | operator<(const rvalstruct& lhs, const rvalstruct& rhs) |
83 | { return lhs.val < rhs.val; } | |
84 | ||
85 | void | |
86 | swap(rvalstruct& lhs, rvalstruct& rhs) | |
60c5236e | 87 | { |
60c5236e | 88 | VERIFY( lhs.valid && rhs.valid ); |
f5783e34 CJ |
89 | int temp = lhs.val; |
90 | lhs.val = rhs.val; | |
91 | rhs.val = temp; | |
92 | } | |
93 | ||
94 | // This is a moveable class which copies how many times it is copied. | |
95 | // This is mainly of use in the containers, where the an element inserted | |
96 | // into a container has to be copied once to get there, but we want to check | |
97 | // nothing else is copied. | |
98 | struct copycounter | |
99 | { | |
100 | static int copycount; | |
101 | int val; | |
102 | bool valid; | |
f92ab29f | 103 | |
f5783e34 CJ |
104 | copycounter() : val(0), valid(true) |
105 | { } | |
106 | ||
107 | copycounter(int inval) : val(inval), valid(true) | |
108 | { } | |
109 | ||
110 | copycounter(const copycounter& in) : val(in.val), valid(true) | |
60c5236e | 111 | { |
60c5236e | 112 | VERIFY( in.valid == true ); |
f5783e34 CJ |
113 | ++copycount; |
114 | } | |
115 | ||
74a2a1b4 | 116 | copycounter(copycounter&& in) noexcept |
60c5236e | 117 | { |
60c5236e | 118 | VERIFY( in.valid == true ); |
f5783e34 CJ |
119 | val = in.val; |
120 | in.valid = false; | |
121 | valid = true; | |
122 | } | |
f92ab29f | 123 | |
f5783e34 CJ |
124 | copycounter& |
125 | operator=(int newval) | |
f92ab29f | 126 | { |
f5783e34 CJ |
127 | val = newval; |
128 | valid = true; | |
7919bb2f | 129 | return *this; |
f5783e34 CJ |
130 | } |
131 | ||
132 | bool | |
f92ab29f | 133 | operator=(const copycounter& in) |
60c5236e | 134 | { |
60c5236e | 135 | VERIFY( in.valid == true ); |
f5783e34 CJ |
136 | ++copycount; |
137 | val = in.val; | |
138 | valid = true; | |
7919bb2f | 139 | return true; |
f5783e34 CJ |
140 | } |
141 | ||
142 | copycounter& | |
143 | operator=(copycounter&& in) | |
60c5236e | 144 | { |
f5783e34 CJ |
145 | VERIFY(in.valid == true); |
146 | val = in.val; | |
147 | in.valid = false; | |
148 | valid = true; | |
149 | return *this; | |
150 | } | |
f92ab29f | 151 | |
74a2a1b4 | 152 | ~copycounter() noexcept |
f5783e34 CJ |
153 | { valid = false; } |
154 | }; | |
155 | ||
156 | int copycounter::copycount = 0; | |
f92ab29f | 157 | |
fb7342fd | 158 | inline bool |
f5783e34 CJ |
159 | operator==(const copycounter& lhs, const copycounter& rhs) |
160 | { return lhs.val == rhs.val; } | |
161 | ||
fb7342fd | 162 | inline bool |
f5783e34 CJ |
163 | operator<(const copycounter& lhs, const copycounter& rhs) |
164 | { return lhs.val < rhs.val; } | |
165 | ||
fb7342fd | 166 | inline void |
f5783e34 | 167 | swap(copycounter& lhs, copycounter& rhs) |
60c5236e | 168 | { |
60c5236e | 169 | VERIFY( lhs.valid && rhs.valid ); |
f5783e34 CJ |
170 | int temp = lhs.val; |
171 | lhs.val = rhs.val; | |
172 | rhs.val = temp; | |
173 | } | |
f5783e34 | 174 | |
60c5236e PC |
175 | // In the occasion of libstdc++/48038. |
176 | struct rvalstruct_compare_by_value | |
177 | { | |
178 | int val; | |
179 | bool ok; | |
9b690d8c | 180 | |
60c5236e PC |
181 | rvalstruct_compare_by_value(int v) |
182 | : val(v), ok(true) { } | |
183 | ||
184 | rvalstruct_compare_by_value(const rvalstruct_compare_by_value& rh) | |
185 | : val(rh.val), ok(rh.ok) | |
186 | { | |
60c5236e PC |
187 | VERIFY(rh.ok); |
188 | } | |
189 | ||
190 | rvalstruct_compare_by_value& | |
191 | operator=(const rvalstruct_compare_by_value& rh) | |
192 | { | |
60c5236e PC |
193 | VERIFY( rh.ok ); |
194 | val = rh.val; | |
195 | ok = rh.ok; | |
196 | return *this; | |
197 | } | |
198 | ||
199 | rvalstruct_compare_by_value(rvalstruct_compare_by_value&& rh) | |
200 | : val(rh.val), ok(rh.ok) | |
201 | { | |
60c5236e PC |
202 | VERIFY( rh.ok ); |
203 | rh.ok = false; | |
204 | } | |
205 | ||
206 | rvalstruct_compare_by_value& | |
207 | operator=(rvalstruct_compare_by_value&& rh) | |
208 | { | |
60c5236e PC |
209 | VERIFY( rh.ok ); |
210 | val = rh.val; | |
211 | ok = rh.ok; | |
212 | rh.ok = false; | |
213 | return *this; | |
214 | } | |
215 | }; | |
216 | ||
217 | inline bool | |
218 | operator<(rvalstruct_compare_by_value lh, | |
219 | rvalstruct_compare_by_value rh) | |
220 | { | |
60c5236e PC |
221 | VERIFY( rh.ok ); |
222 | VERIFY( lh.ok ); | |
223 | return lh.val < rh.val; | |
224 | } | |
225 | ||
226 | inline bool | |
227 | order(rvalstruct_compare_by_value lh, | |
228 | rvalstruct_compare_by_value rh) | |
229 | { | |
60c5236e PC |
230 | VERIFY( rh.ok ); |
231 | VERIFY( lh.ok ); | |
232 | return lh.val < rh.val; | |
233 | } | |
234 | ||
74a2a1b4 PC |
235 | struct throwing_move_constructor |
236 | { | |
237 | throwing_move_constructor() = default; | |
238 | ||
239 | throwing_move_constructor(throwing_move_constructor&&) | |
240 | { throw 1; } | |
241 | ||
242 | throwing_move_constructor(const throwing_move_constructor&) = default; | |
a2e70335 JM |
243 | |
244 | throwing_move_constructor& | |
245 | operator=(const throwing_move_constructor&) = default; | |
74a2a1b4 PC |
246 | }; |
247 | ||
60c5236e | 248 | } // namespace __gnu_test |
9b690d8c | 249 | |
fb7342fd PC |
250 | namespace std |
251 | { | |
60c5236e | 252 | /// std::hash specialization for __gnu_test::rvalstruct. |
fb7342fd PC |
253 | template<> |
254 | struct hash<__gnu_test::rvalstruct> | |
255 | { | |
256 | typedef size_t result_type; | |
257 | typedef __gnu_test::rvalstruct argument_type; | |
258 | ||
259 | size_t | |
260 | operator()(const __gnu_test::rvalstruct& __rvs) const | |
261 | { return __rvs.val; } | |
262 | }; | |
263 | } | |
264 | ||
f5783e34 | 265 | #endif // _GLIBCXX_TESTSUITE_TR1_H |