File indexing completed on 2024-04-06 12:04:13
0001 #ifndef DeepCopyPointerByClone_H
0002 #define DeepCopyPointerByClone_H
0003
0004 #include <algorithm>
0005
0006
0007
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
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
0050 operator bool() const { return theData != 0; }
0051
0052
0053 bool operator==(const T* otherP) const { return theData == otherP; }
0054
0055 private:
0056 T* theData;
0057 };
0058
0059 #endif