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 | iter->second.first->noteBoardIsGone();
|
---|
37 | }
|
---|
38 |
|
---|
39 | template <class T, typename id>
|
---|
40 | typename T::ptr ObservedValuesContainer<T,id>::get(const id _id)
|
---|
41 | {
|
---|
42 | typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
|
---|
43 | ASSERT( iter != ObservedValues.end(),
|
---|
44 | "ObservedValuesContainer::getObservedValues() - no observed values present for "
|
---|
45 | +NameOfType+" "+toString(_id));
|
---|
46 | const typename T::ptr &obsvalues = iter->second.first;
|
---|
47 |
|
---|
48 | return obsvalues;
|
---|
49 | }
|
---|
50 |
|
---|
51 | template <class T, typename id>
|
---|
52 | void ObservedValuesContainer<T,id>::markObservedValuesAsConnected(
|
---|
53 | const id _id)
|
---|
54 | {
|
---|
55 | LOG(3, "DEBUG: ObservedValuesContainer got markObservedValuesAsConnected() for an observed value of "
|
---|
56 | << NameOfType << " " << _id);
|
---|
57 | typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
|
---|
58 | ASSERT (iter != ObservedValues.end(),
|
---|
59 | "ObservedValuesContainer<T,id>::markObservedValuesAsConnected() - Observed value of "
|
---|
60 | +NameOfType+" "+toString(_id)+" is not present yet.");
|
---|
61 | ++(iter->second.second);
|
---|
62 | }
|
---|
63 |
|
---|
64 | template <class T, typename id>
|
---|
65 | bool ObservedValuesContainer<T,id>::checkRefCount(
|
---|
66 | const id _id) const
|
---|
67 | {
|
---|
68 | typename CountedObservedValues_t::const_iterator iter = ObservedValues.find(_id);
|
---|
69 | return ((iter != ObservedValues.end()) && (iter->second.second == 0));
|
---|
70 | }
|
---|
71 |
|
---|
72 | template <class T, typename id>
|
---|
73 | void ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected(
|
---|
74 | const id _id)
|
---|
75 | {
|
---|
76 | LOG(3, "DEBUG: ObservedValuesContainer got markObservedValuesAsDisconnected() for an observed value of "
|
---|
77 | << NameOfType << " " << _id);
|
---|
78 | typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
|
---|
79 | ASSERT (iter != ObservedValues.end(),
|
---|
80 | "ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected() - Observed value of "
|
---|
81 | +NameOfType+" "+toString(_id)+" is not present yet.");
|
---|
82 | ASSERT (iter->second.second != 0,
|
---|
83 | "ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected() - Observed value of "
|
---|
84 | +NameOfType+" "+toString(_id)+" is already signOff() from all.");
|
---|
85 | --(iter->second.second);
|
---|
86 |
|
---|
87 | if (checkRefCount(_id) && checksubjectKilled(_id) && checkMarkedForErase(_id))
|
---|
88 | removeObservedValues(_id);
|
---|
89 | }
|
---|
90 |
|
---|
91 | template <class T, typename id>
|
---|
92 | bool ObservedValuesContainer<T,id>::checksubjectKilled(
|
---|
93 | const id _id) const
|
---|
94 | {
|
---|
95 | typename subjectKilledCount_t::const_iterator iter = subjectKilledCount.find(_id);
|
---|
96 | return ((iter != subjectKilledCount.end()) && (iter->second == T::MAX_ObservedTypes));
|
---|
97 | }
|
---|
98 |
|
---|
99 | template <class T, typename id>
|
---|
100 | void ObservedValuesContainer<T,id>::countsubjectKilled(const id _id)
|
---|
101 | {
|
---|
102 | LOG(3, "DEBUG: ObservedValuesContainer got subjectKilled() for an observed value of "
|
---|
103 | << NameOfType << " " << _id);
|
---|
104 | typename subjectKilledCount_t::iterator iter = subjectKilledCount.find(_id);
|
---|
105 | if (iter == subjectKilledCount.end()) {
|
---|
106 | std::pair<typename subjectKilledCount_t::iterator, bool> inserter =
|
---|
107 | subjectKilledCount.insert( std::make_pair(_id, 0) );
|
---|
108 | iter = inserter.first;
|
---|
109 | }
|
---|
110 | ASSERT (iter->second < T::MAX_ObservedTypes,
|
---|
111 | "ObservedValuesContainer<T,id>::countsubjectKilled() - all subjectKilled() for "
|
---|
112 | +NameOfType+" "+toString(_id)+" for each observed channel came in already.");
|
---|
113 | ++(iter->second);
|
---|
114 |
|
---|
115 | if (checkRefCount(_id) && checksubjectKilled(_id) && checkMarkedForErase(_id))
|
---|
116 | removeObservedValues(_id);
|
---|
117 | }
|
---|
118 |
|
---|
119 | template <class T, typename id>
|
---|
120 | void ObservedValuesContainer<T,id>::removeObservedValues(const id _id)
|
---|
121 | {
|
---|
122 | LOG(3, "DEBUG: ObservedValuesContainer removes " << NameOfType << " " << _id);
|
---|
123 | // call callback function
|
---|
124 | onDestroy(_id);
|
---|
125 | subjectKilledCount.erase(_id);
|
---|
126 | ObservedValues.erase(_id);
|
---|
127 | MarkedForErase.erase(_id);
|
---|
128 | }
|
---|
129 |
|
---|
130 | template <class T, typename id>
|
---|
131 | void ObservedValuesContainer<T,id>::eraseObservedValues(const id _id)
|
---|
132 | {
|
---|
133 | #ifndef NDEBUG
|
---|
134 | std::pair< typename std::set<id>::iterator, bool > inserter =
|
---|
135 | #endif
|
---|
136 | MarkedForErase.insert(_id);
|
---|
137 | ASSERT( inserter.second,
|
---|
138 | "ObservedValuesContainer<T,id>::eraseObservedValues() - received twice for "
|
---|
139 | +NameOfType+" "+toString(_id)+".");
|
---|
140 |
|
---|
141 | if (checkRefCount(_id) && checksubjectKilled(_id) && checkMarkedForErase(_id))
|
---|
142 | removeObservedValues(_id);
|
---|
143 | }
|
---|
144 |
|
---|
145 | template <class T, typename id>
|
---|
146 | bool ObservedValuesContainer<T,id>::checkMarkedForErase(const id _id) const
|
---|
147 | {
|
---|
148 | return MarkedForErase.count(_id);
|
---|
149 | }
|
---|
150 |
|
---|
151 | template <class T, typename id>
|
---|
152 | bool ObservedValuesContainer<T,id>::insert(const id _id, const typename T::ptr &_obsvalues)
|
---|
153 | {
|
---|
154 | std::pair<typename CountedObservedValues_t::iterator, bool> inserter =
|
---|
155 | ObservedValues.insert(
|
---|
156 | std::make_pair( _id, std::make_pair(_obsvalues,0) ) );
|
---|
157 | _obsvalues->activateObserver();
|
---|
158 | return inserter.second;
|
---|
159 | }
|
---|
160 |
|
---|
161 | template <class T, typename id>
|
---|
162 | bool ObservedValuesContainer<T,id>::changeIdentifier(const id _oldid, const id _newid)
|
---|
163 | {
|
---|
164 | const typename CountedObservedValues_t::iterator Colditer = ObservedValues.find(_oldid);
|
---|
165 | const typename CountedObservedValues_t::iterator Cnewiter = ObservedValues.find(_newid);
|
---|
166 | const typename subjectKilledCount_t::iterator Solditer = subjectKilledCount.find(_oldid);
|
---|
167 | const typename subjectKilledCount_t::iterator Snewiter = subjectKilledCount.find(_newid);
|
---|
168 | const typename MarkedForErase_t::iterator Eolditer = MarkedForErase.find(_oldid);
|
---|
169 | const typename MarkedForErase_t::iterator Enewiter = MarkedForErase.find(_newid);
|
---|
170 | bool status = ((Colditer != ObservedValues.end()) && (Cnewiter == ObservedValues.end()));
|
---|
171 | status &= ((Solditer != subjectKilledCount.end()) && (Snewiter == subjectKilledCount.end()));
|
---|
172 | status &= ((Eolditer != MarkedForErase.end()) && (Enewiter == MarkedForErase.end()));
|
---|
173 | // change id here only if still present
|
---|
174 | if (status) {
|
---|
175 | {
|
---|
176 | RefCountedObservedValues_t obsvalues = Colditer->second;
|
---|
177 | ObservedValues.erase(Colditer);
|
---|
178 | ObservedValues.insert( std::make_pair(_newid, obsvalues) );
|
---|
179 | }
|
---|
180 | {
|
---|
181 | const size_t countvalue = Solditer->second;
|
---|
182 | subjectKilledCount.erase(Solditer);
|
---|
183 | subjectKilledCount.insert( std::make_pair(_newid, countvalue) );
|
---|
184 | }
|
---|
185 | {
|
---|
186 | MarkedForErase.erase(Eolditer);
|
---|
187 | MarkedForErase.insert(_newid);
|
---|
188 | }
|
---|
189 | return true;
|
---|
190 | } else
|
---|
191 | return false;
|
---|
192 | }
|
---|
193 |
|
---|
194 | template <class T, typename id>
|
---|
195 | size_t ObservedValuesContainer<T,id>::getRefCount(const id _id) const
|
---|
196 | {
|
---|
197 | typename CountedObservedValues_t::const_iterator iter = ObservedValues.find(_id);
|
---|
198 | return iter->second.second;
|
---|
199 | }
|
---|
200 |
|
---|
201 | template <class T, typename id>
|
---|
202 | bool ObservedValuesContainer<T,id>::isPresent(const id _id) const
|
---|
203 | {
|
---|
204 | typename CountedObservedValues_t::const_iterator iter = ObservedValues.find(_id);
|
---|
205 | return (iter != ObservedValues.end());
|
---|
206 | }
|
---|
207 |
|
---|
208 | #endif /* OBSERVEDVALUESCONTAINER_IMPL_HPP_ */
|
---|