CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_2_9_HLT1_bphpatch4/src/DataFormats/GeometryCommonDetAlgo/interface/DeepCopyPointer.h

Go to the documentation of this file.
00001 #ifndef DeepCopyPointer_H
00002 #define DeepCopyPointer_H
00003 
00004 
00005 #include<algorithm>
00006 
00015 template <class T>
00016 class DeepCopyPointer {
00017 public:
00018   
00019   ~DeepCopyPointer() { delete theData;}
00020 
00021   DeepCopyPointer() : theData(0) {}
00022 
00023   DeepCopyPointer( T* t) : theData(t) {}
00024 
00025   DeepCopyPointer( const DeepCopyPointer& other) {
00026     if (other.theData) theData = new T( *other); else theData = 0;
00027   }
00028 
00029   DeepCopyPointer& operator=( const DeepCopyPointer& other) {
00030     if ( theData != other.theData) {
00031       delete theData;
00032       if (other.theData) theData = new T( *other); else theData = 0;
00033     }
00034     return *this;
00035   }
00036 
00037 #if defined( __GXX_EXPERIMENTAL_CXX0X__)
00038   // straight from https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2027.html
00039 
00040   DeepCopyPointer( DeepCopyPointer&& other) : theData(other.theData) {
00041     other.theData=0;
00042   }
00043 
00044   DeepCopyPointer& operator=( DeepCopyPointer&& other) {
00045     std::swap(theData,other.theData);
00046     return *this;
00047   }
00048 #endif
00049 
00050 
00053   void replaceWith(T * otherP) {
00054     if ( theData != otherP ) {
00055         delete theData;
00056         theData = otherP;
00057     }
00058   }
00059 
00060   // assume that the replacement object is of the very same class!
00061   // at the moment all the work is done by the client i.e.
00062   // call the distructor
00063   // new in place
00064   // with c++0X a templated method can encasulate it all here... 
00065   T* replaceInplace() {
00066     return theData;
00067   }
00068  
00069 
00070   T&       operator*()       { return *theData;}
00071   const T& operator*() const { return *theData;}
00072 
00073   T*       operator->()       { return theData;}
00074   const T* operator->() const { return theData;}
00075 
00077   operator bool() const { return theData != 0;}
00078 
00080   bool operator==( const T* otherP) const { return theData == otherP;}
00081 
00082 private:
00083   T* theData;
00084 };
00085 
00086 #endif // DeepCopyPointer_H