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 |
|
---|
23 | template <class T, typename id>
|
---|
24 | ObservedValuesContainer<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 |
|
---|
33 | template <class T, typename id>
|
---|
34 | ObservedValuesContainer<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 |
|
---|
49 | template <class T, typename id>
|
---|
50 | #ifdef HAVE_INLINE
|
---|
51 | inline
|
---|
52 | #endif
|
---|
53 | void ObservedValuesContainer<T,id>::checkRemoval(const id _id)
|
---|
54 | {
|
---|
55 | if (checkRefCount(_id) && checksubjectKilled(_id) && checkMarkedForErase(_id))
|
---|
56 | removeObservedValues(_id);
|
---|
57 | }
|
---|
58 |
|
---|
59 | template <class T, typename id>
|
---|
60 | typename 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 |
|
---|
72 | template <class T, typename id>
|
---|
73 | void 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 |
|
---|
86 | template <class T, typename id>
|
---|
87 | bool 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 |
|
---|
95 | template <class T, typename id>
|
---|
96 | void 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 |
|
---|
114 | template <class T, typename id>
|
---|
115 | #ifdef HAVE_INLINE
|
---|
116 | inline
|
---|
117 | #endif
|
---|
118 | bool ObservedValuesContainer<T,id>::checksubjectKilled(
|
---|
119 | const id _id) const
|
---|
120 | {
|
---|
121 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
122 | typename subjectKilledCount_t::const_iterator iter = subjectKilledCount.find(_id);
|
---|
123 | return ((iter != subjectKilledCount.end()) && (iter->second == T::MAX_ObservedTypes));
|
---|
124 | }
|
---|
125 |
|
---|
126 | template <class T, typename id>
|
---|
127 | void ObservedValuesContainer<T,id>::countsubjectKilled(const id _id)
|
---|
128 | {
|
---|
129 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
130 | LOG(3, "DEBUG: ObservedValuesContainer got subjectKilled() for an observed value of "
|
---|
131 | << NameOfType << " " << _id);
|
---|
132 | typename subjectKilledCount_t::iterator iter = subjectKilledCount.find(_id);
|
---|
133 | if (iter == subjectKilledCount.end()) {
|
---|
134 | std::pair<typename subjectKilledCount_t::iterator, bool> inserter =
|
---|
135 | subjectKilledCount.insert( std::make_pair(_id, 0) );
|
---|
136 | iter = inserter.first;
|
---|
137 | }
|
---|
138 | ASSERT (iter->second < T::MAX_ObservedTypes,
|
---|
139 | "ObservedValuesContainer<T,id>::countsubjectKilled() - all subjectKilled() for "
|
---|
140 | +NameOfType+" "+toString(_id)+" for each observed channel came in already.");
|
---|
141 | ++(iter->second);
|
---|
142 |
|
---|
143 | checkRemoval(_id);
|
---|
144 | }
|
---|
145 |
|
---|
146 | template <class T, typename id>
|
---|
147 | void ObservedValuesContainer<T,id>::removeObservedValues(const id _id)
|
---|
148 | {
|
---|
149 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
150 | LOG(3, "DEBUG: ObservedValuesContainer removes " << NameOfType << " " << _id);
|
---|
151 | // call callback function
|
---|
152 | onDestroy(_id);
|
---|
153 | subjectKilledCount.erase(_id);
|
---|
154 | typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
|
---|
155 | iter->second.eraseCurrentValue();
|
---|
156 | if (iter->second.empty())
|
---|
157 | ObservedValues.erase(iter);
|
---|
158 | MarkedForErase.erase(_id);
|
---|
159 | }
|
---|
160 |
|
---|
161 | template <class T, typename id>
|
---|
162 | void ObservedValuesContainer<T,id>::eraseObservedValues(const id _id)
|
---|
163 | {
|
---|
164 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
165 | #ifndef NDEBUG
|
---|
166 | std::pair< typename std::set<id>::iterator, bool > inserter =
|
---|
167 | #endif
|
---|
168 | MarkedForErase.insert(_id);
|
---|
169 | ASSERT( inserter.second,
|
---|
170 | "ObservedValuesContainer<T,id>::eraseObservedValues() - received twice for "
|
---|
171 | +NameOfType+" "+toString(_id)+".");
|
---|
172 |
|
---|
173 | checkRemoval(_id);
|
---|
174 | }
|
---|
175 |
|
---|
176 | template <class T, typename id>
|
---|
177 | #ifdef HAVE_INLINE
|
---|
178 | inline
|
---|
179 | #endif
|
---|
180 | bool ObservedValuesContainer<T,id>::checkMarkedForErase(const id _id) const
|
---|
181 | {
|
---|
182 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
183 | return MarkedForErase.count(_id);
|
---|
184 | }
|
---|
185 |
|
---|
186 | template <class T, typename id>
|
---|
187 | void ObservedValuesContainer<T,id>::insert(const id _id, const typename T::ptr &_obsvalues)
|
---|
188 | {
|
---|
189 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
190 | typename ObservedValuesContainer<T,id>::RefCountedObserved::Value_t value(std::make_pair(_obsvalues,0));
|
---|
191 | typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
|
---|
192 | if (iter == ObservedValues.end()) {
|
---|
193 | std::pair<typename CountedObservedValues_t::iterator, bool> inserter =
|
---|
194 | ObservedValues.insert(
|
---|
195 | std::make_pair( _id, RefCountedObserved(value) ) );
|
---|
196 | ASSERT( inserter.second,
|
---|
197 | "ObservedValuesContainer<T,id>::insert() of "+NameOfType
|
---|
198 | +" for "+toString(_id)+", insertion failed though empty?");
|
---|
199 | } else {
|
---|
200 | ASSERT( !iter->second.empty(),
|
---|
201 | "ObservedValuesContainer<T,id>::insert() of "+NameOfType
|
---|
202 | +" for "+toString(_id)+" has an empty list in ObservedValues.");
|
---|
203 | // already an entry present? add to deque
|
---|
204 | iter->second.push_back( value );
|
---|
205 | }
|
---|
206 | _obsvalues->activateObserver();
|
---|
207 | }
|
---|
208 |
|
---|
209 | template <class T, typename id>
|
---|
210 | bool ObservedValuesContainer<T,id>::changeIdentifier(const id _oldid, const id _newid)
|
---|
211 | {
|
---|
212 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
213 | const typename CountedObservedValues_t::iterator Colditer = ObservedValues.find(_oldid);
|
---|
214 | const typename CountedObservedValues_t::iterator Cnewiter = ObservedValues.find(_newid);
|
---|
215 | const typename subjectKilledCount_t::iterator Solditer = subjectKilledCount.find(_oldid);
|
---|
216 | const typename subjectKilledCount_t::iterator Snewiter = subjectKilledCount.find(_newid);
|
---|
217 | const typename MarkedForErase_t::iterator Eolditer = MarkedForErase.find(_oldid);
|
---|
218 | const typename MarkedForErase_t::iterator Enewiter = MarkedForErase.find(_newid);
|
---|
219 | bool status = ((Colditer != ObservedValues.end()) && (Cnewiter == ObservedValues.end()));
|
---|
220 | status &= ((Solditer != subjectKilledCount.end()) && (Snewiter == subjectKilledCount.end()));
|
---|
221 | status &= ((Eolditer != MarkedForErase.end()) && (Enewiter == MarkedForErase.end()));
|
---|
222 | // change id here only if still present
|
---|
223 | if (status) {
|
---|
224 | {
|
---|
225 | // TODO: Actually, we need to think whether we do not have to to split the
|
---|
226 | // deque in two parts: the last entry having the newid, while all other
|
---|
227 | // ones retain the old one until they get removed. But we need to check
|
---|
228 | // whether changing ids "there" is possible when the QtObserved... is under
|
---|
229 | // removal.
|
---|
230 | ObservedValuesContainer<T,id>::RefCountedObserved obsvalues = Colditer->second;
|
---|
231 | ObservedValues.erase(Colditer);
|
---|
232 | ObservedValues.insert( std::make_pair(_newid, obsvalues) );
|
---|
233 | }
|
---|
234 | {
|
---|
235 | const size_t countvalue = Solditer->second;
|
---|
236 | subjectKilledCount.erase(Solditer);
|
---|
237 | subjectKilledCount.insert( std::make_pair(_newid, countvalue) );
|
---|
238 | }
|
---|
239 | {
|
---|
240 | MarkedForErase.erase(Eolditer);
|
---|
241 | MarkedForErase.insert(_newid);
|
---|
242 | }
|
---|
243 | return true;
|
---|
244 | } else
|
---|
245 | return false;
|
---|
246 | }
|
---|
247 |
|
---|
248 | template <class T, typename id>
|
---|
249 | #ifdef HAVE_INLINE
|
---|
250 | inline
|
---|
251 | #endif
|
---|
252 | bool ObservedValuesContainer<T,id>::isPresent(const id _id) const
|
---|
253 | {
|
---|
254 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
255 | typename CountedObservedValues_t::const_iterator iter = ObservedValues.find(_id);
|
---|
256 | return (iter != ObservedValues.end());
|
---|
257 | }
|
---|
258 |
|
---|
259 | #endif /* OBSERVEDVALUESCONTAINER_IMPL_HPP_ */
|
---|