[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 |
|
---|
[65c323] | 21 | template <class T, typename id>
|
---|
| 22 | ObservedValuesContainer<T,id>::ObservedValuesContainer(
|
---|
| 23 | const std::string _name,
|
---|
[68418e] | 24 | QtObservedInstanceBoard &_board,
|
---|
| 25 | const onDestroy_t _onDestroy) :
|
---|
[65c323] | 26 | NameOfType(_name),
|
---|
[68418e] | 27 | board(_board),
|
---|
| 28 | onDestroy(_onDestroy)
|
---|
[65c323] | 29 | {}
|
---|
| 30 |
|
---|
[04c3a3] | 31 | template <class T, typename id>
|
---|
| 32 | ObservedValuesContainer<T,id>::~ObservedValuesContainer()
|
---|
| 33 | {
|
---|
| 34 | for (typename CountedObservedValues_t::iterator iter = ObservedValues.begin();
|
---|
[0af22d] | 35 | iter != ObservedValues.end(); ++iter) {
|
---|
| 36 | ASSERT( !iter->second.empty(),
|
---|
| 37 | "~ObservedValuesContainer() of "+NameOfType+" "+toString(iter->first)
|
---|
| 38 | +" has an empty list in ObservedValues.");
|
---|
[393342] | 39 | for (typename RefCountedObserved::Values_t::iterator listiter = iter->second.values.begin();
|
---|
| 40 | listiter != iter->second.values.end(); ++listiter) {
|
---|
[0af22d] | 41 | listiter->first->noteBoardIsGone();
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
[04c3a3] | 44 | }
|
---|
| 45 |
|
---|
[41e287] | 46 | template <class T, typename id>
|
---|
| 47 | typename T::ptr ObservedValuesContainer<T,id>::get(const id _id)
|
---|
| 48 | {
|
---|
[6bce88] | 49 | LOG(3, "DEBUG: ObservedValuesContainer got get() for an observed value of "
|
---|
| 50 | << NameOfType << " " << _id);
|
---|
[41e287] | 51 | typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
|
---|
[6bce88] | 52 | if (iter == ObservedValues.end())
|
---|
| 53 | return typename T::ptr();
|
---|
| 54 | else
|
---|
| 55 | return iter->second.getCurrentValue().first;
|
---|
[65c323] | 56 | }
|
---|
| 57 |
|
---|
[41e287] | 58 | template <class T, typename id>
|
---|
[68418e] | 59 | void ObservedValuesContainer<T,id>::markObservedValuesAsConnected(
|
---|
| 60 | const id _id)
|
---|
[41e287] | 61 | {
|
---|
[4e6ffe] | 62 | LOG(3, "DEBUG: ObservedValuesContainer got markObservedValuesAsConnected() for an observed value of "
|
---|
| 63 | << NameOfType << " " << _id);
|
---|
[393342] | 64 | typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
|
---|
[68418e] | 65 | ASSERT (iter != ObservedValues.end(),
|
---|
| 66 | "ObservedValuesContainer<T,id>::markObservedValuesAsConnected() - Observed value of "
|
---|
| 67 | +NameOfType+" "+toString(_id)+" is not present yet.");
|
---|
[393342] | 68 | ++(iter->second.getCurrentValue().second);
|
---|
[41e287] | 69 | }
|
---|
| 70 |
|
---|
| 71 | template <class T, typename id>
|
---|
[68418e] | 72 | bool ObservedValuesContainer<T,id>::checkRefCount(
|
---|
| 73 | const id _id) const
|
---|
| 74 | {
|
---|
| 75 | typename CountedObservedValues_t::const_iterator iter = ObservedValues.find(_id);
|
---|
[393342] | 76 | return ((iter != ObservedValues.end()) && (iter->second.getEraseCandidate().second == 0));
|
---|
[68418e] | 77 | }
|
---|
| 78 |
|
---|
| 79 | template <class T, typename id>
|
---|
| 80 | void ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected(
|
---|
| 81 | const id _id)
|
---|
[41e287] | 82 | {
|
---|
[4e6ffe] | 83 | LOG(3, "DEBUG: ObservedValuesContainer got markObservedValuesAsDisconnected() for an observed value of "
|
---|
| 84 | << NameOfType << " " << _id);
|
---|
[41e287] | 85 | typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
|
---|
[68418e] | 86 | ASSERT (iter != ObservedValues.end(),
|
---|
| 87 | "ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected() - Observed value of "
|
---|
| 88 | +NameOfType+" "+toString(_id)+" is not present yet.");
|
---|
[393342] | 89 | ASSERT (iter->second.getEraseCandidate().second != 0,
|
---|
[68418e] | 90 | "ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected() - Observed value of "
|
---|
| 91 | +NameOfType+" "+toString(_id)+" is already signOff() from all.");
|
---|
[393342] | 92 | --(iter->second.getEraseCandidate().second);
|
---|
[68418e] | 93 |
|
---|
[4e6ffe] | 94 | if (checkRefCount(_id) && checksubjectKilled(_id) && checkMarkedForErase(_id))
|
---|
[68418e] | 95 | removeObservedValues(_id);
|
---|
[41e287] | 96 | }
|
---|
| 97 |
|
---|
| 98 | template <class T, typename id>
|
---|
[68418e] | 99 | bool ObservedValuesContainer<T,id>::checksubjectKilled(
|
---|
| 100 | const id _id) const
|
---|
[41e287] | 101 | {
|
---|
[68418e] | 102 | typename subjectKilledCount_t::const_iterator iter = subjectKilledCount.find(_id);
|
---|
| 103 | return ((iter != subjectKilledCount.end()) && (iter->second == T::MAX_ObservedTypes));
|
---|
[41e287] | 104 | }
|
---|
| 105 |
|
---|
[90821d] | 106 | template <class T, typename id>
|
---|
[68418e] | 107 | void ObservedValuesContainer<T,id>::countsubjectKilled(const id _id)
|
---|
[90821d] | 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 | }
|
---|
[68418e] | 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.");
|
---|
[90821d] | 120 | ++(iter->second);
|
---|
| 121 |
|
---|
[4e6ffe] | 122 | if (checkRefCount(_id) && checksubjectKilled(_id) && checkMarkedForErase(_id))
|
---|
[68418e] | 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);
|
---|
[0af22d] | 133 | typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
|
---|
[393342] | 134 | iter->second.eraseCurrentValue();
|
---|
| 135 | if (iter->second.empty())
|
---|
[0af22d] | 136 | ObservedValues.erase(iter);
|
---|
[4e6ffe] | 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);
|
---|
[90821d] | 159 | }
|
---|
| 160 |
|
---|
[41e287] | 161 | template <class T, typename id>
|
---|
[0af22d] | 162 | void ObservedValuesContainer<T,id>::insert(const id _id, const typename T::ptr &_obsvalues)
|
---|
[41e287] | 163 | {
|
---|
[393342] | 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.");
|
---|
[0af22d] | 177 | // already an entry present? add to deque
|
---|
[393342] | 178 | iter->second.push_back( value );
|
---|
[0af22d] | 179 | }
|
---|
[68418e] | 180 | _obsvalues->activateObserver();
|
---|
[41e287] | 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);
|
---|
[90821d] | 188 | const typename subjectKilledCount_t::iterator Solditer = subjectKilledCount.find(_oldid);
|
---|
| 189 | const typename subjectKilledCount_t::iterator Snewiter = subjectKilledCount.find(_newid);
|
---|
[4e6ffe] | 190 | const typename MarkedForErase_t::iterator Eolditer = MarkedForErase.find(_oldid);
|
---|
| 191 | const typename MarkedForErase_t::iterator Enewiter = MarkedForErase.find(_newid);
|
---|
[41e287] | 192 | bool status = ((Colditer != ObservedValues.end()) && (Cnewiter == ObservedValues.end()));
|
---|
[90821d] | 193 | status &= ((Solditer != subjectKilledCount.end()) && (Snewiter == subjectKilledCount.end()));
|
---|
[4e6ffe] | 194 | status &= ((Eolditer != MarkedForErase.end()) && (Enewiter == MarkedForErase.end()));
|
---|
[41e287] | 195 | // change id here only if still present
|
---|
| 196 | if (status) {
|
---|
| 197 | {
|
---|
[0af22d] | 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.
|
---|
[393342] | 203 | ObservedValuesContainer<T,id>::RefCountedObserved obsvalues = Colditer->second;
|
---|
[41e287] | 204 | ObservedValues.erase(Colditer);
|
---|
| 205 | ObservedValues.insert( std::make_pair(_newid, obsvalues) );
|
---|
| 206 | }
|
---|
[90821d] | 207 | {
|
---|
| 208 | const size_t countvalue = Solditer->second;
|
---|
| 209 | subjectKilledCount.erase(Solditer);
|
---|
| 210 | subjectKilledCount.insert( std::make_pair(_newid, countvalue) );
|
---|
| 211 | }
|
---|
[4e6ffe] | 212 | {
|
---|
| 213 | MarkedForErase.erase(Eolditer);
|
---|
| 214 | MarkedForErase.insert(_newid);
|
---|
| 215 | }
|
---|
[41e287] | 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_ */
|
---|