Ignore:
Timestamp:
Feb 3, 2010, 2:11:10 PM (15 years ago)
Author:
Tillmann Crueger <crueger@…>
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)
Message:

Added mechanism that allows for assign priorities to observers

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Patterns/Observer.cpp

    r9ad391 r0c1d97  
    1010
    1111#include <iostream>
     12#include <cassert>
    1213
    1314using namespace std;
     
    2425
    2526map<Observable*, int> Observable::depth;
    26 multimap<Observable*,Observer*> Observable::callTable;
     27map<Observable*,multimap<int,Observer*>*> Observable::callTable;
    2728set<Observable*> Observable::busyObservables;
    2829
     
    5455/************* Notification mechanism for observables **************/
    5556
    56 typedef multimap<Observable*,Observer*>::iterator callIterator;
    57 typedef pair<callIterator, callIterator> iteratorRange;
    5857
    5958void Observable::notifyAll() {
     
    6463  // and call all observers
    6564  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    }
    7173  }
    7274  // done with notification, we can leave the set of busy subjects
     
    9799
    98100// methods to sign-on and off
    99 void Observable::signOn(Observer *target) {
     101void Observable::signOn(Observer *target,int priority) {
     102  assert(priority>=-20 && priority<=+20 && "Priority out of range [-20:+20]");
    100103  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++){
    104115    res |= ((*iter).second == target);
    105116  }
    106117  if(!res)
    107     callTable.insert(pair<Observable*,Observer*>(this,target));
     118    callees->insert(pair<int,Observer*>(priority,target));
    108119}
    109120
    110121void 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++) {
    114126    if((*iter).second == target)
    115       callTable.erase(iter);
     127      callees->erase(iter);
     128  }
     129  if(callees->empty()){
     130    callTable.erase(this);
     131    delete callees;
    116132  }
    117133}
     
    127143Observable::~Observable()
    128144{
    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;
    134154  }
    135   callTable.erase(callerRange.first,callerRange.second);
    136155}
    137156
Note: See TracChangeset for help on using the changeset viewer.