diff --git a/test/Makefile b/test/Makefile index cef4aac..18cfd9e 100755 --- a/test/Makefile +++ b/test/Makefile @@ -1,5 +1,5 @@ SUBTARGETS = Factory flex_string Longevity OrderedStatic RegressionTest \ - SafeFormat ScopeGuard Singleton SmallObj Visitor + SafeFormat ScopeGuard Singleton SmallObj Visitor Pimpl .PHONY: clean all $(SUBTARGETS) all: $(SUBTARGETS) diff --git a/test/Pimpl/Makefile b/test/Pimpl/Makefile new file mode 100755 index 0000000..96dc2d5 --- /dev/null +++ b/test/Pimpl/Makefile @@ -0,0 +1,11 @@ +BIN = main +CC = gcc +CXXFLAGS = -Wall -O2 +CPPFLAGS = -I../../include -DNDEBUG +LDFLAGS = -L../../lib +LDLIBS = -lloki + +.PHONY: build clean +build: $(BIN) +clean: + rm -f $(BIN) $(BIN).exe $(BIN).o diff --git a/test/Pimpl/Pimpl.vcproj b/test/Pimpl/Pimpl.vcproj new file mode 100755 index 0000000..7c2fac5 --- /dev/null +++ b/test/Pimpl/Pimpl.vcproj @@ -0,0 +1,351 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/Pimpl/main.cpp b/test/Pimpl/main.cpp new file mode 100755 index 0000000..49d3ad4 --- /dev/null +++ b/test/Pimpl/main.cpp @@ -0,0 +1,214 @@ +//////////////////////////////////////////////////////////////////////////////// +// The Loki Library +// Copyright (c) 2006 Peter Kümmel +// 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. +//////////////////////////////////////////////////////////////////////////////// + +// $Header: + +#ifdef _MSC_VER +#pragma warning (disable: 4512) +#endif + + +#define LOKI_INHERITED_PIMPL_NAME d +#define LOKI_INHERITED_RIMPL_NAME d + +#include "type.h" + +#include + +#include + + + +///////////////////////////////////////// +// Definition of Impl +///////////////////////////////////////// +namespace Loki // gcc!! +{ + template<> + struct Impl : public SmallObject<> // inherit SmallObj for speed up + { + Impl() : data(0) {Printf("A created\n");} + ~Impl(){Printf("A destroyed, data=%d\n")(data);} + int data; + }; +} +///////////////////////////////////////// +// class A definition +///////////////////////////////////////// +A::A() +{} + +void A::foo() +{ + (*d).data = 1; + d->data = 111; +} + + +///////////////////////////////////////// +// Definition of Impl +///////////////////////////////////////// +namespace Loki // gcc!! +{ + template<> + struct Impl : public SmallObject<> // inherit SmallObj for speed up + { + Impl() : data(0) {Printf("B created\n");} + ~Impl(){Printf("B destroyed, data=%d\n")(data);} + int data; + }; +} +///////////////////////////////////////// +// class B definition +///////////////////////////////////////// +B::B() : Loki::Pimpl::Owner() +{} + +void B::foo() +{ + (*d).data = 2; + d->data = 222; +} + + +///////////////////////////////////////// +// Definition of Impl +///////////////////////////////////////// +namespace Loki // gcc!! +{ + template<> + struct Impl : public SmallObject<> // inherit SmallObj for speed up + { + Impl(): data(0) {Printf("C created\n");} + ~Impl(){Printf("C destroyed, data=%d\n")(data);} + int data; + }; +} +///////////////////////////////////////// +// class C definition +///////////////////////////////////////// +C::C() : d(rlife) +{} + +void C::foo() +{ + d.data = 333; +} + + + +///////////////////////////////////////// +// Definition of Impl +///////////////////////////////////////// +namespace Loki // gcc!! +{ + template<> + struct Impl : public SmallObject<> // inherit SmallObj for speed up + { + Impl(): data(0) {Printf("D created\n");} + ~Impl(){Printf("D destroyed, data=%d\n")(data);} + int data; + }; +} +///////////////////////////////////////// +// class D definition +///////////////////////////////////////// +D::D() : Loki::Rimpl::Owner() +{} + +void D::foo() +{ + d.data = 444; +} + + +///////////////////////////////////////// +// main +///////////////////////////////////////// +void test_more(); + +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 +//////////////////// + + + +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;} + +R1::R1(){d.data = 11;} +R2::R2(){d.data = 22;} +R3::R3(){d.data = 33;} +R4::R4():d(t){d.data = 44;} +R5::R5():d(t){d.data = 55;} +R6::R6():d(t){d.data = 66;} + +void test_more() +{ + Loki::Printf("\nmore tests:\n"); + + P1* p1 = new P1; + P2* p2 = new P2; + P3* p3 = new P3; + P4* p4 = new P4; + P5* p5 = new P5; + + R1* r1 = new R1; + R2* r2 = new R2; + R3* r3 = new R3; + R4* r4 = new R4; + R5* r5 = new R5; + R6* r6 = new R6; + + + delete p1; + delete p2; + delete p3; + delete p4; + delete p5; + + delete r1; + delete r2; + delete r3; + delete r4; + delete r5; + delete r6; +} diff --git a/test/Pimpl/make.msvc.bat b/test/Pimpl/make.msvc.bat new file mode 100755 index 0000000..080e471 --- /dev/null +++ b/test/Pimpl/make.msvc.bat @@ -0,0 +1,6 @@ + +cl -c -Zm200 -O2 -DNDEBUG -MT -EHsc -GR -W0 -wd4710 -I"." -I"..\..\include" main.cpp + +link /NOLOGO /SUBSYSTEM:CONSOLE /incremental:no /OUT:"main-msvc.exe" ..\..\lib\loki.lib main.obj + +del *.obj diff --git a/test/Pimpl/type.h b/test/Pimpl/type.h new file mode 100755 index 0000000..090334e --- /dev/null +++ b/test/Pimpl/type.h @@ -0,0 +1,267 @@ +//////////////////////////////////////////////////////////////////////////////// +// The Loki Library +// Copyright (c) 2006 Peter Kümmel +// 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. +//////////////////////////////////////////////////////////////////////////////// + +// $Header: + + +#include + +//#define TEST_WITH_BOOST +#ifdef TEST_WITH_BOOST +#include +#endif +#include +#include + +using Loki::Pimpl; +using Loki::Rimpl; + + +///////////////////////////////////////// +// class A declaration +///////////////////////////////////////// + +class A +{ +public: + A(); + void foo(); + +private: + Pimpl::Type d; +}; + + +///////////////////////////////////////// +// class B declaration +///////////////////////////////////////// + +class B : private Pimpl::Owner +{ +public: + B(); + void foo(); +}; + + +///////////////////////////////////////// +// class C declaration +///////////////////////////////////////// + +class C +{ +public: + C(); + void foo(); + +private: + Rimpl::Life rlife; + Rimpl::Type& d; +}; + + +///////////////////////////////////////// +// class D declaration +///////////////////////////////////////// + +class D : private Rimpl::Owner +{ +public: + D(); + void foo(); +}; + + + + +//////////////////// +// more test code +//////////////////// + + +// Pimpl + +typedef Loki::PtrImpl +< + Loki::Impl +> +Pimpl1; + + + +typedef Loki::PtrImpl +< + Loki::Impl, + Loki::SmartPtr >, + Loki::DontDeletePimpl +> +Pimpl2; + + + +typedef Loki::PtrImpl +< + Loki::Impl, +#ifdef TEST_WITH_BOOST + boost::shared_ptr >, +#else + Loki::SmartPtr >, +#endif + Loki::DontDeletePimpl +> +Pimpl3; + + + +typedef Loki::PtrImpl +< + Loki::Impl, + Loki::SmartPtr >, + Loki::DontDeletePimpl, + Loki::DeclaredPimpl +> +Pimpl4; + + + +typedef Loki::PtrImpl +< + Loki::Impl, +#ifdef TEST_WITH_BOOST + boost::shared_ptr >, +#else + Loki::SmartPtr >, +#endif + Loki::DontDeletePimpl, + Loki::DeclaredPimpl +> +Pimpl5; + + +// Pimpl + +typedef Loki::PtrImpl +< + Loki::Impl, + Loki::Impl*, + Loki::AutoDeletePimpl, + Loki::InheritedRimpl + +> +Rimpl1; + + + +typedef Loki::PtrImpl +< + Loki::Impl, + Loki::SmartPtr >, + Loki::DontDeletePimpl, + Loki::InheritedRimpl +> +Rimpl2; + + + +typedef Loki::PtrImpl +< + Loki::Impl, +#ifdef TEST_WITH_BOOST + boost::shared_ptr >, +#else + Loki::SmartPtr >, +#endif + Loki::DontDeletePimpl, + Loki::InheritedRimpl +> +Rimpl3; + + +typedef Loki::PtrImpl +< + Loki::Impl, + Loki::Impl*, + Loki::AutoDeletePimpl, + Loki::DeclaredRimpl +> +Rimpl4; + +typedef Loki::PtrImpl +< + Loki::Impl, + Loki::SmartPtr >, + Loki::DontDeletePimpl, + Loki::DeclaredRimpl +> +Rimpl5; + + + +typedef Loki::PtrImpl +< + Loki::Impl, +#ifdef TEST_WITH_BOOST + boost::shared_ptr >, +#else + Loki::SmartPtr >, +#endif + Loki::DontDeletePimpl, + Loki::DeclaredRimpl +> +Rimpl6; + + + +typedef Loki::PtrImpl +< + Loki::Impl, + + //Loki::Impl*, + Loki::SmartPtr >, + //boost::shared_ptr >, + + //Loki::AutoDeletePimpl, // compiler error when used with smart pointers, this is o.k. + Loki::DontDeletePimpl, + + Loki::DeclaredPimpl + //Loki::InheritedPimpl, +> +Pimpl8; + + +template +struct R +{ + typedef Loki::PimplLife + < + T, + T*, + Loki::AutoDeletePimpl + > + Life; +}; + + +struct P1 : private Pimpl1 {P1();}; +struct P2 : private Pimpl2 {P2();}; +struct P3 : private Pimpl3 {P3();}; +struct P4 {Pimpl4 d; P4();}; +struct P5 {Pimpl5 d; P5();}; + +struct R1 : private Rimpl1 {R1();}; +struct R2 : private Rimpl2 {R2();}; +struct R3 : private Rimpl3 {R3();}; +struct R4 {Rimpl4& d; R::Life t; R4();}; +struct R5 {Rimpl5& d; R::Life t; R5();}; +struct R6 {Rimpl6& d; R::Life t; R6();}; + + diff --git a/test/make.msvc.bat b/test/make.msvc.bat index 2caa234..11df8ba 100755 --- a/test/make.msvc.bat +++ b/test/make.msvc.bat @@ -15,6 +15,10 @@ cd OrderedStatic call make.msvc.bat cd .. +cd Pimpl +call make.msvc.bat +cd .. + cd RegressionTest call make.msvc.bat cd ..