remove virtual destructor: inherit private only, clean up

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@478 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
syntheticpp 2006-01-16 13:27:49 +00:00
parent 71448918a7
commit d33f2e6585

View file

@ -16,15 +16,18 @@
#ifndef LOKI_INHERITED_PIMPL_NAME #ifndef LOKI_INHERITED_PIMPL_NAME
#define LOKI_INHERITED_PIMPL_NAME pimpl #define LOKI_INHERITED_PIMPL_NAME d
#endif #endif
#ifndef LOKI_INHERITED_RIMPL_NAME #ifndef LOKI_INHERITED_RIMPL_NAME
#define LOKI_INHERITED_RIMPL_NAME rimpl #define LOKI_INHERITED_RIMPL_NAME d
#endif #endif
namespace Loki namespace Loki
{ {
/////////////////////
// template for the implementations
/////////////////////
template<class T> template<class T>
struct Impl; struct Impl;
@ -32,7 +35,7 @@ namespace Loki
///////////////////// /////////////////////
// creation policies // creation policies
///////////////////// /////////////////////
// hard coded creation prefered // hard coded creation preferred
///////////////////// /////////////////////
@ -41,7 +44,13 @@ namespace Loki
template<class T> template<class T>
struct AutoDeletePimpl struct AutoDeletePimpl
{ {
static void Destroy(T ptr); static void Destroy(T ptr)
{
typedef char T_must_be_defined[
sizeof(typename TypeTraits<T>::PointeeType) ? 1 : -1 ];
delete ptr;
ptr = 0;
}
}; };
template<class T> template<class T>
@ -53,35 +62,49 @@ namespace Loki
///////////////////// /////////////////////
// Lifetime manager // Helper class AutoPtrHolder to manage pimpl lifetime
///////////////////// /////////////////////
template namespace Private
<
class Impl, {
class Ptr, template
template<class> class Del <
> class Impl,
struct PimplLife class Ptr,
{ template<class> class Del
PimplLife(); >
~PimplLife(); struct AutoPtrHolder
{
AutoPtrHolder() //: ptr(Ptr()) this owerwrites the pointer to PtrImpl
{} // when using DeclaredRimpl!!
~AutoPtrHolder()
{
// delete automatically by the delete policy
Del<Ptr>::Destroy( ptr );
}
template<class T>
operator T&()
{
Create();
return *ptr;
}
Ptr Create()
{
ptr = Ptr( new Impl );
return ptr;
}
Ptr ptr;
};
template<class T> template<class T>
operator T&(); struct HavePtrHolder
Ptr Create();
Ptr ptr;
};
namespace Private
{
template<class T>
struct HaveAPimplLife
{ {
T life; T ptr;
}; };
} }
@ -92,9 +115,27 @@ namespace Loki
///////////////////// /////////////////////
template<class Impl, typename Ptr, template<class> class Del> template<class Impl, typename Ptr, template<class> class Del>
class DeclaredPimpl : private Private::HaveAPimplLife<PimplLife<Impl,Ptr,Del> > class InheritedPimpl : private
Private::HavePtrHolder<Private::AutoPtrHolder<Impl,Ptr,Del> >
{ {
typedef Private::HaveAPimplLife<PimplLife<Impl,Ptr,Del> > Life; typedef Private::HavePtrHolder<Private::AutoPtrHolder<Impl,Ptr,Del> > PtrHolder;
public:
const Ptr LOKI_INHERITED_PIMPL_NAME;
protected:
InheritedPimpl() : LOKI_INHERITED_PIMPL_NAME(PtrHolder::ptr.Create())
{}
private:
InheritedPimpl& operator=(const InheritedPimpl&);
};
template<class Impl, typename Ptr, template<class> class Del>
class DeclaredPimpl : private
Private::HavePtrHolder<Private::AutoPtrHolder<Impl,Ptr,Del> >
{
typedef Private::HavePtrHolder<Private::AutoPtrHolder<Impl,Ptr,Del> > PtrHolder;
public: public:
Ptr operator->() const Ptr operator->() const
@ -108,30 +149,37 @@ namespace Loki
} }
protected: protected:
DeclaredPimpl() : ptr(Life::life.Create()) DeclaredPimpl() : ptr(PtrHolder::ptr.Create())
{} {}
private: private:
DeclaredPimpl& operator=(const DeclaredPimpl&);
const Ptr ptr; const Ptr ptr;
}; };
template<class Impl, typename Ptr, template<class> class Del>
class InheritedPimpl : private Private::HaveAPimplLife<PimplLife<Impl,Ptr,Del> >
{
typedef Private::HaveAPimplLife<PimplLife<Impl,Ptr,Del> > Life;
public:
const Ptr LOKI_INHERITED_PIMPL_NAME;
protected:
InheritedPimpl() : LOKI_INHERITED_PIMPL_NAME(Life::life.Create())
{}
};
///////////////////// /////////////////////
// Rimpl usage policies // Rimpl usage policies
///////////////////// /////////////////////
template<class Impl, typename Ptr, template<class> class Del>
class InheritedRimpl : private
Private::HavePtrHolder<Private::AutoPtrHolder<Impl,Ptr,Del> >
{
typedef Private::HavePtrHolder<Private::AutoPtrHolder<Impl,Ptr,Del> > PtrHolder;
public:
Impl& LOKI_INHERITED_RIMPL_NAME;
protected:
InheritedRimpl() :
LOKI_INHERITED_RIMPL_NAME(*PtrHolder::ptr.Create())
{}
private:
InheritedRimpl& operator=(const InheritedRimpl&);
};
template<class Impl, typename PointerPolicy, template<class> class DeletePolicy> template<class Impl, typename PointerPolicy, template<class> class DeletePolicy>
class DeclaredRimpl : public Impl class DeclaredRimpl : public Impl
{ {
@ -141,26 +189,6 @@ namespace Loki
}; };
template<class Impl, typename Ptr, template<class> class Del>
class InheritedRimpl : private Private::HaveAPimplLife<PimplLife<Impl,Ptr,Del> >
{
typedef Private::HaveAPimplLife<PimplLife<Impl,Ptr,Del> > Life;
public:
Impl& LOKI_INHERITED_RIMPL_NAME;
protected:
InheritedRimpl() :
LOKI_INHERITED_RIMPL_NAME(*Life::life.Create())
{}
private:
InheritedRimpl& operator=(const InheritedRimpl&);
};
///////////////////// /////////////////////
// Wrapper for "pointer to implementation" alias pimpl. // Wrapper for "pointer to implementation" alias pimpl.
// Impl: implementation class // Impl: implementation class
@ -179,21 +207,17 @@ namespace Loki
> >
class PtrImpl : public UsagePolicy<Impl,PointerPolicy,DeletePolicy> class PtrImpl : public UsagePolicy<Impl,PointerPolicy,DeletePolicy>
{ {
typedef PimplLife<Impl,PointerPolicy,DeletePolicy> Life;
public: public:
typedef Impl ImplType; typedef Impl ImplType;
typedef PointerPolicy PointerType; typedef PointerPolicy PointerType;
PtrImpl() : UsagePolicy<Impl,PointerPolicy,DeletePolicy>()//Private::HaveALife<Life>::life.Create()) PtrImpl() : UsagePolicy<Impl,PointerPolicy,DeletePolicy>()
{} {}
PtrImpl(PointerPolicy pimpl) : UsagePolicy<Impl,PointerPolicy,DeletePolicy>(pimpl) private:
{} PtrImpl& operator=(const PtrImpl&);
virtual ~PtrImpl()
{}
}; };
@ -205,6 +229,7 @@ namespace Loki
template<class T> template<class T>
struct Pimpl struct Pimpl
{ {
// declare pimpl
typedef PtrImpl typedef PtrImpl
< <
Impl<T>, Impl<T>,
@ -214,6 +239,7 @@ namespace Loki
> >
Type; Type;
// inherit pimpl
typedef PtrImpl typedef PtrImpl
< <
Impl<T>, Impl<T>,
@ -229,6 +255,7 @@ namespace Loki
struct Rimpl struct Rimpl
{ {
// declare rimpl
typedef PtrImpl typedef PtrImpl
< <
Impl<T>, Impl<T>,
@ -238,17 +265,17 @@ namespace Loki
> >
Type; Type;
// init declared rimpl
typedef PimplLife typedef Private::AutoPtrHolder
< <
Type, Type,
Type*, Type*,
AutoDeletePimpl AutoDeletePimpl
> >
Life; Init;
// ingerit rimpl
typedef PtrImpl typedef PtrImpl
< <
Impl<T>, Impl<T>,