1 | //
|
---|
2 | // classtest.cc
|
---|
3 | //
|
---|
4 | // Copyright (C) 1996 Limit Point Systems, Inc.
|
---|
5 | //
|
---|
6 | // Author: Curtis Janssen <cljanss@limitpt.com>
|
---|
7 | // Maintainer: LPS
|
---|
8 | //
|
---|
9 | // This file is part of the SC Toolkit.
|
---|
10 | //
|
---|
11 | // The SC Toolkit is free software; you can redistribute it and/or modify
|
---|
12 | // it under the terms of the GNU Library General Public License as published by
|
---|
13 | // the Free Software Foundation; either version 2, or (at your option)
|
---|
14 | // any later version.
|
---|
15 | //
|
---|
16 | // The SC Toolkit is distributed in the hope that it will be useful,
|
---|
17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | // GNU Library General Public License for more details.
|
---|
20 | //
|
---|
21 | // You should have received a copy of the GNU Library General Public License
|
---|
22 | // along with the SC Toolkit; see the file COPYING.LIB. If not, write to
|
---|
23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
24 | //
|
---|
25 | // The U.S. Government is granted a limited license as per AL 91-7.
|
---|
26 | //
|
---|
27 |
|
---|
28 | // a simple program to test the class stuff
|
---|
29 |
|
---|
30 | #include <iostream>
|
---|
31 |
|
---|
32 | #include <util/misc/formio.h>
|
---|
33 | #include <util/class/class.h>
|
---|
34 |
|
---|
35 | using namespace std;
|
---|
36 | using namespace sc;
|
---|
37 |
|
---|
38 | #undef SIMPLE_TEST
|
---|
39 |
|
---|
40 | class A: virtual public DescribedClass {
|
---|
41 | private:
|
---|
42 | int i;
|
---|
43 | public:
|
---|
44 | A():i(1) {};
|
---|
45 | ~A() { cout << "A dtor\n"; };
|
---|
46 | };
|
---|
47 |
|
---|
48 | static ClassDesc A_cd(typeid(A),"A",1,"virtual public DescribedClass");
|
---|
49 |
|
---|
50 | #ifndef SIMPLE_TEST
|
---|
51 |
|
---|
52 | class B: public A {
|
---|
53 | private:
|
---|
54 | int ib;
|
---|
55 | public:
|
---|
56 | B():ib(2) {};
|
---|
57 | ~B() { cout << "B dtor\n"; };
|
---|
58 | };
|
---|
59 |
|
---|
60 | static ClassDesc B_cd(typeid(B),"B",1,"public A");
|
---|
61 |
|
---|
62 | class C: virtual public DescribedClass {
|
---|
63 | private:
|
---|
64 | int i;
|
---|
65 | public:
|
---|
66 | C():i(3) {};
|
---|
67 | ~C() { cout << "C dtor\n"; };
|
---|
68 | };
|
---|
69 |
|
---|
70 | static ClassDesc C_cd(typeid(C),"C",1,"virtual public DescribedClass");
|
---|
71 |
|
---|
72 | class D: public B, public C {
|
---|
73 | private:
|
---|
74 | int id;
|
---|
75 | A* atst;
|
---|
76 | public:
|
---|
77 | D():id(4),atst(new A) {};
|
---|
78 | ~D() { delete atst; cout << "D dtor\n"; };
|
---|
79 | };
|
---|
80 |
|
---|
81 | static ClassDesc D_cd(typeid(D),"D",1,"public B, public C",create<D>);
|
---|
82 |
|
---|
83 | #endif /* ! SIMPLE_TEST */
|
---|
84 |
|
---|
85 | main()
|
---|
86 | {
|
---|
87 | ClassDesc::list_all_classes();
|
---|
88 |
|
---|
89 | cout << indent << "using 0" << endl;
|
---|
90 | const Ref<DescribedClass> descl2(0);
|
---|
91 | Ref<A> aaa;
|
---|
92 | cout << "getting aaaa" << endl;
|
---|
93 | A* aaaa = 0; //aaa.pointer();
|
---|
94 | cout << "using aaaa" << endl;
|
---|
95 | const Ref<DescribedClass> descl((aaaa==(A*)0)?(DescribedClass*)0:aaaa);
|
---|
96 | cout << "using aaa.pointer()" << endl;
|
---|
97 | const Ref<DescribedClass> descl3((aaa.pointer()==(A*)0)?(DescribedClass*)0:aaa.pointer());
|
---|
98 |
|
---|
99 | A a;
|
---|
100 | cout << "A name:" << a.class_name() << '\n';
|
---|
101 |
|
---|
102 | D* dtst = dynamic_cast<D*>(ClassDesc::name_to_class_desc("D")->create());
|
---|
103 | delete dtst;
|
---|
104 |
|
---|
105 | // check the compiler's handling of virtual inheritance
|
---|
106 | D* dt = new D;
|
---|
107 | C* ct = dt;
|
---|
108 | B* bt = dt;
|
---|
109 | cout << "virtual inheritance test:" << endl;
|
---|
110 | dt->reference();
|
---|
111 | cout << "The following three numbers should be equal:" << endl;
|
---|
112 | cout << ' ' << dt->nreference()
|
---|
113 | << ' ' << ct->nreference()
|
---|
114 | << ' ' << bt->nreference() << endl;
|
---|
115 | ct->reference();
|
---|
116 | cout << "The following three numbers should be equal:" << endl;
|
---|
117 | cout << ' ' << dt->nreference()
|
---|
118 | << ' ' << ct->nreference()
|
---|
119 | << ' ' << bt->nreference() << endl;
|
---|
120 | bt->reference();
|
---|
121 | cout << "The following three numbers should be equal:" << endl;
|
---|
122 | cout << ' ' << dt->nreference()
|
---|
123 | << ' ' << ct->nreference()
|
---|
124 | << ' ' << bt->nreference() << endl;
|
---|
125 | cout << "done with virtual inheritance test:" << endl;
|
---|
126 | dt->dereference();
|
---|
127 | if (dt->nreference() == 0) delete dt;
|
---|
128 | ct->dereference();
|
---|
129 | if (ct->nreference() == 0) delete ct;
|
---|
130 | bt->dereference();
|
---|
131 | if (bt->nreference() == 0) delete bt;
|
---|
132 |
|
---|
133 | #ifndef SIMPLE_TEST
|
---|
134 | D d;
|
---|
135 | cout << "D name:" << d.class_name() << '\n';
|
---|
136 |
|
---|
137 | cout << "&d = " << (void*) &d << '\n';
|
---|
138 | cout << "dynamic_cast<D*>(&d) = " << (void*) dynamic_cast<D*>(&d) << '\n';
|
---|
139 | cout << "dynamic_cast<B*>(&d) = " << (void*) dynamic_cast<B*>(&d) << '\n';
|
---|
140 | cout << "dynamic_cast<A*>(&d) = " << (void*) dynamic_cast<A*>(&d) << '\n';
|
---|
141 | cout << "dynamic_cast<C*>(&d) = " << (void*) dynamic_cast<C*>(&d) << '\n';
|
---|
142 | cout << "dynamic_cast<DescribedClass*>(&d) = "
|
---|
143 | << (void*) dynamic_cast<DescribedClass*>(&d) << '\n';
|
---|
144 |
|
---|
145 | Ref<D> dref(new D);
|
---|
146 | Ref<A> aref(dref);
|
---|
147 |
|
---|
148 | cout << "aref.pointer() is " << aref.pointer() << '\n';
|
---|
149 | cout << "dref.pointer() is " << dref.pointer() << '\n';
|
---|
150 | cout << "aref == dref gives " << (aref == dref) << '\n';
|
---|
151 |
|
---|
152 | dref << aref;
|
---|
153 |
|
---|
154 | cout << "aref.pointer() is " << aref.pointer() << '\n';
|
---|
155 | cout << "dref.pointer() is " << dref.pointer() << '\n';
|
---|
156 | cout << "aref == dref gives " << (aref == dref) << '\n';
|
---|
157 | #endif /* ! SIMPLE_TEST */
|
---|
158 |
|
---|
159 | }
|
---|
160 |
|
---|
161 | /////////////////////////////////////////////////////////////////////////////
|
---|
162 |
|
---|
163 | // Local Variables:
|
---|
164 | // mode: c++
|
---|
165 | // c-file-style: "CLJ"
|
---|
166 | // End:
|
---|