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