Changeset 0c1d97 for src/Patterns/Observer.cpp
- Timestamp:
- Feb 3, 2010, 2:11:10 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:
- 314ff6
- Parents:
- 9ad391
- git-author:
- Tillmann Crueger <crueger@…> (02/03/10 14:07:23)
- git-committer:
- Tillmann Crueger <crueger@…> (02/03/10 14:11:10)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Patterns/Observer.cpp
r9ad391 r0c1d97 10 10 11 11 #include <iostream> 12 #include <cassert> 12 13 13 14 using namespace std; … … 24 25 25 26 map<Observable*, int> Observable::depth; 26 m ultimap<Observable*,Observer*> Observable::callTable;27 map<Observable*,multimap<int,Observer*>*> Observable::callTable; 27 28 set<Observable*> Observable::busyObservables; 28 29 … … 54 55 /************* Notification mechanism for observables **************/ 55 56 56 typedef multimap<Observable*,Observer*>::iterator callIterator;57 typedef pair<callIterator, callIterator> iteratorRange;58 57 59 58 void Observable::notifyAll() { … … 64 63 // and call all observers 65 64 if(callTable.count(this)) { 66 iteratorRange callerRange = callTable.equal_range(this); 67 callIterator iter; 68 for(iter=callerRange.first;iter!=callerRange.second;iter++){ 69 (*iter).second->update(this); 70 } 65 // elements are stored sorted by keys in the multimap 66 // so iterating over it gives us a the callees sorted by 67 // the priorities 68 callees_t *callees = callTable[this]; 69 callees_t::iterator iter; 70 for(iter=callees->begin();iter!=callees->end();iter++){ 71 (*iter).second->update(this); 72 } 71 73 } 72 74 // done with notification, we can leave the set of busy subjects … … 97 99 98 100 // methods to sign-on and off 99 void Observable::signOn(Observer *target) { 101 void Observable::signOn(Observer *target,int priority) { 102 assert(priority>=-20 && priority<=+20 && "Priority out of range [-20:+20]"); 100 103 bool res = false; 101 iteratorRange callerRange = callTable.equal_range(this); 102 callIterator iter; 103 for(iter=callerRange.first;iter!=callerRange.second;iter++){ 104 callees_t *callees = 0; 105 if(callTable.count(this)){ 106 callees = callTable[this]; 107 } 108 else { 109 callees = new multimap<int,Observer*>; 110 callTable.insert(pair<Observable*,callees_t*>(this,callees)); 111 } 112 113 callees_t::iterator iter; 114 for(iter=callees->begin();iter!=callees->end();iter++){ 104 115 res |= ((*iter).second == target); 105 116 } 106 117 if(!res) 107 call Table.insert(pair<Observable*,Observer*>(this,target));118 callees->insert(pair<int,Observer*>(priority,target)); 108 119 } 109 120 110 121 void Observable::signOff(Observer *target) { 111 iteratorRange callerRange = callTable.equal_range(this); 112 callIterator iter; 113 for(iter=callerRange.first;iter!=callerRange.second;iter++) { 122 assert(callTable.count(this) && "SignOff called for an Observable without Observers."); 123 callees_t *callees = callTable[this]; 124 callees_t::iterator iter; 125 for(iter=callees->begin();iter!=callees->end();iter++) { 114 126 if((*iter).second == target) 115 callTable.erase(iter); 127 callees->erase(iter); 128 } 129 if(callees->empty()){ 130 callTable.erase(this); 131 delete callees; 116 132 } 117 133 } … … 127 143 Observable::~Observable() 128 144 { 129 // delete all entries for this observable 130 iteratorRange callerRange = callTable.equal_range(this); 131 callIterator iter; 132 for(iter=callerRange.first;iter!=callerRange.second;iter++){ 133 (*iter).second->subjectKilled(this); 145 if(callTable.count(this)) { 146 // delete all entries for this observable 147 callees_t *callees = callTable[this]; 148 callees_t::iterator iter; 149 for(iter=callees->begin();iter!=callees->end();iter++){ 150 (*iter).second->subjectKilled(this); 151 } 152 callTable.erase(this); 153 delete callees; 134 154 } 135 callTable.erase(callerRange.first,callerRange.second);136 155 } 137 156
Note:
See TracChangeset
for help on using the changeset viewer.