]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/util/testsuite_rvalref.h
testsuite_allocator.h, [...]: Remove semi-colons after namespace declarations.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / util / testsuite_rvalref.h
CommitLineData
f5783e34
CJ
1// -*- C++ -*-
2// Testing utilities for the rvalue reference.
3//
4// Copyright (C) 2005, 2007 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 2, 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 COPYING. If not, write to the Free
19// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20// USA.
21//
22// As a special exception, you may use this file as part of a free software
23// library without restriction. Specifically, if other files instantiate
24// templates or use macros or inline functions from this file, or you compile
25// this file and link it with other files to produce an executable, this
26// file does not by itself cause the resulting executable to be covered by
27// the GNU General Public License. This exception does not however
28// invalidate any other reasons why the executable file might be covered by
29// the GNU General Public License.
30
31#ifndef _GLIBCXX_TESTSUITE_RVALREF_H
32#define _GLIBCXX_TESTSUITE_RVALREF_H 1
33
34#include <testsuite_hooks.h>
35
36namespace __gnu_test
37{
38
39 // This class is designed to test libstdc++'s template-based rvalue
40 // reference support. It should fail at compile-time if there is an attempt
41 // to copy it (although see note just below).
42 class rvalstruct
43 {
44 bool
45 operator=(const rvalstruct&);
46
47// Normally we don't define a copy constructor, as any use of it would
48// show an inefficency. In some cases we know it will be aliased away
49// by the compiler, but it still insists it is defined, so we provide
50// a way of making it public but not giving a body, so any usage would
51// instead fail at link-time.
52#ifdef _GLIBCXX_TESTSUITE_ALLOW_RVALREF_ALIASING
53 public:
54 rvalstruct(const rvalstruct&);
55#else
56 rvalstruct(const rvalstruct&);
57
58 public:
59#endif
60 int val;
61 bool valid;
62
63 rvalstruct() : valid(false)
64 { }
65
66 rvalstruct(int inval) : val(inval), valid(true)
67 { }
68
69 rvalstruct&
70 operator=(int newval)
71 {
72 VERIFY(valid == false);
73 val = newval;
74 valid = true;
75 }
76
77 rvalstruct(rvalstruct&& in)
78 {
79 VERIFY(in.valid == true);
80 val = in.val;
81 in.valid = false;
82 valid = true;
83 }
84
85 rvalstruct&
86 operator=(rvalstruct&& in)
87 {
88 VERIFY(in.valid == true);
89 val = in.val;
90 in.valid = false;
91 valid = true;
92 return *this;
93 }
94 };
95
96 bool
97 operator==(const rvalstruct& lhs, const rvalstruct& rhs)
98 { return lhs.val == rhs.val; }
99
100 bool
101 operator<(const rvalstruct& lhs, const rvalstruct& rhs)
102 { return lhs.val < rhs.val; }
103
104 void
105 swap(rvalstruct& lhs, rvalstruct& rhs)
106 {
107 VERIFY(lhs.valid && rhs.valid);
108 int temp = lhs.val;
109 lhs.val = rhs.val;
110 rhs.val = temp;
111 }
112
113 // This is a moveable class which copies how many times it is copied.
114 // This is mainly of use in the containers, where the an element inserted
115 // into a container has to be copied once to get there, but we want to check
116 // nothing else is copied.
117 struct copycounter
118 {
119 static int copycount;
120 int val;
121 bool valid;
122
123 copycounter() : val(0), valid(true)
124 { }
125
126 copycounter(int inval) : val(inval), valid(true)
127 { }
128
129 copycounter(const copycounter& in) : val(in.val), valid(true)
130 {
131 VERIFY(in.valid == true);
132 ++copycount;
133 }
134
135 copycounter(copycounter&& in)
136 {
137 VERIFY(in.valid == true);
138 val = in.val;
139 in.valid = false;
140 valid = true;
141 }
142
143 copycounter&
144 operator=(int newval)
145 {
146 val = newval;
147 valid = true;
148 }
149
150 bool
151 operator=(const copycounter& in)
152 {
153 VERIFY(in.valid == true);
154 ++copycount;
155 val = in.val;
156 valid = true;
157 }
158
159 copycounter&
160 operator=(copycounter&& in)
161 {
162 VERIFY(in.valid == true);
163 val = in.val;
164 in.valid = false;
165 valid = true;
166 return *this;
167 }
168
169 ~copycounter()
170 { valid = false; }
171 };
172
173 int copycounter::copycount = 0;
174
175 bool
176 operator==(const copycounter& lhs, const copycounter& rhs)
177 { return lhs.val == rhs.val; }
178
179 bool
180 operator<(const copycounter& lhs, const copycounter& rhs)
181 { return lhs.val < rhs.val; }
182
183 void
184 swap(copycounter& lhs, copycounter& rhs)
185 {
186 VERIFY(lhs.valid && rhs.valid);
187 int temp = lhs.val;
188 lhs.val = rhs.val;
189 rhs.val = temp;
190 }
191
799a6e36 192} // namespace __gnu_test
f5783e34
CJ
193
194#endif // _GLIBCXX_TESTSUITE_TR1_H