source: src/UIElements/Qt4/InstanceBoard/ObservedValuesContainer_impl.hpp@ 1c0961

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

QtObserved... now count the subjectKilled() from the containes values.

  • Property mode set to 100644
File size: 7.7 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) && 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>
115void ObservedValuesContainer<T,id>::removeObservedValues(const id _id)
116{
117 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
118 LOG(3, "DEBUG: ObservedValuesContainer removes " << NameOfType << " " << _id);
119 // call callback function
120 onDestroy(_id);
121 typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
122 iter->second.eraseCurrentValue();
123 if (iter->second.empty())
124 ObservedValues.erase(iter);
125 MarkedForErase.erase(_id);
126}
127
128template <class T, typename id>
129void ObservedValuesContainer<T,id>::eraseObservedValues(const id _id)
130{
131 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
132#ifndef NDEBUG
133 std::pair< typename std::set<id>::iterator, bool > inserter =
134#endif
135 MarkedForErase.insert(_id);
136 ASSERT( inserter.second,
137 "ObservedValuesContainer<T,id>::eraseObservedValues() - received twice for "
138 +NameOfType+" "+toString(_id)+".");
139
140 checkRemoval(_id);
141}
142
143template <class T, typename id>
144#ifdef HAVE_INLINE
145inline
146#endif
147bool ObservedValuesContainer<T,id>::checkMarkedForErase(const id _id) const
148{
149 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
150 return MarkedForErase.count(_id);
151}
152
153template <class T, typename id>
154void ObservedValuesContainer<T,id>::insert(const id _id, const typename T::ptr &_obsvalues)
155{
156 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
157 typename ObservedValuesContainer<T,id>::RefCountedObserved::Value_t value(std::make_pair(_obsvalues,0));
158 typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
159 if (iter == ObservedValues.end()) {
160 std::pair<typename CountedObservedValues_t::iterator, bool> inserter =
161 ObservedValues.insert(
162 std::make_pair( _id, RefCountedObserved(value) ) );
163 ASSERT( inserter.second,
164 "ObservedValuesContainer<T,id>::insert() of "+NameOfType
165 +" for "+toString(_id)+", insertion failed though empty?");
166 } else {
167 ASSERT( !iter->second.empty(),
168 "ObservedValuesContainer<T,id>::insert() of "+NameOfType
169 +" for "+toString(_id)+" has an empty list in ObservedValues.");
170 // already an entry present? add to deque
171 iter->second.push_back( value );
172 }
173 _obsvalues->activateObserver();
174}
175
176template <class T, typename id>
177bool ObservedValuesContainer<T,id>::changeIdentifier(const id _oldid, const id _newid)
178{
179 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
180 const typename CountedObservedValues_t::iterator Colditer = ObservedValues.find(_oldid);
181 const typename CountedObservedValues_t::iterator Cnewiter = ObservedValues.find(_newid);
182 const typename MarkedForErase_t::iterator Eolditer = MarkedForErase.find(_oldid);
183 const typename MarkedForErase_t::iterator Enewiter = MarkedForErase.find(_newid);
184 bool status = ((Colditer != ObservedValues.end()) && (Cnewiter == ObservedValues.end()));
185 status &= ((Eolditer != MarkedForErase.end()) && (Enewiter == MarkedForErase.end()));
186 // change id here only if still present
187 if (status) {
188 {
189 // TODO: Actually, we need to think whether we do not have to to split the
190 // deque in two parts: the last entry having the newid, while all other
191 // ones retain the old one until they get removed. But we need to check
192 // whether changing ids "there" is possible when the QtObserved... is under
193 // removal.
194 ObservedValuesContainer<T,id>::RefCountedObserved obsvalues = Colditer->second;
195 ObservedValues.erase(Colditer);
196 ObservedValues.insert( std::make_pair(_newid, obsvalues) );
197 }
198 {
199 MarkedForErase.erase(Eolditer);
200 MarkedForErase.insert(_newid);
201 }
202 return true;
203 } else
204 return false;
205}
206
207template <class T, typename id>
208#ifdef HAVE_INLINE
209inline
210#endif
211bool ObservedValuesContainer<T,id>::isPresent(const id _id) const
212{
213 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
214 typename CountedObservedValues_t::const_iterator iter = ObservedValues.find(_id);
215 return (iter != ObservedValues.end());
216}
217
218#endif /* OBSERVEDVALUESCONTAINER_IMPL_HPP_ */
Note: See TracBrowser for help on using the repository browser.