[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 | {
|
---|
[62a0ee] | 55 | if (checkRefCount(_id) && checkMarkedForErase(_id))
|
---|
[e7ed12] | 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 |
|
---|
[68418e] | 114 | template <class T, typename id>
|
---|
| 115 | void ObservedValuesContainer<T,id>::removeObservedValues(const id _id)
|
---|
| 116 | {
|
---|
[387a84] | 117 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[68418e] | 118 | LOG(3, "DEBUG: ObservedValuesContainer removes " << NameOfType << " " << _id);
|
---|
| 119 | // call callback function
|
---|
| 120 | onDestroy(_id);
|
---|
[0af22d] | 121 | typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
|
---|
[393342] | 122 | iter->second.eraseCurrentValue();
|
---|
| 123 | if (iter->second.empty())
|
---|
[0af22d] | 124 | ObservedValues.erase(iter);
|
---|
[4e6ffe] | 125 | MarkedForErase.erase(_id);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | template <class T, typename id>
|
---|
| 129 | void ObservedValuesContainer<T,id>::eraseObservedValues(const id _id)
|
---|
| 130 | {
|
---|
[387a84] | 131 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[4e6ffe] | 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 |
|
---|
[e7ed12] | 140 | checkRemoval(_id);
|
---|
[4e6ffe] | 141 | }
|
---|
| 142 |
|
---|
| 143 | template <class T, typename id>
|
---|
[e7ed12] | 144 | #ifdef HAVE_INLINE
|
---|
| 145 | inline
|
---|
| 146 | #endif
|
---|
[4e6ffe] | 147 | bool ObservedValuesContainer<T,id>::checkMarkedForErase(const id _id) const
|
---|
| 148 | {
|
---|
[387a84] | 149 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[4e6ffe] | 150 | return MarkedForErase.count(_id);
|
---|
[90821d] | 151 | }
|
---|
| 152 |
|
---|
[41e287] | 153 | template <class T, typename id>
|
---|
[0af22d] | 154 | void ObservedValuesContainer<T,id>::insert(const id _id, const typename T::ptr &_obsvalues)
|
---|
[41e287] | 155 | {
|
---|
[387a84] | 156 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[393342] | 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.");
|
---|
[0af22d] | 170 | // already an entry present? add to deque
|
---|
[393342] | 171 | iter->second.push_back( value );
|
---|
[0af22d] | 172 | }
|
---|
[68418e] | 173 | _obsvalues->activateObserver();
|
---|
[41e287] | 174 | }
|
---|
| 175 |
|
---|
| 176 | template <class T, typename id>
|
---|
| 177 | bool ObservedValuesContainer<T,id>::changeIdentifier(const id _oldid, const id _newid)
|
---|
| 178 | {
|
---|
[387a84] | 179 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[41e287] | 180 | const typename CountedObservedValues_t::iterator Colditer = ObservedValues.find(_oldid);
|
---|
| 181 | const typename CountedObservedValues_t::iterator Cnewiter = ObservedValues.find(_newid);
|
---|
[4e6ffe] | 182 | const typename MarkedForErase_t::iterator Eolditer = MarkedForErase.find(_oldid);
|
---|
| 183 | const typename MarkedForErase_t::iterator Enewiter = MarkedForErase.find(_newid);
|
---|
[41e287] | 184 | bool status = ((Colditer != ObservedValues.end()) && (Cnewiter == ObservedValues.end()));
|
---|
[4e6ffe] | 185 | status &= ((Eolditer != MarkedForErase.end()) && (Enewiter == MarkedForErase.end()));
|
---|
[41e287] | 186 | // change id here only if still present
|
---|
| 187 | if (status) {
|
---|
| 188 | {
|
---|
[0af22d] | 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.
|
---|
[393342] | 194 | ObservedValuesContainer<T,id>::RefCountedObserved obsvalues = Colditer->second;
|
---|
[41e287] | 195 | ObservedValues.erase(Colditer);
|
---|
| 196 | ObservedValues.insert( std::make_pair(_newid, obsvalues) );
|
---|
| 197 | }
|
---|
[4e6ffe] | 198 | {
|
---|
| 199 | MarkedForErase.erase(Eolditer);
|
---|
| 200 | MarkedForErase.insert(_newid);
|
---|
| 201 | }
|
---|
[41e287] | 202 | return true;
|
---|
| 203 | } else
|
---|
| 204 | return false;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | template <class T, typename id>
|
---|
[e7ed12] | 208 | #ifdef HAVE_INLINE
|
---|
| 209 | inline
|
---|
| 210 | #endif
|
---|
[41e287] | 211 | bool ObservedValuesContainer<T,id>::isPresent(const id _id) const
|
---|
| 212 | {
|
---|
[387a84] | 213 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[41e287] | 214 | typename CountedObservedValues_t::const_iterator iter = ObservedValues.find(_id);
|
---|
| 215 | return (iter != ObservedValues.end());
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | #endif /* OBSERVEDVALUESCONTAINER_IMPL_HPP_ */
|
---|