Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:53:29

0001 #ifndef DeepCopyPointerByClone_H
0002 #define DeepCopyPointerByClone_H
0003 
0004 #include <algorithm>
0005 
0006 /** Same as DeepCopyPointer, except that it copies the object
0007  *  pointed to wsing the clone() virtual copy constructor.
0008  */
0009 
0010 template <class T>
0011 class DeepCopyPointerByClone {
0012 public:
0013   ~DeepCopyPointerByClone() { delete theData; }
0014   DeepCopyPointerByClone() : theData(nullptr) {}
0015 
0016   DeepCopyPointerByClone(T* t) : theData(t) {}
0017 
0018   DeepCopyPointerByClone(const DeepCopyPointerByClone& other) {
0019     if (other.theData)
0020       theData = other->clone();
0021     else
0022       theData = nullptr;
0023   }
0024 
0025   DeepCopyPointerByClone& operator=(const DeepCopyPointerByClone& other) {
0026     if (theData != other.theData) {
0027       delete theData;
0028       if (other.theData)
0029         theData = other->clone();
0030       else
0031         theData = 0;
0032     }
0033     return *this;
0034   }
0035 
0036   // straight from http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2027.html
0037   DeepCopyPointerByClone(DeepCopyPointerByClone&& other) : theData(other.theData) { other.theData = 0; }
0038   DeepCopyPointerByClone& operator=(DeepCopyPointerByClone&& other) {
0039     std::swap(theData, other.theData);
0040     return *this;
0041   }
0042 
0043   T& operator*() { return *theData; }
0044   const T& operator*() const { return *theData; }
0045 
0046   T* operator->() { return theData; }
0047   const T* operator->() const { return theData; }
0048 
0049   /// to allow test like " if (p) {...}"
0050   operator bool() const { return theData != 0; }
0051 
0052   /// to allow test like " if (p == &someT) {...}"
0053   bool operator==(const T* otherP) const { return theData == otherP; }
0054 
0055 private:
0056   T* theData;
0057 };
0058 
0059 #endif  // DeepCopyPointerByClone_H