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 | iter->second->noteBoardIsGone();
|
---|
40 | }
|
---|
41 |
|
---|
42 | template <class T, typename id>
|
---|
43 | #ifdef HAVE_INLINE
|
---|
44 | inline
|
---|
45 | #endif
|
---|
46 | void ObservedValuesContainer<T,id>::checkRemoval(const id _id)
|
---|
47 | {
|
---|
48 | if (checkMarkedForConnected(_id) && checkMarkedForErase(_id))
|
---|
49 | removeObservedValues(_id);
|
---|
50 | }
|
---|
51 |
|
---|
52 | template <class T, typename id>
|
---|
53 | typename T::ptr ObservedValuesContainer<T,id>::get(const id _id)
|
---|
54 | {
|
---|
55 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
56 | LOG(3, "DEBUG: ObservedValuesContainer got get() for an observed value of "
|
---|
57 | << NameOfType << " " << _id);
|
---|
58 | typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
|
---|
59 | if (iter == ObservedValues.end())
|
---|
60 | return typename T::ptr();
|
---|
61 | else
|
---|
62 | return iter->second;
|
---|
63 | }
|
---|
64 |
|
---|
65 | template <class T, typename id>
|
---|
66 | void ObservedValuesContainer<T,id>::markObservedValuesAsConnected(
|
---|
67 | const id _id)
|
---|
68 | {
|
---|
69 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
70 | LOG(3, "DEBUG: ObservedValuesContainer got markObservedValuesAsConnected() for an observed value of "
|
---|
71 | << NameOfType << " " << _id);
|
---|
72 | ASSERT (MarkedForConnected.find(_id) == MarkedForConnected.end(),
|
---|
73 | "ObservedValuesContainer<T,id>::markObservedValuesAsConnected() - Observed value of "
|
---|
74 | +NameOfType+" "+toString(_id)+" is already marked as connected.");
|
---|
75 | MarkedForConnected.insert( _id );
|
---|
76 | }
|
---|
77 |
|
---|
78 | template <class T, typename id>
|
---|
79 | bool ObservedValuesContainer<T,id>::checkMarkedForConnected(
|
---|
80 | const id _id) const
|
---|
81 | {
|
---|
82 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
83 | typename MarkedSet_t::const_iterator const iter = MarkedForConnected.find(_id);
|
---|
84 | return iter == MarkedForConnected.end();
|
---|
85 | }
|
---|
86 |
|
---|
87 | template <class T, typename id>
|
---|
88 | void ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected(
|
---|
89 | const id _id)
|
---|
90 | {
|
---|
91 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
92 | LOG(3, "DEBUG: ObservedValuesContainer got markObservedValuesAsDisconnected() for an observed value of "
|
---|
93 | << NameOfType << " " << _id);
|
---|
94 | typename MarkedSet_t::iterator const iter = MarkedForConnected.find(_id);
|
---|
95 | ASSERT (iter != MarkedForConnected.end(),
|
---|
96 | "ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected() - Observed value of "
|
---|
97 | +NameOfType+" "+toString(_id)+" is not marked as connected.");
|
---|
98 | MarkedForConnected.erase(iter);
|
---|
99 |
|
---|
100 | checkRemoval(_id);
|
---|
101 | }
|
---|
102 |
|
---|
103 | template <class T, typename id>
|
---|
104 | void ObservedValuesContainer<T,id>::removeObservedValues(const id _id)
|
---|
105 | {
|
---|
106 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
107 | LOG(3, "DEBUG: ObservedValuesContainer removes " << NameOfType << " " << _id);
|
---|
108 | MarkedForConnected.erase(_id);
|
---|
109 | MarkedForErase.erase(_id);
|
---|
110 | ObservedValues.erase(_id);
|
---|
111 | // call callback function
|
---|
112 | onDestroy(_id);
|
---|
113 | }
|
---|
114 |
|
---|
115 | template <class T, typename id>
|
---|
116 | void ObservedValuesContainer<T,id>::eraseObservedValues(const id _id)
|
---|
117 | {
|
---|
118 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
119 | #ifndef NDEBUG
|
---|
120 | std::pair< typename std::set<id>::iterator, bool > inserter =
|
---|
121 | #endif
|
---|
122 | MarkedForErase.insert(_id);
|
---|
123 | ASSERT( inserter.second,
|
---|
124 | "ObservedValuesContainer<T,id>::eraseObservedValues() - received twice for "
|
---|
125 | +NameOfType+" "+toString(_id)+".");
|
---|
126 |
|
---|
127 | checkRemoval(_id);
|
---|
128 | }
|
---|
129 |
|
---|
130 | template <class T, typename id>
|
---|
131 | #ifdef HAVE_INLINE
|
---|
132 | inline
|
---|
133 | #endif
|
---|
134 | bool ObservedValuesContainer<T,id>::checkMarkedForErase(const id _id) const
|
---|
135 | {
|
---|
136 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
137 | typename MarkedSet_t::const_iterator const iter = MarkedForErase.find(_id);
|
---|
138 | return iter != MarkedForErase.end();
|
---|
139 | }
|
---|
140 |
|
---|
141 | template <class T, typename id>
|
---|
142 | void ObservedValuesContainer<T,id>::insert(const id _id, const typename T::ptr &_obsvalues)
|
---|
143 | {
|
---|
144 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
145 | typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
|
---|
146 | ASSERT (iter == ObservedValues.end(),
|
---|
147 | "ObservedValuesContainer<T,id>::insert() of "+NameOfType
|
---|
148 | +" for "+toString(_id)+" failed because "+toString(_id)+" already present.");
|
---|
149 | std::pair<typename CountedObservedValues_t::iterator, bool> inserter =
|
---|
150 | ObservedValues.insert(
|
---|
151 | std::make_pair( _id, _obsvalues ) );
|
---|
152 | ASSERT( inserter.second,
|
---|
153 | "ObservedValuesContainer<T,id>::insert() of "+NameOfType
|
---|
154 | +" for "+toString(_id)+", insertion failed though empty?");
|
---|
155 |
|
---|
156 | _obsvalues->activateObserver();
|
---|
157 | }
|
---|
158 |
|
---|
159 | template <class T, typename id>
|
---|
160 | bool ObservedValuesContainer<T,id>::changeIdentifier(const id _oldid, const id _newid)
|
---|
161 | {
|
---|
162 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
163 | const typename CountedObservedValues_t::iterator Colditer = ObservedValues.find(_oldid);
|
---|
164 | const typename CountedObservedValues_t::iterator Cnewiter = ObservedValues.find(_newid);
|
---|
165 | const typename MarkedSet_t::iterator MEolditer = MarkedForErase.find(_oldid);
|
---|
166 | const typename MarkedSet_t::iterator MEnewiter = MarkedForErase.find(_newid);
|
---|
167 | const typename MarkedSet_t::iterator MColditer = MarkedForConnected.find(_oldid);
|
---|
168 | const typename MarkedSet_t::iterator MCnewiter = MarkedForConnected.find(_newid);
|
---|
169 | bool status = ((Colditer != ObservedValues.end()) && (Cnewiter == ObservedValues.end()));
|
---|
170 | status &= ((MEolditer != MarkedForErase.end()) && (MEnewiter == MarkedForErase.end()));
|
---|
171 | status &= ((MColditer != MarkedForConnected.end()) && (MCnewiter == MarkedForConnected.end()));
|
---|
172 | // change id here only if still present
|
---|
173 | if (status) {
|
---|
174 | {
|
---|
175 | // TODO: Actually, we need to think whether we do not have to to split the
|
---|
176 | // deque in two parts: the last entry having the newid, while all other
|
---|
177 | // ones retain the old one until they get removed. But we need to check
|
---|
178 | // whether changing ids "there" is possible when the QtObserved... is under
|
---|
179 | // removal.
|
---|
180 | typename T::ptr obsvalues = Colditer->second;
|
---|
181 | ObservedValues.erase(Colditer);
|
---|
182 | ObservedValues.insert( std::make_pair(_newid, obsvalues) );
|
---|
183 | }
|
---|
184 | {
|
---|
185 | MarkedForErase.erase(MEolditer);
|
---|
186 | MarkedForErase.insert(_newid);
|
---|
187 | }
|
---|
188 | {
|
---|
189 | MarkedForConnected.erase(MColditer);
|
---|
190 | MarkedForConnected.insert(_newid);
|
---|
191 | }
|
---|
192 | return true;
|
---|
193 | } else
|
---|
194 | return false;
|
---|
195 | }
|
---|
196 |
|
---|
197 | template <class T, typename id>
|
---|
198 | #ifdef HAVE_INLINE
|
---|
199 | inline
|
---|
200 | #endif
|
---|
201 | bool ObservedValuesContainer<T,id>::isPresent(const id _id) const
|
---|
202 | {
|
---|
203 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
204 | typename CountedObservedValues_t::const_iterator iter = ObservedValues.find(_id);
|
---|
205 | return (iter != ObservedValues.end());
|
---|
206 | }
|
---|
207 |
|
---|
208 | #endif /* OBSERVEDVALUESCONTAINER_IMPL_HPP_ */
|
---|