Changeset bd58fb


Ignore:
Timestamp:
Mar 4, 2010, 3:20:51 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:
e87acf
Parents:
57adc7
Message:

Added an iterator pattern for observed Data structures

Location:
src
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • src/Patterns/Observer.cpp

    r57adc7 rbd58fb  
    7575Observable::_Observable_protector::_Observable_protector(Observable *_protege) :
    7676  protege(_protege)
     77{
     78  start_observer_internal(protege);
     79}
     80
     81Observable::_Observable_protector::_Observable_protector(const _Observable_protector &dest) :
     82    protege(dest.protege)
    7783{
    7884  start_observer_internal(protege);
  • src/Patterns/Observer.hpp

    r57adc7 rbd58fb  
    2929
    3030class Observable;
     31template<class _Set>
     32class ObservedIterator;
    3133
    3234/**
     
    4648{
    4749  friend class Observable;
     50  template<class> friend class ObservedIterator;
    4851public:
    4952  Observer();
     
    129132  public:
    130133    _Observable_protector(Observable *);
     134    _Observable_protector(const _Observable_protector&);
    131135    ~_Observable_protector();
    132136  private:
  • src/molecule.cpp

    r57adc7 rbd58fb  
    3131 * Initialises molecule list with correctly referenced start and end, and sets molecule::last_atom to zero.
    3232 */
    33 molecule::molecule(const periodentafel * const teil) : elemente(teil), start(World::get()->createAtom()), end(World::get()->createAtom()),
    34   first(new bond(start, end, 1, -1)), last(new bond(start, end, 1, -1)), MDSteps(0), AtomCount(0),
     33molecule::molecule(const periodentafel * const teil) : elemente(teil),start(World::get()->createAtom()),end(World::get()->createAtom()),
     34  first(new bond(0, 0, 1, -1)), last(new bond(0, 0, 1, -1)), MDSteps(0), AtomCount(0),
    3535  BondCount(0), ElementCount(0), NoNonHydrogen(0), NoNonBonds(0), NoCyclicBonds(0), BondDistance(0.),
    3636  ActiveFlag(false), IndexNr(-1),
     
    113113}
    114114
     115/************************** Access to the List of Atoms ****************/
     116
     117
     118molecule::iterator molecule::begin(){
     119  return molecule::iterator(atoms.begin(),this);
     120}
     121
     122molecule::const_iterator molecule::begin() const{
     123  return atoms.begin();
     124}
     125
     126molecule::iterator molecule::ende(){
     127  return molecule::iterator(atoms.end(),this);
     128}
     129
     130molecule::const_iterator molecule::ende() const{
     131  return atoms.end();
     132}
    115133
    116134/** Adds given atom \a *pointer from molecule list.
  • src/molecule.hpp

    r57adc7 rbd58fb  
    3434#include "tesselation.hpp"
    3535#include "Patterns/Observer.hpp"
     36#include "Patterns/ObservedIterator.hpp"
    3637#include "Patterns/Cacheable.hpp"
    3738
     
    8889  friend molecule *NewMolecule();
    8990  friend void DeleteMolecule(molecule *);
     91
    9092  public:
     93
     94    typedef std::set<atom*> atomSet;
     95    typedef ObservedIterator<atomSet> iterator;
     96    typedef atomSet::const_iterator const_iterator;
    9197    double cell_size[6];//!< cell size
    9298    const periodentafel * const elemente; //!< periodic table with each element
     99    // old deprecated atom handling
    93100    atom *start;        //!< start of atom list
    94101    atom *end;          //!< end of atom list
     
    112119    Cacheable<string> formula;
    113120    moleculeId_t id;
     121    atomSet atoms; //<!set of atoms
    114122  protected:
    115123    molecule(const periodentafel * const teil);
     
    125133  const std::string getFormula();
    126134  std::string calcFormula();
     135
     136  iterator begin();
     137  const_iterator begin() const;
     138  iterator ende();
     139  const_iterator ende() const;
    127140
    128141
  • src/unittests/ObserverTest.cpp

    r57adc7 rbd58fb  
    1111#include <cppunit/extensions/TestFactoryRegistry.h>
    1212#include <cppunit/ui/text/TestRunner.h>
     13#include <set>
    1314
    1415#include "Patterns/Observer.hpp"
     16#include "Patterns/ObservedIterator.hpp"
    1517
    1618#include <iostream>
     
    8385};
    8486
     87class ObservableCollection : public Observable {
     88public:
     89  typedef std::set<SimpleObservable*> set;
     90  typedef ObservedIterator<set> iterator;
     91
     92  ObservableCollection(int num){
     93    for(int i=0; i<num; ++i){
     94      SimpleObservable *content = new SimpleObservable();
     95      content->signOn(this);
     96      theSet.insert(content);
     97    }
     98  }
     99
     100  ~ObservableCollection(){
     101    set::iterator iter;
     102    for(iter=theSet.begin(); iter!=theSet.end(); ++iter ){
     103      delete (*iter);
     104    }
     105  }
     106
     107  iterator begin(){
     108    return iterator(theSet.begin(),this);
     109  }
     110
     111  iterator end(){
     112    return iterator(theSet.end(),this);
     113  }
     114
     115private:
     116  set theSet;
     117};
     118
    85119/******************* actuall tests ***************/
    86120
     
    95129  observer3 = new UpdateCountObserver();
    96130  observer4 = new UpdateCountObserver();
     131
     132  collection = new ObservableCollection(5);
    97133}
    98134
     
    107143  delete observer3;
    108144  delete observer4;
     145
     146  delete collection;
    109147}
    110148
     
    163201}
    164202
     203void ObserverTest::iteratorTest(){
     204  collection->signOn(observer1);
     205  {
     206    ObservableCollection::iterator iter;
     207    for(iter=collection->begin(); iter!=collection->end(); ++iter){
     208      (*iter)->changeMethod();
     209    }
     210    // At this point no change should have been propagated
     211    CPPUNIT_ASSERT_EQUAL( 0, observer1->updates);
     212  }
     213  // After the Iterator has died the propagation should take place
     214  CPPUNIT_ASSERT_EQUAL( 1, observer1->updates);
     215  collection->signOff(observer1);
     216}
     217
    165218
    166219void ObserverTest::CircleDetectionTest() {
  • src/unittests/ObserverTest.hpp

    r57adc7 rbd58fb  
    1616class CallObservable;
    1717class SuperObservable;
     18class ObservableCollection;
    1819
    1920
     
    2425  CPPUNIT_TEST ( doesBlockUpdateTest );
    2526  CPPUNIT_TEST ( doesSubObservableTest );
     27  CPPUNIT_TEST ( iteratorTest );
    2628  CPPUNIT_TEST ( CircleDetectionTest );
    2729  CPPUNIT_TEST_SUITE_END();
     
    3436  void doesBlockUpdateTest();
    3537  void doesSubObservableTest();
     38  void iteratorTest();
    3639  void CircleDetectionTest();
    3740
     
    4649  CallObservable *callObservable;
    4750  SuperObservable *superObservable;
     51
     52  ObservableCollection *collection;
    4853};
    4954
Note: See TracChangeset for help on using the changeset viewer.