1 ////////////////////////////////////////////////////////////////////////////////
2 // The Loki Library
3 // Copyright (c) 2006 Peter K�mmel
4 // Permission to use, copy, modify, distribute and sell this software for any
5 // purpose is hereby granted without fee, provided that the above copyright
6 // notice appear in all copies and that both that copyright notice and this
7 // permission notice appear in supporting documentation.
8 // The author makes no representations about the
9 // suitability of this software for any purpose. It is provided "as is"
10 // without express or implied warranty.
11 ////////////////////////////////////////////////////////////////////////////////
12
13 // $Id: main.cpp 760 2006-10-17 20:36:13Z syntheticpp $
14
15
16 #ifdef _MSC_VER
17 #pragma warning (disable: 4512)
18 #endif
19
20
21 #define LOKI_INHERITED_PIMPL_NAME d
22 #define LOKI_INHERITED_RIMPL_NAME d
23
24 #include "type.h"
25 #include "type2.h"
26
27 #include <loki/SafeFormat.h>
28
29
30
31 /////////////////////////////////////////
32 // Definition of ImplOf<A>
33 /////////////////////////////////////////
34 namespace Loki // gcc!!
35 {
36 template<>
37 struct ImplOf<A> : public SmallObject<> // inherit SmallObj for speed up
38 {
ImplOfLoki::ImplOf39 ImplOf() : data(0) {Printf("A created\n");}
~ImplOfLoki::ImplOf40 ~ImplOf(){Printf("A destroyed, data=%d\n")(data);}
41 int data;
42 };
43 }
44 /////////////////////////////////////////
45 // class A definition
46 /////////////////////////////////////////
A()47 A::A()
48 {}
49
foo()50 void A::foo()
51 {
52 (*d).data = 1;
53 d->data = 111;
54 }
55
56
57 /////////////////////////////////////////
58 // Definition of ImplOf<B>
59 /////////////////////////////////////////
60 namespace Loki // gcc!!
61 {
62 template<>
63 struct ImplOf<B> : public SmallObject<> // inherit SmallObj for speed up
64 {
ImplOfLoki::ImplOf65 ImplOf() : data(0) {Printf("B created\n");}
~ImplOfLoki::ImplOf66 ~ImplOf(){Printf("B destroyed, data=%d\n")(data);}
67 int data;
68 };
69 }
70 /////////////////////////////////////////
71 // class B definition
72 /////////////////////////////////////////
B()73 B::B() : Loki::PimplOf<B>::Owner()
74 {}
75
foo()76 void B::foo()
77 {
78 (*d).data = 2;
79 d->data = 222;
80 }
81
82
83 /////////////////////////////////////////
84 // Definition of ImplOf<C>
85 /////////////////////////////////////////
86 namespace Loki // gcc!!
87 {
88 template<>
89 struct ImplOf<C> : public SmallObject<> // inherit SmallObj for speed up
90 {
ImplOfLoki::ImplOf91 ImplOf(): data(0) {Printf("C created\n");}
~ImplOfLoki::ImplOf92 ~ImplOf(){Printf("C destroyed, data=%d\n")(data);}
93 int data;
94 };
95 }
96 /////////////////////////////////////////
97 // class C definition
98 /////////////////////////////////////////
C()99 C::C() : p(), d(*p)
100 {}
101
foo()102 void C::foo()
103 {
104 d.data = 333;
105 }
106
107
108
109 /////////////////////////////////////////
110 // Definition of ImplOf<D>
111 /////////////////////////////////////////
112 namespace Loki // gcc!!
113 {
114 template<>
115 struct ImplOf<D> : public SmallObject<> // inherit SmallObj for speed up
116 {
ImplOfLoki::ImplOf117 ImplOf(): data(0) {Printf("D created\n");}
~ImplOfLoki::ImplOf118 ~ImplOf(){Printf("D destroyed, data=%d\n")(data);}
119 int data;
120 };
121 }
122
123 /////////////////////////////////////////
124 // class D definition
125 /////////////////////////////////////////
D()126 D::D() : Loki::RimplOf<D>::Owner()
127 {}
128
foo()129 void D::foo()
130 {
131 d.data = 444;
132 }
133
134
135 /////////////////////////////////////////
136 // main
137 /////////////////////////////////////////
138
139 void test_more();
140
main()141 int main()
142 {
143 A* a = new A;
144 B* b = new B;
145 C* c = new C;
146 D* d = new D;
147 a->foo();
148 b->foo();
149 c->foo();
150 d->foo();
151 delete a;
152 delete b;
153 delete c;
154 delete d;
155
156 test_more();
157
158 #if defined(__BORLANDC__) || defined(_MSC_VER)
159 system("PAUSE");
160 #endif
161
162 return 0;
163 }
164
165
166 ////////////////////
167 // more test code
168 ////////////////////
169
170 // incomplete type test
171 // this must give errors
172
173 //Incomplete1 ii; // compiler error
174 //Incomplete2 i2; // linker error
175 //Incomplete4 i4; // compiler error
176
177 /////////////////////////////////////////
178 // Definition of ImplOf<E>
179 /////////////////////////////////////////
180 namespace Loki // gcc!!
181 {
182 template<>
183 struct ImplOf<E> : public SmallObject<> // inherit SmallObj for speed up
184 {
ImplOfLoki::ImplOf185 ImplOf() : data(0) {Printf("E created\n");}
~ImplOfLoki::ImplOf186 ~ImplOf(){Printf("E destroyed, data=%d\n")(data);}
187 int data;
188
fooLoki::ImplOf189 void foo() {Printf("E foo() \n");}
fooLoki::ImplOf190 void foo() const {Printf("E foo() const \n");}
191 };
192 }
193
194
195
P1()196 P1::P1(){d->data = 1;}
P2()197 P2::P2(){d->data = 2;}
P3()198 P3::P3(){d->data = 3;}
P4()199 P4::P4(){d->data = 4;}
P5()200 P5::P5(){d->data = 5;}
201
PO1()202 PO1::PO1(){d->data = 6;}
PO2()203 PO2::PO2(){d->data = 7;}
PO3()204 PO3::PO3(){d->data = 8;}
PO4()205 PO4::PO4(){d->data = 9;}
PO5()206 PO5::PO5(){d->data = 10;}
207
f()208 void P1::f(){d->foo();}
f()209 void P2::f(){d->foo();}
f()210 void P3::f(){d->foo();}
f()211 void P4::f(){d->foo();}
f()212 void P5::f(){d->foo();}
213
f()214 void PO1::f(){d->foo();}
f()215 void PO2::f(){d->foo();}
f()216 void PO3::f(){d->foo();}
f()217 void PO4::f(){d->foo();}
f()218 void PO5::f(){d->foo();}
219
f() const220 void P1::f()const{d->foo();}
f() const221 void P2::f()const{d->foo();}
f() const222 void P3::f()const{d->foo();}
f() const223 void P4::f()const{d->foo();}
f() const224 void P5::f()const{d->foo();}
225
f() const226 void PO1::f()const{d->foo();}
f() const227 void PO2::f()const{d->foo();}
f() const228 void PO3::f()const{d->foo();}
f() const229 void PO4::f()const{d->foo();}
f() const230 void PO5::f()const{d->foo();}
231
232
233
234
R1()235 R1::R1():d(*p){d.data = 11;}
R2()236 R2::R2():d(*p){d.data = 22;}
R3()237 R3::R3():d(*p){d.data = 33;}
R4()238 R4::R4():d(*p){d.data = 44;}
R5()239 R5::R5():d(*p){d.data = 55;}
240
f()241 void R1::f(){d.foo();}
f()242 void R2::f(){d.foo();}
f()243 void R3::f(){d.foo();}
f()244 void R4::f(){d.foo();}
f()245 void R5::f(){d.foo();}
246
f() const247 void R1::f()const{d.foo();}
f() const248 void R2::f()const{d.foo();}
f() const249 void R3::f()const{d.foo();}
f() const250 void R4::f()const{d.foo();}
f() const251 void R5::f()const{d.foo();}
252
253
254
RO1()255 RO1::RO1(){d.data = 66;}
RO2()256 RO2::RO2(){d.data = 77;}
RO3()257 RO3::RO3(){d.data = 88;}
RO4()258 RO4::RO4(){d.data = 99;}
RO5()259 RO5::RO5(){d.data = 1010;}
260
f()261 void RO1::f(){d.foo();}
f()262 void RO2::f(){d.foo();}
f()263 void RO3::f(){d.foo();}
f()264 void RO4::f(){d.foo();}
f()265 void RO5::f(){d.foo();}
266
f() const267 void RO1::f()const{d.foo();}
f() const268 void RO2::f()const{d.foo();}
f() const269 void RO3::f()const{d.foo();}
f() const270 void RO4::f()const{d.foo();}
f() const271 void RO5::f()const{d.foo();}
272
273
test_more()274 void test_more()
275 {
276 Loki::Printf("\n\nMore tests:\n");
277
278 Loki::Printf("\nCreating Pimpls\n");
279 P1* p1 = new P1;
280 P2* p2 = new P2;
281 P3* p3 = new P3;
282 P4* p4 = new P4;
283 P5* p5 = new P5;
284 PO1* p6 = new PO1;
285 PO2* p7 = new PO2;
286 PO3* p8 = new PO3;
287 PO4* p9 = new PO4;
288 PO5* p10 = new PO5;
289
290 Loki::Printf("\nConst check\n");
291 p1->f();
292 p2->f();
293 p3->f();
294 p4->f();
295 p5->f();
296 p6->f();
297 p7->f();
298 p8->f();
299 p9->f();
300 p10->f();
301
302 Loki::Printf("\nDeleting Pimpls\n");
303 delete p1;
304 delete p2;
305 delete p3;
306 delete p4;
307 delete p5;
308 delete p6;
309 delete p7;
310 delete p8;
311 delete p9;
312 delete p10;
313
314
315 Loki::Printf("\nCreating Rimpls\n");
316 R1* r1 = new R1;
317 R2* r2 = new R2;
318 R3* r3 = new R3;
319 R4* r4 = new R4;
320 R5* r5 = new R5;
321
322 RO1* r6 = new RO1;
323 RO2* r7 = new RO2;
324 RO3* r8 = new RO3;
325 RO4* r9 = new RO4;
326 RO5* r10 = new RO5;
327
328 r1->f();
329 r2->f();
330 r3->f();
331 r4->f();
332 r5->f();
333 r6->f();
334 r7->f();
335 r8->f();
336 r9->f();
337 r10->f();
338
339 Loki::Printf("\nDeleting Rimpls\n");
340 delete r1;
341 delete r2;
342 delete r3;
343 delete r4;
344 delete r5;
345 delete r6;
346 delete r7;
347 delete r8;
348 delete r9;
349 delete r10;
350
351
352 Loki::Printf("\nCreating const Pimpls\n");
353 const P1* cp1 = new P1;
354 const P2* cp2 = new P2;
355 const P3* cp3 = new P3;
356 const P4* cp4 = new P4;
357 const P5* cp5 = new P5;
358
359 const PO1* cp6 = new PO1;
360 const PO2* cp7 = new PO2;
361 const PO3* cp8 = new PO3;
362 const PO4* cp9 = new PO4;
363 const PO5* cp10 = new PO5;
364
365 Loki::Printf("\nConst check\n");
366 cp1->f();
367 cp2->f();
368 cp3->f();
369 cp4->f();
370 cp5->f();
371 cp6->f();
372 cp7->f();
373 cp8->f();
374 cp9->f();
375 cp10->f();
376
377 Loki::Printf("\nDeleting const Pimpls\n");
378 delete cp1;
379 delete cp2;
380 delete cp3;
381 delete cp4;
382 delete cp5;
383 delete cp6;
384 delete cp7;
385 delete cp8;
386 delete cp9;
387 delete cp10;
388 }
389