[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 |
|
---|
[41e287] | 31 | template <class T, typename id>
|
---|
| 32 | typename T::ptr ObservedValuesContainer<T,id>::get(const id _id)
|
---|
| 33 | {
|
---|
| 34 | typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
|
---|
| 35 | ASSERT( iter != ObservedValues.end(),
|
---|
| 36 | "ObservedValuesContainer::getObservedValues() - no observed values present for "
|
---|
| 37 | +NameOfType+" "+toString(_id));
|
---|
[68418e] | 38 | const typename T::ptr &obsvalues = iter->second.first;
|
---|
[65c323] | 39 |
|
---|
[68418e] | 40 | return obsvalues;
|
---|
[65c323] | 41 | }
|
---|
| 42 |
|
---|
[41e287] | 43 | template <class T, typename id>
|
---|
[68418e] | 44 | void ObservedValuesContainer<T,id>::markObservedValuesAsConnected(
|
---|
| 45 | const id _id)
|
---|
[41e287] | 46 | {
|
---|
| 47 | typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
|
---|
[68418e] | 48 | ASSERT (iter != ObservedValues.end(),
|
---|
| 49 | "ObservedValuesContainer<T,id>::markObservedValuesAsConnected() - Observed value of "
|
---|
| 50 | +NameOfType+" "+toString(_id)+" is not present yet.");
|
---|
| 51 | ++(iter->second.second);
|
---|
[41e287] | 52 | }
|
---|
| 53 |
|
---|
| 54 | template <class T, typename id>
|
---|
[68418e] | 55 | bool ObservedValuesContainer<T,id>::checkRefCount(
|
---|
| 56 | const id _id) const
|
---|
| 57 | {
|
---|
| 58 | typename CountedObservedValues_t::const_iterator iter = ObservedValues.find(_id);
|
---|
| 59 | return ((iter != ObservedValues.end()) && (iter->second.second == 0));
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | template <class T, typename id>
|
---|
| 63 | void ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected(
|
---|
| 64 | const id _id)
|
---|
[41e287] | 65 | {
|
---|
| 66 | typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
|
---|
[68418e] | 67 | ASSERT (iter != ObservedValues.end(),
|
---|
| 68 | "ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected() - Observed value of "
|
---|
| 69 | +NameOfType+" "+toString(_id)+" is not present yet.");
|
---|
| 70 | ASSERT (iter->second.second != 0,
|
---|
| 71 | "ObservedValuesContainer<T,id>::markObservedValuesAsDisconnected() - Observed value of "
|
---|
| 72 | +NameOfType+" "+toString(_id)+" is already signOff() from all.");
|
---|
| 73 | --(iter->second.second);
|
---|
| 74 |
|
---|
| 75 | if (checkRefCount(_id) && checksubjectKilled(_id))
|
---|
| 76 | removeObservedValues(_id);
|
---|
[41e287] | 77 | }
|
---|
| 78 |
|
---|
| 79 | template <class T, typename id>
|
---|
[68418e] | 80 | bool ObservedValuesContainer<T,id>::checksubjectKilled(
|
---|
| 81 | const id _id) const
|
---|
[41e287] | 82 | {
|
---|
[68418e] | 83 | typename subjectKilledCount_t::const_iterator iter = subjectKilledCount.find(_id);
|
---|
| 84 | return ((iter != subjectKilledCount.end()) && (iter->second == T::MAX_ObservedTypes));
|
---|
[41e287] | 85 | }
|
---|
| 86 |
|
---|
[90821d] | 87 | template <class T, typename id>
|
---|
[68418e] | 88 | void ObservedValuesContainer<T,id>::countsubjectKilled(const id _id)
|
---|
[90821d] | 89 | {
|
---|
| 90 | LOG(3, "DEBUG: ObservedValuesContainer got subjectKilled() for an observed value of "
|
---|
| 91 | << NameOfType << " " << _id);
|
---|
| 92 | typename subjectKilledCount_t::iterator iter = subjectKilledCount.find(_id);
|
---|
| 93 | if (iter == subjectKilledCount.end()) {
|
---|
| 94 | std::pair<typename subjectKilledCount_t::iterator, bool> inserter =
|
---|
| 95 | subjectKilledCount.insert( std::make_pair(_id, 0) );
|
---|
| 96 | iter = inserter.first;
|
---|
| 97 | }
|
---|
[68418e] | 98 | ASSERT (iter->second < T::MAX_ObservedTypes,
|
---|
| 99 | "ObservedValuesContainer<T,id>::countsubjectKilled() - all subjectKilled() for "
|
---|
| 100 | +NameOfType+" "+toString(_id)+" for each observed channel came in already.");
|
---|
[90821d] | 101 | ++(iter->second);
|
---|
| 102 |
|
---|
[68418e] | 103 | if (checkRefCount(_id) && checksubjectKilled(_id))
|
---|
| 104 | removeObservedValues(_id);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | template <class T, typename id>
|
---|
| 108 | void ObservedValuesContainer<T,id>::removeObservedValues(const id _id)
|
---|
| 109 | {
|
---|
| 110 | LOG(3, "DEBUG: ObservedValuesContainer removes " << NameOfType << " " << _id);
|
---|
| 111 | // call callback function
|
---|
| 112 | onDestroy(_id);
|
---|
| 113 | subjectKilledCount.erase(_id);
|
---|
| 114 | ObservedValues.erase(_id);
|
---|
[90821d] | 115 | }
|
---|
| 116 |
|
---|
[41e287] | 117 | template <class T, typename id>
|
---|
[68418e] | 118 | bool ObservedValuesContainer<T,id>::insert(const id _id, const typename T::ptr &_obsvalues)
|
---|
[41e287] | 119 | {
|
---|
| 120 | std::pair<typename CountedObservedValues_t::iterator, bool> inserter =
|
---|
| 121 | ObservedValues.insert(
|
---|
| 122 | std::make_pair( _id, std::make_pair(_obsvalues,0) ) );
|
---|
[68418e] | 123 | _obsvalues->activateObserver();
|
---|
[41e287] | 124 | return inserter.second;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | template <class T, typename id>
|
---|
| 128 | bool ObservedValuesContainer<T,id>::changeIdentifier(const id _oldid, const id _newid)
|
---|
| 129 | {
|
---|
| 130 | const typename CountedObservedValues_t::iterator Colditer = ObservedValues.find(_oldid);
|
---|
| 131 | const typename CountedObservedValues_t::iterator Cnewiter = ObservedValues.find(_newid);
|
---|
[90821d] | 132 | const typename subjectKilledCount_t::iterator Solditer = subjectKilledCount.find(_oldid);
|
---|
| 133 | const typename subjectKilledCount_t::iterator Snewiter = subjectKilledCount.find(_newid);
|
---|
[41e287] | 134 | bool status = ((Colditer != ObservedValues.end()) && (Cnewiter == ObservedValues.end()));
|
---|
[90821d] | 135 | status &= ((Solditer != subjectKilledCount.end()) && (Snewiter == subjectKilledCount.end()));
|
---|
[41e287] | 136 | // change id here only if still present
|
---|
| 137 | if (status) {
|
---|
| 138 | {
|
---|
| 139 | RefCountedObservedValues_t obsvalues = Colditer->second;
|
---|
| 140 | ObservedValues.erase(Colditer);
|
---|
| 141 | ObservedValues.insert( std::make_pair(_newid, obsvalues) );
|
---|
| 142 | }
|
---|
[90821d] | 143 | {
|
---|
| 144 | const size_t countvalue = Solditer->second;
|
---|
| 145 | subjectKilledCount.erase(Solditer);
|
---|
| 146 | subjectKilledCount.insert( std::make_pair(_newid, countvalue) );
|
---|
| 147 | }
|
---|
[41e287] | 148 | return true;
|
---|
| 149 | } else
|
---|
| 150 | return false;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | template <class T, typename id>
|
---|
| 154 | size_t ObservedValuesContainer<T,id>::getRefCount(const id _id) const
|
---|
| 155 | {
|
---|
| 156 | typename CountedObservedValues_t::const_iterator iter = ObservedValues.find(_id);
|
---|
| 157 | return iter->second.second;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | template <class T, typename id>
|
---|
| 161 | bool ObservedValuesContainer<T,id>::isPresent(const id _id) const
|
---|
| 162 | {
|
---|
| 163 | typename CountedObservedValues_t::const_iterator iter = ObservedValues.find(_id);
|
---|
| 164 | return (iter != ObservedValues.end());
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | #endif /* OBSERVEDVALUESCONTAINER_IMPL_HPP_ */
|
---|