Ignore:
Timestamp:
Feb 2, 2010, 4:46:29 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:
0c1d97
Parents:
f721c6
Message:

Added possibility to disable caching mechanism at compile time

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Patterns/Cacheable.hpp

    rf721c6 r9ad391  
    1212#include <boost/function.hpp>
    1313
    14 template<typename T>
    15 class Cacheable : public Observer
    16 {
    17 public:
    18   Cacheable(Observable *_owner, boost::function<T()> _recalcMethod);
    19   virtual ~Cacheable();
     14#ifndef NO_CACHING
    2015
    21   const bool isValid();
    22   const T& operator*();
    23   const bool operator==(const T&);
    24   const bool operator!=(const T&);
     16  template<typename T>
     17  class Cacheable : public Observer
     18  {
     19  public:
     20    Cacheable(Observable *_owner, boost::function<T()> _recalcMethod);
     21    virtual ~Cacheable();
    2522
    26   // methods implemented for base-class Observer
    27   void update(Observable *subject);
    28   void subjectKilled(Observable *subject);
    29 private:
    30   void checkValid();
     23    const bool isValid();
     24    const T& operator*();
     25    const bool operator==(const T&);
     26    const bool operator!=(const T&);
    3127
    32   T content;
    33   Observable *owner;
    34   bool valid;
    35   bool canBeUsed;
    36   boost::function<T()> recalcMethod;
    37 };
     28    // methods implemented for base-class Observer
     29    void update(Observable *subject);
     30    void subjectKilled(Observable *subject);
     31  private:
     32    void checkValid();
    3833
    39 template<typename T>
    40 Cacheable<T>::Cacheable(Observable *_owner, boost::function<T()> _recalcMethod) :
    41   owner(_owner),
    42   recalcMethod(_recalcMethod),
    43   valid(false),
    44   canBeUsed(true)
    45 {
    46   owner->signOn(this);
    47 }
     34    T content;
     35    Observable *owner;
     36    bool valid;
     37    bool canBeUsed;
     38    boost::function<T()> recalcMethod;
     39  };
    4840
    49 template<typename T>
    50 const T& Cacheable<T>::operator*(){
    51   checkValid();
    52   return content;
    53 }
    5441
    55 template<typename T>
    56 const bool Cacheable<T>::operator==(const T& rval){
    57   checkValid();
    58   return (content == rval);
    59 }
     42  template<typename T>
     43  Cacheable<T>::Cacheable(Observable *_owner, boost::function<T()> _recalcMethod) :
     44    owner(_owner),
     45    recalcMethod(_recalcMethod),
     46    valid(false),
     47    canBeUsed(true)
     48  {
     49    owner->signOn(this);
     50  }
    6051
    61 template<typename T>
    62 const bool Cacheable<T>::operator!=(const T& rval){
    63   checkValid();
    64   return (content != rval);
    65 }
     52  template<typename T>
     53  const T& Cacheable<T>::operator*(){
     54    checkValid();
     55    return content;
     56  }
    6657
    67 template<typename T>
    68 Cacheable<T>::~Cacheable()
    69 {
    70   owner->signOff(this);
    71 }
     58  template<typename T>
     59  const bool Cacheable<T>::operator==(const T& rval){
     60    checkValid();
     61    return (content == rval);
     62  }
    7263
    73 template<typename T>
    74 const bool Cacheable<T>::isValid(){
    75   return valid;
    76 }
     64  template<typename T>
     65  const bool Cacheable<T>::operator!=(const T& rval){
     66    checkValid();
     67    return (content != rval);
     68  }
    7769
    78 template<typename T>
    79 void Cacheable<T>::update(Observable *subject) {
    80   valid = false;
    81 }
     70  template<typename T>
     71  Cacheable<T>::~Cacheable()
     72  {
     73    owner->signOff(this);
     74  }
    8275
    83 template<typename T>
    84 void Cacheable<T>::subjectKilled(Observable *subject) {
    85   valid = false;
    86   canBeUsed = false;
    87 }
     76  template<typename T>
     77  const bool Cacheable<T>::isValid(){
     78    return valid;
     79  }
    8880
    89 template<typename T>
    90 void Cacheable<T>::checkValid(){
    91   assert(canBeUsed && "Cacheable used after owner was deleted");
    92   if(!isValid()){
    93     content = recalcMethod();
     81  template<typename T>
     82  void Cacheable<T>::update(Observable *subject) {
     83    valid = false;
    9484  }
    95 }
     85
     86  template<typename T>
     87  void Cacheable<T>::subjectKilled(Observable *subject) {
     88    valid = false;
     89    canBeUsed = false;
     90  }
     91
     92  template<typename T>
     93  void Cacheable<T>::checkValid(){
     94    assert(canBeUsed && "Cacheable used after owner was deleted");
     95    if(!isValid()){
     96      content = recalcMethod();
     97    }
     98  }
     99#else
     100  template<typename T>
     101  class Cacheable : public Observer
     102  {
     103  public:
     104    Cacheable(Observable *_owner, boost::function<T()> _recalcMethod);
     105    virtual ~Cacheable();
     106
     107    const bool isValid();
     108    const T& operator*();
     109    const bool operator==(const T&);
     110    const bool operator!=(const T&);
     111
     112    // methods implemented for base-class Observer
     113    void update(Observable *subject);
     114    void subjectKilled(Observable *subject);
     115  private:
     116
     117    boost::function<T()> recalcMethod;
     118  };
     119
     120  template<typename T>
     121  Cacheable<T>::Cacheable(Observable *_owner, boost::function<T()> _recalcMethod) :
     122    recalcMethod(_recalcMethod)
     123  {}
     124
     125  template<typename T>
     126  const T& Cacheable<T>::operator*(){
     127    return recalcMethod();
     128  }
     129
     130  template<typename T>
     131  const bool Cacheable<T>::operator==(const T& rval){
     132    return (recalcMethod() == rval);
     133  }
     134
     135  template<typename T>
     136  const bool Cacheable<T>::operator!=(const T& rval){
     137    return (recalcMethod() != rval);
     138  }
     139
     140  template<typename T>
     141  Cacheable<T>::~Cacheable()
     142  {}
     143
     144  template<typename T>
     145  const bool Cacheable<T>::isValid(){
     146    return true;
     147  }
     148
     149  template<typename T>
     150  void Cacheable<T>::update(Observable *subject) {
     151    assert(0 && "Cacheable::update should never be called when caching is disabled");
     152  }
     153
     154  template<typename T>
     155  void Cacheable<T>::subjectKilled(Observable *subject) {
     156    assert(0 && "Cacheable::subjectKilled should never be called when caching is disabled");
     157  }
     158#endif
    96159
    97160#endif /* CACHEABLE_HPP_ */
Note: See TracChangeset for help on using the changeset viewer.