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) && 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 | void ObservedValuesContainer<T,id>::removeObservedValues(const id _id)
|
---|
116 | {
|
---|
117 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
118 | LOG(3, "DEBUG: ObservedValuesContainer removes " << NameOfType << " " << _id);
|
---|
119 | // call callback function
|
---|
120 | onDestroy(_id);
|
---|
121 | typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
|
---|
122 | iter->second.eraseCurrentValue();
|
---|
123 | if (iter->second.empty())
|
---|
124 | ObservedValues.erase(iter);
|
---|
125 | MarkedForErase.erase(_id);
|
---|
126 | }
|
---|
127 |
|
---|
128 | template <class T, typename id>
|
---|
129 | void ObservedValuesContainer<T,id>::eraseObservedValues(const id _id)
|
---|
130 | {
|
---|
131 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
132 | #ifndef NDEBUG
|
---|
133 | std::pair< typename std::set<id>::iterator, bool > inserter =
|
---|
134 | #endif
|
---|
135 | MarkedForErase.insert(_id);
|
---|
136 | ASSERT( inserter.second,
|
---|
137 | "ObservedValuesContainer<T,id>::eraseObservedValues() - received twice for "
|
---|
138 | +NameOfType+" "+toString(_id)+".");
|
---|
139 |
|
---|
140 | checkRemoval(_id);
|
---|
141 | }
|
---|
142 |
|
---|
143 | template <class T, typename id>
|
---|
144 | #ifdef HAVE_INLINE
|
---|
145 | inline
|
---|
146 | #endif
|
---|
147 | bool ObservedValuesContainer<T,id>::checkMarkedForErase(const id _id) const
|
---|
148 | {
|
---|
149 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
150 | return MarkedForErase.count(_id);
|
---|
151 | }
|
---|
152 |
|
---|
153 | template <class T, typename id>
|
---|
154 | void ObservedValuesContainer<T,id>::insert(const id _id, const typename T::ptr &_obsvalues)
|
---|
155 | {
|
---|
156 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
157 | typename ObservedValuesContainer<T,id>::RefCountedObserved::Value_t value(std::make_pair(_obsvalues,0));
|
---|
158 | typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
|
---|
159 | if (iter == ObservedValues.end()) {
|
---|
160 | std::pair<typename CountedObservedValues_t::iterator, bool> inserter =
|
---|
161 | ObservedValues.insert(
|
---|
162 | std::make_pair( _id, RefCountedObserved(value) ) );
|
---|
163 | ASSERT( inserter.second,
|
---|
164 | "ObservedValuesContainer<T,id>::insert() of "+NameOfType
|
---|
165 | +" for "+toString(_id)+", insertion failed though empty?");
|
---|
166 | } else {
|
---|
167 | ASSERT( !iter->second.empty(),
|
---|
168 | "ObservedValuesContainer<T,id>::insert() of "+NameOfType
|
---|
169 | +" for "+toString(_id)+" has an empty list in ObservedValues.");
|
---|
170 | // already an entry present? add to deque
|
---|
171 | iter->second.push_back( value );
|
---|
172 | }
|
---|
173 | _obsvalues->activateObserver();
|
---|
174 | }
|
---|
175 |
|
---|
176 | template <class T, typename id>
|
---|
177 | bool ObservedValuesContainer<T,id>::changeIdentifier(const id _oldid, const id _newid)
|
---|
178 | {
|
---|
179 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
180 | const typename CountedObservedValues_t::iterator Colditer = ObservedValues.find(_oldid);
|
---|
181 | const typename CountedObservedValues_t::iterator Cnewiter = ObservedValues.find(_newid);
|
---|
182 | const typename MarkedForErase_t::iterator Eolditer = MarkedForErase.find(_oldid);
|
---|
183 | const typename MarkedForErase_t::iterator Enewiter = MarkedForErase.find(_newid);
|
---|
184 | bool status = ((Colditer != ObservedValues.end()) && (Cnewiter == ObservedValues.end()));
|
---|
185 | status &= ((Eolditer != MarkedForErase.end()) && (Enewiter == MarkedForErase.end()));
|
---|
186 | // change id here only if still present
|
---|
187 | if (status) {
|
---|
188 | {
|
---|
189 | // TODO: Actually, we need to think whether we do not have to to split the
|
---|
190 | // deque in two parts: the last entry having the newid, while all other
|
---|
191 | // ones retain the old one until they get removed. But we need to check
|
---|
192 | // whether changing ids "there" is possible when the QtObserved... is under
|
---|
193 | // removal.
|
---|
194 | ObservedValuesContainer<T,id>::RefCountedObserved obsvalues = Colditer->second;
|
---|
195 | ObservedValues.erase(Colditer);
|
---|
196 | ObservedValues.insert( std::make_pair(_newid, obsvalues) );
|
---|
197 | }
|
---|
198 | {
|
---|
199 | MarkedForErase.erase(Eolditer);
|
---|
200 | MarkedForErase.insert(_newid);
|
---|
201 | }
|
---|
202 | return true;
|
---|
203 | } else
|
---|
204 | return false;
|
---|
205 | }
|
---|
206 |
|
---|
207 | template <class T, typename id>
|
---|
208 | #ifdef HAVE_INLINE
|
---|
209 | inline
|
---|
210 | #endif
|
---|
211 | bool ObservedValuesContainer<T,id>::isPresent(const id _id) const
|
---|
212 | {
|
---|
213 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
214 | typename CountedObservedValues_t::const_iterator iter = ObservedValues.find(_id);
|
---|
215 | return (iter != ObservedValues.end());
|
---|
216 | }
|
---|
217 |
|
---|
218 | #endif /* OBSERVEDVALUESCONTAINER_IMPL_HPP_ */
|
---|