source: src/Patterns/Cacheable.hpp@ ea7176

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
Last change on this file since ea7176 was ea7176, checked in by Tillmann Crueger <crueger@…>, 15 years ago

FIX: Made AtomCount a Cacheable so that the number of atoms in a molecule will always be correct

All unittests working.
All Complete testcases fail.

  • Property mode set to 100644
File size: 3.0 KB
Line 
1/*
2 * Cacheable.hpp
3 *
4 * Created on: Feb 2, 2010
5 * Author: crueger
6 */
7
8#ifndef CACHEABLE_HPP_
9#define CACHEABLE_HPP_
10
11#include "Patterns/Observer.hpp"
12#include <boost/function.hpp>
13
14#ifndef NO_CACHING
15
16 template<typename T>
17 class Cacheable : public Observer
18 {
19 public:
20 Cacheable(Observable *_owner, boost::function<T()> _recalcMethod);
21 virtual ~Cacheable();
22
23 const bool isValid() const;
24 const T& operator*() const;
25
26 // methods implemented for base-class Observer
27 void update(Observable *subject);
28 void subjectKilled(Observable *subject);
29 private:
30 void checkValid() const;
31
32 mutable T content;
33 Observable *owner;
34 mutable bool valid;
35 mutable bool canBeUsed;
36 boost::function<T()> recalcMethod;
37 };
38
39
40 template<typename T>
41 Cacheable<T>::Cacheable(Observable *_owner, boost::function<T()> _recalcMethod) :
42 owner(_owner),
43 valid(false),
44 canBeUsed(true),
45 recalcMethod(_recalcMethod)
46 {
47 // we sign on with the best(=lowest) priority, so cached values are recalculated before
48 // anybody else might ask for updated values
49 owner->signOn(this,-20);
50 }
51
52 template<typename T>
53 const T& Cacheable<T>::operator*() const{
54 checkValid();
55 return content;
56 }
57
58 template<typename T>
59 Cacheable<T>::~Cacheable()
60 {
61 owner->signOff(this);
62 }
63
64 template<typename T>
65 const bool Cacheable<T>::isValid() const{
66 return valid;
67 }
68
69 template<typename T>
70 void Cacheable<T>::update(Observable *subject) {
71 valid = false;
72 }
73
74 template<typename T>
75 void Cacheable<T>::subjectKilled(Observable *subject) {
76 valid = false;
77 canBeUsed = false;
78 }
79
80 template<typename T>
81 void Cacheable<T>::checkValid() const{
82 assert(canBeUsed && "Cacheable used after owner was deleted");
83 if(!isValid()){
84 content = recalcMethod();
85 }
86 }
87#else
88 template<typename T>
89 class Cacheable : public Observer
90 {
91 public:
92 Cacheable(Observable *_owner, boost::function<T()> _recalcMethod);
93 virtual ~Cacheable();
94
95 const bool isValid() const;
96 const T& operator*() const;
97
98 // methods implemented for base-class Observer
99 void update(Observable *subject);
100 void subjectKilled(Observable *subject);
101 private:
102
103 boost::function<T()> recalcMethod;
104 };
105
106 template<typename T>
107 Cacheable<T>::Cacheable(Observable *_owner, boost::function<T()> _recalcMethod) :
108 recalcMethod(_recalcMethod)
109 {}
110
111 template<typename T>
112 const T& Cacheable<T>::operator*() const{
113 return recalcMethod();
114 }
115
116 template<typename T>
117 Cacheable<T>::~Cacheable()
118 {}
119
120 template<typename T>
121 const bool Cacheable<T>::isValid() const{
122 return true;
123 }
124
125 template<typename T>
126 void Cacheable<T>::update(Observable *subject) {
127 assert(0 && "Cacheable::update should never be called when caching is disabled");
128 }
129
130 template<typename T>
131 void Cacheable<T>::subjectKilled(Observable *subject){
132 assert(0 && "Cacheable::subjectKilled should never be called when caching is disabled");
133 }
134#endif
135
136#endif /* CACHEABLE_HPP_ */
Note: See TracBrowser for help on using the repository browser.