- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Patterns/ObservedIterator.hpp
rf2bb0f rf2cefdb 36 36 37 37 ObservedIterator(_Iter iter,Observable *obs) : 38 iter(iter) 39 { 40 // for this we actually get a lock on the heap, 41 // so we can copy ourselves 42 protector = new Observable::_Observable_protector(obs); 43 } 38 iter(iter), 39 collection(obs), 40 protector(0) 41 {} 44 42 45 43 ObservedIterator(const ObservedIterator &dest) : 46 iter(dest.iter) 47 {48 protector = new Observable::_Observable_protector(*dest.protector);49 }44 iter(dest.iter), 45 collection(dest.collection), 46 protector(dest.copyLock()) 47 {} 50 48 51 49 ~ObservedIterator(){ … … 58 56 if(&dest !=this){ 59 57 // get the new lock first, in case the two locks point to the same observable 60 Observable::_Observable_protector *newLock = new Observable::_Observable_protector(*dest.protector); 61 delete protector; 58 Observable::_Observable_protector *newLock = dest.copyLock(); 59 if(protector) 60 delete protector; 62 61 protector = newLock; 63 62 // After the new lock is aquired we can safely set the iterator 64 63 iter = dest.iter; 64 // we need to know the collection, in case we still have to set the lock 65 collection = dest.collection; 65 66 } 66 67 return *this; … … 83 84 { 84 85 --iter; 86 return *this; 85 87 } 86 88 … … 101 103 102 104 value_type operator*(){ 105 // access is requested... time to get the lock 106 acquireLock(); 103 107 return (*iter); 104 108 } … … 111 115 112 116 private: 117 118 /** 119 * gets the lock for the collection when needed 120 * 121 * The lock is only acquired when the first change is done, so we can be free to do 122 * anything with the iterator before that. I.e. step forward, turn into a const_iterator 123 * etc. 124 */ 125 void acquireLock(){ 126 if(!protector) 127 protector = new Observable::_Observable_protector(collection); 128 } 129 130 Observable::_Observable_protector *copyLock() const{ 131 // we only copy if we actually carry a lock 132 if(protector){ 133 return new Observable::_Observable_protector(*protector); 134 } 135 else{ 136 return 0; 137 } 138 } 139 113 140 _Iter iter; 141 Observable *collection; 114 142 Observable::_Observable_protector *protector; 115 143 };
Note:
See TracChangeset
for help on using the changeset viewer.