1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2008, 2009, 2010 Free Software Foundation, Inc.
5 Contributed by Red Hat, originally written by Keith Seitz.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program 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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 Please email any bugs, comments, and/or additions to this file to:
30 // A simple template with specializations
35 void do_something () { } // tclass<T>::do_something
39 void tclass<char>::do_something () { } // tclass<char>::do_something
42 void tclass<int>::do_something () { } // tclass<int>::do_something
45 void tclass<long>::do_something () { } // tclass<long>::do_something
48 void tclass<short>::do_something () { } // tclass<short>::do_something
50 // A simple template with multiple template parameters
51 template <class A, class B, class C, class D, class E>
52 void flubber (void) // flubber
67 // Some contrived policies
71 static void function (void) { } // operation_1<T>::function
77 static void function (void) { } // operation_2<T>::function
83 static void function (void) { } // operation_3<T>::function
89 static void function (void) { } // operation_4<T>::function
92 // A policy-based class w/ and w/o default policy
93 template <class T, class Policy>
94 class policy : public Policy
97 policy (T obj) : obj_ (obj) { } // policy<T, Policy>::policy
103 template <class T, class Policy = operation_1<T> >
104 class policyd : public Policy
107 policyd (T obj) : obj_ (obj) { } // policyd<T, Policy>::policyd
108 ~policyd (void) { } // policyd<T, Policy>::~policyd
114 typedef policy<int, operation_1<void*> > policy1;
115 typedef policy<int, operation_2<void*> > policy2;
116 typedef policy<int, operation_3<void*> > policy3;
117 typedef policy<int, operation_4<void*> > policy4;
119 typedef policyd<int> policyd1;
120 typedef policyd<long> policyd2;
121 typedef policyd<char> policyd3;
122 typedef policyd<base> policyd4;
123 typedef policyd<tclass<int> > policyd5;
126 static fluff *g_fluff = new fluff ();
134 base (void) : foo_ (42) { } // base::base(void)
135 base (int foo) : foo_ (foo) { } // base::base(int)
136 ~base (void) { } // base::~base
138 // Some overloaded methods
139 int overload (void) const { return 0; } // base::overload(void) const
140 int overload (int i) const { return 1; } // base::overload(int) const
141 int overload (short s) const { return 2; } // base::overload(short) const
142 int overload (long l) const { return 3; } // base::overload(long) const
143 int overload (char* a) const { return 4; } // base::overload(char*) const
144 int overload (base& b) const { return 5; } // base::overload(base&) const
147 int operator+ (base const& o) const // base::operator+
148 { return foo_ + o.foo_; }
150 base operator++ (void) // base::operator++
151 { ++foo_; return *this; }
153 base operator+=(base const& o) // base::operator+=
154 { foo_ += o.foo_; return *this; }
156 int operator- (base const& o) const // base::operator-
157 { return foo_ - o.foo_; }
159 base operator-- (void) // base::operator--
160 { --foo_; return *this; }
162 base operator-= (base const& o) // base::operator-=
163 { foo_ -= o.foo_; return *this; }
165 int operator* (base const& o) const // base::operator*
166 { return foo_ * o.foo_; }
168 base operator*= (base const& o) // base::operator*=
169 { foo_ *= o.foo_; return *this; }
171 int operator/ (base const& o) const // base::operator/
172 { return foo_ / o.foo_; }
174 base operator/= (base const& o) // base::operator/=
175 { foo_ /= o.foo_; return *this; }
177 int operator% (base const& o) const // base::operator%
178 { return foo_ % o.foo_; }
180 base operator%= (base const& o) // base::operator%=
181 { foo_ %= o.foo_; return *this; }
183 bool operator< (base const& o) const // base::operator<
184 { return foo_ < o.foo_; }
186 bool operator<= (base const& o) const // base::operator<=
187 { return foo_ <= o.foo_; }
189 bool operator> (base const& o) const // base::operator>
190 { return foo_ > o.foo_; }
192 bool operator>= (base const& o) const // base::operator>=
193 { return foo_ >= o.foo_; }
195 bool operator!= (base const& o) const // base::operator!=
196 { return foo_ != o.foo_; }
198 bool operator== (base const& o) const // base::operator==
199 { return foo_ == o.foo_; }
201 bool operator! (void) const // base::operator!
204 bool operator&& (base const& o) const // base::operator&&
205 { return foo_ && o.foo_; }
207 bool operator|| (base const& o) const // base::operator||
208 { return foo_ || o.foo_; }
210 int operator<< (int value) const // base::operator<<
211 { return foo_ << value; }
213 base operator<<= (int value) // base::operator<<=
214 { foo_ <<= value; return *this; }
216 int operator>> (int value) const // base::operator>>
217 { return foo_ >> value; }
219 base operator>>= (int value) // base::operator>>=
220 { foo_ >>= value; return *this; }
222 int operator~ (void) const // base::operator~
225 int operator& (base const& o) const // base::operator&
226 { return foo_ & o.foo_; }
228 base operator&= (base const& o) // base::operator&=
229 { foo_ &= o.foo_; return *this; }
231 int operator| (base const& o) const // base::operator|
232 { return foo_ | o.foo_; }
234 base operator|= (base const& o) // base::operator|=
235 { foo_ |= o.foo_; return *this; }
237 int operator^ (base const& o) const // base::operator^
238 { return foo_ ^ o.foo_; }
240 base operator^= (base const& o) // base::operator^=
241 { foo_ ^= o.foo_; return *this; }
243 base operator= (base const& o) // base::operator=
244 { foo_ = o.foo_; return *this; }
246 void operator() (void) const // base::operator()
249 int operator[] (int idx) const // base::operator[]
252 void* operator new (size_t size) throw () // base::operator new
253 { return malloc (size); }
255 void operator delete (void* ptr) // base::operator delete
258 void* operator new[] (size_t size) throw () // base::operator new[]
259 { return malloc (size); }
261 void operator delete[] (void* ptr) // base::operator delete[]
264 base const* operator-> (void) const // base::opeartor->
267 int operator->* (base const& b) const // base::operator->*
268 { return foo_ * b.foo_; }
270 operator char* () const { return const_cast<char*> ("hello"); } // base::operator char*
271 operator int () const { return 21; } // base::operator int
272 operator fluff* () const { return new fluff (); } // base::operator fluff*
273 operator fluff** () const { return &g_fluff; } // base::operator fluff**
276 class base1 : public virtual base
279 base1 (void) : foo_ (21) { } // base1::base1(void)
280 base1 (int a) : foo_(a) { } // base1::base1(int)
281 void a_function (void) const { } // base1::a_function
287 class base2 : public virtual base
290 base2 () : foo_ (3) { } // base2::base2
293 void a_function (void) const { } // base2::a_function
297 class derived : public base1, public base2
300 derived(void) : foo_ (4) { } // derived::derived
301 void a_function (void) const // derived::a_function
303 this->base1::a_function ();
304 this->base2::a_function ();
312 main (int argc, char* argv[]) // main
315 void (derived::*pfunc) (void) const = &derived::a_function;
318 base a (1), b (3), c (8);
319 (void) a.overload ();
320 (void) a.overload (static_cast<int> (0));
321 (void) a.overload (static_cast<short> (0));
322 (void) a.overload (static_cast<long> (0));
323 (void) a.overload (static_cast<char*> (0));
324 (void) a.overload (a);
362 derived* f = new derived ();
363 derived* g = new derived[3];
369 tclass<char> char_tclass;
370 tclass<int> int_tclass;
371 tclass<short> short_tclass;
372 tclass<long> long_tclass;
373 tclass<base> base_tclass;
374 char_tclass.do_something ();
375 int_tclass.do_something ();
376 short_tclass.do_something ();
377 long_tclass.do_something ();
378 base_tclass.do_something ();
380 flubber<int, int, int, int, int> ();
381 flubber<int, int, int, int, short> ();
382 flubber<int, int, int, int, long> ();
383 flubber<int, int, int, int, char> ();
384 flubber<int, int, int, short, int> ();
385 flubber<int, int, int, short, short> ();
386 flubber<int, int, int, short, long> ();
387 flubber<int, int, int, short, char> ();
388 flubber<int, int, int, long, int> ();
389 flubber<int, int, int, long, short> ();
390 flubber<int, int, int, long, long> ();
391 flubber<int, int, int, long, char> ();
392 flubber<int, int, int, char, int> ();
393 flubber<int, int, int, char, short> ();
394 flubber<int, int, int, char, long> ();
395 flubber<int, int, int, char, char> ();
396 flubber<int, int, short, int, int> ();
397 flubber<int, int, short, int, short> ();
398 flubber<int, int, short, int, long> ();
399 flubber<int, int, short, int, char> ();
400 flubber<int, int, short, short, int> ();
401 flubber<short, int, short, int, short> ();
402 flubber<long, short, long, short, long> ();
421 policyd5 pd5 (int_tclass);