source: src/UIElements/Qt4/InstanceBoard/ObservedValuesContainer_impl.hpp@ b43585

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 b43585 was 4e6ffe, checked in by Frederik Heber <heber@…>, 9 years ago

FIX: ObservedValue need to be removed with a bit of delay.

  • Property mode set to 100644
File size: 7.3 KB
Line 
1/*
2 * ObservedValuesContainer_impl.hpp
3 *
4 * Created on: Oct 29, 2015
5 * Author: heber
6 */
7
8
9#ifndef OBSERVEDVALUESCONTAINER_IMPL_HPP_
10#define OBSERVEDVALUESCONTAINER_IMPL_HPP_
11
12// include config.h
13#ifdef HAVE_CONFIG_H
14#include <config.h>
15#endif
16
17#include "ObservedValuesContainer.hpp"
18
19#include "CodePatterns/Assert.hpp"
20
21template <class T, typename id>
22ObservedValuesContainer<T,id>::ObservedValuesContainer(
23 const std::string _name,
24 QtObservedInstanceBoard &_board,
25 const onDestroy_t _onDestroy) :
26 NameOfType(_name),
27 board(_board),
28 onDestroy(_onDestroy)
29{}
30
31template <class T, typename id>
32ObservedValuesContainer<T,id>::~ObservedValuesContainer()
33{
34 for (typename CountedObservedValues_t::iterator iter = ObservedValues.begin();
35 iter != ObservedValues.end(); ++iter)
36 iter->second.first->noteBoardIsGone();
37}
38
39template <class T, typename id>
40typename T::ptr ObservedValuesContainer<T,id>::get(const id _id)
41{
42 typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
43 ASSERT( iter != ObservedValues.end(),
44 "ObservedValuesContainer::getObservedValues() - no observed values present for "
45 +NameOfType+" "+toString(_id));
46 const typename T::ptr &obsvalues = iter->second.first;
47
48 return obsvalues;
49}
50
51template <class T, typename id>
52void ObservedValuesContainer<T,id>::markObservedValuesAsConnected(
53 const id _id)
54{
55 LOG(3, "DEBUG: ObservedValuesContainer got markObservedValuesAsConnected() for an observed value of "
56 << NameOfType << " " << _id);
57 typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
58 ASSERT (iter != ObservedValues.end(),
59 "ObservedValuesContainer<T,id>::markObservedValuesAsConnected() - Observed value of "
60 +NameOfType+" "+toString(_id)+" is not present yet.");
61 ++(iter->second.second);
62}
63
64template <class T, typename id>
65bool ObservedValuesContainer<T,id>::checkRefCount(
66 const id _id) const
67{
68 typename CountedObservedValues_t::const_iterator iter = ObservedValues.find(_id);
69 return ((iter != ObservedValues.end()) && (iter->second.second == 0));
70}
71
72template <class T, typename id>
73void ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected(
74 const id _id)
75{
76 LOG(3, "DEBUG: ObservedValuesContainer got markObservedValuesAsDisconnected() for an observed value of "
77 << NameOfType << " " << _id);
78 typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
79 ASSERT (iter != ObservedValues.end(),
80 "ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected() - Observed value of "
81 +NameOfType+" "+toString(_id)+" is not present yet.");
82 ASSERT (iter->second.second != 0,
83 "ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected() - Observed value of "
84 +NameOfType+" "+toString(_id)+" is already signOff() from all.");
85 --(iter->second.second);
86
87 if (checkRefCount(_id) && checksubjectKilled(_id) && checkMarkedForErase(_id))
88 removeObservedValues(_id);
89}
90
91template <class T, typename id>
92bool ObservedValuesContainer<T,id>::checksubjectKilled(
93 const id _id) const
94{
95 typename subjectKilledCount_t::const_iterator iter = subjectKilledCount.find(_id);
96 return ((iter != subjectKilledCount.end()) && (iter->second == T::MAX_ObservedTypes));
97}
98
99template <class T, typename id>
100void ObservedValuesContainer<T,id>::countsubjectKilled(const id _id)
101{
102 LOG(3, "DEBUG: ObservedValuesContainer got subjectKilled() for an observed value of "
103 << NameOfType << " " << _id);
104 typename subjectKilledCount_t::iterator iter = subjectKilledCount.find(_id);
105 if (iter == subjectKilledCount.end()) {
106 std::pair<typename subjectKilledCount_t::iterator, bool> inserter =
107 subjectKilledCount.insert( std::make_pair(_id, 0) );
108 iter = inserter.first;
109 }
110 ASSERT (iter->second < T::MAX_ObservedTypes,
111 "ObservedValuesContainer<T,id>::countsubjectKilled() - all subjectKilled() for "
112 +NameOfType+" "+toString(_id)+" for each observed channel came in already.");
113 ++(iter->second);
114
115 if (checkRefCount(_id) && checksubjectKilled(_id) && checkMarkedForErase(_id))
116 removeObservedValues(_id);
117}
118
119template <class T, typename id>
120void ObservedValuesContainer<T,id>::removeObservedValues(const id _id)
121{
122 LOG(3, "DEBUG: ObservedValuesContainer removes " << NameOfType << " " << _id);
123 // call callback function
124 onDestroy(_id);
125 subjectKilledCount.erase(_id);
126 ObservedValues.erase(_id);
127 MarkedForErase.erase(_id);
128}
129
130template <class T, typename id>
131void ObservedValuesContainer<T,id>::eraseObservedValues(const id _id)
132{
133#ifndef NDEBUG
134 std::pair< typename std::set<id>::iterator, bool > inserter =
135#endif
136 MarkedForErase.insert(_id);
137 ASSERT( inserter.second,
138 "ObservedValuesContainer<T,id>::eraseObservedValues() - received twice for "
139 +NameOfType+" "+toString(_id)+".");
140
141 if (checkRefCount(_id) && checksubjectKilled(_id) && checkMarkedForErase(_id))
142 removeObservedValues(_id);
143}
144
145template <class T, typename id>
146bool ObservedValuesContainer<T,id>::checkMarkedForErase(const id _id) const
147{
148 return MarkedForErase.count(_id);
149}
150
151template <class T, typename id>
152bool ObservedValuesContainer<T,id>::insert(const id _id, const typename T::ptr &_obsvalues)
153{
154 std::pair<typename CountedObservedValues_t::iterator, bool> inserter =
155 ObservedValues.insert(
156 std::make_pair( _id, std::make_pair(_obsvalues,0) ) );
157 _obsvalues->activateObserver();
158 return inserter.second;
159}
160
161template <class T, typename id>
162bool ObservedValuesContainer<T,id>::changeIdentifier(const id _oldid, const id _newid)
163{
164 const typename CountedObservedValues_t::iterator Colditer = ObservedValues.find(_oldid);
165 const typename CountedObservedValues_t::iterator Cnewiter = ObservedValues.find(_newid);
166 const typename subjectKilledCount_t::iterator Solditer = subjectKilledCount.find(_oldid);
167 const typename subjectKilledCount_t::iterator Snewiter = subjectKilledCount.find(_newid);
168 const typename MarkedForErase_t::iterator Eolditer = MarkedForErase.find(_oldid);
169 const typename MarkedForErase_t::iterator Enewiter = MarkedForErase.find(_newid);
170 bool status = ((Colditer != ObservedValues.end()) && (Cnewiter == ObservedValues.end()));
171 status &= ((Solditer != subjectKilledCount.end()) && (Snewiter == subjectKilledCount.end()));
172 status &= ((Eolditer != MarkedForErase.end()) && (Enewiter == MarkedForErase.end()));
173 // change id here only if still present
174 if (status) {
175 {
176 RefCountedObservedValues_t obsvalues = Colditer->second;
177 ObservedValues.erase(Colditer);
178 ObservedValues.insert( std::make_pair(_newid, obsvalues) );
179 }
180 {
181 const size_t countvalue = Solditer->second;
182 subjectKilledCount.erase(Solditer);
183 subjectKilledCount.insert( std::make_pair(_newid, countvalue) );
184 }
185 {
186 MarkedForErase.erase(Eolditer);
187 MarkedForErase.insert(_newid);
188 }
189 return true;
190 } else
191 return false;
192}
193
194template <class T, typename id>
195size_t ObservedValuesContainer<T,id>::getRefCount(const id _id) const
196{
197 typename CountedObservedValues_t::const_iterator iter = ObservedValues.find(_id);
198 return iter->second.second;
199}
200
201template <class T, typename id>
202bool ObservedValuesContainer<T,id>::isPresent(const id _id) const
203{
204 typename CountedObservedValues_t::const_iterator iter = ObservedValues.find(_id);
205 return (iter != ObservedValues.end());
206}
207
208#endif /* OBSERVEDVALUESCONTAINER_IMPL_HPP_ */
Note: See TracBrowser for help on using the repository browser.