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 <map>
|
---|
20 | #include <string>
|
---|
21 |
|
---|
22 | #include <boost/function.hpp>
|
---|
23 |
|
---|
24 | class QtObservedInstanceBoard;
|
---|
25 |
|
---|
26 | /** This class contains ObservedValues of the class \b T each instance identified
|
---|
27 | * by the id type \b id.
|
---|
28 | *
|
---|
29 | * All the reference counting is done inside this container.
|
---|
30 | */
|
---|
31 | template <class T, typename id>
|
---|
32 | class ObservedValuesContainer
|
---|
33 | {
|
---|
34 | public:
|
---|
35 |
|
---|
36 | //!> typedef for callback functions to be used on last SubjectKilled()
|
---|
37 | typedef boost::function<void (const id _id)> onDestroy_t;
|
---|
38 |
|
---|
39 | /** Cstor of class ObservedValuesContainer.
|
---|
40 | *
|
---|
41 | * \param _name name used in debugging and prints
|
---|
42 | * \param _board ref to InstanceBoard
|
---|
43 | * \param _onDestroy function to call when last subjectKilled() was received and
|
---|
44 | * ObservedValues are destroyed
|
---|
45 | */
|
---|
46 | ObservedValuesContainer(
|
---|
47 | const std::string _name,
|
---|
48 | QtObservedInstanceBoard &_board,
|
---|
49 | const onDestroy_t _onDestroy);
|
---|
50 |
|
---|
51 | /** Destor of class ObservedValuesContainer.
|
---|
52 | *
|
---|
53 | */
|
---|
54 | ~ObservedValuesContainer();
|
---|
55 |
|
---|
56 | /** Delivers the set of Observed value for the instance identified by \a _id.
|
---|
57 | *
|
---|
58 | * \param _id identifier of the instance
|
---|
59 | * \return shared ptr to observed instance.
|
---|
60 | */
|
---|
61 | typename T::ptr get(const id _id);
|
---|
62 |
|
---|
63 | /** Used by QtObserved.. instance to note that signOn() has been called.
|
---|
64 | *
|
---|
65 | * \param _id identifier of the instance who called signOn()
|
---|
66 | */
|
---|
67 | void markObservedValuesAsConnected(const id _id);
|
---|
68 |
|
---|
69 | /** Used by QtObserved.. instance to note that signOff() has been called.
|
---|
70 | *
|
---|
71 | * \param _id identifier of the instance who called signOff()
|
---|
72 | */
|
---|
73 | void markObservedValuesAsDisconnected(const id _id);
|
---|
74 |
|
---|
75 | /** Inform this container that subjectKilled() was received by one of the ObservedValues.
|
---|
76 | *
|
---|
77 | * \param _id identifier of the receiving instance
|
---|
78 | */
|
---|
79 | void countsubjectKilled(const id _id);
|
---|
80 |
|
---|
81 | /** Prepares removeal a vector of observed values of an instance identified by \a _id.
|
---|
82 | *
|
---|
83 | * \param _id identifier of instance
|
---|
84 | */
|
---|
85 | void removeObservedValues(const id _id);
|
---|
86 |
|
---|
87 | /** Erases a vector of observed values of an instance identified by \a _id.
|
---|
88 | *
|
---|
89 | * \param _id identifier of instance
|
---|
90 | */
|
---|
91 | void eraseObservedValues(const id _id);
|
---|
92 |
|
---|
93 | private:
|
---|
94 | typedef std::pair<typename T::ptr, size_t> RefCountedObservedValues_t;
|
---|
95 | typedef std::map<id, RefCountedObservedValues_t> CountedObservedValues_t;
|
---|
96 | //!> internal vector of observed values
|
---|
97 | CountedObservedValues_t ObservedValues;
|
---|
98 |
|
---|
99 | //!> typedef for map with subjectKilledCounts for each instance
|
---|
100 | typedef std::map<id, size_t> subjectKilledCount_t;
|
---|
101 |
|
---|
102 | //!> counts how many ObservedValues have already been subjectKilled()
|
---|
103 | subjectKilledCount_t subjectKilledCount;
|
---|
104 |
|
---|
105 | //!> typedef for the set with ids to be erase
|
---|
106 | typedef std::set<id> MarkedForErase_t;
|
---|
107 |
|
---|
108 | //!> marks ids marked for erase (i.e. all subjectKilled() received)
|
---|
109 | MarkedForErase_t MarkedForErase;
|
---|
110 |
|
---|
111 | //!> name used in describing the instance type
|
---|
112 | const std::string NameOfType;
|
---|
113 |
|
---|
114 | //!> reference to InstanceBoard for callbacks on subjectKilled()
|
---|
115 | QtObservedInstanceBoard &board;
|
---|
116 |
|
---|
117 | //!> callback function when ObservedValues need to be destroyed
|
---|
118 | const onDestroy_t onDestroy;
|
---|
119 |
|
---|
120 | private:
|
---|
121 | /** Internal function to check whether an Observed instance identified by
|
---|
122 | * \a _id is still signOn() to its associated World instance.
|
---|
123 | *
|
---|
124 | * \param _id identifier of instance
|
---|
125 | * \return true - no more signOn()s, false - else
|
---|
126 | */
|
---|
127 | bool checkRefCount(const id _id) const;
|
---|
128 |
|
---|
129 | /** Internal function to check whether any ObservedValue identified by
|
---|
130 | * \a _id is still signOn() to its associated World instance.
|
---|
131 | *
|
---|
132 | * \param _id identifier of instance
|
---|
133 | * \return true - no more signOn()s, false - else
|
---|
134 | */
|
---|
135 | bool checksubjectKilled(const id _id) const;
|
---|
136 |
|
---|
137 | /** Internal function to check whether the vector of ObservedValue's
|
---|
138 | * identified by \a _id has been marked for erase.
|
---|
139 | *
|
---|
140 | * Marked for erase means that it has received all subjectKilled()
|
---|
141 | * (the container not the values themselves).
|
---|
142 | *
|
---|
143 | * \param _id identifier of instance
|
---|
144 | * \return true - marked for erase, false - else
|
---|
145 | */
|
---|
146 | bool checkMarkedForErase(const id _id) const;
|
---|
147 |
|
---|
148 | private:
|
---|
149 | //!> QtObservedInstanceBoard may access anything
|
---|
150 | friend class QtObservedInstanceBoard;
|
---|
151 |
|
---|
152 | /** Inserts a new ObservedValue vector into the container.
|
---|
153 | *
|
---|
154 | * \param _id identifier of instance associated with observed values
|
---|
155 | * \param _obsvalues vector of observed values of instance
|
---|
156 | * \return true - insertion successful, false - else
|
---|
157 | */
|
---|
158 | bool insert(const id _id, const typename T::ptr &_obsvalues);
|
---|
159 |
|
---|
160 | /** Use to change the identifier associated with a vector of observed values.
|
---|
161 | *
|
---|
162 | * \param _oldid old identifier
|
---|
163 | * \param _newid new identifier
|
---|
164 | * \return true - change successful, false - else
|
---|
165 | */
|
---|
166 | bool changeIdentifier(const id _oldid, const id _newid);
|
---|
167 |
|
---|
168 | /** Returns the number of reference delivered for this instance identified by \a _id.
|
---|
169 | *
|
---|
170 | * \param _id identifier of instance
|
---|
171 | * \return reference count
|
---|
172 | */
|
---|
173 | size_t getRefCount(const id _id) const;
|
---|
174 |
|
---|
175 | /** Checks whether a vector of observed values of an instance identified by \a _id
|
---|
176 | * is present.
|
---|
177 | *
|
---|
178 | * \param _id identifier of instance
|
---|
179 | * \return true - present, false - else
|
---|
180 | */
|
---|
181 | bool isPresent(const id _id) const;
|
---|
182 | };
|
---|
183 |
|
---|
184 | #endif /* OBSERVEDVALUESCONTAINER_HPP_ */
|
---|