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