Changeset 287 in Sophya
- Timestamp:
- Apr 30, 1999, 8:59:28 PM (26 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/BaseTools/rcptr.h
r282 r287 6 6 // This implements a very simple "smart pointer" with reference counting. 7 7 // Many refinements are possible 8 // - const propagation9 // ...10 8 11 9 // Principle : use RCPtr<T> instead of T* … … 17 15 // can be passed by reference, copied, etc 18 16 // the object is destroyed when the last Xp is destroyed. 17 18 // The class RCCPtr<X> is a const smart pointer, to be used like 19 // a X const *. 20 21 // Eric Aubourg 1999 CEA/DAPNIA/SPP 22 23 24 template <class T> class RCCPtr; 19 25 20 26 template <class T> … … 33 39 } 34 40 35 RCPtr<T>& operator = (RCPtr const& other) {41 RCPtr<T>& operator = (RCPtr<T> const& other) { 36 42 (*cnt)--; 37 43 if (!*cnt) { … … 59 65 T* x; // the object we are referring 60 66 int* cnt; // how many smart pointers ? 67 friend class RCCPtr<T>; 68 }; 69 70 71 // A const smart pointer 72 template <class T> 73 class RCCPtr { 74 public: 75 RCCPtr(T const* obj=0) { 76 x = obj; 77 cnt = new int; 78 *cnt = 1; 79 } 80 81 RCCPtr(RCCPtr<T> const& other) { 82 x = other.x; 83 cnt = other.cnt; 84 (*cnt)++; 85 } 86 87 RCCPtr(RCPtr<T> const& other) { 88 x = other.x; 89 cnt = other.cnt; 90 (*cnt)++; 91 } 92 93 RCCPtr<T>& operator = (RCCPtr<T> const& other) { 94 (*cnt)--; 95 if (!*cnt) { 96 delete (T*) x; 97 delete cnt; 98 } 99 x = other.x; 100 cnt = other.cnt; 101 (*cnt)++; 102 return *this; 103 } 104 105 ~RCCPtr() { 106 (*cnt)--; 107 if (!*cnt) { 108 delete (T*) x; 109 delete cnt; 110 } 111 } 112 113 T const* operator->() {return x;} 114 T const& operator*() {return *x;} 115 116 private: 117 T const * x; // the object we are referring 118 int* cnt; // how many smart pointers ? 61 119 }; 62 120
Note:
See TracChangeset
for help on using the changeset viewer.