Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Patterns/ObservedIterator.hpp

    rf2cefdb rf2bb0f  
    3636
    3737  ObservedIterator(_Iter iter,Observable *obs) :
    38     iter(iter),
    39     collection(obs),
    40     protector(0)
    41   {}
     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  }
    4244
    4345  ObservedIterator(const ObservedIterator &dest) :
    44     iter(dest.iter),
    45     collection(dest.collection),
    46     protector(dest.copyLock())
    47   {}
     46    iter(dest.iter)
     47  {
     48    protector = new Observable::_Observable_protector(*dest.protector);
     49  }
    4850
    4951  ~ObservedIterator(){
     
    5658    if(&dest !=this){
    5759      // get the new lock first, in case the two locks point to the same observable
    58       Observable::_Observable_protector *newLock = dest.copyLock();
    59       if(protector)
    60         delete protector;
     60      Observable::_Observable_protector *newLock = new Observable::_Observable_protector(*dest.protector);
     61      delete protector;
    6162      protector = newLock;
    6263      // After the new lock is aquired we can safely set the iterator
    6364      iter = dest.iter;
    64       // we need to know the collection, in case we still have to set the lock
    65       collection = dest.collection;
    6665    }
    6766    return *this;
     
    8483  {
    8584    --iter;
    86     return *this;
    8785  }
    8886
     
    103101
    104102  value_type operator*(){
    105     // access is requested... time to get the lock
    106     acquireLock();
    107103    return (*iter);
    108104  }
     
    115111
    116112private:
    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 
    140113  _Iter iter;
    141   Observable *collection;
    142114  Observable::_Observable_protector *protector;
    143115};
Note: See TracChangeset for help on using the changeset viewer.