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

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

Removed RefCounted... again.

  • Property mode set to 100644
File size: 7.1 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 iter->second->noteBoardIsGone();
40}
41
42template <class T, typename id>
43#ifdef HAVE_INLINE
44inline
45#endif
46void ObservedValuesContainer<T,id>::checkRemoval(const id _id)
47{
48 if (checkMarkedForConnected(_id) && checkMarkedForErase(_id))
49 removeObservedValues(_id);
50}
51
52template <class T, typename id>
53typename T::ptr ObservedValuesContainer<T,id>::get(const id _id)
54{
55 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
56 LOG(3, "DEBUG: ObservedValuesContainer got get() for an observed value of "
57 << NameOfType << " " << _id);
58 typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
59 if (iter == ObservedValues.end())
60 return typename T::ptr();
61 else
62 return iter->second;
63}
64
65template <class T, typename id>
66void ObservedValuesContainer<T,id>::markObservedValuesAsConnected(
67 const id _id)
68{
69 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
70 LOG(3, "DEBUG: ObservedValuesContainer got markObservedValuesAsConnected() for an observed value of "
71 << NameOfType << " " << _id);
72 ASSERT (MarkedForConnected.find(_id) == MarkedForConnected.end(),
73 "ObservedValuesContainer<T,id>::markObservedValuesAsConnected() - Observed value of "
74 +NameOfType+" "+toString(_id)+" is already marked as connected.");
75 MarkedForConnected.insert( _id );
76}
77
78template <class T, typename id>
79bool ObservedValuesContainer<T,id>::checkMarkedForConnected(
80 const id _id) const
81{
82 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
83 typename MarkedSet_t::const_iterator const iter = MarkedForConnected.find(_id);
84 return iter == MarkedForConnected.end();
85}
86
87template <class T, typename id>
88void ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected(
89 const id _id)
90{
91 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
92 LOG(3, "DEBUG: ObservedValuesContainer got markObservedValuesAsDisconnected() for an observed value of "
93 << NameOfType << " " << _id);
94 typename MarkedSet_t::iterator const iter = MarkedForConnected.find(_id);
95 ASSERT (iter != MarkedForConnected.end(),
96 "ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected() - Observed value of "
97 +NameOfType+" "+toString(_id)+" is not marked as connected.");
98 MarkedForConnected.erase(iter);
99
100 checkRemoval(_id);
101}
102
103template <class T, typename id>
104void ObservedValuesContainer<T,id>::removeObservedValues(const id _id)
105{
106 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
107 LOG(3, "DEBUG: ObservedValuesContainer removes " << NameOfType << " " << _id);
108 MarkedForConnected.erase(_id);
109 MarkedForErase.erase(_id);
110 ObservedValues.erase(_id);
111 // call callback function
112 onDestroy(_id);
113}
114
115template <class T, typename id>
116void ObservedValuesContainer<T,id>::eraseObservedValues(const id _id)
117{
118 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
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
127 checkRemoval(_id);
128}
129
130template <class T, typename id>
131#ifdef HAVE_INLINE
132inline
133#endif
134bool ObservedValuesContainer<T,id>::checkMarkedForErase(const id _id) const
135{
136 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
137 typename MarkedSet_t::const_iterator const iter = MarkedForErase.find(_id);
138 return iter != MarkedForErase.end();
139}
140
141template <class T, typename id>
142void ObservedValuesContainer<T,id>::insert(const id _id, const typename T::ptr &_obsvalues)
143{
144 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
145 typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
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
156 _obsvalues->activateObserver();
157}
158
159template <class T, typename id>
160bool ObservedValuesContainer<T,id>::changeIdentifier(const id _oldid, const id _newid)
161{
162 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
163 const typename CountedObservedValues_t::iterator Colditer = ObservedValues.find(_oldid);
164 const typename CountedObservedValues_t::iterator Cnewiter = ObservedValues.find(_newid);
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);
169 bool status = ((Colditer != ObservedValues.end()) && (Cnewiter == ObservedValues.end()));
170 status &= ((MEolditer != MarkedForErase.end()) && (MEnewiter == MarkedForErase.end()));
171 status &= ((MColditer != MarkedForConnected.end()) && (MCnewiter == MarkedForConnected.end()));
172 // change id here only if still present
173 if (status) {
174 {
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.
180 typename T::ptr obsvalues = Colditer->second;
181 ObservedValues.erase(Colditer);
182 ObservedValues.insert( std::make_pair(_newid, obsvalues) );
183 }
184 {
185 MarkedForErase.erase(MEolditer);
186 MarkedForErase.insert(_newid);
187 }
188 {
189 MarkedForConnected.erase(MColditer);
190 MarkedForConnected.insert(_newid);
191 }
192 return true;
193 } else
194 return false;
195}
196
197template <class T, typename id>
198#ifdef HAVE_INLINE
199inline
200#endif
201bool ObservedValuesContainer<T,id>::isPresent(const id _id) const
202{
203 boost::recursive_mutex::scoped_lock lock(atomic_mutex);
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.