Changeset 281 in Sophya for trunk/SophyaLib/BaseTools
- Timestamp:
- Apr 29, 1999, 2:39:34 PM (26 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/BaseTools/rcptr.h
r280 r281 19 19 // the object is destroyed when the last Xp is destroyed. 20 20 21 #include <list>22 23 21 template <class T> 24 22 class RCPtr { … … 26 24 RCPtr(T* obj) { 27 25 x = obj; 26 cnt = new int; 27 *cnt = 1; 28 28 } 29 29 30 30 RCPtr(RCPtr<T> const& other) { 31 peers = other.peers;32 31 x = other.x; 33 peers.push_back((RCPtr<T>*)&other); 34 for (peerlist::iterator i = peers.begin(); 35 i != peers.end(); i++) { 36 (*i)->hello(this); 37 } 32 cnt = other.cnt; 33 (*cnt)++; 38 34 } 39 35 40 36 ~RCPtr() { 41 for (peerlist::iterator i = peers.begin(); 42 i != peers.end(); i++) { 43 (*i)->byebye(this); 37 (*cnt)--; 38 if (!*cnt) { 39 delete x; 40 delete cnt; 44 41 } 45 if (peers.size() == 0) delete x;46 42 } 47 43 … … 50 46 51 47 private: 52 typedef list<RCPtr<T>*> peerlist;53 54 48 T* x; // the object we are referring 55 peerlist peers; // the other smart pointers to the same object 56 57 void hello(RCPtr<T>* newcomer) { 58 peers.push_back(newcomer); 59 } 60 61 void byebye(RCPtr<T>* dying) { 62 peers.remove(dying); 63 } 49 int* cnt; // how many smart pointers ? 64 50 }; 65 51
Note:
See TracChangeset
for help on using the changeset viewer.