
raffimoni skrev:
Hello everybody, I did not find any boost library that handle with a situation described below, so that I request for your comments. Is there any interest in such a library?
Seems like a useful tool.
clone_ptr uses the same concept of cloning that ptr_containers do, so new_clone function should be defined for Base class, if it is not and clone_ptr< Base > points to some type that derives from Base, an appropriate assert will fail the same way it does with ptr_container. Defining new_clone for Base makes it cloneable for ptr_pointer and clone_ptr simultaneously.
===================== SAMPLE IMPLEMENTATION =====================
class clone_ptr { private: T* ptr_;
public:
default constructor?
explicit clone_ptr(T* ptr) throw(): ptr_( ptr ) {}
~clone_ptr() { delete ptr_; };
if( ptr_ ) delete_clone(ptr_); (if-statement allows for better optimization)
clone_ptr(const clone_ptr<T>& ptr)
Provide templated version also.
ptr_ = new_clone( *ptr.ptr_ ); //default is defined in ptr_container library
ptr_ = new_clone( ptr.ptr_ ); ?
assert( typeid( *ptr_ ) == typeid( *ptr.ptr_ ) ); }
public: void reset( T* ptr = 0) throw() { if (ptr != ptr_) { delete ptr_; ptr_ = ptr; } }
if( ptr_ ) delete_clone(ptr_); ptr_ = ptr;
T* operator -> () const throw() { return ptr_; }
Please overload on const for a deep-copied object, same for operator()*
};
Of course this implementation is not complete. Notice the way copy constructor is implemented.
===================== REMARKS =====================
clone_ptr should definitely not be used with stl containers (e.g. stl::vector< clone_ptr< Base > >) because of performance, ptr_containers are the best way to achieve such functionality.
making the clone_ptr movable could make vector<clone_ptr<T>> ok. -Thorsten