[41e287] | 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 |
|
---|
[387a84] | 21 | #include <boost/thread/recursive_mutex.hpp>
|
---|
| 22 |
|
---|
[65c323] | 23 | template <class T, typename id>
|
---|
| 24 | ObservedValuesContainer<T,id>::ObservedValuesContainer(
|
---|
| 25 | const std::string _name,
|
---|
[68418e] | 26 | QtObservedInstanceBoard &_board,
|
---|
| 27 | const onDestroy_t _onDestroy) :
|
---|
[65c323] | 28 | NameOfType(_name),
|
---|
[68418e] | 29 | board(_board),
|
---|
| 30 | onDestroy(_onDestroy)
|
---|
[65c323] | 31 | {}
|
---|
| 32 |
|
---|
[04c3a3] | 33 | template <class T, typename id>
|
---|
| 34 | ObservedValuesContainer<T,id>::~ObservedValuesContainer()
|
---|
| 35 | {
|
---|
[387a84] | 36 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[04c3a3] | 37 | for (typename CountedObservedValues_t::iterator iter = ObservedValues.begin();
|
---|
[0af22d] | 38 | iter != ObservedValues.end(); ++iter) {
|
---|
| 39 | ASSERT( !iter->second.empty(),
|
---|
| 40 | "~ObservedValuesContainer() of "+NameOfType+" "+toString(iter->first)
|
---|
| 41 | +" has an empty list in ObservedValues.");
|
---|
[393342] | 42 | for (typename RefCountedObserved::Values_t::iterator listiter = iter->second.values.begin();
|
---|
| 43 | listiter != iter->second.values.end(); ++listiter) {
|
---|
[0af22d] | 44 | listiter->first->noteBoardIsGone();
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
[04c3a3] | 47 | }
|
---|
| 48 |
|
---|
[e7ed12] | 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) && checksubjectKilled(_id) && checkMarkedForErase(_id))
|
---|
| 56 | removeObservedValues(_id);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[41e287] | 59 | template <class T, typename id>
|
---|
| 60 | typename T::ptr ObservedValuesContainer<T,id>::get(const id _id)
|
---|
| 61 | {
|
---|
[387a84] | 62 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[6bce88] | 63 | LOG(3, "DEBUG: ObservedValuesContainer got get() for an observed value of "
|
---|
| 64 | << NameOfType << " " << _id);
|
---|
[41e287] | 65 | typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
|
---|
[6bce88] | 66 | if (iter == ObservedValues.end())
|
---|
| 67 | return typename T::ptr();
|
---|
| 68 | else
|
---|
| 69 | return iter->second.getCurrentValue().first;
|
---|
[65c323] | 70 | }
|
---|
| 71 |
|
---|
[41e287] | 72 | template <class T, typename id>
|
---|
[68418e] | 73 | void ObservedValuesContainer<T,id>::markObservedValuesAsConnected(
|
---|
| 74 | const id _id)
|
---|
[41e287] | 75 | {
|
---|
[387a84] | 76 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[4e6ffe] | 77 | LOG(3, "DEBUG: ObservedValuesContainer got markObservedValuesAsConnected() for an observed value of "
|
---|
| 78 | << NameOfType << " " << _id);
|
---|
[393342] | 79 | typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
|
---|
[68418e] | 80 | ASSERT (iter != ObservedValues.end(),
|
---|
| 81 | "ObservedValuesContainer<T,id>::markObservedValuesAsConnected() - Observed value of "
|
---|
| 82 | +NameOfType+" "+toString(_id)+" is not present yet.");
|
---|
[393342] | 83 | ++(iter->second.getCurrentValue().second);
|
---|
[41e287] | 84 | }
|
---|
| 85 |
|
---|
| 86 | template <class T, typename id>
|
---|
[68418e] | 87 | bool ObservedValuesContainer<T,id>::checkRefCount(
|
---|
| 88 | const id _id) const
|
---|
| 89 | {
|
---|
[387a84] | 90 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[68418e] | 91 | typename CountedObservedValues_t::const_iterator iter = ObservedValues.find(_id);
|
---|
[393342] | 92 | return ((iter != ObservedValues.end()) && (iter->second.getEraseCandidate().second == 0));
|
---|
[68418e] | 93 | }
|
---|
| 94 |
|
---|
| 95 | template <class T, typename id>
|
---|
| 96 | void ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected(
|
---|
| 97 | const id _id)
|
---|
[41e287] | 98 | {
|
---|
[387a84] | 99 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[4e6ffe] | 100 | LOG(3, "DEBUG: ObservedValuesContainer got markObservedValuesAsDisconnected() for an observed value of "
|
---|
| 101 | << NameOfType << " " << _id);
|
---|
[41e287] | 102 | typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
|
---|
[68418e] | 103 | ASSERT (iter != ObservedValues.end(),
|
---|
| 104 | "ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected() - Observed value of "
|
---|
| 105 | +NameOfType+" "+toString(_id)+" is not present yet.");
|
---|
[393342] | 106 | ASSERT (iter->second.getEraseCandidate().second != 0,
|
---|
[68418e] | 107 | "ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected() - Observed value of "
|
---|
| 108 | +NameOfType+" "+toString(_id)+" is already signOff() from all.");
|
---|
[393342] | 109 | --(iter->second.getEraseCandidate().second);
|
---|
[68418e] | 110 |
|
---|
[e7ed12] | 111 | checkRemoval(_id);
|
---|
[41e287] | 112 | }
|
---|
| 113 |
|
---|
| 114 | template <class T, typename id>
|
---|
[e7ed12] | 115 | #ifdef HAVE_INLINE
|
---|
| 116 | inline
|
---|
| 117 | #endif
|
---|
[68418e] | 118 | bool ObservedValuesContainer<T,id>::checksubjectKilled(
|
---|
| 119 | const id _id) const
|
---|
[41e287] | 120 | {
|
---|
[387a84] | 121 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[68418e] | 122 | typename subjectKilledCount_t::const_iterator iter = subjectKilledCount.find(_id);
|
---|
| 123 | return ((iter != subjectKilledCount.end()) && (iter->second == T::MAX_ObservedTypes));
|
---|
[41e287] | 124 | }
|
---|
| 125 |
|
---|
[90821d] | 126 | template <class T, typename id>
|
---|
[68418e] | 127 | void ObservedValuesContainer<T,id>::countsubjectKilled(const id _id)
|
---|
[90821d] | 128 | {
|
---|
[387a84] | 129 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[90821d] | 130 | LOG(3, "DEBUG: ObservedValuesContainer got subjectKilled() for an observed value of "
|
---|
| 131 | << NameOfType << " " << _id);
|
---|
| 132 | typename subjectKilledCount_t::iterator iter = subjectKilledCount.find(_id);
|
---|
| 133 | if (iter == subjectKilledCount.end()) {
|
---|
| 134 | std::pair<typename subjectKilledCount_t::iterator, bool> inserter =
|
---|
| 135 | subjectKilledCount.insert( std::make_pair(_id, 0) );
|
---|
| 136 | iter = inserter.first;
|
---|
| 137 | }
|
---|
[68418e] | 138 | ASSERT (iter->second < T::MAX_ObservedTypes,
|
---|
| 139 | "ObservedValuesContainer<T,id>::countsubjectKilled() - all subjectKilled() for "
|
---|
| 140 | +NameOfType+" "+toString(_id)+" for each observed channel came in already.");
|
---|
[90821d] | 141 | ++(iter->second);
|
---|
| 142 |
|
---|
[e7ed12] | 143 | checkRemoval(_id);
|
---|
[68418e] | 144 | }
|
---|
| 145 |
|
---|
| 146 | template <class T, typename id>
|
---|
| 147 | void ObservedValuesContainer<T,id>::removeObservedValues(const id _id)
|
---|
| 148 | {
|
---|
[387a84] | 149 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[68418e] | 150 | LOG(3, "DEBUG: ObservedValuesContainer removes " << NameOfType << " " << _id);
|
---|
| 151 | // call callback function
|
---|
| 152 | onDestroy(_id);
|
---|
| 153 | subjectKilledCount.erase(_id);
|
---|
[0af22d] | 154 | typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
|
---|
[393342] | 155 | iter->second.eraseCurrentValue();
|
---|
| 156 | if (iter->second.empty())
|
---|
[0af22d] | 157 | ObservedValues.erase(iter);
|
---|
[4e6ffe] | 158 | MarkedForErase.erase(_id);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | template <class T, typename id>
|
---|
| 162 | void ObservedValuesContainer<T,id>::eraseObservedValues(const id _id)
|
---|
| 163 | {
|
---|
[387a84] | 164 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[4e6ffe] | 165 | #ifndef NDEBUG
|
---|
| 166 | std::pair< typename std::set<id>::iterator, bool > inserter =
|
---|
| 167 | #endif
|
---|
| 168 | MarkedForErase.insert(_id);
|
---|
| 169 | ASSERT( inserter.second,
|
---|
| 170 | "ObservedValuesContainer<T,id>::eraseObservedValues() - received twice for "
|
---|
| 171 | +NameOfType+" "+toString(_id)+".");
|
---|
| 172 |
|
---|
[e7ed12] | 173 | checkRemoval(_id);
|
---|
[4e6ffe] | 174 | }
|
---|
| 175 |
|
---|
| 176 | template <class T, typename id>
|
---|
[e7ed12] | 177 | #ifdef HAVE_INLINE
|
---|
| 178 | inline
|
---|
| 179 | #endif
|
---|
[4e6ffe] | 180 | bool ObservedValuesContainer<T,id>::checkMarkedForErase(const id _id) const
|
---|
| 181 | {
|
---|
[387a84] | 182 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[4e6ffe] | 183 | return MarkedForErase.count(_id);
|
---|
[90821d] | 184 | }
|
---|
| 185 |
|
---|
[41e287] | 186 | template <class T, typename id>
|
---|
[0af22d] | 187 | void ObservedValuesContainer<T,id>::insert(const id _id, const typename T::ptr &_obsvalues)
|
---|
[41e287] | 188 | {
|
---|
[387a84] | 189 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[393342] | 190 | typename ObservedValuesContainer<T,id>::RefCountedObserved::Value_t value(std::make_pair(_obsvalues,0));
|
---|
| 191 | typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
|
---|
| 192 | if (iter == ObservedValues.end()) {
|
---|
| 193 | std::pair<typename CountedObservedValues_t::iterator, bool> inserter =
|
---|
| 194 | ObservedValues.insert(
|
---|
| 195 | std::make_pair( _id, RefCountedObserved(value) ) );
|
---|
| 196 | ASSERT( inserter.second,
|
---|
| 197 | "ObservedValuesContainer<T,id>::insert() of "+NameOfType
|
---|
| 198 | +" for "+toString(_id)+", insertion failed though empty?");
|
---|
| 199 | } else {
|
---|
| 200 | ASSERT( !iter->second.empty(),
|
---|
| 201 | "ObservedValuesContainer<T,id>::insert() of "+NameOfType
|
---|
| 202 | +" for "+toString(_id)+" has an empty list in ObservedValues.");
|
---|
[0af22d] | 203 | // already an entry present? add to deque
|
---|
[393342] | 204 | iter->second.push_back( value );
|
---|
[0af22d] | 205 | }
|
---|
[68418e] | 206 | _obsvalues->activateObserver();
|
---|
[41e287] | 207 | }
|
---|
| 208 |
|
---|
| 209 | template <class T, typename id>
|
---|
| 210 | bool ObservedValuesContainer<T,id>::changeIdentifier(const id _oldid, const id _newid)
|
---|
| 211 | {
|
---|
[387a84] | 212 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[41e287] | 213 | const typename CountedObservedValues_t::iterator Colditer = ObservedValues.find(_oldid);
|
---|
| 214 | const typename CountedObservedValues_t::iterator Cnewiter = ObservedValues.find(_newid);
|
---|
[90821d] | 215 | const typename subjectKilledCount_t::iterator Solditer = subjectKilledCount.find(_oldid);
|
---|
| 216 | const typename subjectKilledCount_t::iterator Snewiter = subjectKilledCount.find(_newid);
|
---|
[4e6ffe] | 217 | const typename MarkedForErase_t::iterator Eolditer = MarkedForErase.find(_oldid);
|
---|
| 218 | const typename MarkedForErase_t::iterator Enewiter = MarkedForErase.find(_newid);
|
---|
[41e287] | 219 | bool status = ((Colditer != ObservedValues.end()) && (Cnewiter == ObservedValues.end()));
|
---|
[90821d] | 220 | status &= ((Solditer != subjectKilledCount.end()) && (Snewiter == subjectKilledCount.end()));
|
---|
[4e6ffe] | 221 | status &= ((Eolditer != MarkedForErase.end()) && (Enewiter == MarkedForErase.end()));
|
---|
[41e287] | 222 | // change id here only if still present
|
---|
| 223 | if (status) {
|
---|
| 224 | {
|
---|
[0af22d] | 225 | // TODO: Actually, we need to think whether we do not have to to split the
|
---|
| 226 | // deque in two parts: the last entry having the newid, while all other
|
---|
| 227 | // ones retain the old one until they get removed. But we need to check
|
---|
| 228 | // whether changing ids "there" is possible when the QtObserved... is under
|
---|
| 229 | // removal.
|
---|
[393342] | 230 | ObservedValuesContainer<T,id>::RefCountedObserved obsvalues = Colditer->second;
|
---|
[41e287] | 231 | ObservedValues.erase(Colditer);
|
---|
| 232 | ObservedValues.insert( std::make_pair(_newid, obsvalues) );
|
---|
| 233 | }
|
---|
[90821d] | 234 | {
|
---|
| 235 | const size_t countvalue = Solditer->second;
|
---|
| 236 | subjectKilledCount.erase(Solditer);
|
---|
| 237 | subjectKilledCount.insert( std::make_pair(_newid, countvalue) );
|
---|
| 238 | }
|
---|
[4e6ffe] | 239 | {
|
---|
| 240 | MarkedForErase.erase(Eolditer);
|
---|
| 241 | MarkedForErase.insert(_newid);
|
---|
| 242 | }
|
---|
[41e287] | 243 | return true;
|
---|
| 244 | } else
|
---|
| 245 | return false;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | template <class T, typename id>
|
---|
[e7ed12] | 249 | #ifdef HAVE_INLINE
|
---|
| 250 | inline
|
---|
| 251 | #endif
|
---|
[41e287] | 252 | bool ObservedValuesContainer<T,id>::isPresent(const id _id) const
|
---|
| 253 | {
|
---|
[387a84] | 254 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[41e287] | 255 | typename CountedObservedValues_t::const_iterator iter = ObservedValues.find(_id);
|
---|
| 256 | return (iter != ObservedValues.end());
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | #endif /* OBSERVEDVALUESCONTAINER_IMPL_HPP_ */
|
---|