Changeset bc1fbf for molecuilder/src


Ignore:
Timestamp:
Feb 3, 2010, 4:31:45 PM (16 years ago)
Author:
Tillmann Crueger <crueger@…>
Children:
8501eb
Parents:
2e8296
Message:

Changed Observer to use RAII-style for locking changes.

Location:
molecuilder/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • molecuilder/src/Patterns/Observer.cpp

    r2e8296 rbc1fbf  
    5151    depth.erase(publisher);
    5252  }
     53}
     54
     55Observable::_Observable_protector::_Observable_protector(Observable *_protege) :
     56  protege(_protege)
     57{
     58  start_observer_internal(protege);
     59}
     60
     61Observable::_Observable_protector::~_Observable_protector()
     62{
     63  finish_observer_internal(protege);
    5364}
    5465
  • molecuilder/src/Patterns/Observer.hpp

    r2e8296 rbc1fbf  
    1616 *
    1717 * Observers register themselves with the observables to be notified when something changes.
    18  * In the Observable code that changes attributes should be wrapped in
    19  * START_OBSERVER ... END_OBSERVER blocks. If other methods are called that also contain
    20  * Observation blocks from there, these functions wont trigger notifications. Only the end
    21  * of the final observation block triggers the update of Observers.
    22  *
    23  * Returning from observed code should be done using the RETURN_OBSERVER() macro. This way the Observer mechanism
    24  * is notified that the method is left.
     18 * In the Observable code that changes attributes should be started with OBSERVE;. This macro
     19 * locks the observer mechanism while changes are done. At the end of the scope in which the
     20 * macro was placed the lock is released. When the last lock is released all changes are
     21 * propagated to the observers.
    2522 *
    2623 * Each observerable can have sub-observables. When one of these sub-observables changes and
     
    6259  /**
    6360   * Internal method.
    64    * Do not call directly. Use START_OBSERVER macro instead
     61   * Do not call directly. Use OBSERVE macro instead
    6562   */
    6663  static void start_observer_internal(Observable *publisher);
    6764  /**
    6865   * Internal method.
    69    * Do not call directly. Use FINISH_OBSERVER macro instead
     66   * Do not call directly. Use OBSERVE macro instead
    7067   */
    7168  static void finish_observer_internal(Observable *publisher);
     
    7673  static std::map<Observable*,callees_t*> callTable;
    7774  static std::set<Observable*> busyObservables;
     75
     76  // Structure for RAII-Style notification
     77protected:
     78  class _Observable_protector {
     79  public:
     80    _Observable_protector(Observable *);
     81    ~_Observable_protector();
     82  private:
     83    Observable *protege;
     84  };
    7885};
    7986
    8087
    81 
     88#define OBSERVE Observable::_Observable_protector _scope_obs_protector(this)
    8289#define START_OBSERVER Observable::start_observer_internal(this);do{do{}while(0)
    8390#define FINISH_OBSERVER }while(0);Observable::finish_observer_internal(this)
  • molecuilder/src/unittests/ObserverTest.cpp

    r2e8296 rbc1fbf  
    3939public:
    4040  void changeMethod() {
    41     START_OBSERVER;
    42     int i;
    43     i++;
    44     FINISH_OBSERVER;
     41    OBSERVE;
     42    int i;
     43    i++;
    4544  }
    4645};
     
    4948public:
    5049  void changeMethod1() {
    51     START_OBSERVER;
    52     int i;
    53     i++;
    54     FINISH_OBSERVER;
     50    OBSERVE;
     51    int i;
     52    i++;
    5553  }
    5654
    5755  void changeMethod2() {
    58     START_OBSERVER;
     56    OBSERVE;
    5957    int i;
    6058    i++;
    6159    changeMethod1();
    62     FINISH_OBSERVER;
    6360  }
    6461};
     
    7471  }
    7572  void changeMethod() {
    76     START_OBSERVER;
     73    OBSERVE;
    7774    int i;
    7875    i++;
    7976    subObservable->changeMethod();
    80     FINISH_OBSERVER;
    8177  }
    8278  SimpleObservable *subObservable;
Note: See TracChangeset for help on using the changeset viewer.