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