[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();
|
---|
[23221f] | 38 | iter != ObservedValues.end(); ++iter)
|
---|
| 39 | iter->second->noteBoardIsGone();
|
---|
[04c3a3] | 40 | }
|
---|
| 41 |
|
---|
[e7ed12] | 42 | template <class T, typename id>
|
---|
| 43 | #ifdef HAVE_INLINE
|
---|
| 44 | inline
|
---|
| 45 | #endif
|
---|
| 46 | void ObservedValuesContainer<T,id>::checkRemoval(const id _id)
|
---|
| 47 | {
|
---|
[23221f] | 48 | if (checkMarkedForConnected(_id) && checkMarkedForErase(_id))
|
---|
[e7ed12] | 49 | removeObservedValues(_id);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[41e287] | 52 | template <class T, typename id>
|
---|
| 53 | typename T::ptr ObservedValuesContainer<T,id>::get(const id _id)
|
---|
| 54 | {
|
---|
[387a84] | 55 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[6bce88] | 56 | LOG(3, "DEBUG: ObservedValuesContainer got get() for an observed value of "
|
---|
| 57 | << NameOfType << " " << _id);
|
---|
[41e287] | 58 | typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
|
---|
[6bce88] | 59 | if (iter == ObservedValues.end())
|
---|
| 60 | return typename T::ptr();
|
---|
| 61 | else
|
---|
[23221f] | 62 | return iter->second;
|
---|
[65c323] | 63 | }
|
---|
| 64 |
|
---|
[41e287] | 65 | template <class T, typename id>
|
---|
[68418e] | 66 | void ObservedValuesContainer<T,id>::markObservedValuesAsConnected(
|
---|
| 67 | const id _id)
|
---|
[41e287] | 68 | {
|
---|
[387a84] | 69 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[4e6ffe] | 70 | LOG(3, "DEBUG: ObservedValuesContainer got markObservedValuesAsConnected() for an observed value of "
|
---|
| 71 | << NameOfType << " " << _id);
|
---|
[23221f] | 72 | ASSERT (MarkedForConnected.find(_id) == MarkedForConnected.end(),
|
---|
[68418e] | 73 | "ObservedValuesContainer<T,id>::markObservedValuesAsConnected() - Observed value of "
|
---|
[23221f] | 74 | +NameOfType+" "+toString(_id)+" is already marked as connected.");
|
---|
| 75 | MarkedForConnected.insert( _id );
|
---|
[41e287] | 76 | }
|
---|
| 77 |
|
---|
| 78 | template <class T, typename id>
|
---|
[23221f] | 79 | bool ObservedValuesContainer<T,id>::checkMarkedForConnected(
|
---|
[68418e] | 80 | const id _id) const
|
---|
| 81 | {
|
---|
[387a84] | 82 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[23221f] | 83 | typename MarkedSet_t::const_iterator const iter = MarkedForConnected.find(_id);
|
---|
| 84 | return iter == MarkedForConnected.end();
|
---|
[68418e] | 85 | }
|
---|
| 86 |
|
---|
| 87 | template <class T, typename id>
|
---|
| 88 | void ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected(
|
---|
| 89 | const id _id)
|
---|
[41e287] | 90 | {
|
---|
[387a84] | 91 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[4e6ffe] | 92 | LOG(3, "DEBUG: ObservedValuesContainer got markObservedValuesAsDisconnected() for an observed value of "
|
---|
| 93 | << NameOfType << " " << _id);
|
---|
[23221f] | 94 | typename MarkedSet_t::iterator const iter = MarkedForConnected.find(_id);
|
---|
| 95 | ASSERT (iter != MarkedForConnected.end(),
|
---|
[68418e] | 96 | "ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected() - Observed value of "
|
---|
[23221f] | 97 | +NameOfType+" "+toString(_id)+" is not marked as connected.");
|
---|
| 98 | MarkedForConnected.erase(iter);
|
---|
[68418e] | 99 |
|
---|
[e7ed12] | 100 | checkRemoval(_id);
|
---|
[41e287] | 101 | }
|
---|
| 102 |
|
---|
[68418e] | 103 | template <class T, typename id>
|
---|
| 104 | void ObservedValuesContainer<T,id>::removeObservedValues(const id _id)
|
---|
| 105 | {
|
---|
[387a84] | 106 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[68418e] | 107 | LOG(3, "DEBUG: ObservedValuesContainer removes " << NameOfType << " " << _id);
|
---|
[23221f] | 108 | MarkedForConnected.erase(_id);
|
---|
| 109 | MarkedForErase.erase(_id);
|
---|
| 110 | ObservedValues.erase(_id);
|
---|
[68418e] | 111 | // call callback function
|
---|
| 112 | onDestroy(_id);
|
---|
[4e6ffe] | 113 | }
|
---|
| 114 |
|
---|
| 115 | template <class T, typename id>
|
---|
| 116 | void ObservedValuesContainer<T,id>::eraseObservedValues(const id _id)
|
---|
| 117 | {
|
---|
[387a84] | 118 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[4e6ffe] | 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 |
|
---|
[e7ed12] | 127 | checkRemoval(_id);
|
---|
[4e6ffe] | 128 | }
|
---|
| 129 |
|
---|
| 130 | template <class T, typename id>
|
---|
[e7ed12] | 131 | #ifdef HAVE_INLINE
|
---|
| 132 | inline
|
---|
| 133 | #endif
|
---|
[4e6ffe] | 134 | bool ObservedValuesContainer<T,id>::checkMarkedForErase(const id _id) const
|
---|
| 135 | {
|
---|
[387a84] | 136 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[23221f] | 137 | typename MarkedSet_t::const_iterator const iter = MarkedForErase.find(_id);
|
---|
| 138 | return iter != MarkedForErase.end();
|
---|
[90821d] | 139 | }
|
---|
| 140 |
|
---|
[41e287] | 141 | template <class T, typename id>
|
---|
[0af22d] | 142 | void ObservedValuesContainer<T,id>::insert(const id _id, const typename T::ptr &_obsvalues)
|
---|
[41e287] | 143 | {
|
---|
[387a84] | 144 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[393342] | 145 | typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
|
---|
[23221f] | 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 |
|
---|
[68418e] | 156 | _obsvalues->activateObserver();
|
---|
[41e287] | 157 | }
|
---|
| 158 |
|
---|
| 159 | template <class T, typename id>
|
---|
| 160 | bool ObservedValuesContainer<T,id>::changeIdentifier(const id _oldid, const id _newid)
|
---|
| 161 | {
|
---|
[387a84] | 162 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[41e287] | 163 | const typename CountedObservedValues_t::iterator Colditer = ObservedValues.find(_oldid);
|
---|
| 164 | const typename CountedObservedValues_t::iterator Cnewiter = ObservedValues.find(_newid);
|
---|
[23221f] | 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);
|
---|
[41e287] | 169 | bool status = ((Colditer != ObservedValues.end()) && (Cnewiter == ObservedValues.end()));
|
---|
[23221f] | 170 | status &= ((MEolditer != MarkedForErase.end()) && (MEnewiter == MarkedForErase.end()));
|
---|
| 171 | status &= ((MColditer != MarkedForConnected.end()) && (MCnewiter == MarkedForConnected.end()));
|
---|
[41e287] | 172 | // change id here only if still present
|
---|
| 173 | if (status) {
|
---|
| 174 | {
|
---|
[0af22d] | 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.
|
---|
[23221f] | 180 | typename T::ptr obsvalues = Colditer->second;
|
---|
[41e287] | 181 | ObservedValues.erase(Colditer);
|
---|
| 182 | ObservedValues.insert( std::make_pair(_newid, obsvalues) );
|
---|
| 183 | }
|
---|
[4e6ffe] | 184 | {
|
---|
[23221f] | 185 | MarkedForErase.erase(MEolditer);
|
---|
[4e6ffe] | 186 | MarkedForErase.insert(_newid);
|
---|
| 187 | }
|
---|
[23221f] | 188 | {
|
---|
| 189 | MarkedForConnected.erase(MColditer);
|
---|
| 190 | MarkedForConnected.insert(_newid);
|
---|
| 191 | }
|
---|
[41e287] | 192 | return true;
|
---|
| 193 | } else
|
---|
| 194 | return false;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | template <class T, typename id>
|
---|
[e7ed12] | 198 | #ifdef HAVE_INLINE
|
---|
| 199 | inline
|
---|
| 200 | #endif
|
---|
[41e287] | 201 | bool ObservedValuesContainer<T,id>::isPresent(const id _id) const
|
---|
| 202 | {
|
---|
[387a84] | 203 | boost::recursive_mutex::scoped_lock lock(atomic_mutex);
|
---|
[41e287] | 204 | typename CountedObservedValues_t::const_iterator iter = ObservedValues.find(_id);
|
---|
| 205 | return (iter != ObservedValues.end());
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | #endif /* OBSERVEDVALUESCONTAINER_IMPL_HPP_ */
|
---|