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

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

Combined checks into checkRemoval() in ObservedValuesContainer.

  • Property mode set to 100644
File size: 9.4 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
21#include <boost/thread/recursive_mutex.hpp>
22
23template <class T, typename id>
24ObservedValuesContainer<T,id>::ObservedValuesContainer(
25 const std::string _name,
26 QtObservedInstanceBoard &_board,
27 const onDestroy_t _onDestroy) :
28 NameOfType(_name),
29 board(_board),
30 onDestroy(_onDestroy)
31{}
32
33template <class T, typename id>
34ObservedValuesContainer<T,id>::~ObservedValuesContainer()
35{
36 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
37 for (typename CountedObservedValues_t::iterator iter = ObservedValues.begin();
38 iter != ObservedValues.end(); ++iter) {
39 ASSERT( !iter->second.empty(),
40 "~ObservedValuesContainer() of "+NameOfType+" "+toString(iter->first)
41 +" has an empty list in ObservedValues.");
42 for (typename RefCountedObserved::Values_t::iterator listiter = iter->second.values.begin();
43 listiter != iter->second.values.end(); ++listiter) {
44 listiter->first->noteBoardIsGone();
45 }
46 }
47}
48
49template <class T, typename id>
50#ifdef HAVE_INLINE
51inline
52#endif
53void ObservedValuesContainer<T,id>::checkRemoval(const id _id)
54{
55 if (checkRefCount(_id) && checksubjectKilled(_id) && checkMarkedForErase(_id))
56 removeObservedValues(_id);
57}
58
59template <class T, typename id>
60typename T::ptr ObservedValuesContainer<T,id>::get(const id _id)
61{
62 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
63 LOG(3, "DEBUG: ObservedValuesContainer got get() for an observed value of "
64 << NameOfType << " " << _id);
65 typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
66 if (iter == ObservedValues.end())
67 return typename T::ptr();
68 else
69 return iter->second.getCurrentValue().first;
70}
71
72template <class T, typename id>
73void ObservedValuesContainer<T,id>::markObservedValuesAsConnected(
74 const id _id)
75{
76 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
77 LOG(3, "DEBUG: ObservedValuesContainer got markObservedValuesAsConnected() for an observed value of "
78 << NameOfType << " " << _id);
79 typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
80 ASSERT (iter != ObservedValues.end(),
81 "ObservedValuesContainer<T,id>::markObservedValuesAsConnected() - Observed value of "
82 +NameOfType+" "+toString(_id)+" is not present yet.");
83 ++(iter->second.getCurrentValue().second);
84}
85
86template <class T, typename id>
87bool ObservedValuesContainer<T,id>::checkRefCount(
88 const id _id) const
89{
90 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
91 typename CountedObservedValues_t::const_iterator iter = ObservedValues.find(_id);
92 return ((iter != ObservedValues.end()) && (iter->second.getEraseCandidate().second == 0));
93}
94
95template <class T, typename id>
96void ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected(
97 const id _id)
98{
99 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
100 LOG(3, "DEBUG: ObservedValuesContainer got markObservedValuesAsDisconnected() for an observed value of "
101 << NameOfType << " " << _id);
102 typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
103 ASSERT (iter != ObservedValues.end(),
104 "ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected() - Observed value of "
105 +NameOfType+" "+toString(_id)+" is not present yet.");
106 ASSERT (iter->second.getEraseCandidate().second != 0,
107 "ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected() - Observed value of "
108 +NameOfType+" "+toString(_id)+" is already signOff() from all.");
109 --(iter->second.getEraseCandidate().second);
110
111 checkRemoval(_id);
112}
113
114template <class T, typename id>
115#ifdef HAVE_INLINE
116inline
117#endif
118bool ObservedValuesContainer<T,id>::checksubjectKilled(
119 const id _id) const
120{
121 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
122 typename subjectKilledCount_t::const_iterator iter = subjectKilledCount.find(_id);
123 return ((iter != subjectKilledCount.end()) && (iter->second == T::MAX_ObservedTypes));
124}
125
126template <class T, typename id>
127void ObservedValuesContainer<T,id>::countsubjectKilled(const id _id)
128{
129 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
130 LOG(3, "DEBUG: ObservedValuesContainer got subjectKilled() for an observed value of "
131 << NameOfType << " " << _id);
132 typename subjectKilledCount_t::iterator iter = subjectKilledCount.find(_id);
133 if (iter == subjectKilledCount.end()) {
134 std::pair<typename subjectKilledCount_t::iterator, bool> inserter =
135 subjectKilledCount.insert( std::make_pair(_id, 0) );
136 iter = inserter.first;
137 }
138 ASSERT (iter->second < T::MAX_ObservedTypes,
139 "ObservedValuesContainer<T,id>::countsubjectKilled() - all subjectKilled() for "
140 +NameOfType+" "+toString(_id)+" for each observed channel came in already.");
141 ++(iter->second);
142
143 checkRemoval(_id);
144}
145
146template <class T, typename id>
147void ObservedValuesContainer<T,id>::removeObservedValues(const id _id)
148{
149 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
150 LOG(3, "DEBUG: ObservedValuesContainer removes " << NameOfType << " " << _id);
151 // call callback function
152 onDestroy(_id);
153 subjectKilledCount.erase(_id);
154 typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
155 iter->second.eraseCurrentValue();
156 if (iter->second.empty())
157 ObservedValues.erase(iter);
158 MarkedForErase.erase(_id);
159}
160
161template <class T, typename id>
162void ObservedValuesContainer<T,id>::eraseObservedValues(const id _id)
163{
164 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
165#ifndef NDEBUG
166 std::pair< typename std::set<id>::iterator, bool > inserter =
167#endif
168 MarkedForErase.insert(_id);
169 ASSERT( inserter.second,
170 "ObservedValuesContainer<T,id>::eraseObservedValues() - received twice for "
171 +NameOfType+" "+toString(_id)+".");
172
173 checkRemoval(_id);
174}
175
176template <class T, typename id>
177#ifdef HAVE_INLINE
178inline
179#endif
180bool ObservedValuesContainer<T,id>::checkMarkedForErase(const id _id) const
181{
182 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
183 return MarkedForErase.count(_id);
184}
185
186template <class T, typename id>
187void ObservedValuesContainer<T,id>::insert(const id _id, const typename T::ptr &_obsvalues)
188{
189 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
190 typename ObservedValuesContainer<T,id>::RefCountedObserved::Value_t value(std::make_pair(_obsvalues,0));
191 typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
192 if (iter == ObservedValues.end()) {
193 std::pair<typename CountedObservedValues_t::iterator, bool> inserter =
194 ObservedValues.insert(
195 std::make_pair( _id, RefCountedObserved(value) ) );
196 ASSERT( inserter.second,
197 "ObservedValuesContainer<T,id>::insert() of "+NameOfType
198 +" for "+toString(_id)+", insertion failed though empty?");
199 } else {
200 ASSERT( !iter->second.empty(),
201 "ObservedValuesContainer<T,id>::insert() of "+NameOfType
202 +" for "+toString(_id)+" has an empty list in ObservedValues.");
203 // already an entry present? add to deque
204 iter->second.push_back( value );
205 }
206 _obsvalues->activateObserver();
207}
208
209template <class T, typename id>
210bool ObservedValuesContainer<T,id>::changeIdentifier(const id _oldid, const id _newid)
211{
212 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
213 const typename CountedObservedValues_t::iterator Colditer = ObservedValues.find(_oldid);
214 const typename CountedObservedValues_t::iterator Cnewiter = ObservedValues.find(_newid);
215 const typename subjectKilledCount_t::iterator Solditer = subjectKilledCount.find(_oldid);
216 const typename subjectKilledCount_t::iterator Snewiter = subjectKilledCount.find(_newid);
217 const typename MarkedForErase_t::iterator Eolditer = MarkedForErase.find(_oldid);
218 const typename MarkedForErase_t::iterator Enewiter = MarkedForErase.find(_newid);
219 bool status = ((Colditer != ObservedValues.end()) && (Cnewiter == ObservedValues.end()));
220 status &= ((Solditer != subjectKilledCount.end()) && (Snewiter == subjectKilledCount.end()));
221 status &= ((Eolditer != MarkedForErase.end()) && (Enewiter == MarkedForErase.end()));
222 // change id here only if still present
223 if (status) {
224 {
225 // TODO: Actually, we need to think whether we do not have to to split the
226 // deque in two parts: the last entry having the newid, while all other
227 // ones retain the old one until they get removed. But we need to check
228 // whether changing ids "there" is possible when the QtObserved... is under
229 // removal.
230 ObservedValuesContainer<T,id>::RefCountedObserved obsvalues = Colditer->second;
231 ObservedValues.erase(Colditer);
232 ObservedValues.insert( std::make_pair(_newid, obsvalues) );
233 }
234 {
235 const size_t countvalue = Solditer->second;
236 subjectKilledCount.erase(Solditer);
237 subjectKilledCount.insert( std::make_pair(_newid, countvalue) );
238 }
239 {
240 MarkedForErase.erase(Eolditer);
241 MarkedForErase.insert(_newid);
242 }
243 return true;
244 } else
245 return false;
246}
247
248template <class T, typename id>
249#ifdef HAVE_INLINE
250inline
251#endif
252bool ObservedValuesContainer<T,id>::isPresent(const id _id) const
253{
254 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
255 typename CountedObservedValues_t::const_iterator iter = ObservedValues.find(_id);
256 return (iter != ObservedValues.end());
257}
258
259#endif /* OBSERVEDVALUESCONTAINER_IMPL_HPP_ */
Note: See TracBrowser for help on using the repository browser.