Changeset bc1fbf for molecuilder/src
- Timestamp:
- Feb 3, 2010, 4:31:45 PM (16 years ago)
- Children:
- 8501eb
- Parents:
- 2e8296
- Location:
- molecuilder/src
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
molecuilder/src/Patterns/Observer.cpp
r2e8296 rbc1fbf 51 51 depth.erase(publisher); 52 52 } 53 } 54 55 Observable::_Observable_protector::_Observable_protector(Observable *_protege) : 56 protege(_protege) 57 { 58 start_observer_internal(protege); 59 } 60 61 Observable::_Observable_protector::~_Observable_protector() 62 { 63 finish_observer_internal(protege); 53 64 } 54 65 -
molecuilder/src/Patterns/Observer.hpp
r2e8296 rbc1fbf 16 16 * 17 17 * 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. 25 22 * 26 23 * Each observerable can have sub-observables. When one of these sub-observables changes and … … 62 59 /** 63 60 * Internal method. 64 * Do not call directly. Use START_OBSERVERmacro instead61 * Do not call directly. Use OBSERVE macro instead 65 62 */ 66 63 static void start_observer_internal(Observable *publisher); 67 64 /** 68 65 * Internal method. 69 * Do not call directly. Use FINISH_OBSERVERmacro instead66 * Do not call directly. Use OBSERVE macro instead 70 67 */ 71 68 static void finish_observer_internal(Observable *publisher); … … 76 73 static std::map<Observable*,callees_t*> callTable; 77 74 static std::set<Observable*> busyObservables; 75 76 // Structure for RAII-Style notification 77 protected: 78 class _Observable_protector { 79 public: 80 _Observable_protector(Observable *); 81 ~_Observable_protector(); 82 private: 83 Observable *protege; 84 }; 78 85 }; 79 86 80 87 81 88 #define OBSERVE Observable::_Observable_protector _scope_obs_protector(this) 82 89 #define START_OBSERVER Observable::start_observer_internal(this);do{do{}while(0) 83 90 #define FINISH_OBSERVER }while(0);Observable::finish_observer_internal(this) -
molecuilder/src/unittests/ObserverTest.cpp
r2e8296 rbc1fbf 39 39 public: 40 40 void changeMethod() { 41 START_OBSERVER; 42 int i; 43 i++; 44 FINISH_OBSERVER; 41 OBSERVE; 42 int i; 43 i++; 45 44 } 46 45 }; … … 49 48 public: 50 49 void changeMethod1() { 51 START_OBSERVER; 52 int i; 53 i++; 54 FINISH_OBSERVER; 50 OBSERVE; 51 int i; 52 i++; 55 53 } 56 54 57 55 void changeMethod2() { 58 START_OBSERVER;56 OBSERVE; 59 57 int i; 60 58 i++; 61 59 changeMethod1(); 62 FINISH_OBSERVER;63 60 } 64 61 }; … … 74 71 } 75 72 void changeMethod() { 76 START_OBSERVER;73 OBSERVE; 77 74 int i; 78 75 i++; 79 76 subObservable->changeMethod(); 80 FINISH_OBSERVER;81 77 } 82 78 SimpleObservable *subObservable;
Note:
See TracChangeset
for help on using the changeset viewer.