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
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
[387a84]21#include <boost/thread/recursive_mutex.hpp>
22
[65c323]23template <class T, typename id>
24ObservedValuesContainer<T,id>::ObservedValuesContainer(
25 const std::string _name,
[68418e]26 QtObservedInstanceBoard &_board,
27 const onDestroy_t _onDestroy) :
[65c323]28 NameOfType(_name),
[68418e]29 board(_board),
30 onDestroy(_onDestroy)
[65c323]31{}
32
[04c3a3]33template <class T, typename id>
34ObservedValuesContainer<T,id>::~ObservedValuesContainer()
35{
[387a84]36 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
[04c3a3]37 for (typename CountedObservedValues_t::iterator iter = ObservedValues.begin();
[0af22d]38 iter != ObservedValues.end(); ++iter) {
39 ASSERT( !iter->second.empty(),
40 "~ObservedValuesContainer() of "+NameOfType+" "+toString(iter->first)
41 +" has an empty list in ObservedValues.");
[393342]42 for (typename RefCountedObserved::Values_t::iterator listiter = iter->second.values.begin();
43 listiter != iter->second.values.end(); ++listiter) {
[0af22d]44 listiter->first->noteBoardIsGone();
45 }
46 }
[04c3a3]47}
48
[e7ed12]49template <class T, typename id>
50#ifdef HAVE_INLINE
51inline
52#endif
53void ObservedValuesContainer<T,id>::checkRemoval(const id _id)
54{
[62a0ee]55 if (checkRefCount(_id) && checkMarkedForErase(_id))
[e7ed12]56 removeObservedValues(_id);
57}
58
[41e287]59template <class T, typename id>
60typename T::ptr ObservedValuesContainer<T,id>::get(const id _id)
61{
[387a84]62 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
[6bce88]63 LOG(3, "DEBUG: ObservedValuesContainer got get() for an observed value of "
64 << NameOfType << " " << _id);
[41e287]65 typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
[6bce88]66 if (iter == ObservedValues.end())
67 return typename T::ptr();
68 else
69 return iter->second.getCurrentValue().first;
[65c323]70}
71
[41e287]72template <class T, typename id>
[68418e]73void ObservedValuesContainer<T,id>::markObservedValuesAsConnected(
74 const id _id)
[41e287]75{
[387a84]76 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
[4e6ffe]77 LOG(3, "DEBUG: ObservedValuesContainer got markObservedValuesAsConnected() for an observed value of "
78 << NameOfType << " " << _id);
[393342]79 typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
[68418e]80 ASSERT (iter != ObservedValues.end(),
81 "ObservedValuesContainer<T,id>::markObservedValuesAsConnected() - Observed value of "
82 +NameOfType+" "+toString(_id)+" is not present yet.");
[393342]83 ++(iter->second.getCurrentValue().second);
[41e287]84}
85
86template <class T, typename id>
[68418e]87bool ObservedValuesContainer<T,id>::checkRefCount(
88 const id _id) const
89{
[387a84]90 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
[68418e]91 typename CountedObservedValues_t::const_iterator iter = ObservedValues.find(_id);
[393342]92 return ((iter != ObservedValues.end()) && (iter->second.getEraseCandidate().second == 0));
[68418e]93}
94
95template <class T, typename id>
96void ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected(
97 const id _id)
[41e287]98{
[387a84]99 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
[4e6ffe]100 LOG(3, "DEBUG: ObservedValuesContainer got markObservedValuesAsDisconnected() for an observed value of "
101 << NameOfType << " " << _id);
[41e287]102 typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
[68418e]103 ASSERT (iter != ObservedValues.end(),
104 "ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected() - Observed value of "
105 +NameOfType+" "+toString(_id)+" is not present yet.");
[393342]106 ASSERT (iter->second.getEraseCandidate().second != 0,
[68418e]107 "ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected() - Observed value of "
108 +NameOfType+" "+toString(_id)+" is already signOff() from all.");
[393342]109 --(iter->second.getEraseCandidate().second);
[68418e]110
[e7ed12]111 checkRemoval(_id);
[41e287]112}
113
[68418e]114template <class T, typename id>
115void ObservedValuesContainer<T,id>::removeObservedValues(const id _id)
116{
[387a84]117 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
[68418e]118 LOG(3, "DEBUG: ObservedValuesContainer removes " << NameOfType << " " << _id);
119 // call callback function
120 onDestroy(_id);
[0af22d]121 typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
[393342]122 iter->second.eraseCurrentValue();
123 if (iter->second.empty())
[0af22d]124 ObservedValues.erase(iter);
[4e6ffe]125 MarkedForErase.erase(_id);
126}
127
128template <class T, typename id>
129void ObservedValuesContainer<T,id>::eraseObservedValues(const id _id)
130{
[387a84]131 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
[4e6ffe]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
[e7ed12]140 checkRemoval(_id);
[4e6ffe]141}
142
143template <class T, typename id>
[e7ed12]144#ifdef HAVE_INLINE
145inline
146#endif
[4e6ffe]147bool ObservedValuesContainer<T,id>::checkMarkedForErase(const id _id) const
148{
[387a84]149 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
[4e6ffe]150 return MarkedForErase.count(_id);
[90821d]151}
152
[41e287]153template <class T, typename id>
[0af22d]154void ObservedValuesContainer<T,id>::insert(const id _id, const typename T::ptr &_obsvalues)
[41e287]155{
[387a84]156 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
[393342]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.");
[0af22d]170 // already an entry present? add to deque
[393342]171 iter->second.push_back( value );
[0af22d]172 }
[68418e]173 _obsvalues->activateObserver();
[41e287]174}
175
176template <class T, typename id>
177bool ObservedValuesContainer<T,id>::changeIdentifier(const id _oldid, const id _newid)
178{
[387a84]179 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
[41e287]180 const typename CountedObservedValues_t::iterator Colditer = ObservedValues.find(_oldid);
181 const typename CountedObservedValues_t::iterator Cnewiter = ObservedValues.find(_newid);
[4e6ffe]182 const typename MarkedForErase_t::iterator Eolditer = MarkedForErase.find(_oldid);
183 const typename MarkedForErase_t::iterator Enewiter = MarkedForErase.find(_newid);
[41e287]184 bool status = ((Colditer != ObservedValues.end()) && (Cnewiter == ObservedValues.end()));
[4e6ffe]185 status &= ((Eolditer != MarkedForErase.end()) && (Enewiter == MarkedForErase.end()));
[41e287]186 // change id here only if still present
187 if (status) {
188 {
[0af22d]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.
[393342]194 ObservedValuesContainer<T,id>::RefCountedObserved obsvalues = Colditer->second;
[41e287]195 ObservedValues.erase(Colditer);
196 ObservedValues.insert( std::make_pair(_newid, obsvalues) );
197 }
[4e6ffe]198 {
199 MarkedForErase.erase(Eolditer);
200 MarkedForErase.insert(_newid);
201 }
[41e287]202 return true;
203 } else
204 return false;
205}
206
207template <class T, typename id>
[e7ed12]208#ifdef HAVE_INLINE
209inline
210#endif
[41e287]211bool ObservedValuesContainer<T,id>::isPresent(const id _id) const
212{
[387a84]213 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
[41e287]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.