add support of deep constness, only supported by (future) Loki::SmartPtr, not supported by boost::shared_ptr and plain pointer. Maybe deep constness forces a redesign of Pimpl. Is there a way to support deep constness by a rimpl?

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@517 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
syntheticpp 2006-01-23 17:23:46 +00:00
parent f6a31b5916
commit 853d58a218
3 changed files with 193 additions and 55 deletions

View file

@ -13,6 +13,7 @@
#define LOKI_PIMPL_H
#include <loki/TypeTraits.h>
#include <loki/SmartPtr.h>
#include <exception>
#include <memory>
@ -184,7 +185,7 @@ namespace Loki
typedef Private::HavePtrHolder<Private::AutoPtrHolder<Impl,Ptr,Del> > PtrHolder;
public:
const Ptr LOKI_INHERITED_PIMPL_NAME;
Ptr LOKI_INHERITED_PIMPL_NAME;
protected:
InheritedPimpl() : LOKI_INHERITED_PIMPL_NAME(PtrHolder::ptr.Create())
@ -200,13 +201,32 @@ namespace Loki
{
typedef Private::HavePtrHolder<Private::AutoPtrHolder<Impl,Ptr,Del> > PtrHolder;
#if defined(LOKI_DEFAULT_CONSTNESS)
typedef typename LOKI_DEFAULT_CONSTNESS<Ptr>::Type ConstPtr;
typedef typename LOKI_DEFAULT_CONSTNESS<Impl>::Type ConstImpl;
#else // default: enable
typedef typename const Ptr ConstPtr;
typedef typename const Impl ConstImpl;
#endif
public:
Ptr operator->() const
Ptr operator->()
{
return ptr;
}
Impl& operator*() const
Impl& operator*()
{
return *ptr;
}
ConstPtr operator->() const
{
return ptr;
}
ConstImpl& operator*() const
{
return *ptr;
}
@ -358,6 +378,9 @@ namespace Loki
#endif
// $Log$
// Revision 1.13 2006/01/23 17:22:49 syntheticpp
// add support of deep constness, only supported by (future) Loki::SmartPtr, not supported by boost::shared_ptr and plain pointer. Maybe deep constness forces a redesign of Pimpl. Is there a way to support deep constness by a rimpl?
//
// Revision 1.12 2006/01/19 18:16:39 syntheticpp
// disable usage with auto_ptr: don't compile with std::auto_ptr
//

View file

@ -199,6 +199,23 @@ int main()
// more test code
////////////////////
/////////////////////////////////////////
// Definition of Impl<E>
/////////////////////////////////////////
namespace Loki // gcc!!
{
template<>
struct Impl<E> : public SmallObject<> // inherit SmallObj for speed up
{
Impl() : data(0) {Printf("E created\n");}
~Impl(){Printf("E destroyed, data=%d\n")(data);}
int data;
void foo() {Printf("E foo() \n");}
void foo() const {Printf("E foo() const \n");}
};
}
P1::P1(){d->data = 1;}
@ -208,6 +225,21 @@ P4::P4(){d->data = 4;}
P5::P5(){d->data = 5;}
P6::P6(){d->data = 6;}
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();}
void P6::f(){d->foo();}
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();}
void P6::f()const{d->foo();}
R1::R1(){d.data = 11;}
R2::R2(){d.data = 22;}
R3::R3(){d.data = 33;}
@ -215,6 +247,21 @@ R4::R4():d(rinit){d.data = 44;}
R5::R5():d(rinit){d.data = 55;}
R6::R6():d(rinit){d.data = 66;}
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 R6::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();}
void R6::f()const{d.foo();}
void test_more()
{
Loki::Printf("\n\nMore tests:\n");
@ -227,14 +274,13 @@ void test_more()
P5* p5 = new P5;
P6* p6 = new P6;
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;
R6* r6 = new R6;
Loki::Printf("\nConst check\n");
p1->f();
p2->f();
p3->f();
p4->f();
p5->f();
p6->f();
Loki::Printf("\nDeleting Pimpls\n");
delete p1;
@ -244,6 +290,22 @@ void test_more()
delete p5;
delete p6;
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;
R6* r6 = new R6;
Loki::Printf("\nConst check\n");
r1->f();
r2->f();
r3->f();
r4->f();
r5->f();
r6->f();
Loki::Printf("\nDeleting Rimpls\n");
delete r1;
delete r2;
@ -251,4 +313,54 @@ void test_more()
delete r4;
delete r5;
delete r6;
Loki::Printf("\nCreating Pimpls\n");
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;
const P6* cp6 = new P6;
Loki::Printf("\nConst check\n");
cp1->f();
cp2->f();
cp3->f();
cp4->f();
cp5->f();
cp6->f();
Loki::Printf("\nDeleting Rimpls\n");
delete cp1;
delete cp2;
delete cp3;
delete cp4;
delete cp5;
delete cp6;
Loki::Printf("\nCreating Rimpls\n");
const R1* cr1 = new R1;
const R2* cr2 = new R2;
const R3* cr3 = new R3;
const R4* cr4 = new R4;
const R5* cr5 = new R5;
const R6* cr6 = new R6;
Loki::Printf("\nConst check\n");
cr1->f();
cr2->f();
cr3->f();
cr4->f();
cr5->f();
cr6->f();
Loki::Printf("\nDeleting Rimpls\n");
delete cr1;
delete cr2;
delete cr3;
delete cr4;
delete cr5;
delete cr6;
}

View file

@ -12,6 +12,8 @@
// $Header:
//#define LOKI_DEFAULT_CONSTNESS DeepConstness //default
//#define LOKI_DEFAULT_CONSTNESS FlatConstness
#include <loki/Pimpl.h>
@ -88,12 +90,13 @@ public:
// more test code
////////////////////
struct E;
// Pimpl
typedef Loki::PtrImpl
<
Loki::Impl<B>
Loki::Impl<E>
>
Pimpl1;
@ -101,8 +104,8 @@ Pimpl1;
typedef Loki::PtrImpl
<
Loki::Impl<B>,
Loki::SmartPtr<Loki::Impl<B> >,
Loki::Impl<E>,
Loki::SmartPtr<Loki::Impl<E> >,
Loki::DontDeletePimpl
>
Pimpl2;
@ -111,11 +114,11 @@ Pimpl2;
typedef Loki::PtrImpl
<
Loki::Impl<B>,
Loki::Impl<E>,
#ifdef TEST_WITH_BOOST
boost::shared_ptr<Loki::Impl<B> >,
boost::shared_ptr<Loki::Impl<E> >,
#else
Loki::SmartPtr<Loki::Impl<B> >,
Loki::SmartPtr<Loki::Impl<E> >,
#endif
Loki::DontDeletePimpl
>
@ -125,8 +128,8 @@ Pimpl3;
typedef Loki::PtrImpl
<
Loki::Impl<B>,
Loki::SmartPtr<Loki::Impl<B> >,
Loki::Impl<E>,
Loki::SmartPtr<Loki::Impl<E> >,
Loki::DontDeletePimpl,
Loki::DeclaredPimpl
>
@ -136,11 +139,11 @@ Pimpl4;
typedef Loki::PtrImpl
<
Loki::Impl<B>,
Loki::Impl<E>,
#ifdef TEST_WITH_BOOST
boost::shared_ptr<Loki::Impl<B> >,
boost::shared_ptr<Loki::Impl<E> >,
#else
Loki::SmartPtr<Loki::Impl<B> >,
Loki::SmartPtr<Loki::Impl<E> >,
#endif
Loki::DontDeletePimpl,
Loki::DeclaredPimpl
@ -149,20 +152,20 @@ Pimpl5;
typedef Loki::PtrImpl
<
Loki::Impl<B>,
Loki::Impl<B>*,
Loki::Impl<E>,
Loki::Impl<E>*,
Loki::AutoDeletePimpl,
Loki::DeclaredPimpl
>
Pimpl6;
// Pimpl
// Rimpl
typedef Loki::PtrImpl
<
Loki::Impl<B>,
Loki::Impl<B>*,
Loki::Impl<E>,
Loki::Impl<E>*,
Loki::AutoDeletePimpl,
Loki::InheritedRimpl
@ -173,8 +176,8 @@ Rimpl1;
typedef Loki::PtrImpl
<
Loki::Impl<B>,
Loki::SmartPtr<Loki::Impl<B> >,
Loki::Impl<E>,
Loki::SmartPtr<Loki::Impl<E> >,
Loki::DontDeletePimpl,
Loki::InheritedRimpl
>
@ -184,11 +187,11 @@ Rimpl2;
typedef Loki::PtrImpl
<
Loki::Impl<B>,
Loki::Impl<E>,
#ifdef TEST_WITH_BOOST
boost::shared_ptr<Loki::Impl<B> >,
boost::shared_ptr<Loki::Impl<E> >,
#else
Loki::SmartPtr<Loki::Impl<B> >,
Loki::SmartPtr<Loki::Impl<E> >,
#endif
Loki::DontDeletePimpl,
Loki::InheritedRimpl
@ -198,8 +201,8 @@ Rimpl3;
typedef Loki::PtrImpl
<
Loki::Impl<B>,
Loki::Impl<B>*,
Loki::Impl<E>,
Loki::Impl<E>*,
Loki::AutoDeletePimpl,
Loki::DeclaredRimpl
>
@ -207,8 +210,8 @@ Rimpl4;
typedef Loki::PtrImpl
<
Loki::Impl<B>,
Loki::SmartPtr<Loki::Impl<B> >,
Loki::Impl<E>,
Loki::SmartPtr<Loki::Impl<E> >,
Loki::DontDeletePimpl,
Loki::DeclaredRimpl
>
@ -218,11 +221,11 @@ Rimpl5;
typedef Loki::PtrImpl
<
Loki::Impl<B>,
Loki::Impl<E>,
#ifdef TEST_WITH_BOOST
boost::shared_ptr<Loki::Impl<B> >,
boost::shared_ptr<Loki::Impl<E> >,
#else
Loki::SmartPtr<Loki::Impl<B> >,
Loki::SmartPtr<Loki::Impl<E> >,
#endif
Loki::DontDeletePimpl,
Loki::DeclaredRimpl
@ -233,11 +236,11 @@ Rimpl6;
typedef Loki::PtrImpl
<
Loki::Impl<B>,
Loki::Impl<E>,
//Loki::Impl<B>*,
Loki::SmartPtr<Loki::Impl<B> >,
//boost::shared_ptr<Loki::Impl<B> >,
//Loki::Impl<E>*,
Loki::SmartPtr<Loki::Impl<E> >,
//boost::shared_ptr<Loki::Impl<E> >,
//Loki::AutoDeletePimpl, // compiler error when used with smart pointers, this is o.k.
Loki::DontDeletePimpl,
@ -261,18 +264,18 @@ struct R
};
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 P6 {Pimpl6 d; P6();};
struct P1 : private Pimpl1 {P1();void f();void f()const;};
struct P2 : private Pimpl2 {P2();void f();void f()const;};
struct P3 : private Pimpl3 {P3();void f();void f()const;};
struct P4 {Pimpl4 d; P4();void f();void f()const;};
struct P5 {Pimpl5 d; P5();void f();void f()const;};
struct P6 {Pimpl6 d; P6();void f();void f()const;};
struct R1 : private Rimpl1 {R1();};
struct R2 : private Rimpl2 {R2();};
struct R3 : private Rimpl3 {R3();};
struct R4 {R<Rimpl4>::Init rinit; Rimpl4& d; R4();};
struct R5 {R<Rimpl5>::Init rinit; Rimpl5& d; R5();};
struct R6 {R<Rimpl6>::Init rinit; Rimpl6& d; R6();};
struct R1 : private Rimpl1 {R1();void f();void f()const;};
struct R2 : private Rimpl2 {R2();void f();void f()const;};
struct R3 : private Rimpl3 {R3();void f();void f()const;};
struct R4 {R<Rimpl4>::Init rinit; Rimpl4& d; R4();void f();void f()const;};
struct R5 {R<Rimpl5>::Init rinit; Rimpl5& d; R5();void f();void f()const;};
struct R6 {R<Rimpl6>::Init rinit; Rimpl6& d; R6();void f();void f()const;};