00001 #ifndef DeepCopyPointerByClone_H 00002 #define DeepCopyPointerByClone_H 00003 00004 #include<algorithm> 00005 00010 template <class T> 00011 class DeepCopyPointerByClone { 00012 public: 00013 00014 ~DeepCopyPointerByClone() { delete theData;} 00015 DeepCopyPointerByClone() : theData(0) {} 00016 00017 DeepCopyPointerByClone( T* t) : theData(t) {} 00018 00019 DeepCopyPointerByClone( const DeepCopyPointerByClone& other) { 00020 if (other.theData) theData = other->clone(); else theData = 0; 00021 } 00022 00023 00024 DeepCopyPointerByClone& operator=( const DeepCopyPointerByClone& other) { 00025 if ( theData != other.theData) { 00026 delete theData; 00027 if (other.theData) theData = other->clone(); else theData = 0; 00028 } 00029 return *this; 00030 } 00031 00032 #if defined( __GXX_EXPERIMENTAL_CXX0X__) 00033 // straight from https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2027.html 00034 00035 DeepCopyPointerByClone( DeepCopyPointerByClone&& other) : theData(other.theData) { 00036 other.theData=0; 00037 } 00038 00039 DeepCopyPointerByClone& operator=( DeepCopyPointerByClone&& other) { 00040 std::swap(theData,other.theData); 00041 return *this; 00042 } 00043 #endif 00044 00045 00046 T& operator*() { return *theData;} 00047 const T& operator*() const { return *theData;} 00048 00049 T* operator->() { return theData;} 00050 const T* operator->() const { return theData;} 00051 00053 operator bool() const { return theData != 0;} 00054 00056 bool operator==( const T* otherP) const { return theData == otherP;} 00057 00058 private: 00059 T* theData; 00060 }; 00061 00062 #endif // DeepCopyPointerByClone_H