source: src/UIElements/Qt4/InstanceBoard/ObservedValuesContainer_impl.hpp@ 6bce88

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

ObservedValuesContainer now asserts that observedinstance has been requested at least once before deletion.

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