473,761 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Exception safe & Exception Neutral deep_copy smart pointer

Hi,

I am in need of a deep copy smart pointer (Boost doesn't provide one)
which doesnt require the contained types to have a virtual copy
constructor. I wrote a smart pointer class that I think meets these
requirements, but after reading the chapter on exceptions in
'Exceptional C++':Sutter, I am not sure if its is really Exception safe
or Exception Neutral. I suppose putting the theory in that chapter into
practice isn't trivial.

--------------------------------------------------------------------------------------------
<Inner_deepcpy_ ptr.h>
#ifndef DEEP_COPY_IMPL_ HEADER_GUARD
#define DEEP_COPY_IMPL_ HEADER_GUARD

#include<boost/pool/singleton_pool. hpp>
#include<boost/utility.hpp>
template<class BaseClass>
class Inner_deepcpy_p trAbstract : boost::noncopya ble {
public:
virtual BaseClass * GetBasePointer( )=0;
virtual Inner_deepcpy_p trAbstract<Base Class*clone()=0 ;
virtual ~Inner_deepcpy_ ptrAbstract(){} ;
static void Delete( Inner_deepcpy_p trAbstract<Base Class*);
protected:
typedef void (*FreeFunc)(voi d *const);
FreeFunc eraser;

};

template<class BaseClass,class DerivedClass>
class Inner_deepcpy_p tr :public Inner_deepcpy_p trAbstract<Base Class>{
public:
Inner_deepcpy_p tr(DerivedClass *
theDerivedPoint er_):theDerived Pointer(theDeri vedPointer_){};
DerivedClass * GetBasePointer( ){return theDerivedPoint er;}
virtual Inner_deepcpy_p tr<BaseClass,De rivedClass*clon e()
{
return New(new DerivedClass(*t heDerivedPointe r) );
}
static Inner_deepcpy_p tr<BaseClass,De rivedClass* New(DerivedClas s*
theDerivedPoint er_);
virtual ~Inner_deepcpy_ ptr(){delete theDerivedPoint er;}

private:
DerivedClass *theDerivedPoin ter;

static void * operator new(std::size_t ); //DO NOT IMPLEMENT THIS ,
DON'T WANT IT USED BY ACCIDENT
static void * operator new(std::size_t ,void *ptr){return ptr;}
static void operator delete(void *){};

};

template<class BaseClass,class DerivedClass>
struct Pool {
typedef
boost::singleto n_pool<int,size of(Inner_deepcp y_ptr<BaseClass ,DerivedClass>) >
type;

};

template<class BaseClass,class DerivedClass>
Inner_deepcpy_p tr<BaseClass,De rivedClass*
Inner_deepcpy_p tr<BaseClass,De rivedClass>::Ne w( DerivedClass*
theDerivedPoint er_)
{
Inner_deepcpy_p tr<BaseClass,De rivedClass* thePtr =
static_cast<Inn er_deepcpy_ptr< BaseClass,Deriv edClass>
*>(Pool<BaseCla ss,DerivedClass >::type::malloc ());

new
(thePtr)Inner_d eepcpy_ptr<Base Class,DerivedCl ass>(theDerived Pointer_);

thePtr->eraser = &Pool<BaseClass ,DerivedClass>: :type::free;

return thePtr;
}

template<class BaseClass>
void
Inner_deepcpy_p trAbstract<Base Class>::Delete( Inner_deepcpy_p trAbstract<Base Class>
*ptr)
{
if(ptr) {
ptr->~Inner_deepcpy _ptrAbstract<Ba seClass>();
FreeFunc eraser = ptr->eraser;
eraser(ptr);
}
}
#endif
--------------------------------------------------------------------------------------------

<deepcpy_ptr. h>
#ifndef DEEP_COPY_HEADE R
#define DEEP_COPY_HEADE R

#include"Inner_ deepcpy_ptr.h"
template<class BaseClass>
class deepcpy_ptr {
public:
template<class DerivedClass>
deepcpy_ptr(Der ivedClass * theDerivedPoint er_):
theWrappedPoint er(Inner_deepcp y_ptr<BaseClass ,DerivedClass>: :New(theDerived Pointer_)){}

deepcpy_ptr():t heWrappedPointe r(0){}
deepcpy_ptr(con st deepcpy_ptr<Bas eClass>& theOtherdeepcpy _ptr);
deepcpy_ptr & operator=(const deepcpy_ptr<Bas eClass>&
theOtherdeepcpy _ptr);
BaseClass *operator->();
const BaseClass *operator->()const;
virtual
~deepcpy_ptr(){ Inner_deepcpy_p trAbstract<Base Class>::Delete( theWrappedPoint er);}

private:
deepcpy_ptr & operator=(const BaseClass *ptr);
Inner_deepcpy_p trAbstract<Base Class*theWrappe dPointer;
};

template<class BaseClass>
deepcpy_ptr<Bas eClass>::deepcp y_ptr(const deepcpy_ptr<Bas eClass>&
theOtherdeepcpy _ptr){

if(theOtherdeep cpy_ptr.theWrap pedPointer)
// don't need to set theWrappedPoint er to 0, if the following throws,
don't get an
// object anyway
theWrappedPoint er =(theOtherdeepc py_ptr.theWrapp edPointer)->clone();

else theWrappedPoint er=0;
}
template<class BaseClass>
BaseClass *deepcpy_ptr<Ba seClass>::opera tor->(){

if(theWrappedPo inter)return theWrappedPoint er->GetBasePointer ();

return 0;
}

template<class BaseClass>
const BaseClass *deepcpy_ptr<Ba seClass>::opera tor->()const {

if(theWrappedPo inter)return theWrappedPoint er->GetBasePointer ();

return 0;
}

template<class BaseClass>
deepcpy_ptr<Bas eClass& deepcpy_ptr<Bas eClass>::operat or=(const
deepcpy_ptr<Bas eClass>& theOtherdeepcpy _ptr){

Inner_deepcpy_p trAbstract<Base Class*temp =
(theOtherdeepcp y_ptr.theWrappe dPointer)->clone();

if(temp) {
// we have the temp object to get a bit of exception saftey
Inner_deepcpy_p trAbstract<Base Class>::Delete( theWrappedPoint er);
theWrappedPoint er = temp;
}
return *this;
}
#endif

---------------------------------------------------------------------------------------------------------
<main.cpp>
#include<iostre am>
#include"deepcp y_ptr.h"

using namespace std;

class Base {
public:
Base():ch('*'){
Count++;
}
Base(const Base&):ch('*'){
Count++;
}
virtual char show(){return ch;}
virtual ~Base(){Count--;}
virtual void v()=0;
static unsigned long Count;
private:
char ch;
};
unsigned long Base::Count(0);

/// this will give us lots of derived classes
template<char ch_>
class Derived :public Base {
public:
Derived():ch(ch _){
Count++;
}
Derived(const Derived<ch_>&): ch(ch_){
Count++;
}
virtual char show(){return ch;}
virtual ~Derived(){Coun t--;}
virtual void v(){}
static unsigned long Count;
private:
char ch;

};
template<char ch_>
unsigned long Derived<ch_>::C ount(0);

int main() {

cout << "There are " << Derived<'A'>::C ount << "As" <<'\n';
{
deepcpy_ptr<Bas etheDPA(new Derived<'A'>);
deepcpy_ptr<Bas etheDPA2(theDPA );
cout << "There are " << Derived<'B'>::C ount << "Bs" <<'\n';
try {
deepcpy_ptr<Bas etheDPB(new Derived<'B'>);
deepcpy_ptr<Bas etheDPB2;
theDPB2 = theDPB;

cout << "There are " << Derived<'B'>::C ount << "Bs" <<'\n';

deepcpy_ptr<Bas etheDPB3(new Derived<'B'>);
deepcpy_ptr<Bas etheDPB4(theDPB 3);
deepcpy_ptr<Bas etheDPB5(theDPB 4);
}
catch(int i) {
}
cout << "There are " << Derived<'B'>::C ount << "Bs" <<'\n';

deepcpy_ptr<Bas etheDPA3;
theDPA3 = theDPA;
}
cout << "There are " << Derived<'A'>::C ount << "As";

}

Nov 10 '06 #1
3 2127
Ni*****@yahoo.c o.uk wrote:
Hi,

I am in need of a deep copy smart pointer (Boost doesn't provide one)
which doesnt require the contained types to have a virtual copy
constructor. I wrote a smart pointer class that I think meets these
requirements, but after reading the chapter on exceptions in
'Exceptional C++':Sutter, I am not sure if its is really Exception safe
or Exception Neutral. I suppose putting the theory in that chapter into
practice isn't trivial.

--------------------------------------------------------------------------------------------
<Inner_deepcpy_ ptr.h>
#ifndef DEEP_COPY_IMPL_ HEADER_GUARD
#define DEEP_COPY_IMPL_ HEADER_GUARD

#include<boost/pool/singleton_pool. hpp>
#include<boost/utility.hpp>
template<class BaseClass>
class Inner_deepcpy_p trAbstract : boost::noncopya ble {
public:
virtual BaseClass * GetBasePointer( )=0;
virtual Inner_deepcpy_p trAbstract<Base Class*clone()=0 ;
virtual ~Inner_deepcpy_ ptrAbstract(){} ;
static void Delete( Inner_deepcpy_p trAbstract<Base Class*);
protected:
typedef void (*FreeFunc)(voi d *const);
FreeFunc eraser;

};

template<class BaseClass,class DerivedClass>
class Inner_deepcpy_p tr :public Inner_deepcpy_p trAbstract<Base Class>{
public:
Inner_deepcpy_p tr(DerivedClass *
theDerivedPoint er_):theDerived Pointer(theDeri vedPointer_){};
DerivedClass * GetBasePointer( ){return theDerivedPoint er;}
virtual Inner_deepcpy_p tr<BaseClass,De rivedClass*clon e()
{
return New(new DerivedClass(*t heDerivedPointe r) );
}
static Inner_deepcpy_p tr<BaseClass,De rivedClass* New(DerivedClas s*
theDerivedPoint er_);
virtual ~Inner_deepcpy_ ptr(){delete theDerivedPoint er;}

private:
DerivedClass *theDerivedPoin ter;

static void * operator new(std::size_t ); //DO NOT IMPLEMENT THIS ,
DON'T WANT IT USED BY ACCIDENT
static void * operator new(std::size_t ,void *ptr){return ptr;}
static void operator delete(void *){};

};

template<class BaseClass,class DerivedClass>
struct Pool {
typedef
boost::singleto n_pool<int,size of(Inner_deepcp y_ptr<BaseClass ,DerivedClass>) >
type;

};

template<class BaseClass,class DerivedClass>
Inner_deepcpy_p tr<BaseClass,De rivedClass*
Inner_deepcpy_p tr<BaseClass,De rivedClass>::Ne w( DerivedClass*
theDerivedPoint er_)
{
Inner_deepcpy_p tr<BaseClass,De rivedClass* thePtr =
static_cast<Inn er_deepcpy_ptr< BaseClass,Deriv edClass>
*>(Pool<BaseCla ss,DerivedClass >::type::malloc ());

new
(thePtr)Inner_d eepcpy_ptr<Base Class,DerivedCl ass>(theDerived Pointer_);

thePtr->eraser = &Pool<BaseClass ,DerivedClass>: :type::free;

return thePtr;
}

template<class BaseClass>
void
Inner_deepcpy_p trAbstract<Base Class>::Delete( Inner_deepcpy_p trAbstract<Base Class>
*ptr)
{
if(ptr) {
ptr->~Inner_deepcpy _ptrAbstract<Ba seClass>();
FreeFunc eraser = ptr->eraser;
eraser(ptr);
}
}
#endif
--------------------------------------------------------------------------------------------

<deepcpy_ptr. h>
#ifndef DEEP_COPY_HEADE R
#define DEEP_COPY_HEADE R

#include"Inner_ deepcpy_ptr.h"
template<class BaseClass>
class deepcpy_ptr {
public:
template<class DerivedClass>
deepcpy_ptr(Der ivedClass * theDerivedPoint er_):
theWrappedPoint er(Inner_deepcp y_ptr<BaseClass ,DerivedClass>: :New(theDerived Pointer_)
{}
>
deepcpy_ptr():t heWrappedPointe r(0){}
deepcpy_ptr(con st deepcpy_ptr<Bas eClass>& theOtherdeepcpy _ptr);
deepcpy_ptr & operator=(const deepcpy_ptr<Bas eClass>&
theOtherdeepcpy _ptr);
BaseClass *operator->();
const BaseClass *operator->()const;
virtual
~deepcpy_ptr(
{Inner_deepcpy_ ptrAbstract<Bas eClass>::Delete (theWrappedPoin ter);}
>
private:
deepcpy_ptr & operator=(const BaseClass *ptr);
Inner_deepcpy_p trAbstract<Base Class*theWrappe dPointer;
};

template<class BaseClass>
deepcpy_ptr<Bas eClass>::deepcp y_ptr(const deepcpy_ptr<Bas eClass>&
theOtherdeepcpy _ptr){

if(theOtherdeep cpy_ptr.theWrap pedPointer)
// don't need to set theWrappedPoint er to 0, if the following throws,
don't get an
// object anyway
theWrappedPoint er =(theOtherdeepc py_ptr.theWrapp edPointer)->clone();

else theWrappedPoint er=0;
}
template<class BaseClass>
BaseClass *deepcpy_ptr<Ba seClass>::opera tor->(){

if(theWrappedPo inter)return theWrappedPoint er->GetBasePointer ();

return 0;
}

template<class BaseClass>
const BaseClass *deepcpy_ptr<Ba seClass>::opera tor->()const {

if(theWrappedPo inter)return theWrappedPoint er->GetBasePointer ();

return 0;
}

template<class BaseClass>
deepcpy_ptr<Bas eClass& deepcpy_ptr<Bas eClass>::operat or=(const
deepcpy_ptr<Bas eClass>& theOtherdeepcpy _ptr){

Inner_deepcpy_p trAbstract<Base Class*temp =
(theOtherdeepcp y_ptr.theWrappe dPointer)->clone();

if(temp) {
// we have the temp object to get a bit of exception saftey
Inner_deepcpy_p trAbstract<Base Class>::Delete( theWrappedPoint er);
theWrappedPoint er = temp;
}
At this point, I would go all the way and use the copy-swap idiom. Although
bad things are supposed to happen when a destructor throws, you may want
them to happen *after* the current object has been modified and not *while*
it is teared down.
>

return *this;
}
#endif

---------------------------------------------------------------------------------------------------------
<main.cpp>
#include<iostre am>
#include"deepcp y_ptr.h"

using namespace std;

class Base {
public:
Base():ch('*'){
Count++;
}
Base(const Base&):ch('*'){
Count++;
}
virtual char show(){return ch;}
virtual ~Base(){Count--;}
virtual void v()=0;
static unsigned long Count;
private:
char ch;
};
unsigned long Base::Count(0);

/// this will give us lots of derived classes
template<char ch_>
class Derived :public Base {
public:
Derived():ch(ch _){
Count++;
}
Derived(const Derived<ch_>&): ch(ch_){
Count++;
}
virtual char show(){return ch;}
virtual ~Derived(){Coun t--;}
virtual void v(){}
static unsigned long Count;
private:
char ch;

};
template<char ch_>
unsigned long Derived<ch_>::C ount(0);

int main() {

cout << "There are " << Derived<'A'>::C ount << "As" <<'\n';
{
deepcpy_ptr<Bas etheDPA(new Derived<'A'>);
deepcpy_ptr<Bas etheDPA2(theDPA );
cout << "There are " << Derived<'B'>::C ount << "Bs" <<'\n';
try {
deepcpy_ptr<Bas etheDPB(new Derived<'B'>);
deepcpy_ptr<Bas etheDPB2;
theDPB2 = theDPB;

cout << "There are " << Derived<'B'>::C ount << "Bs" <<'\n';

deepcpy_ptr<Bas etheDPB3(new Derived<'B'>);
deepcpy_ptr<Bas etheDPB4(theDPB 3);
deepcpy_ptr<Bas etheDPB5(theDPB 4);
}
catch(int i) {
}
cout << "There are " << Derived<'B'>::C ount << "Bs" <<'\n';

deepcpy_ptr<Bas etheDPA3;
theDPA3 = theDPA;
}
cout << "There are " << Derived<'A'>::C ount << "As";

}

Just for inspiration, here is a version that I wrote. It uses a little trick
for the cloning that saves quite a few lines of code. The trade-off is that
copy_ptr<Basean d copy_ptr<Derive dare unrelated.

The assignment operator uses the copy-swap idiom and, therefore, makes the
strong exception guarantee.

#include <cassert>
#include <algorithm// std::swap

// The clone functions:
// =============== =====
template < typename T, typename D >
T * clone ( T * ptr ) {
return ( ptr == 0 ? 0 : new D ( *( static_cast<D*> ( ptr ) ) ) );
}

template < typename T >
T * simple_clone ( T * ptr ) {
return ( ptr == 0 ? 0 : new T ( *ptr ) );
}

// The copy_ptr:
// =============

template < typename T >
class copy_ptr {

friend void swap ( copy_ptr<T& p, copy_ptr<T& q ) {
std::swap( p.raw_ptr, q.raw_ptr );
std::swap( p.clone_fct, q.clone_fct );
}

/*
The idea is that in addition to a pointer, we also need
a pointer to the _appropriate_ clone function.
*/
T * raw_ptr;
T * ( *clone_fct ) ( T * );

public:

copy_ptr ( T * ptr = 0)
: raw_ptr ( ptr )
, clone_fct ( simple_clone<T)
{}

template < typename D >
copy_ptr ( D * ptr )
: raw_ptr ( ptr )
, clone_fct ( clone<T,D)
{}

// copy construction clones:
copy_ptr ( copy_ptr const & other )
: raw_ptr ( other.clone_fct ( other.raw_ptr ) )
, clone_fct ( other.clone_fct )
{}

// destruction frees the pointee
~copy_ptr ( void ) {
delete( raw_ptr );
}

// assignment reduces to copy construction
// (for correctness and exception safety):
copy_ptr & operator= ( copy_ptr const & other ) {
copy_ptr dummy ( other );
swap( *this, dummy );
return( *this );
}

T const * operator-( void ) const {
return( raw_ptr );
}

T * operator-( void ) {
return( raw_ptr );
}

T const & operator* ( void ) const {
return( *raw_ptr );
}

T & operator* ( void ) {
return( *raw_ptr );
}

}; // copy_ptr<T>


Best

Kai-Uwe Bux

Nov 10 '06 #2
Thanks .... I think the two are very similar in principle EXCEPT that
mine is completely over -engineered !!. Where you are using clone
funtion I am using a whole object, and hence all the neccessary code
for its allocation. I much prefer yours and will use that intead. I
will make sure that it fullfils all the requirements that I have
Thanks again
Kai-Uwe Bux wrote:
Ni*****@yahoo.c o.uk wrote:
Hi,

I am in need of a deep copy smart pointer (Boost doesn't provide one)
which doesnt require the contained types to have a virtual copy
constructor. I wrote a smart pointer class that I think meets these
requirements, but after reading the chapter on exceptions in
'Exceptional C++':Sutter, I am not sure if its is really Exception safe
or Exception Neutral. I suppose putting the theory in that chapter into
practice isn't trivial.

--------------------------------------------------------------------------------------------
<Inner_deepcpy_ ptr.h>
#ifndef DEEP_COPY_IMPL_ HEADER_GUARD
#define DEEP_COPY_IMPL_ HEADER_GUARD

#include<boost/pool/singleton_pool. hpp>
#include<boost/utility.hpp>
template<class BaseClass>
class Inner_deepcpy_p trAbstract : boost::noncopya ble {
public:
virtual BaseClass * GetBasePointer( )=0;
virtual Inner_deepcpy_p trAbstract<Base Class*clone()=0 ;
virtual ~Inner_deepcpy_ ptrAbstract(){} ;
static void Delete( Inner_deepcpy_p trAbstract<Base Class*);
protected:
typedef void (*FreeFunc)(voi d *const);
FreeFunc eraser;

};

template<class BaseClass,class DerivedClass>
class Inner_deepcpy_p tr :public Inner_deepcpy_p trAbstract<Base Class>{
public:
Inner_deepcpy_p tr(DerivedClass *
theDerivedPoint er_):theDerived Pointer(theDeri vedPointer_){};
DerivedClass * GetBasePointer( ){return theDerivedPoint er;}
virtual Inner_deepcpy_p tr<BaseClass,De rivedClass*clon e()
{
return New(new DerivedClass(*t heDerivedPointe r) );
}
static Inner_deepcpy_p tr<BaseClass,De rivedClass* New(DerivedClas s*
theDerivedPoint er_);
virtual ~Inner_deepcpy_ ptr(){delete theDerivedPoint er;}

private:
DerivedClass *theDerivedPoin ter;

static void * operator new(std::size_t ); //DO NOT IMPLEMENT THIS ,
DON'T WANT IT USED BY ACCIDENT
static void * operator new(std::size_t ,void *ptr){return ptr;}
static void operator delete(void *){};

};

template<class BaseClass,class DerivedClass>
struct Pool {
typedef
boost::singleto n_pool<int,size of(Inner_deepcp y_ptr<BaseClass ,DerivedClass>) >
type;

};

template<class BaseClass,class DerivedClass>
Inner_deepcpy_p tr<BaseClass,De rivedClass*
Inner_deepcpy_p tr<BaseClass,De rivedClass>::Ne w( DerivedClass*
theDerivedPoint er_)
{
Inner_deepcpy_p tr<BaseClass,De rivedClass* thePtr =
static_cast<Inn er_deepcpy_ptr< BaseClass,Deriv edClass>
*>(Pool<BaseCla ss,DerivedClass >::type::malloc ());

new
(thePtr)Inner_d eepcpy_ptr<Base Class,DerivedCl ass>(theDerived Pointer_);

thePtr->eraser = &Pool<BaseClass ,DerivedClass>: :type::free;

return thePtr;
}

template<class BaseClass>
void
Inner_deepcpy_p trAbstract<Base Class>::Delete( Inner_deepcpy_p trAbstract<Base Class>
*ptr)
{
if(ptr) {
ptr->~Inner_deepcpy _ptrAbstract<Ba seClass>();
FreeFunc eraser = ptr->eraser;
eraser(ptr);
}
}
#endif
--------------------------------------------------------------------------------------------

<deepcpy_ptr. h>
#ifndef DEEP_COPY_HEADE R
#define DEEP_COPY_HEADE R

#include"Inner_ deepcpy_ptr.h"
template<class BaseClass>
class deepcpy_ptr {
public:
template<class DerivedClass>
deepcpy_ptr(Der ivedClass * theDerivedPoint er_):
theWrappedPoint er(Inner_deepcp y_ptr<BaseClass ,DerivedClass>: :New(theDerived Pointer_)
{}

deepcpy_ptr():t heWrappedPointe r(0){}
deepcpy_ptr(con st deepcpy_ptr<Bas eClass>& theOtherdeepcpy _ptr);
deepcpy_ptr & operator=(const deepcpy_ptr<Bas eClass>&
theOtherdeepcpy _ptr);
BaseClass *operator->();
const BaseClass *operator->()const;
virtual
~deepcpy_ptr(
{Inner_deepcpy_ ptrAbstract<Bas eClass>::Delete (theWrappedPoin ter);}

private:
deepcpy_ptr & operator=(const BaseClass *ptr);
Inner_deepcpy_p trAbstract<Base Class*theWrappe dPointer;
};

template<class BaseClass>
deepcpy_ptr<Bas eClass>::deepcp y_ptr(const deepcpy_ptr<Bas eClass>&
theOtherdeepcpy _ptr){

if(theOtherdeep cpy_ptr.theWrap pedPointer)
// don't need to set theWrappedPoint er to 0, if the following throws,
don't get an
// object anyway
theWrappedPoint er =(theOtherdeepc py_ptr.theWrapp edPointer)->clone();

else theWrappedPoint er=0;
}
template<class BaseClass>
BaseClass *deepcpy_ptr<Ba seClass>::opera tor->(){

if(theWrappedPo inter)return theWrappedPoint er->GetBasePointer ();

return 0;
}

template<class BaseClass>
const BaseClass *deepcpy_ptr<Ba seClass>::opera tor->()const {

if(theWrappedPo inter)return theWrappedPoint er->GetBasePointer ();

return 0;
}

template<class BaseClass>
deepcpy_ptr<Bas eClass& deepcpy_ptr<Bas eClass>::operat or=(const
deepcpy_ptr<Bas eClass>& theOtherdeepcpy _ptr){

Inner_deepcpy_p trAbstract<Base Class*temp =
(theOtherdeepcp y_ptr.theWrappe dPointer)->clone();

if(temp) {
// we have the temp object to get a bit of exception saftey
Inner_deepcpy_p trAbstract<Base Class>::Delete( theWrappedPoint er);
theWrappedPoint er = temp;
}

At this point, I would go all the way and use the copy-swap idiom. Although
bad things are supposed to happen when a destructor throws, you may want
them to happen *after* the current object has been modified and not *while*
it is teared down.


return *this;
}
#endif

---------------------------------------------------------------------------------------------------------
<main.cpp>
#include<iostre am>
#include"deepcp y_ptr.h"

using namespace std;

class Base {
public:
Base():ch('*'){
Count++;
}
Base(const Base&):ch('*'){
Count++;
}
virtual char show(){return ch;}
virtual ~Base(){Count--;}
virtual void v()=0;
static unsigned long Count;
private:
char ch;
};
unsigned long Base::Count(0);

/// this will give us lots of derived classes
template<char ch_>
class Derived :public Base {
public:
Derived():ch(ch _){
Count++;
}
Derived(const Derived<ch_>&): ch(ch_){
Count++;
}
virtual char show(){return ch;}
virtual ~Derived(){Coun t--;}
virtual void v(){}
static unsigned long Count;
private:
char ch;

};
template<char ch_>
unsigned long Derived<ch_>::C ount(0);

int main() {

cout << "There are " << Derived<'A'>::C ount << "As" <<'\n';
{
deepcpy_ptr<Bas etheDPA(new Derived<'A'>);
deepcpy_ptr<Bas etheDPA2(theDPA );
cout << "There are " << Derived<'B'>::C ount << "Bs" <<'\n';
try {
deepcpy_ptr<Bas etheDPB(new Derived<'B'>);
deepcpy_ptr<Bas etheDPB2;
theDPB2 = theDPB;

cout << "There are " << Derived<'B'>::C ount << "Bs" <<'\n';

deepcpy_ptr<Bas etheDPB3(new Derived<'B'>);
deepcpy_ptr<Bas etheDPB4(theDPB 3);
deepcpy_ptr<Bas etheDPB5(theDPB 4);
}
catch(int i) {
}
cout << "There are " << Derived<'B'>::C ount << "Bs" <<'\n';

deepcpy_ptr<Bas etheDPA3;
theDPA3 = theDPA;
}
cout << "There are " << Derived<'A'>::C ount << "As";

}


Just for inspiration, here is a version that I wrote. It uses a little trick
for the cloning that saves quite a few lines of code. The trade-off is that
copy_ptr<Basean d copy_ptr<Derive dare unrelated.

The assignment operator uses the copy-swap idiom and, therefore, makes the
strong exception guarantee.

#include <cassert>
#include <algorithm// std::swap

// The clone functions:
// =============== =====
template < typename T, typename D >
T * clone ( T * ptr ) {
return ( ptr == 0 ? 0 : new D ( *( static_cast<D*> ( ptr ) ) ) );
}

template < typename T >
T * simple_clone ( T * ptr ) {
return ( ptr == 0 ? 0 : new T ( *ptr ) );
}

// The copy_ptr:
// =============

template < typename T >
class copy_ptr {

friend void swap ( copy_ptr<T& p, copy_ptr<T& q ) {
std::swap( p.raw_ptr, q.raw_ptr );
std::swap( p.clone_fct, q.clone_fct );
}

/*
The idea is that in addition to a pointer, we also need
a pointer to the _appropriate_ clone function.
*/
T * raw_ptr;
T * ( *clone_fct ) ( T * );

public:

copy_ptr ( T * ptr = 0)
: raw_ptr ( ptr )
, clone_fct ( simple_clone<T)
{}

template < typename D >
copy_ptr ( D * ptr )
: raw_ptr ( ptr )
, clone_fct ( clone<T,D)
{}

// copy construction clones:
copy_ptr ( copy_ptr const & other )
: raw_ptr ( other.clone_fct ( other.raw_ptr ) )
, clone_fct ( other.clone_fct )
{}

// destruction frees the pointee
~copy_ptr ( void ) {
delete( raw_ptr );
}

// assignment reduces to copy construction
// (for correctness and exception safety):
copy_ptr & operator= ( copy_ptr const & other ) {
copy_ptr dummy ( other );
swap( *this, dummy );
return( *this );
}

T const * operator-( void ) const {
return( raw_ptr );
}

T * operator-( void ) {
return( raw_ptr );
}

T const & operator* ( void ) const {
return( *raw_ptr );
}

T & operator* ( void ) {
return( *raw_ptr );
}

}; // copy_ptr<T>


Best

Kai-Uwe Bux
Nov 10 '06 #3
OK here is my new smart pointer incorporating the 'Bux' trick . I have
left the cloning mechanism as policy input to the pointer, this gives
more flexibility for example if my pointees come from a pool.

#ifndef DEEP_COPY_HEADE R
#define DEEP_COPY_HEADE R

#include<algori thm>

//
http://groups.google.co.uk/group/com...e5c18bc6bb5c7e

template<class BaseClass,class DerivedClass>
BaseClass * Buxclone(const BaseClass *theOtherPtr){
return new DerivedClass(*s tatic_cast<cons t
DerivedClass*>( theOtherPtr));
}

template<class BaseClass>
struct BuxWrappedPoint er {

BuxWrappedPoint er():raw_pointe r(0),theCloner( 0){}

template<class DerivedClass>
BuxWrappedPoint er(DerivedClass *
ptr):raw_pointe r(ptr),theClone r(&Buxclone<Bas eClass,DerivedC lass>){}

BuxWrappedPoint er(const BuxWrappedPoint er
&theOther):theC loner(theOther. theCloner),
raw_pointer(the Other.theCloner (theOther.raw_p ointer)){}

BuxWrappedPoint er<BaseClass& operator=(const
BuxWrappedPoint er<BaseClass&th eOther){
BuxWrappedPoint er<BaseClasstem p(theOther);
std::swap(temp. raw_pointer,raw _pointer);
return *this;
}

BaseClass * raw_pointer;

~BuxWrappedPoin ter(){delete raw_pointer;}
private:
typedef BaseClass * (*clone_)(const BaseClass *);
clone_ theCloner;
};
template<
class BaseClass,
template <classclass CLONE_POLICY = BuxWrappedPoint er
>
class deepcpy_ptr {
public:
template<class DerivedClass>
deepcpy_ptr(Der ivedClass *
theDerivedPoint er_):theWrapped Pointer(theDeri vedPointer_){}

deepcpy_ptr(){}

BaseClass *operator->(){return theWrappedPoint er->raw_pointer; }

const BaseClass *operator->()const{retu rn
theWrappedPoint er->raw_pointer; }

virtual ~deepcpy_ptr(){ }

private:
deepcpy_ptr & operator=(const BaseClass *ptr);

CLONE_POLICY<Ba seClasstheWrapp edPointer;

};
#endif
Ni*****@yahoo.c o.uk wrote:
Thanks .... I think the two are very similar in principle EXCEPT that
mine is completely over -engineered !!. Where you are using clone
funtion I am using a whole object, and hence all the neccessary code
for its allocation. I much prefer yours and will use that intead. I
will make sure that it fullfils all the requirements that I have
Thanks again
Kai-Uwe Bux wrote:
Ni*****@yahoo.c o.uk wrote:
Hi,
>
I am in need of a deep copy smart pointer (Boost doesn't provide one)
which doesnt require the contained types to have a virtual copy
constructor. I wrote a smart pointer class that I think meets these
requirements, but after reading the chapter on exceptions in
'Exceptional C++':Sutter, I am not sure if its is really Exception safe
or Exception Neutral. I suppose putting the theory in that chapter into
practice isn't trivial.
>
--------------------------------------------------------------------------------------------
<Inner_deepcpy_ ptr.h>
#ifndef DEEP_COPY_IMPL_ HEADER_GUARD
#define DEEP_COPY_IMPL_ HEADER_GUARD
>
#include<boost/pool/singleton_pool. hpp>
#include<boost/utility.hpp>
>
>
template<class BaseClass>
class Inner_deepcpy_p trAbstract : boost::noncopya ble {
public:
virtual BaseClass * GetBasePointer( )=0;
virtual Inner_deepcpy_p trAbstract<Base Class*clone()=0 ;
virtual ~Inner_deepcpy_ ptrAbstract(){} ;
static void Delete( Inner_deepcpy_p trAbstract<Base Class*);
protected:
typedef void (*FreeFunc)(voi d *const);
FreeFunc eraser;
>
};
>
template<class BaseClass,class DerivedClass>
class Inner_deepcpy_p tr :public Inner_deepcpy_p trAbstract<Base Class>{
public:
Inner_deepcpy_p tr(DerivedClass *
theDerivedPoint er_):theDerived Pointer(theDeri vedPointer_){};
DerivedClass * GetBasePointer( ){return theDerivedPoint er;}
virtual Inner_deepcpy_p tr<BaseClass,De rivedClass*clon e()
{
return New(new DerivedClass(*t heDerivedPointe r) );
}
static Inner_deepcpy_p tr<BaseClass,De rivedClass* New(DerivedClas s*
theDerivedPoint er_);
virtual ~Inner_deepcpy_ ptr(){delete theDerivedPoint er;}
>
private:
DerivedClass *theDerivedPoin ter;
>
static void * operator new(std::size_t ); //DO NOT IMPLEMENT THIS ,
DON'T WANT IT USED BY ACCIDENT
static void * operator new(std::size_t ,void *ptr){return ptr;}
static void operator delete(void *){};
>
};
>
template<class BaseClass,class DerivedClass>
struct Pool {
typedef
>
boost::singleto n_pool<int,size of(Inner_deepcp y_ptr<BaseClass ,DerivedClass>) >
type;
>
};
>
template<class BaseClass,class DerivedClass>
Inner_deepcpy_p tr<BaseClass,De rivedClass*
Inner_deepcpy_p tr<BaseClass,De rivedClass>::Ne w( DerivedClass*
theDerivedPoint er_)
{
Inner_deepcpy_p tr<BaseClass,De rivedClass* thePtr =
static_cast<Inn er_deepcpy_ptr< BaseClass,Deriv edClass>
*>(Pool<BaseCla ss,DerivedClass >::type::malloc ());
>
new
(thePtr)Inner_d eepcpy_ptr<Base Class,DerivedCl ass>(theDerived Pointer_);
>
thePtr->eraser = &Pool<BaseClass ,DerivedClass>: :type::free;
>
return thePtr;
}
>
template<class BaseClass>
void
>
Inner_deepcpy_p trAbstract<Base Class>::Delete( Inner_deepcpy_p trAbstract<Base Class>
*ptr)
{
if(ptr) {
ptr->~Inner_deepcpy _ptrAbstract<Ba seClass>();
FreeFunc eraser = ptr->eraser;
eraser(ptr);
}
}
>
>
#endif
--------------------------------------------------------------------------------------------
>
<deepcpy_ptr. h>
#ifndef DEEP_COPY_HEADE R
#define DEEP_COPY_HEADE R
>
#include"Inner_ deepcpy_ptr.h"
>
>
template<class BaseClass>
class deepcpy_ptr {
public:
template<class DerivedClass>
deepcpy_ptr(Der ivedClass * theDerivedPoint er_):
>
theWrappedPoint er(Inner_deepcp y_ptr<BaseClass ,DerivedClass>: :New(theDerived Pointer_)
{}
>
deepcpy_ptr():t heWrappedPointe r(0){}
deepcpy_ptr(con st deepcpy_ptr<Bas eClass>& theOtherdeepcpy _ptr);
deepcpy_ptr & operator=(const deepcpy_ptr<Bas eClass>&
theOtherdeepcpy _ptr);
BaseClass *operator->();
const BaseClass *operator->()const;
virtual
~deepcpy_ptr(
{Inner_deepcpy_ ptrAbstract<Bas eClass>::Delete (theWrappedPoin ter);}
>
private:
deepcpy_ptr & operator=(const BaseClass *ptr);
Inner_deepcpy_p trAbstract<Base Class*theWrappe dPointer;
};
>
template<class BaseClass>
deepcpy_ptr<Bas eClass>::deepcp y_ptr(const deepcpy_ptr<Bas eClass>&
theOtherdeepcpy _ptr){
>
if(theOtherdeep cpy_ptr.theWrap pedPointer)
// don't need to set theWrappedPoint er to 0, if the following throws,
don't get an
// object anyway
theWrappedPoint er =(theOtherdeepc py_ptr.theWrapp edPointer)->clone();
>
else theWrappedPoint er=0;
}
>
>
template<class BaseClass>
BaseClass *deepcpy_ptr<Ba seClass>::opera tor->(){
>
if(theWrappedPo inter)return theWrappedPoint er->GetBasePointer ();
>
return 0;
}
>
template<class BaseClass>
const BaseClass *deepcpy_ptr<Ba seClass>::opera tor->()const {
>
if(theWrappedPo inter)return theWrappedPoint er->GetBasePointer ();
>
return 0;
}
>
template<class BaseClass>
deepcpy_ptr<Bas eClass& deepcpy_ptr<Bas eClass>::operat or=(const
deepcpy_ptr<Bas eClass>& theOtherdeepcpy _ptr){
>
Inner_deepcpy_p trAbstract<Base Class*temp =
(theOtherdeepcp y_ptr.theWrappe dPointer)->clone();
>
if(temp) {
// we have the temp object to get a bit of exception saftey
Inner_deepcpy_p trAbstract<Base Class>::Delete( theWrappedPoint er);
theWrappedPoint er = temp;
}
At this point, I would go all the way and use the copy-swap idiom. Although
bad things are supposed to happen when a destructor throws, you may want
them to happen *after* the current object has been modified and not *while*
it is teared down.
>
>
return *this;
}
>
>
#endif
>
---------------------------------------------------------------------------------------------------------
<main.cpp>
#include<iostre am>
#include"deepcp y_ptr.h"
>
using namespace std;
>
class Base {
public:
Base():ch('*'){
Count++;
}
Base(const Base&):ch('*'){
Count++;
}
virtual char show(){return ch;}
virtual ~Base(){Count--;}
virtual void v()=0;
static unsigned long Count;
private:
char ch;
};
unsigned long Base::Count(0);
>
/// this will give us lots of derived classes
template<char ch_>
class Derived :public Base {
public:
Derived():ch(ch _){
Count++;
}
Derived(const Derived<ch_>&): ch(ch_){
Count++;
}
virtual char show(){return ch;}
virtual ~Derived(){Coun t--;}
virtual void v(){}
static unsigned long Count;
private:
char ch;
>
};
template<char ch_>
unsigned long Derived<ch_>::C ount(0);
>
>
>
int main() {
>
cout << "There are " << Derived<'A'>::C ount << "As" <<'\n';
{
deepcpy_ptr<Bas etheDPA(new Derived<'A'>);
deepcpy_ptr<Bas etheDPA2(theDPA );
>
>
cout << "There are " << Derived<'B'>::C ount << "Bs" <<'\n';
try {
deepcpy_ptr<Bas etheDPB(new Derived<'B'>);
deepcpy_ptr<Bas etheDPB2;
theDPB2 = theDPB;
>
cout << "There are " << Derived<'B'>::C ount << "Bs" <<'\n';
>
deepcpy_ptr<Bas etheDPB3(new Derived<'B'>);
deepcpy_ptr<Bas etheDPB4(theDPB 3);
deepcpy_ptr<Bas etheDPB5(theDPB 4);
>
>
}
catch(int i) {
}
cout << "There are " << Derived<'B'>::C ount << "Bs" <<'\n';
>
deepcpy_ptr<Bas etheDPA3;
theDPA3 = theDPA;
}
cout << "There are " << Derived<'A'>::C ount << "As";
>
}

Just for inspiration, here is a version that I wrote. It uses a little trick
for the cloning that saves quite a few lines of code. The trade-off is that
copy_ptr<Basean d copy_ptr<Derive dare unrelated.

The assignment operator uses the copy-swap idiom and, therefore, makes the
strong exception guarantee.

#include <cassert>
#include <algorithm// std::swap

// The clone functions:
// =============== =====
template < typename T, typename D >
T * clone ( T * ptr ) {
return ( ptr == 0 ? 0 : new D ( *( static_cast<D*> ( ptr ) ) ) );
}

template < typename T >
T * simple_clone ( T * ptr ) {
return ( ptr == 0 ? 0 : new T ( *ptr ) );
}

// The copy_ptr:
// =============

template < typename T >
class copy_ptr {

friend void swap ( copy_ptr<T& p, copy_ptr<T& q ) {
std::swap( p.raw_ptr, q.raw_ptr );
std::swap( p.clone_fct, q.clone_fct );
}

/*
The idea is that in addition to a pointer, we also need
a pointer to the _appropriate_ clone function.
*/
T * raw_ptr;
T * ( *clone_fct ) ( T * );

public:

copy_ptr ( T * ptr = 0)
: raw_ptr ( ptr )
, clone_fct ( simple_clone<T)
{}

template < typename D >
copy_ptr ( D * ptr )
: raw_ptr ( ptr )
, clone_fct ( clone<T,D)
{}

// copy construction clones:
copy_ptr ( copy_ptr const & other )
: raw_ptr ( other.clone_fct ( other.raw_ptr ) )
, clone_fct ( other.clone_fct )
{}

// destruction frees the pointee
~copy_ptr ( void ) {
delete( raw_ptr );
}

// assignment reduces to copy construction
// (for correctness and exception safety):
copy_ptr & operator= ( copy_ptr const & other ) {
copy_ptr dummy ( other );
swap( *this, dummy );
return( *this );
}

T const * operator-( void ) const {
return( raw_ptr );
}

T * operator-( void ) {
return( raw_ptr );
}

T const & operator* ( void ) const {
return( *raw_ptr );
}

T & operator* ( void ) {
return( *raw_ptr );
}

}; // copy_ptr<T>


Best

Kai-Uwe Bux
Nov 10 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2591
by: Bikash | last post by:
Hello, I am a specific problem in exception handling. The code snippets is attached below. void f() { char *ptr = new char(20); throw 2; }
11
1964
by: Lloyd Dupont | last post by:
(not I use 2.0, so new return a "normal" pointer and gcnew return a managed one, my question below regarding new concern plain standart C++ allocator) - if I use the default new operator, are all the instance variable initialize to NULL / 0 ? - if there is not enough memory what happend with new ? does it return NULL or throw an exception? - if new throw a native C++ exception what happen in Managed C++ ?! - if there is an exception in...
11
11984
by: mangesh | last post by:
I read , FAQ : 17.4] How should I handle resources if my constructors may throw exceptions? Above faq says that use smart pointer in construcors . Because if exception is thrown from constructor it's destructor is not run .So to avoid memory lekage use smart pointer . But if exception is thrown we can release resources in catch block . So use of smart pointer is not must , we have this secnd option . Am i right ?
1
2812
by: yancheng.cheok | last post by:
Hi all, According to "How can I handle a constructor that fails?" in http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.2, whenever there is a constructor fail, we will throw exception. However, how can we make the interface easy to use correctly and hard to use incorrectly? Client may forget/ ignore from having a try...catch block whenever they call the constructor. Is there any way we can prevent this from happen?
8
7705
by: jporter188 | last post by:
Hello, I am working on a project to manipulate XML files. All of the files, the code, and the output are on network drives. When I run my program I get an exception (see below). I tried giving the LocalIntranet_Zone full permissions in the .NET Framework 2.0 Configuration msc. This had no effect whatsoever. What do I need to do to fix this? Thanks,
42
2701
by: coder_lol | last post by:
Thanks everyone again for contributing to helping me clear C++ confusions. I did some serious reading on copy constructors and assignments and I think I've got a good handle on the memory stuff. Well, I came across Scott Meyer's SmartPtr example from some 10 years ago. I like the template member function for type conversion to solve inheritance issues. On MSVS 2003, I get the below error, if I declare the constructor SmartPtr(T*...
4
1140
by: cronusf | last post by:
I have code like this (pseudocode): MyClass::MyClass() { Array1 = new D3DXVECTOR3; Array2 = new USHORT; fx = gcnew Effect(); }
7
1650
by: junw2000 | last post by:
class A { public: A() { try { p = new int; //Other lines that may throw exception. }
3
3078
by: andreas.zetterstrom | last post by:
I'm implementing some different c++ classes which I want to be thread safe. All these classes contain lists or arrays of some kind. I'm using protected sections to make them thread safe. The question then is: how do you in a nice safe way pick values out of the list? The easiest way is to have a pair of Lock, Unlock functions, but this also presents a lot of ways of doing a misstake. Say the list class has 5 functions, one to get the...
0
9554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9377
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10136
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3913
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.