source: src/UIElements/Qt4/InstanceBoard/ObservedValuesContainer_impl.hpp@ 96e145

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

Removed RefCounted... again.

  • Property mode set to 100644
File size: 7.1 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();
[23221f]38 iter != ObservedValues.end(); ++iter)
39 iter->second->noteBoardIsGone();
[04c3a3]40}
41
[e7ed12]42template <class T, typename id>
43#ifdef HAVE_INLINE
44inline
45#endif
46void ObservedValuesContainer<T,id>::checkRemoval(const id _id)
47{
[23221f]48 if (checkMarkedForConnected(_id) && checkMarkedForErase(_id))
[e7ed12]49 removeObservedValues(_id);
50}
51
[41e287]52template <class T, typename id>
53typename T::ptr ObservedValuesContainer<T,id>::get(const id _id)
54{
[387a84]55 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
[6bce88]56 LOG(3, "DEBUG: ObservedValuesContainer got get() for an observed value of "
57 << NameOfType << " " << _id);
[41e287]58 typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
[6bce88]59 if (iter == ObservedValues.end())
60 return typename T::ptr();
61 else
[23221f]62 return iter->second;
[65c323]63}
64
[41e287]65template <class T, typename id>
[68418e]66void ObservedValuesContainer<T,id>::markObservedValuesAsConnected(
67 const id _id)
[41e287]68{
[387a84]69 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
[4e6ffe]70 LOG(3, "DEBUG: ObservedValuesContainer got markObservedValuesAsConnected() for an observed value of "
71 << NameOfType << " " << _id);
[23221f]72 ASSERT (MarkedForConnected.find(_id) == MarkedForConnected.end(),
[68418e]73 "ObservedValuesContainer<T,id>::markObservedValuesAsConnected() - Observed value of "
[23221f]74 +NameOfType+" "+toString(_id)+" is already marked as connected.");
75 MarkedForConnected.insert( _id );
[41e287]76}
77
78template <class T, typename id>
[23221f]79bool ObservedValuesContainer<T,id>::checkMarkedForConnected(
[68418e]80 const id _id) const
81{
[387a84]82 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
[23221f]83 typename MarkedSet_t::const_iterator const iter = MarkedForConnected.find(_id);
84 return iter == MarkedForConnected.end();
[68418e]85}
86
87template <class T, typename id>
88void ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected(
89 const id _id)
[41e287]90{
[387a84]91 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
[4e6ffe]92 LOG(3, "DEBUG: ObservedValuesContainer got markObservedValuesAsDisconnected() for an observed value of "
93 << NameOfType << " " << _id);
[23221f]94 typename MarkedSet_t::iterator const iter = MarkedForConnected.find(_id);
95 ASSERT (iter != MarkedForConnected.end(),
[68418e]96 "ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected() - Observed value of "
[23221f]97 +NameOfType+" "+toString(_id)+" is not marked as connected.");
98 MarkedForConnected.erase(iter);
[68418e]99
[e7ed12]100 checkRemoval(_id);
[41e287]101}
102
[68418e]103template <class T, typename id>
104void ObservedValuesContainer<T,id>::removeObservedValues(const id _id)
105{
[387a84]106 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
[68418e]107 LOG(3, "DEBUG: ObservedValuesContainer removes " << NameOfType << " " << _id);
[23221f]108 MarkedForConnected.erase(_id);
109 MarkedForErase.erase(_id);
110 ObservedValues.erase(_id);
[68418e]111 // call callback function
112 onDestroy(_id);
[4e6ffe]113}
114
115template <class T, typename id>
116void ObservedValuesContainer<T,id>::eraseObservedValues(const id _id)
117{
[387a84]118 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
[4e6ffe]119#ifndef NDEBUG
120 std::pair< typename std::set<id>::iterator, bool > inserter =
121#endif
122 MarkedForErase.insert(_id);
123 ASSERT( inserter.second,
124 "ObservedValuesContainer<T,id>::eraseObservedValues() - received twice for "
125 +NameOfType+" "+toString(_id)+".");
126
[e7ed12]127 checkRemoval(_id);
[4e6ffe]128}
129
130template <class T, typename id>
[e7ed12]131#ifdef HAVE_INLINE
132inline
133#endif
[4e6ffe]134bool ObservedValuesContainer<T,id>::checkMarkedForErase(const id _id) const
135{
[387a84]136 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
[23221f]137 typename MarkedSet_t::const_iterator const iter = MarkedForErase.find(_id);
138 return iter != MarkedForErase.end();
[90821d]139}
140
[41e287]141template <class T, typename id>
[0af22d]142void ObservedValuesContainer<T,id>::insert(const id _id, const typename T::ptr &_obsvalues)
[41e287]143{
[387a84]144 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
[393342]145 typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
[23221f]146 ASSERT (iter == ObservedValues.end(),
147 "ObservedValuesContainer<T,id>::insert() of "+NameOfType
148 +" for "+toString(_id)+" failed because "+toString(_id)+" already present.");
149 std::pair<typename CountedObservedValues_t::iterator, bool> inserter =
150 ObservedValues.insert(
151 std::make_pair( _id, _obsvalues ) );
152 ASSERT( inserter.second,
153 "ObservedValuesContainer<T,id>::insert() of "+NameOfType
154 +" for "+toString(_id)+", insertion failed though empty?");
155
[68418e]156 _obsvalues->activateObserver();
[41e287]157}
158
159template <class T, typename id>
160bool ObservedValuesContainer<T,id>::changeIdentifier(const id _oldid, const id _newid)
161{
[387a84]162 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
[41e287]163 const typename CountedObservedValues_t::iterator Colditer = ObservedValues.find(_oldid);
164 const typename CountedObservedValues_t::iterator Cnewiter = ObservedValues.find(_newid);
[23221f]165 const typename MarkedSet_t::iterator MEolditer = MarkedForErase.find(_oldid);
166 const typename MarkedSet_t::iterator MEnewiter = MarkedForErase.find(_newid);
167 const typename MarkedSet_t::iterator MColditer = MarkedForConnected.find(_oldid);
168 const typename MarkedSet_t::iterator MCnewiter = MarkedForConnected.find(_newid);
[41e287]169 bool status = ((Colditer != ObservedValues.end()) && (Cnewiter == ObservedValues.end()));
[23221f]170 status &= ((MEolditer != MarkedForErase.end()) && (MEnewiter == MarkedForErase.end()));
171 status &= ((MColditer != MarkedForConnected.end()) && (MCnewiter == MarkedForConnected.end()));
[41e287]172 // change id here only if still present
173 if (status) {
174 {
[0af22d]175 // TODO: Actually, we need to think whether we do not have to to split the
176 // deque in two parts: the last entry having the newid, while all other
177 // ones retain the old one until they get removed. But we need to check
178 // whether changing ids "there" is possible when the QtObserved... is under
179 // removal.
[23221f]180 typename T::ptr obsvalues = Colditer->second;
[41e287]181 ObservedValues.erase(Colditer);
182 ObservedValues.insert( std::make_pair(_newid, obsvalues) );
183 }
[4e6ffe]184 {
[23221f]185 MarkedForErase.erase(MEolditer);
[4e6ffe]186 MarkedForErase.insert(_newid);
187 }
[23221f]188 {
189 MarkedForConnected.erase(MColditer);
190 MarkedForConnected.insert(_newid);
191 }
[41e287]192 return true;
193 } else
194 return false;
195}
196
197template <class T, typename id>
[e7ed12]198#ifdef HAVE_INLINE
199inline
200#endif
[41e287]201bool ObservedValuesContainer<T,id>::isPresent(const id _id) const
202{
[387a84]203 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
[41e287]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.