Changeset ccacba
- Timestamp:
- Apr 24, 2010, 3:27:00 PM (15 years ago)
- Branches:
- Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
- Children:
- 033a05
- Parents:
- 8a34392
- Location:
- src
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Helpers/Assert.hpp
r8a34392 rccacba 263 263 }while(0) 264 264 265 #define ASSERT_NOCATCH(message) \ 266 catch(Assert::AssertionFailure&){throw;}\ 267 catch(...){\ 268 static bool ignore = false; \ 269 _my_assert::check(false,"Exception caught",(message),__FILE__,__LINE__,ignore);\ 270 } 271 265 272 #define ASSERT_DO(action) do{_my_assert::setDefault(action);}while(0) 266 273 #define ASSERT_HOOK(hook) do{_my_assert::addHook(hook);}while(0) -
src/Patterns/Observer.cpp
r8a34392 rccacba 27 27 map<Observable*, int> Observable::depth; //!< Map of Observables to the depth of the DAG of Observers 28 28 map<Observable*,multimap<int,Observer*>*> Observable::callTable; //!< Table for each Observable of all its Observers 29 std::map<Observable*,std::set<Notification*> > Observable::notifications; 29 30 set<Observable*> Observable::busyObservables; //!< Set of Observables that are currently busy notifying their sign-on'ed Observers 30 31 … … 68 69 } 69 70 71 void Observable::enque_notification_internal(Observable *publisher, Notification_ptr notification){ 72 ASSERT(notification->owner==publisher,"Some object tried to send a notification it does not own"); 73 notifications[publisher].insert(notification); 74 } 75 70 76 /** Constructor for Observable Protector. 71 77 * Basically, calls start_observer_internal(). Hence use this class instead of … … 97 103 * and removes from busy list. 98 104 */ 99 void Observable::notifyAll() {105 void Observable::notifyAll() try { 100 106 // we are busy notifying others right now 101 107 // add ourselves to the list of busy subjects to enable circle detection … … 113 119 } 114 120 } 115 // done with notification, we can leave the set of busy subjects 121 122 // send out all notifications that need to be done 123 124 notificationSet currentNotifications = notifications[this]; 125 for(notificationSet::iterator it = currentNotifications.begin(); 126 it != currentNotifications.end();++it){ 127 (*it)->notifyAll(); 128 } 129 130 notifications.erase(this); 131 132 // done with notification, we can leave the set of busy subjects 116 133 busyObservables.erase(this); 117 134 } 135 ASSERT_NOCATCH("Exception thrown from Observer Update") 118 136 119 137 /** Handles passing on updates from sub-Observables. … … 191 209 } 192 210 211 void Observable::signOn(Observer *target, Notification_ptr notification){ 212 ASSERT(notification->owner==this, 213 "Trying to sign on for a notification that is not provided by this object"); 214 215 notification->addObserver(target); 216 } 217 218 void Observable::signOff(Observer *target, Notification_ptr notification){ 219 ASSERT(notification->owner==this, 220 "Trying to sign off from a notification that is not provided by this object"); 221 222 notification->removeObserver(target); 223 } 224 193 225 bool Observable::isBlocked(){ 194 226 return depth.count(this) > 0; … … 233 265 Observer::~Observer() 234 266 {} 267 268 /** 269 * Method for specialized notifications. 270 * Most Observers wont need or use this, so it is implemented 271 * empty in the base case; 272 */ 273 void Observer::recieveNotification(Observable *publisher, Notification_ptr notification){ 274 } 275 276 Notification::Notification(Observable *_owner) : 277 owner(_owner) 278 {} 279 280 Notification::~Notification(){} 281 282 void Notification::addObserver(Observer *target){ 283 targets.insert(target); 284 } 285 286 void Notification::removeObserver(Observer *target){ 287 targets.erase(target); 288 } 289 290 void Notification::notifyAll(){ 291 for(std::set<Observer*>::iterator it=targets.begin(); 292 it!=targets.end();++it){ 293 (*it)->recieveNotification(owner,this); 294 } 295 } -
src/Patterns/Observer.hpp
r8a34392 rccacba 29 29 30 30 class Observable; 31 class Notification; 32 33 // Pointers to notifications are used for unique identification 34 // using this typedef makes it hard for others to mess up this 35 // identification process 36 typedef Notification *const Notification_ptr; 31 37 32 38 /** … … 46 52 { 47 53 friend class Observable; 54 friend class Notification; 48 55 public: 49 56 Observer(); … … 55 62 */ 56 63 virtual void update(Observable *publisher)=0; 64 65 /** 66 * This method is called when a special named change 67 * of the Observable occured 68 */ 69 virtual void recieveNotification(Observable *publisher, Notification_ptr notification); 57 70 58 71 /** … … 96 109 97 110 /** 111 * Sign on for specialized notifications 112 */ 113 virtual void signOn(Observer *target, Notification_ptr notification); 114 115 /** 116 * Stop receiving a specialized notification 117 */ 118 virtual void signOff(Observer *target, Notification_ptr notification); 119 120 /** 98 121 * Ask an Observer if it is currently in a blocked state, i.e. if 99 122 * Changes are in Progress, that are not yet published. … … 119 142 static void finish_observer_internal(Observable *publisher); 120 143 144 static void enque_notification_internal(Observable *publisher, Notification_ptr notification); 145 121 146 private: 122 147 typedef std::multimap<int,Observer*> callees_t; 148 typedef std::set<Notification*> notificationSet; 123 149 static std::map<Observable*, int> depth; 124 150 static std::map<Observable*,callees_t*> callTable; 151 static std::map<Observable*,notificationSet> notifications; 125 152 static std::set<Observable*> busyObservables; 153 126 154 127 155 //! @cond … … 143 171 }; 144 172 173 class Notification { 174 friend class Observable; 175 public: 176 Notification(Observable *_owner); 177 virtual ~Notification(); 178 protected: 179 180 void addObserver(Observer *target); 181 void removeObserver(Observer *target); 182 183 void notifyAll(); 184 private: 185 Observable * const owner; 186 std::set<Observer*> targets; 187 }; 188 145 189 // extra macro is necessary to work with __LINE__ 146 190 #define PASTE(a,b) PASTE_HELPER(a,b) 147 191 #define PASTE_HELPER(a,b) a ## b 148 192 #define OBSERVE Observable::_Observable_protector PASTE(_scope_obs_protector_,__LINE__)(this) 149 // deprecated macros from before RAII was used 150 //#define START_OBSERVER Observable::start_observer_internal(this);do{do{}while(0) 151 //#define FINISH_OBSERVER }while(0);Observable::finish_observer_internal(this) 152 //#define RETURN_OBSERVER( retval ) do{Observable::finish_observer_internal(this); return (retval);}while(0) 193 #define NOTIFY(notification) do{Observable::enque_notification_internal(this,notification);}while(0) 194 153 195 #endif /* OBSERVER_HPP_ */ -
src/unittests/ObserverTest.cpp
r8a34392 rccacba 116 116 } 117 117 SimpleObservable *subObservable; 118 }; 119 120 class NotificationObservable : public Observable { 121 public: 122 NotificationObservable() : 123 notification1(new Notification(this)), 124 notification2(new Notification(this)) 125 {} 126 127 ~NotificationObservable(){ 128 delete notification1; 129 delete notification2; 130 } 131 132 void operation1(){ 133 OBSERVE; 134 NOTIFY(notification1); 135 } 136 137 void operation2(){ 138 OBSERVE; 139 NOTIFY(notification2); 140 } 141 142 Notification_ptr notification1; 143 Notification_ptr notification2; 144 }; 145 146 class NotificationObserver : public Observer { 147 public: 148 NotificationObserver(Notification_ptr notification) : 149 requestedNotification(notification), 150 wasNotified(false) 151 {} 152 153 void update(Observable*){} 154 void subjectKilled(Observable*){} 155 void recieveNotification(Observable *publisher, Notification_ptr notification){ 156 ASSERT(requestedNotification==notification,"Notification recieved that was not requested"); 157 wasNotified = true; 158 } 159 160 Notification_ptr requestedNotification; 161 162 bool wasNotified; 118 163 }; 119 164 … … 127 172 superObservable = new SuperObservable(); 128 173 blockObservable = new BlockObservable(); 174 notificationObservable = new NotificationObservable(); 129 175 130 176 observer1 = new UpdateCountObserver(); … … 132 178 observer3 = new UpdateCountObserver(); 133 179 observer4 = new UpdateCountObserver(); 180 181 notificationObserver1 = new NotificationObserver(notificationObservable->notification1); 182 notificationObserver2 = new NotificationObserver(notificationObservable->notification2); 183 134 184 } 135 185 … … 139 189 delete callObservable; 140 190 delete superObservable; 191 delete notificationObservable; 141 192 142 193 delete observer1; … … 144 195 delete observer3; 145 196 delete observer4; 197 delete notificationObserver1; 198 delete notificationObserver2; 146 199 } 147 200 … … 198 251 CPPUNIT_ASSERT_EQUAL( 2, observer1->updates ); 199 252 CPPUNIT_ASSERT_EQUAL( 2, observer2->updates ); 253 } 254 255 void ObserverTest::doesNotifyTest(){ 256 notificationObservable->signOn(notificationObserver1, 257 notificationObservable->notification1); 258 notificationObservable->signOn(notificationObserver2, 259 notificationObservable->notification2); 260 261 notificationObservable->operation1(); 262 CPPUNIT_ASSERT(notificationObserver1->wasNotified); 263 CPPUNIT_ASSERT(!notificationObserver2->wasNotified); 264 265 notificationObserver1->wasNotified=false; 266 267 notificationObservable->operation2(); 268 CPPUNIT_ASSERT(!notificationObserver1->wasNotified); 269 CPPUNIT_ASSERT(notificationObserver2->wasNotified); 270 200 271 } 201 272 -
src/unittests/ObserverTest.hpp
r8a34392 rccacba 13 13 // forward declaration for test stubs 14 14 class UpdateCountObserver; 15 class NotificationObserver; 15 16 class SimpleObservable; 16 17 class CallObservable; 17 18 class SuperObservable; 18 19 class BlockObservable; 20 class NotificationObservable; 19 21 20 22 … … 25 27 CPPUNIT_TEST ( doesBlockUpdateTest ); 26 28 CPPUNIT_TEST ( doesSubObservableTest ); 29 CPPUNIT_TEST ( doesNotifyTest ); 27 30 CPPUNIT_TEST ( doesReportTest ); 28 31 CPPUNIT_TEST ( CircleDetectionTest ); … … 36 39 void doesBlockUpdateTest(); 37 40 void doesSubObservableTest(); 41 void doesNotifyTest(); 38 42 void doesReportTest(); 39 43 void CircleDetectionTest(); … … 45 49 UpdateCountObserver *observer4; 46 50 51 NotificationObserver *notificationObserver1; 52 NotificationObserver *notificationObserver2; 53 47 54 SimpleObservable *simpleObservable1; 48 55 SimpleObservable *simpleObservable2; … … 50 57 BlockObservable *blockObservable; 51 58 SuperObservable *superObservable; 59 NotificationObservable *notificationObservable; 52 60 }; 53 61
Note:
See TracChangeset
for help on using the changeset viewer.