00001 #ifndef clone_ptr_H_HEADER_GUARD_
00002 #define clone_ptr_H_HEADER_GUARD_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include "value_semantic_ptr.h"
00043 #include "pointer_semantic_ptr.h"
00044
00045 typedef void clone_smart_ptr_default_allocate;
00046
00047 template<class T, class ALLOCATE_POLICY = clone_smart_ptr_default_allocate >
00048 class clone_ptr
00049 {
00050 typedef T * ( *clone_fct_Type ) ( T *, bool);
00051 clone_fct_Type m_clone_fct;
00052 T* m_type;
00053 public:
00054
00055 template<typename T_obj>
00056 clone_ptr(T_obj* type):m_type(type), m_clone_fct(get_clone_funct(type, (ALLOCATE_POLICY*)0)){}
00057
00058 ~clone_ptr()throw(){m_type=m_clone_fct(m_type, false);}
00059
00060 clone_ptr(const clone_ptr& Src):m_type(Src.m_clone_fct(Src.m_type, true)), m_clone_fct(Src.m_clone_fct){}
00061
00062 clone_ptr& operator=(const clone_ptr& Src){return assign(Src);}
00063 template<class CompatibleT>
00064 clone_ptr& operator=(const clone_ptr<CompatibleT>& Src){return assign(Src);}
00065 T* operator->() const{return m_type;}
00066 T& operator*() const{return *m_type;}
00067 const T* const c_ptr()const{return m_type;}
00068 const T& c_ref()const{return *m_type;}
00069 T* get_ptr(){return m_type;}
00070
00071 void swap(clone_ptr<T> & other)throw(){std::swap(m_type, other.m_type);std::swap(m_func_ptr_interface, other.m_func_ptr_interface);}
00072 private:
00073 template<class CompatibleSmartPtr>
00074 clone_ptr& assign(CompatibleSmartPtr& Src)
00075 {
00076 if (m_type != Src.m_type)
00077 {
00078 m_clone_fct(m_type, false);
00079 m_type = Src.m_clone_fct(Src.m_type, true);
00080 m_clone_fct = Src.m_clone_fct;
00081 }
00082 return *this;
00083 }
00084 template <typename D>
00085 clone_fct_Type get_clone_funct(D* , clone_smart_ptr_default_allocate*){return construct_and_destruct_default_allocator<T, D>;}
00086 template <typename D, class FUNC_ALLOC_POLICY>
00087 clone_fct_Type get_clone_funct(D* , FUNC_ALLOC_POLICY*){return construct_and_destruct<T,D, FUNC_ALLOC_POLICY>;}
00088
00089 template < typename T, typename DT> static T * construct_and_destruct_default_allocator(T * ptr, bool bConstruct) {
00090 if (bConstruct){return new DT(*static_cast<DT*>(ptr));}
00091 delete ptr;
00092 return NULL;
00093 }
00094 template < typename T, typename D, class FUNC_ALLOC_POLICY> T * construct_and_destruct(T * ptr, bool bConstruct) {
00095 D * Obj = static_cast<D*>( ptr );
00096 if (bConstruct)
00097 {
00098 D* tmp_ptr = NULL;
00099 FUNC_ALLOC_POLICY::allocate(tmp_ptr);
00100 FUNC_ALLOC_POLICY::construct(tmp_ptr, *(Obj));
00101 return tmp_ptr;
00102 }
00103 FUNC_ALLOC_POLICY::destroy(Obj);
00104 FUNC_ALLOC_POLICY::deallocate(Obj);
00105 return NULL;
00106 }
00107 };
00108
00109
00110 #endif
00111