OSDN Git Service

Initial revision
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / g++.old-deja / g++.jason / 2371.C
1 // GROUPS passed templates nested-classes
2 // Special g++ Options: 
3 //
4 // The SetLS template test
5 //
6 // Wendell Baker, Berkeley CAD Group, 1993 (wbaker@ic.Berkeley.EDU)
7 //
8
9
10 #pragma implementation "ListS.h"
11 #pragma implementation "SetLS.h"
12
13 #include <stdlib.h>
14 #include <iostream.h>
15 # 1 "../../templates/SetLS.h" 1
16 // -*- C++ -*-
17
18
19
20 //
21 // A Set Template - implemented with an ListS
22 //
23 // Wendell Baker, Berkeley CAD Group, 1993 (wbaker@ic.Berkeley.EDU)
24 //
25
26
27
28
29
30 #pragma interface
31
32
33 // $Source: /egcs/carton/cvsfiles/egcs/gcc/testsuite/g++.old-deja/g++.jason/2371.C,v $
34 // $Author: law $
35 // $Revision: 1.1 $
36 // $Date: 1997/08/19 07:35:23 $
37
38
39
40 #define XTRUE true
41 #define XFALSE false
42
43 # 37 "../../templates/SetLS.h"
44
45
46 # 1 "../../templates/ListS.h" 1
47 // -*- C++ -*-
48
49
50
51 //
52 // A List Template - providing a singly linked capability
53 //
54 // Wendell Baker, Berkeley CAD Group, 1993 (wbaker@ic.Berkeley.EDU)
55 //
56
57
58
59
60
61 #pragma interface
62
63
64 // $Source: /egcs/carton/cvsfiles/egcs/gcc/testsuite/g++.old-deja/g++.jason/2371.C,v $
65 // $Author: law $
66 // $Revision: 1.1 $
67 // $Date: 1997/08/19 07:35:23 $
68
69
70
71
72 # 1 "/projects/gnu-cygnus/gnu-cygnus-14/mips/lib/gcc-lib/decstation/cygnus-reno-1/g++-include/bool.h" 1 3
73 // Defining XTRUE and XFALSE is usually a Bad Idea,
74 // because you will probably be inconsistent with anyone
75 // else who had the same clever idea.
76 // Therefore:  DON'T USE THIS FILE.
77
78
79
80
81
82
83
84
85
86 # 26 "../../templates/ListS.h" 2
87
88 # 37 "../../templates/ListS.h"
89
90
91
92 // g++ reno-1 is not yet capable of creating templates with nested
93 // classes which instantiate the template arguments.
94 template<class T>
95 struct ListS_link {
96     T item;
97     ListS_link<T> *next;
98
99     ListS_link(const T& i, ListS_link<T> *n = 0): item(i), next(n)
100         { }
101 };
102
103
104 //
105 // For now, errors are raised by ::abort() because exceptions
106 // are not well implemented in cxx or at all in CC 3.0.1
107 //
108 template<class T>
109 class ListS {
110 public:
111     ListS();
112     ListS(const ListS<T>&);
113     ~ListS();
114
115     void operator=(const ListS<T>&);
116     
117     unsigned length() const
118         { return count; }
119     
120     void prepend(const T& item);
121     void append(const T& item);
122     void clear();
123
124     const T& head() const
125         { ensure_1();
126           return head_link->item; }
127     T& head()
128         { ensure_1();
129           return head_link->item; }
130     void head(T& fill) const
131         { ensure_1();
132           fill = head_link->item; }
133     void remove_head()
134         { remove_head_filling(0); }
135     void remove_head(T& fill)
136         { remove_head_filling(&fill); }
137
138     const T& tail() const
139         { ensure_1();
140           return tail_link->item; }
141     T& tail()
142         { ensure_1();
143           return tail_link->item; }
144     void tail(T& fill) const
145         { ensure_1();
146           fill = tail_link->item; }
147
148     class Vix {
149     public:
150         Vix(): owner(0), index(0)
151             { }
152         
153         // These are friend functions so that v == x is the same as x == v
154         friend int operator==(void *v, const Vix& x)
155             { return v == x.index; }
156         friend int operator==(const Vix& x, void *v)
157             { return v == x.index; }
158         friend int operator!=(void *v, const Vix& x)
159             { return v != x.index; }
160         friend int operator!=(const Vix& x, void *v)
161             { return v != x.index; }
162         friend int operator==(const Vix& x1, const Vix& x2)
163             { return x1.owner == x2.owner && x1.index == x2.index; }
164         friend int operator!=(const Vix& x1, const Vix& x2)
165             { return x1.owner != x2.owner || x1.index != x2.index; }
166     private:
167         friend class ListS<T>;
168         
169
170         Vix(const ListS<T> *o, ListS_link<T> *i): owner(o), index(i)
171             { }
172
173
174
175
176         
177         const ListS<T> *owner;
178
179         ListS_link<T> *index;
180
181
182
183     };
184     
185     Vix first() const
186         { return Vix(this, head_link); }
187     void next(Vix& x) const
188         { check(x);
189           if (x.index != 0)
190               x.index = x.index->next; }
191     T& operator()(const Vix& x)
192         { check(x);
193           return x.index->item; }
194     const T& operator()(const Vix& x) const
195         { check(x);
196           return x.index->item; }
197 protected:
198 # 154 "../../templates/ListS.h"
199
200
201     unsigned count;
202
203     ListS_link<T> *head_link;   // 0 for a zero-length list
204     ListS_link<T> *tail_link;   // 0 for a zero-length list
205
206
207
208
209
210 private:
211     // fill may be 0 (then don't fill)
212     void remove_head_filling(T *fill);
213
214     void ensure_1() const
215         { if (0 == head_link)
216               ::abort(); }
217     void check(const Vix& x) const
218         { if (this != x.owner)
219               ::abort();
220           if (0 == x.index)
221               ::abort(); }
222 };
223
224 template<class T>
225 ListS<T>::ListS():
226 count(0),
227 head_link(0),
228 tail_link(0)
229 { }
230
231 template<class T>
232 ListS<T>::ListS(const ListS<T>& other):
233 count(0),
234 head_link(0),
235 tail_link(0)
236 {
237     for (Vix x=other.first(); 0 != x; other.next(x))
238         append(other(x));
239 }
240
241 template<class T>
242 ListS<T>::~ListS()
243 {
244     clear();
245 }
246
247 template<class T>
248 void
249 ListS<T>::operator=(const ListS<T>& other)
250 {
251     clear();
252     for (Vix x=other.first(); 0 != x; other.next(x))
253         append(other(x));
254 }
255
256 template<class T>
257 void
258 ListS<T>::prepend(const T& item)
259 {
260
261     head_link = new ListS_link<T>(item, head_link);
262
263
264
265     if (0 == tail_link)
266         tail_link = head_link;
267     count++;
268 }
269
270 template<class T>
271 void
272 ListS<T>::append(const T& item)
273 {
274
275     ListS_link<T> *new_link = new ListS_link<T>(item); 
276
277
278
279     if (0 == tail_link) {
280         head_link = new_link;
281         tail_link = new_link;
282     } else {
283         tail_link->next = new_link;
284         tail_link = tail_link->next;
285     }
286     count++;
287 }
288
289 template<class T>
290 void
291 ListS<T>::clear()
292 {
293
294     ListS_link<T> *next, *l;
295
296
297
298     for (l=head_link; 0 != l; l=next) {
299         next = l->next;
300         delete l;
301     }
302
303     count = 0;
304     head_link = 0;
305     tail_link = 0;
306 }
307
308 template<class T>
309 void
310 ListS<T>::remove_head_filling(T* fill)
311 // fill may be 0 in which case don't assign into it
312 {
313     ensure_1();
314
315     ListS_link<T> *ohead = head_link;
316
317
318
319     if (0 != fill)
320         *fill = ohead->item;
321     head_link = ohead->next;
322     if (0 == head_link)
323         tail_link = 0;
324     count--;
325     delete ohead;
326 }
327
328
329 # 39 "../../templates/SetLS.h" 2
330
331
332 # 62 "../../templates/SetLS.h"
333
334 template<class T>
335 class SetLS {
336 public:
337     SetLS();
338     
339     void add(const T& item);
340     // There is no remove(const T& item) for this set
341     bool contains(const T& item) const;
342
343     unsigned length() const
344         { return list.length(); }
345
346     void clear()
347         { list.clear(); }
348
349     class Vix {
350     public:
351         Vix(): owner(0), vix()
352             { }
353
354         // These are friend functions so that v == x is the same as x == v
355         friend int operator==(void *v, const Vix& x)
356             { return v == x.vix; }
357         friend int operator==(const Vix& x, void *v)
358             { return v == x.vix; }
359         friend int operator!=(void *v, const Vix& x)
360             { return v != x.vix; }
361         friend int operator!=(const Vix& x, void *v)
362             { return v != x.vix; }
363         friend int operator==(const Vix& x1, const Vix& x2)
364             { return x1.owner == x2.owner && x1.vix == x2.vix; }
365         friend int operator!=(const Vix& x1, const Vix& x2)
366             { return x1.owner != x2.owner || x1.vix != x2.vix; }
367     private:
368         friend class SetLS<T>;
369
370         Vix(const SetLS<T> *o, const ListS<T>::Vix& x): owner(o), vix(x)
371             { }
372
373         const SetLS<T> *owner;
374         ListS<T>::Vix vix;
375     };
376     friend class Vix;
377     
378     Vix first() const
379         { return Vix(this, list.first()); }
380     void next(Vix& x) const
381         { check(x);
382           list.next(x.vix); }
383     const T& operator()(const Vix& x) const
384         { check(x);
385           return list(x.vix); }
386     // There is item no remove(const Vix&) for this set
387 protected:
388     ListS<T> list;
389
390 private:
391     void check(const Vix& x) const
392         { if (this != x.owner)
393               ::abort(); }
394 };
395
396
397 template<class T>
398 SetLS<T>::SetLS():
399
400
401
402 list()
403
404 { }
405
406 template<class T>
407 void
408 SetLS<T>::add(const T& item)
409 {
410     if ( ! contains(item) ) {
411
412
413
414         list.append(item);
415
416     }
417 }
418
419 template<class T>
420 bool
421 SetLS<T>::contains(const T& item) const
422 {
423     for (Vix x=first(); 0 != x; next(x)) {
424         if (operator()(x) == item)
425             return XTRUE;
426     }
427     return XFALSE;
428 }
429
430
431 # 14 "SetLS.cc" 2
432
433
434 // $Source: /egcs/carton/cvsfiles/egcs/gcc/testsuite/g++.old-deja/g++.jason/2371.C,v $
435 // $Author: law $
436 // $Revision: 1.1 $
437 // $Date: 1997/08/19 07:35:23 $
438
439 # 1 "/projects/gnu-cygnus/gnu-cygnus-14/mips/lib/gcc-lib/decstation/cygnus-reno-1/g++-include/iostream.h" 1 3
440 //    This is part of the iostream library, providing -*- C++ -*- input/output.
441 //    Copyright (C) 1991 Per Bothner.
442 //
443 //    This library is free software; you can redistribute it and/or
444 //    modify it under the terms of the GNU Library General Public
445 //    License as published by the Free Software Foundation; either
446 //    version 2 of the License, or (at your option) any later version.
447 //
448 //    This library is distributed in the hope that it will be useful,
449 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
450 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
451 //    Library General Public License for more details.
452 //
453 //    You should have received a copy of the GNU Library General Public
454 //    License along with this library; if not, write to the Free
455 //    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
456
457 # 211 "/projects/gnu-cygnus/gnu-cygnus-14/mips/lib/gcc-lib/decstation/cygnus-reno-1/g++-include/iostream.h" 3
458
459 # 21 "SetLS.cc" 2
460
461
462 // In (most versions of) g++ 2.X, this use of typedefs has the effect
463 // of causing the instantiation of the templates, thereby testing the
464 // templates
465
466 class test {
467 public:
468     test(): value(0)
469         { }
470     test(int v): value(v)
471         { }
472
473     void print(ostream& out) const
474         { out << value; }
475
476     friend int operator==(const test& a, const test& b);
477 private:
478     int value;
479 };
480
481 int
482 operator==(const test& a, const test& b)
483 {
484     return a.value == b.value;
485 }
486
487 ostream&
488 operator<<(ostream& o, const test& t)
489 {
490     t.print(o);
491     return o;
492 }
493
494 typedef SetLS<test> SLS;
495
496 static ostream&
497 operator<<(ostream& o, const SLS& s)
498 {
499     o << "set of " << s.length() << " = {";
500
501     bool first;
502     SetLS<test>::Vix x;
503     for (first=XTRUE, x=s.first(); 0 != x; s.next(x), first=XFALSE) {
504         if ( ! first )
505             o << ',';
506         o << ' ';
507         s(x).print(o);
508     }
509     o << '}';
510
511     return o;
512 }
513
514 SLS gsls;
515 const SLS gcsls;
516
517 foo()
518 {
519     const unsigned SIZE = 20;
520
521     //
522     // SetLS()
523     // SetLS(const SetLS<T>&)
524     // 
525     SLS sls;
526     {
527         // Fill sls with some interesting values
528         for (unsigned i=0; i<SIZE; i++) {
529             test t = i;
530             sls.add(t);
531         }
532     }
533
534     const SLS csls(sls);
535
536     //
537     // void operator=(const SetLS<T>&);
538     //
539     sls = csls;
540
541     //
542     // bool contains(const T& item) const
543     //
544     for (unsigned i=0; i<SIZE; i++) {
545         test t = i;
546
547         int contains = sls.contains(t);
548     }
549
550     //
551     // void clear()
552     //
553     sls.clear();
554     if (sls.length() != 0)
555         ::abort();
556
557     sls = csls;
558
559     //
560     // Vix first() const
561     // void next(Vix& x) const
562     // T& operator()(const Vix& x)
563     // const T& operator()(const Vix& x) const
564     //
565     SetLS<test>::Vix cx;
566     for (cx=csls.first(); 0 != cx; sls.next(cx)) {
567         if ( ! sls.contains(csls(cx)) )
568             ::abort();
569     }
570
571     cout << "gsls:\t" << gsls << '\n';
572     cout << "gcsls:\t" << gcsls << '\n';
573     cout << "sls:\t" << sls << '\n';
574     cout << "csls:\t" << csls << '\n';
575 }
576
577 // Dummy function so it'll run
578 main()
579 {
580   cout << "PASS" << endl;
581 }
582
583 template class ListS<test>;