2006-01-15 22:09:32 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// The Loki Library
|
2006-01-15 23:17:57 +00:00
|
|
|
|
// Copyright (c) 2006 Peter K<>mmel
|
2006-01-15 22:09:32 +00:00
|
|
|
|
// Permission to use, copy, modify, distribute and sell this software for any
|
|
|
|
|
// purpose is hereby granted without fee, provided that the above copyright
|
|
|
|
|
// notice appear in all copies and that both that copyright notice and this
|
|
|
|
|
// permission notice appear in supporting documentation.
|
|
|
|
|
// The author makes no representations about the
|
|
|
|
|
// suitability of this software for any purpose. It is provided "as is"
|
|
|
|
|
// without express or implied warranty.
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2006-03-02 09:57:39 +00:00
|
|
|
|
// $Header$
|
2006-01-15 22:09:32 +00:00
|
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
|
#pragma warning (disable: 4512)
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define LOKI_INHERITED_PIMPL_NAME d
|
|
|
|
|
#define LOKI_INHERITED_RIMPL_NAME d
|
|
|
|
|
|
|
|
|
|
#include "type.h"
|
2006-01-16 00:05:02 +00:00
|
|
|
|
#include "type2.h"
|
2006-01-15 22:09:32 +00:00
|
|
|
|
|
|
|
|
|
#include <loki/SafeFormat.h>
|
2006-01-28 20:13:57 +00:00
|
|
|
|
|
2006-01-15 22:09:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////
|
2006-01-28 20:13:57 +00:00
|
|
|
|
// Definition of ImplT<A>
|
2006-01-15 22:09:32 +00:00
|
|
|
|
/////////////////////////////////////////
|
|
|
|
|
namespace Loki // gcc!!
|
|
|
|
|
{
|
|
|
|
|
template<>
|
2006-01-28 20:13:57 +00:00
|
|
|
|
struct ImplT<A> : public SmallObject<> // inherit SmallObj for speed up
|
2006-01-15 22:09:32 +00:00
|
|
|
|
{
|
2006-01-28 20:13:57 +00:00
|
|
|
|
ImplT() : data(0) {Printf("A created\n");}
|
|
|
|
|
~ImplT(){Printf("A destroyed, data=%d\n")(data);}
|
2006-01-15 22:09:32 +00:00
|
|
|
|
int data;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
/////////////////////////////////////////
|
|
|
|
|
// class A definition
|
|
|
|
|
/////////////////////////////////////////
|
|
|
|
|
A::A()
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
void A::foo()
|
|
|
|
|
{
|
|
|
|
|
(*d).data = 1;
|
|
|
|
|
d->data = 111;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////
|
2006-01-28 20:13:57 +00:00
|
|
|
|
// Definition of ImplT<B>
|
2006-01-15 22:09:32 +00:00
|
|
|
|
/////////////////////////////////////////
|
|
|
|
|
namespace Loki // gcc!!
|
|
|
|
|
{
|
|
|
|
|
template<>
|
2006-01-28 20:13:57 +00:00
|
|
|
|
struct ImplT<B> : public SmallObject<> // inherit SmallObj for speed up
|
2006-01-15 22:09:32 +00:00
|
|
|
|
{
|
2006-01-28 20:13:57 +00:00
|
|
|
|
ImplT() : data(0) {Printf("B created\n");}
|
|
|
|
|
~ImplT(){Printf("B destroyed, data=%d\n")(data);}
|
2006-01-15 22:09:32 +00:00
|
|
|
|
int data;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
/////////////////////////////////////////
|
|
|
|
|
// class B definition
|
|
|
|
|
/////////////////////////////////////////
|
2006-01-28 20:13:57 +00:00
|
|
|
|
B::B() : Loki::PimplT<B>::Owner()
|
2006-01-15 22:09:32 +00:00
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
void B::foo()
|
|
|
|
|
{
|
|
|
|
|
(*d).data = 2;
|
|
|
|
|
d->data = 222;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////
|
2006-01-28 20:13:57 +00:00
|
|
|
|
// Definition of ImplT<C>
|
2006-01-15 22:09:32 +00:00
|
|
|
|
/////////////////////////////////////////
|
|
|
|
|
namespace Loki // gcc!!
|
|
|
|
|
{
|
|
|
|
|
template<>
|
2006-01-28 20:13:57 +00:00
|
|
|
|
struct ImplT<C> : public SmallObject<> // inherit SmallObj for speed up
|
2006-01-15 22:09:32 +00:00
|
|
|
|
{
|
2006-01-28 20:13:57 +00:00
|
|
|
|
ImplT(): data(0) {Printf("C created\n");}
|
|
|
|
|
~ImplT(){Printf("C destroyed, data=%d\n")(data);}
|
2006-01-15 22:09:32 +00:00
|
|
|
|
int data;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
/////////////////////////////////////////
|
|
|
|
|
// class C definition
|
|
|
|
|
/////////////////////////////////////////
|
2006-01-28 20:13:57 +00:00
|
|
|
|
C::C() : p(), d(*p)
|
2006-01-15 22:09:32 +00:00
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
void C::foo()
|
|
|
|
|
{
|
|
|
|
|
d.data = 333;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////
|
2006-01-28 20:13:57 +00:00
|
|
|
|
// Definition of ImplT<D>
|
2006-01-15 22:09:32 +00:00
|
|
|
|
/////////////////////////////////////////
|
|
|
|
|
namespace Loki // gcc!!
|
|
|
|
|
{
|
|
|
|
|
template<>
|
2006-01-28 20:13:57 +00:00
|
|
|
|
struct ImplT<D> : public SmallObject<> // inherit SmallObj for speed up
|
2006-01-15 22:09:32 +00:00
|
|
|
|
{
|
2006-01-28 20:13:57 +00:00
|
|
|
|
ImplT(): data(0) {Printf("D created\n");}
|
|
|
|
|
~ImplT(){Printf("D destroyed, data=%d\n")(data);}
|
2006-01-15 22:09:32 +00:00
|
|
|
|
int data;
|
|
|
|
|
};
|
|
|
|
|
}
|
2006-01-28 20:13:57 +00:00
|
|
|
|
|
2006-01-15 22:09:32 +00:00
|
|
|
|
/////////////////////////////////////////
|
|
|
|
|
// class D definition
|
|
|
|
|
/////////////////////////////////////////
|
2006-01-28 20:13:57 +00:00
|
|
|
|
D::D() : Loki::RimplT<D>::Owner()
|
2006-01-15 22:09:32 +00:00
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
void D::foo()
|
|
|
|
|
{
|
|
|
|
|
d.data = 444;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////
|
|
|
|
|
// main
|
|
|
|
|
/////////////////////////////////////////
|
2006-01-15 23:17:57 +00:00
|
|
|
|
|
2006-01-28 20:13:57 +00:00
|
|
|
|
void test_more();
|
2006-01-15 23:17:57 +00:00
|
|
|
|
|
2006-01-15 22:09:32 +00:00
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
A* a = new A;
|
|
|
|
|
B* b = new B;
|
|
|
|
|
C* c = new C;
|
|
|
|
|
D* d = new D;
|
|
|
|
|
a->foo();
|
|
|
|
|
b->foo();
|
|
|
|
|
c->foo();
|
|
|
|
|
d->foo();
|
|
|
|
|
delete a;
|
|
|
|
|
delete b;
|
|
|
|
|
delete c;
|
|
|
|
|
delete d;
|
|
|
|
|
|
|
|
|
|
test_more();
|
|
|
|
|
|
|
|
|
|
#if defined(__BORLANDC__) || defined(_MSC_VER)
|
|
|
|
|
system("PAUSE");
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////
|
|
|
|
|
// more test code
|
|
|
|
|
////////////////////
|
|
|
|
|
|
2006-03-02 09:57:39 +00:00
|
|
|
|
// incomplete type test
|
|
|
|
|
// this must give errors
|
|
|
|
|
|
|
|
|
|
//Incomplete1 ii; // compiler error
|
|
|
|
|
//Incomplete2 i2; // linker error
|
|
|
|
|
//Incomplete4 i4; // compiler error
|
|
|
|
|
|
2006-01-23 17:23:46 +00:00
|
|
|
|
/////////////////////////////////////////
|
2006-01-28 20:13:57 +00:00
|
|
|
|
// Definition of ImplT<E>
|
2006-01-23 17:23:46 +00:00
|
|
|
|
/////////////////////////////////////////
|
|
|
|
|
namespace Loki // gcc!!
|
|
|
|
|
{
|
|
|
|
|
template<>
|
2006-01-28 20:13:57 +00:00
|
|
|
|
struct ImplT<E> : public SmallObject<> // inherit SmallObj for speed up
|
2006-01-23 17:23:46 +00:00
|
|
|
|
{
|
2006-01-28 20:13:57 +00:00
|
|
|
|
ImplT() : data(0) {Printf("E created\n");}
|
|
|
|
|
~ImplT(){Printf("E destroyed, data=%d\n")(data);}
|
2006-01-23 17:23:46 +00:00
|
|
|
|
int data;
|
|
|
|
|
|
|
|
|
|
void foo() {Printf("E foo() \n");}
|
|
|
|
|
void foo() const {Printf("E foo() const \n");}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-15 22:09:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
P1::P1(){d->data = 1;}
|
|
|
|
|
P2::P2(){d->data = 2;}
|
|
|
|
|
P3::P3(){d->data = 3;}
|
|
|
|
|
P4::P4(){d->data = 4;}
|
|
|
|
|
P5::P5(){d->data = 5;}
|
2006-01-28 20:13:57 +00:00
|
|
|
|
|
|
|
|
|
PO1::PO1(){d->data = 6;}
|
|
|
|
|
PO2::PO2(){d->data = 7;}
|
|
|
|
|
PO3::PO3(){d->data = 8;}
|
|
|
|
|
PO4::PO4(){d->data = 9;}
|
|
|
|
|
PO5::PO5(){d->data = 10;}
|
2006-01-15 22:09:32 +00:00
|
|
|
|
|
2006-01-23 17:23:46 +00:00
|
|
|
|
void P1::f(){d->foo();}
|
|
|
|
|
void P2::f(){d->foo();}
|
|
|
|
|
void P3::f(){d->foo();}
|
|
|
|
|
void P4::f(){d->foo();}
|
|
|
|
|
void P5::f(){d->foo();}
|
2006-01-28 20:13:57 +00:00
|
|
|
|
|
|
|
|
|
void PO1::f(){d->foo();}
|
|
|
|
|
void PO2::f(){d->foo();}
|
|
|
|
|
void PO3::f(){d->foo();}
|
|
|
|
|
void PO4::f(){d->foo();}
|
|
|
|
|
void PO5::f(){d->foo();}
|
2006-01-23 17:23:46 +00:00
|
|
|
|
|
|
|
|
|
void P1::f()const{d->foo();}
|
|
|
|
|
void P2::f()const{d->foo();}
|
|
|
|
|
void P3::f()const{d->foo();}
|
|
|
|
|
void P4::f()const{d->foo();}
|
|
|
|
|
void P5::f()const{d->foo();}
|
2006-01-28 20:13:57 +00:00
|
|
|
|
|
|
|
|
|
void PO1::f()const{d->foo();}
|
|
|
|
|
void PO2::f()const{d->foo();}
|
|
|
|
|
void PO3::f()const{d->foo();}
|
|
|
|
|
void PO4::f()const{d->foo();}
|
|
|
|
|
void PO5::f()const{d->foo();}
|
|
|
|
|
|
2006-01-23 17:23:46 +00:00
|
|
|
|
|
|
|
|
|
|
2006-01-28 20:13:57 +00:00
|
|
|
|
|
|
|
|
|
R1::R1():d(*p){d.data = 11;}
|
|
|
|
|
R2::R2():d(*p){d.data = 22;}
|
|
|
|
|
R3::R3():d(*p){d.data = 33;}
|
|
|
|
|
R4::R4():d(*p){d.data = 44;}
|
|
|
|
|
R5::R5():d(*p){d.data = 55;}
|
2006-01-15 22:09:32 +00:00
|
|
|
|
|
2006-01-23 17:23:46 +00:00
|
|
|
|
void R1::f(){d.foo();}
|
|
|
|
|
void R2::f(){d.foo();}
|
|
|
|
|
void R3::f(){d.foo();}
|
|
|
|
|
void R4::f(){d.foo();}
|
|
|
|
|
void R5::f(){d.foo();}
|
|
|
|
|
|
|
|
|
|
void R1::f()const{d.foo();}
|
|
|
|
|
void R2::f()const{d.foo();}
|
|
|
|
|
void R3::f()const{d.foo();}
|
|
|
|
|
void R4::f()const{d.foo();}
|
|
|
|
|
void R5::f()const{d.foo();}
|
2006-01-28 20:13:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RO1::RO1(){d.data = 66;}
|
|
|
|
|
RO2::RO2(){d.data = 77;}
|
|
|
|
|
RO3::RO3(){d.data = 88;}
|
|
|
|
|
RO4::RO4(){d.data = 99;}
|
|
|
|
|
RO5::RO5(){d.data = 1010;}
|
|
|
|
|
|
|
|
|
|
void RO1::f(){d.foo();}
|
|
|
|
|
void RO2::f(){d.foo();}
|
|
|
|
|
void RO3::f(){d.foo();}
|
|
|
|
|
void RO4::f(){d.foo();}
|
|
|
|
|
void RO5::f(){d.foo();}
|
|
|
|
|
|
|
|
|
|
void RO1::f()const{d.foo();}
|
|
|
|
|
void RO2::f()const{d.foo();}
|
|
|
|
|
void RO3::f()const{d.foo();}
|
|
|
|
|
void RO4::f()const{d.foo();}
|
|
|
|
|
void RO5::f()const{d.foo();}
|
2006-01-23 17:23:46 +00:00
|
|
|
|
|
|
|
|
|
|
2006-01-15 22:09:32 +00:00
|
|
|
|
void test_more()
|
|
|
|
|
{
|
2006-01-16 13:29:09 +00:00
|
|
|
|
Loki::Printf("\n\nMore tests:\n");
|
2006-01-15 22:09:32 +00:00
|
|
|
|
|
2006-01-16 13:30:26 +00:00
|
|
|
|
Loki::Printf("\nCreating Pimpls\n");
|
2006-06-19 12:46:22 +00:00
|
|
|
|
P1* p1 = new P1;
|
2006-01-15 22:09:32 +00:00
|
|
|
|
P2* p2 = new P2;
|
|
|
|
|
P3* p3 = new P3;
|
|
|
|
|
P4* p4 = new P4;
|
|
|
|
|
P5* p5 = new P5;
|
2006-06-19 12:46:22 +00:00
|
|
|
|
PO1* p6 = new PO1;
|
|
|
|
|
PO2* p7 = new PO2;
|
2006-01-28 20:13:57 +00:00
|
|
|
|
PO3* p8 = new PO3;
|
2006-06-19 12:46:22 +00:00
|
|
|
|
PO4* p9 = new PO4;
|
|
|
|
|
PO5* p10 = new PO5;
|
2006-01-15 22:09:32 +00:00
|
|
|
|
|
2006-01-23 17:23:46 +00:00
|
|
|
|
Loki::Printf("\nConst check\n");
|
|
|
|
|
p1->f();
|
|
|
|
|
p2->f();
|
|
|
|
|
p3->f();
|
|
|
|
|
p4->f();
|
|
|
|
|
p5->f();
|
2006-06-19 12:46:22 +00:00
|
|
|
|
p6->f();
|
|
|
|
|
p7->f();
|
2006-01-28 20:13:57 +00:00
|
|
|
|
p8->f();
|
2006-06-19 12:46:22 +00:00
|
|
|
|
p9->f();
|
|
|
|
|
p10->f();
|
2006-01-15 22:09:32 +00:00
|
|
|
|
|
2006-01-16 13:30:26 +00:00
|
|
|
|
Loki::Printf("\nDeleting Pimpls\n");
|
|
|
|
|
delete p1;
|
2006-01-15 22:09:32 +00:00
|
|
|
|
delete p2;
|
|
|
|
|
delete p3;
|
|
|
|
|
delete p4;
|
|
|
|
|
delete p5;
|
2006-01-16 13:30:26 +00:00
|
|
|
|
delete p6;
|
2006-06-19 12:46:22 +00:00
|
|
|
|
delete p7;
|
|
|
|
|
delete p8;
|
|
|
|
|
delete p9;
|
|
|
|
|
delete p10;
|
2006-01-15 22:09:32 +00:00
|
|
|
|
|
2006-03-08 17:07:20 +00:00
|
|
|
|
|
2006-01-23 17:23:46 +00:00
|
|
|
|
Loki::Printf("\nCreating Rimpls\n");
|
|
|
|
|
R1* r1 = new R1;
|
|
|
|
|
R2* r2 = new R2;
|
|
|
|
|
R3* r3 = new R3;
|
|
|
|
|
R4* r4 = new R4;
|
|
|
|
|
R5* r5 = new R5;
|
|
|
|
|
|
2006-06-19 12:46:22 +00:00
|
|
|
|
RO1* r6 = new RO1;
|
2006-01-28 20:13:57 +00:00
|
|
|
|
RO2* r7 = new RO2;
|
|
|
|
|
RO3* r8 = new RO3;
|
|
|
|
|
RO4* r9 = new RO4;
|
|
|
|
|
RO5* r10 = new RO5;
|
|
|
|
|
|
2006-06-19 12:46:22 +00:00
|
|
|
|
r1->f();
|
2006-01-23 17:23:46 +00:00
|
|
|
|
r2->f();
|
|
|
|
|
r3->f();
|
|
|
|
|
r4->f();
|
|
|
|
|
r5->f();
|
|
|
|
|
r6->f();
|
2006-06-19 12:46:22 +00:00
|
|
|
|
r7->f();
|
|
|
|
|
r8->f();
|
|
|
|
|
r9->f();
|
|
|
|
|
r10->f();
|
2006-01-23 17:23:46 +00:00
|
|
|
|
|
2006-01-16 13:29:09 +00:00
|
|
|
|
Loki::Printf("\nDeleting Rimpls\n");
|
2006-01-16 13:30:26 +00:00
|
|
|
|
delete r1;
|
2006-01-15 22:09:32 +00:00
|
|
|
|
delete r2;
|
|
|
|
|
delete r3;
|
|
|
|
|
delete r4;
|
|
|
|
|
delete r5;
|
|
|
|
|
delete r6;
|
2006-06-19 12:46:22 +00:00
|
|
|
|
delete r7;
|
|
|
|
|
delete r8;
|
|
|
|
|
delete r9;
|
|
|
|
|
delete r10;
|
2006-01-23 17:23:46 +00:00
|
|
|
|
|
2006-01-28 20:13:57 +00:00
|
|
|
|
|
|
|
|
|
Loki::Printf("\nCreating const Pimpls\n");
|
2006-01-23 17:23:46 +00:00
|
|
|
|
const P1* cp1 = new P1;
|
|
|
|
|
const P2* cp2 = new P2;
|
|
|
|
|
const P3* cp3 = new P3;
|
|
|
|
|
const P4* cp4 = new P4;
|
|
|
|
|
const P5* cp5 = new P5;
|
2006-01-28 20:13:57 +00:00
|
|
|
|
|
|
|
|
|
const PO1* cp6 = new PO1;
|
2006-06-19 12:46:22 +00:00
|
|
|
|
const PO2* cp7 = new PO2;
|
2006-01-28 20:13:57 +00:00
|
|
|
|
const PO3* cp8 = new PO3;
|
2006-06-19 12:46:22 +00:00
|
|
|
|
const PO4* cp9 = new PO4;
|
|
|
|
|
const PO5* cp10 = new PO5;
|
2006-01-23 17:23:46 +00:00
|
|
|
|
|
|
|
|
|
Loki::Printf("\nConst check\n");
|
|
|
|
|
cp1->f();
|
|
|
|
|
cp2->f();
|
|
|
|
|
cp3->f();
|
|
|
|
|
cp4->f();
|
|
|
|
|
cp5->f();
|
|
|
|
|
cp6->f();
|
2006-06-19 12:46:22 +00:00
|
|
|
|
cp7->f();
|
|
|
|
|
cp8->f();
|
|
|
|
|
cp9->f();
|
|
|
|
|
cp10->f();
|
2006-01-23 17:23:46 +00:00
|
|
|
|
|
2006-01-28 20:13:57 +00:00
|
|
|
|
Loki::Printf("\nDeleting const Pimpls\n");
|
2006-01-23 17:23:46 +00:00
|
|
|
|
delete cp1;
|
|
|
|
|
delete cp2;
|
|
|
|
|
delete cp3;
|
|
|
|
|
delete cp4;
|
|
|
|
|
delete cp5;
|
2006-06-19 12:46:22 +00:00
|
|
|
|
delete cp6;
|
|
|
|
|
delete cp7;
|
|
|
|
|
delete cp8;
|
2006-01-28 20:13:57 +00:00
|
|
|
|
delete cp9;
|
2006-06-19 12:46:22 +00:00
|
|
|
|
delete cp10;
|
2006-01-15 22:09:32 +00:00
|
|
|
|
}
|