File indexing completed on 2024-10-29 06:08:13
0001 #ifndef DeepCopyPointerByClone_H
0002 #define DeepCopyPointerByClone_H
0003
0004 #include <algorithm>
0005 #include <cassert>
0006
0007
0008
0009
0010
0011 template <class T>
0012 class DeepCopyPointerByClone {
0013 public:
0014 ~DeepCopyPointerByClone() { delete theData; }
0015 DeepCopyPointerByClone() : theData(nullptr) {}
0016
0017 DeepCopyPointerByClone(T* t) : theData(t) {}
0018
0019 DeepCopyPointerByClone(const DeepCopyPointerByClone& other) {
0020 if (other.theData)
0021 theData = other->clone();
0022 else
0023 theData = nullptr;
0024 }
0025
0026 DeepCopyPointerByClone& operator=(const DeepCopyPointerByClone& other) {
0027 if (theData != other.theData) {
0028 delete theData;
0029 if (other.theData)
0030 theData = other->clone();
0031 else
0032 theData = 0;
0033 }
0034 return *this;
0035 }
0036
0037
0038 DeepCopyPointerByClone(DeepCopyPointerByClone&& other) : theData(other.theData) { other.theData = nullptr; }
0039 DeepCopyPointerByClone& operator=(DeepCopyPointerByClone&& other) {
0040 std::swap(theData, other.theData);
0041 return *this;
0042 }
0043
0044 T& operator*() {
0045 assert(theData);
0046 return *theData;
0047 }
0048 const T& operator*() const {
0049 assert(theData);
0050 return *theData;
0051 }
0052
0053 T* operator->() {
0054 assert(theData);
0055 return theData;
0056 }
0057 const T* operator->() const {
0058 assert(theData);
0059 return theData;
0060 }
0061
0062
0063 operator bool() const { return theData != 0; }
0064
0065
0066 bool operator==(const T* otherP) const { return theData == otherP; }
0067
0068 private:
0069 T* theData;
0070 };
0071
0072 #endif