1 | /*
|
---|
2 | * ObservedValuesContainer.hpp
|
---|
3 | *
|
---|
4 | * Created on: Oct 29, 2015
|
---|
5 | * Author: heber
|
---|
6 | */
|
---|
7 |
|
---|
8 |
|
---|
9 | #ifndef OBSERVEDVALUESCONTAINER_HPP_
|
---|
10 | #define OBSERVEDVALUESCONTAINER_HPP_
|
---|
11 |
|
---|
12 | // include config.h
|
---|
13 | #ifdef HAVE_CONFIG_H
|
---|
14 | #include <config.h>
|
---|
15 | #endif
|
---|
16 |
|
---|
17 | #include "ObservedValue_types.hpp"
|
---|
18 |
|
---|
19 | #include <deque>
|
---|
20 | #include <map>
|
---|
21 | #include <string>
|
---|
22 |
|
---|
23 | #include <boost/function.hpp>
|
---|
24 |
|
---|
25 | class QtObservedInstanceBoard;
|
---|
26 |
|
---|
27 | /** This class contains ObservedValues of the class \b T each instance identified
|
---|
28 | * by the id type \b id.
|
---|
29 | *
|
---|
30 | * All the reference counting is done inside this container.
|
---|
31 | */
|
---|
32 | template <class T, typename id>
|
---|
33 | class ObservedValuesContainer
|
---|
34 | {
|
---|
35 | public:
|
---|
36 |
|
---|
37 | //!> typedef for callback functions to be used on last SubjectKilled()
|
---|
38 | typedef boost::function<void (const id _id)> onDestroy_t;
|
---|
39 |
|
---|
40 | /** Cstor of class ObservedValuesContainer.
|
---|
41 | *
|
---|
42 | * \param _name name used in debugging and prints
|
---|
43 | * \param _board ref to InstanceBoard
|
---|
44 | * \param _onDestroy function to call when last subjectKilled() was received and
|
---|
45 | * ObservedValues are destroyed
|
---|
46 | */
|
---|
47 | ObservedValuesContainer(
|
---|
48 | const std::string _name,
|
---|
49 | QtObservedInstanceBoard &_board,
|
---|
50 | const onDestroy_t _onDestroy);
|
---|
51 |
|
---|
52 | /** Destor of class ObservedValuesContainer.
|
---|
53 | *
|
---|
54 | */
|
---|
55 | ~ObservedValuesContainer();
|
---|
56 |
|
---|
57 | /** Delivers the set of Observed value for the instance identified by \a _id.
|
---|
58 | *
|
---|
59 | * \param _id identifier of the instance
|
---|
60 | * \return shared ptr to observed instance.
|
---|
61 | */
|
---|
62 | typename T::ptr get(const id _id);
|
---|
63 |
|
---|
64 | /** Used by QtObserved.. instance to note that signOn() has been called.
|
---|
65 | *
|
---|
66 | * \param _id identifier of the instance who called signOn()
|
---|
67 | */
|
---|
68 | void markObservedValuesAsConnected(const id _id);
|
---|
69 |
|
---|
70 | /** Used by QtObserved.. instance to note that signOff() has been called.
|
---|
71 | *
|
---|
72 | * \param _id identifier of the instance who called signOff()
|
---|
73 | */
|
---|
74 | void markObservedValuesAsDisconnected(const id _id);
|
---|
75 |
|
---|
76 | /** Prepares removeal a vector of observed values of an instance identified by \a _id.
|
---|
77 | *
|
---|
78 | * \param _id identifier of instance
|
---|
79 | */
|
---|
80 | void removeObservedValues(const id _id);
|
---|
81 |
|
---|
82 | /** Erases a vector of observed values of an instance identified by \a _id.
|
---|
83 | *
|
---|
84 | * \param _id identifier of instance
|
---|
85 | */
|
---|
86 | void eraseObservedValues(const id _id);
|
---|
87 |
|
---|
88 | private:
|
---|
89 | /** \note CountedObservedValues_t needs to contain a list in the second argument
|
---|
90 | * as the ObservedValuesContainer may fall behind when a single atom is created
|
---|
91 | * and destroyed a number of times in a row (e.g. in the undo/redo cases). This
|
---|
92 | * problem is inherent because on the one hand we keep the QtObservedAtom alive
|
---|
93 | * till the last moment, i.e. till the GUI does not need it anymore where the
|
---|
94 | * original instance may long have been removed from the World. On the other hand
|
---|
95 | * we instantiate the e.g. QtObservedAtom as soon as the observed atom is created.
|
---|
96 | */
|
---|
97 |
|
---|
98 | /** Structure to contain the sequence of created observed memories.
|
---|
99 | *
|
---|
100 | * \note QtGUI may lag behind the creation/destruction of observed instances a
|
---|
101 | * lot but one after the other must be served. Here, we keep an additional
|
---|
102 | * iterator that indicates the current instance to be served.
|
---|
103 | */
|
---|
104 | struct RefCountedObserved
|
---|
105 | {
|
---|
106 | typedef std::pair<typename T::ptr, size_t> Value_t;
|
---|
107 | typedef std::deque<Value_t> Values_t;
|
---|
108 |
|
---|
109 | /** Default cstor of RefCountedObservedValues.
|
---|
110 | *
|
---|
111 | * Make sure that currentvalue has some sensible default value.
|
---|
112 | */
|
---|
113 | RefCountedObserved() :
|
---|
114 | tobedestroyed(values.end()),
|
---|
115 | currentvalue(values.end())
|
---|
116 | {}
|
---|
117 |
|
---|
118 | /** Default copy cstor of RefCountedObservedValues.
|
---|
119 | *
|
---|
120 | * \param _instance instance to copy
|
---|
121 | */
|
---|
122 | RefCountedObserved(const RefCountedObserved & _instance) :
|
---|
123 | values(_instance.values),
|
---|
124 | tobedestroyed(values.begin()),
|
---|
125 | currentvalue(values.begin())
|
---|
126 | {}
|
---|
127 |
|
---|
128 | /** Cstor of RefCountedObservedValues.
|
---|
129 | *
|
---|
130 | * Make sure that currentvalue has some sensible default value.
|
---|
131 | *
|
---|
132 | * \param _value initial value for the list
|
---|
133 | */
|
---|
134 | RefCountedObserved(Value_t _value) :
|
---|
135 | values(1, _value),
|
---|
136 | tobedestroyed(values.begin()),
|
---|
137 | currentvalue(values.begin())
|
---|
138 | {}
|
---|
139 |
|
---|
140 | bool empty() const
|
---|
141 | {
|
---|
142 | return values.empty();
|
---|
143 | }
|
---|
144 |
|
---|
145 | void push_back(Value_t _value)
|
---|
146 | {
|
---|
147 | values.push_back(_value);
|
---|
148 | if (currentvalue == values.end())
|
---|
149 | currentvalue = values.end()--;
|
---|
150 | if (tobedestroyed == values.end())
|
---|
151 | tobedestroyed = currentvalue;
|
---|
152 | }
|
---|
153 |
|
---|
154 | Value_t& getCurrentValue() const
|
---|
155 | {
|
---|
156 | ASSERT (currentvalue != values.end(),
|
---|
157 | "RefCountedObservedValues::getCurrentValue() - cannot get, no values present.");
|
---|
158 | return *currentvalue;
|
---|
159 | }
|
---|
160 |
|
---|
161 | Value_t& getEraseCandidate() const
|
---|
162 | {
|
---|
163 | ASSERT (tobedestroyed != values.end(),
|
---|
164 | "RefCountedObservedValues::getCurrentValue() - cannot get, no values present.");
|
---|
165 | return *tobedestroyed;
|
---|
166 | }
|
---|
167 |
|
---|
168 | void eraseCurrentValue()
|
---|
169 | {
|
---|
170 | ASSERT (tobedestroyed != values.end(),
|
---|
171 | "RefCountedObservedValues::eraseCurrentValue() - cannot erase, no values present.");
|
---|
172 | if (tobedestroyed == currentvalue)
|
---|
173 | ++currentvalue;
|
---|
174 | values.erase(tobedestroyed);
|
---|
175 | tobedestroyed = currentvalue;
|
---|
176 | // removal of this instance when both iterators point to end is done inside QtObservedInstanceBoard
|
---|
177 | }
|
---|
178 |
|
---|
179 | //!> set of values
|
---|
180 | Values_t values;
|
---|
181 | //!> iterator on the value that is next gonna be destroyed
|
---|
182 | typename Values_t::iterator tobedestroyed;
|
---|
183 | //!> iterator on the current value that is served on get
|
---|
184 | typename Values_t::iterator currentvalue;
|
---|
185 | };
|
---|
186 |
|
---|
187 | typedef std::map<id, RefCountedObserved > CountedObservedValues_t;
|
---|
188 | //!> internal vector of observed values
|
---|
189 | CountedObservedValues_t ObservedValues;
|
---|
190 |
|
---|
191 | //!> typedef for the set with ids to be erase
|
---|
192 | typedef std::set<id> MarkedForErase_t;
|
---|
193 |
|
---|
194 | //!> marks ids marked for erase (i.e. all subjectKilled() received)
|
---|
195 | MarkedForErase_t MarkedForErase;
|
---|
196 |
|
---|
197 | //!> name used in describing the instance type
|
---|
198 | const std::string NameOfType;
|
---|
199 |
|
---|
200 | //!> reference to InstanceBoard for callbacks on subjectKilled()
|
---|
201 | QtObservedInstanceBoard &board;
|
---|
202 |
|
---|
203 | //!> callback function when ObservedValues need to be destroyed
|
---|
204 | const onDestroy_t onDestroy;
|
---|
205 |
|
---|
206 | //!> internal mutex to have atomic access to control maps
|
---|
207 | mutable boost::recursive_mutex atomic_mutex;
|
---|
208 |
|
---|
209 | private:
|
---|
210 | /** Internal function to check whether an Observed instance identified by
|
---|
211 | * \a _id is still signOn() to its associated World instance.
|
---|
212 | *
|
---|
213 | * \param _id identifier of instance
|
---|
214 | * \return true - no more signOn()s, false - else
|
---|
215 | */
|
---|
216 | bool checkRefCount(const id _id) const;
|
---|
217 |
|
---|
218 | /** Internal function to check whether the vector of ObservedValue's
|
---|
219 | * identified by \a _id has been marked for erase.
|
---|
220 | *
|
---|
221 | * Marked for erase means that it has received all subjectKilled()
|
---|
222 | * (the container not the values themselves).
|
---|
223 | *
|
---|
224 | * \param _id identifier of instance
|
---|
225 | * \return true - marked for erase, false - else
|
---|
226 | */
|
---|
227 | bool checkMarkedForErase(const id _id) const;
|
---|
228 |
|
---|
229 | /** Combines all of the above checks with call to removeObservedValues() on true.
|
---|
230 | *
|
---|
231 | * \param _id identifier of instance
|
---|
232 | */
|
---|
233 | void checkRemoval(const id _id);
|
---|
234 |
|
---|
235 | private:
|
---|
236 | //!> QtObservedInstanceBoard may access anything
|
---|
237 | friend class QtObservedInstanceBoard;
|
---|
238 |
|
---|
239 | /** Inserts a new ObservedValue vector into the container.
|
---|
240 | *
|
---|
241 | * \param _id identifier of instance associated with observed values
|
---|
242 | * \param _obsvalues vector of observed values of instance
|
---|
243 | */
|
---|
244 | void insert(const id _id, const typename T::ptr &_obsvalues);
|
---|
245 |
|
---|
246 | /** Use to change the identifier associated with a vector of observed values.
|
---|
247 | *
|
---|
248 | * \param _oldid old identifier
|
---|
249 | * \param _newid new identifier
|
---|
250 | * \return true - change successful, false - else
|
---|
251 | */
|
---|
252 | bool changeIdentifier(const id _oldid, const id _newid);
|
---|
253 |
|
---|
254 | /** Checks whether a vector of observed values of an instance identified by \a _id
|
---|
255 | * is present.
|
---|
256 | *
|
---|
257 | * \param _id identifier of instance
|
---|
258 | * \return true - present, false - else
|
---|
259 | */
|
---|
260 | bool isPresent(const id _id) const;
|
---|
261 | };
|
---|
262 |
|
---|
263 | #endif /* OBSERVEDVALUESCONTAINER_HPP_ */
|
---|