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 |
|
---|
21 | template <class T, typename id>
|
---|
22 | ObservedValuesContainer<T,id>::ObservedValuesContainer(
|
---|
23 | const std::string _name,
|
---|
24 | QtObservedInstanceBoard &_board,
|
---|
25 | const onDestroy_t _onDestroy) :
|
---|
26 | NameOfType(_name),
|
---|
27 | board(_board),
|
---|
28 | onDestroy(_onDestroy)
|
---|
29 | {}
|
---|
30 |
|
---|
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));
|
---|
38 | const typename T::ptr &obsvalues = iter->second.first;
|
---|
39 |
|
---|
40 | return obsvalues;
|
---|
41 | }
|
---|
42 |
|
---|
43 | template <class T, typename id>
|
---|
44 | void ObservedValuesContainer<T,id>::markObservedValuesAsConnected(
|
---|
45 | const id _id)
|
---|
46 | {
|
---|
47 | typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
|
---|
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);
|
---|
52 | }
|
---|
53 |
|
---|
54 | template <class T, typename id>
|
---|
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)
|
---|
65 | {
|
---|
66 | typename CountedObservedValues_t::iterator iter = ObservedValues.find(_id);
|
---|
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);
|
---|
77 | }
|
---|
78 |
|
---|
79 | template <class T, typename id>
|
---|
80 | bool ObservedValuesContainer<T,id>::checksubjectKilled(
|
---|
81 | const id _id) const
|
---|
82 | {
|
---|
83 | typename subjectKilledCount_t::const_iterator iter = subjectKilledCount.find(_id);
|
---|
84 | return ((iter != subjectKilledCount.end()) && (iter->second == T::MAX_ObservedTypes));
|
---|
85 | }
|
---|
86 |
|
---|
87 | template <class T, typename id>
|
---|
88 | void ObservedValuesContainer<T,id>::countsubjectKilled(const id _id)
|
---|
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 | }
|
---|
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.");
|
---|
101 | ++(iter->second);
|
---|
102 |
|
---|
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);
|
---|
115 | }
|
---|
116 |
|
---|
117 | template <class T, typename id>
|
---|
118 | bool ObservedValuesContainer<T,id>::insert(const id _id, const typename T::ptr &_obsvalues)
|
---|
119 | {
|
---|
120 | std::pair<typename CountedObservedValues_t::iterator, bool> inserter =
|
---|
121 | ObservedValues.insert(
|
---|
122 | std::make_pair( _id, std::make_pair(_obsvalues,0) ) );
|
---|
123 | _obsvalues->activateObserver();
|
---|
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);
|
---|
132 | const typename subjectKilledCount_t::iterator Solditer = subjectKilledCount.find(_oldid);
|
---|
133 | const typename subjectKilledCount_t::iterator Snewiter = subjectKilledCount.find(_newid);
|
---|
134 | bool status = ((Colditer != ObservedValues.end()) && (Cnewiter == ObservedValues.end()));
|
---|
135 | status &= ((Solditer != subjectKilledCount.end()) && (Snewiter == subjectKilledCount.end()));
|
---|
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 | }
|
---|
143 | {
|
---|
144 | const size_t countvalue = Solditer->second;
|
---|
145 | subjectKilledCount.erase(Solditer);
|
---|
146 | subjectKilledCount.insert( std::make_pair(_newid, countvalue) );
|
---|
147 | }
|
---|
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_ */
|
---|