Changeset 317df8


Ignore:
Timestamp:
Feb 3, 2010, 4:31:45 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:
2ba827
Parents:
5d1611
Message:

Changed Observer to use RAII-style for locking changes.

Location:
src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/Patterns/Observer.cpp

    r5d1611 r317df8  
    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
  • src/Patterns/Observer.hpp

    r5d1611 r317df8  
    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)
  • src/unittests/ObserverTest.cpp

    r5d1611 r317df8  
    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.