| 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 | /** Inform this container that subjectKilled() was received by one of the ObservedValues.
|
|---|
| 77 | *
|
|---|
| 78 | * \param _id identifier of the receiving instance
|
|---|
| 79 | */
|
|---|
| 80 | void countsubjectKilled(const id _id);
|
|---|
| 81 |
|
|---|
| 82 | /** Prepares removeal a vector of observed values of an instance identified by \a _id.
|
|---|
| 83 | *
|
|---|
| 84 | * \param _id identifier of instance
|
|---|
| 85 | */
|
|---|
| 86 | void removeObservedValues(const id _id);
|
|---|
| 87 |
|
|---|
| 88 | /** Erases a vector of observed values of an instance identified by \a _id.
|
|---|
| 89 | *
|
|---|
| 90 | * \param _id identifier of instance
|
|---|
| 91 | */
|
|---|
| 92 | void eraseObservedValues(const id _id);
|
|---|
| 93 |
|
|---|
| 94 | private:
|
|---|
| 95 | /** \note CountedObservedValues_t needs to contain a list in the second argument
|
|---|
| 96 | * as the ObservedValuesContainer may fall behind when a single atom is created
|
|---|
| 97 | * and destroyed a number of times in a row (e.g. in the undo/redo cases). This
|
|---|
| 98 | * problem is inherent because on the one hand we keep the QtObservedAtom alive
|
|---|
| 99 | * till the last moment, i.e. till the GUI does not need it anymore where the
|
|---|
| 100 | * original instance may long have been removed from the World. On the other hand
|
|---|
| 101 | * we instantiate the e.g. QtObservedAtom as soon as the observed atom is created.
|
|---|
| 102 | */
|
|---|
| 103 |
|
|---|
| 104 | /** Structure to contain the sequence of created observed memories.
|
|---|
| 105 | *
|
|---|
| 106 | * \note QtGUI may lag behind the creation/destruction of observed instances a
|
|---|
| 107 | * lot but one after the other must be served. Here, we keep an additional
|
|---|
| 108 | * iterator that indicates the current instance to be served.
|
|---|
| 109 | */
|
|---|
| 110 | struct RefCountedObserved
|
|---|
| 111 | {
|
|---|
| 112 | typedef std::pair<typename T::ptr, size_t> Value_t;
|
|---|
| 113 | typedef std::deque<Value_t> Values_t;
|
|---|
| 114 |
|
|---|
| 115 | /** Default cstor of RefCountedObservedValues.
|
|---|
| 116 | *
|
|---|
| 117 | * Make sure that currentvalue has some sensible default value.
|
|---|
| 118 | */
|
|---|
| 119 | RefCountedObserved() :
|
|---|
| 120 | tobedestroyed(values.end()),
|
|---|
| 121 | currentvalue(values.end())
|
|---|
| 122 | {}
|
|---|
| 123 |
|
|---|
| 124 | /** Default copy cstor of RefCountedObservedValues.
|
|---|
| 125 | *
|
|---|
| 126 | * \param _instance instance to copy
|
|---|
| 127 | */
|
|---|
| 128 | RefCountedObserved(const RefCountedObserved & _instance) :
|
|---|
| 129 | values(_instance.values),
|
|---|
| 130 | tobedestroyed(values.begin()),
|
|---|
| 131 | currentvalue(values.begin())
|
|---|
| 132 | {}
|
|---|
| 133 |
|
|---|
| 134 | /** Cstor of RefCountedObservedValues.
|
|---|
| 135 | *
|
|---|
| 136 | * Make sure that currentvalue has some sensible default value.
|
|---|
| 137 | *
|
|---|
| 138 | * \param _value initial value for the list
|
|---|
| 139 | */
|
|---|
| 140 | RefCountedObserved(Value_t _value) :
|
|---|
| 141 | values(1, _value),
|
|---|
| 142 | tobedestroyed(values.begin()),
|
|---|
| 143 | currentvalue(values.begin())
|
|---|
| 144 | {}
|
|---|
| 145 |
|
|---|
| 146 | bool empty() const
|
|---|
| 147 | {
|
|---|
| 148 | return values.empty();
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | void push_back(Value_t _value)
|
|---|
| 152 | {
|
|---|
| 153 | values.push_back(_value);
|
|---|
| 154 | if (currentvalue == values.end())
|
|---|
| 155 | currentvalue = values.end()--;
|
|---|
| 156 | if (tobedestroyed == values.end())
|
|---|
| 157 | tobedestroyed = currentvalue;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | Value_t& getCurrentValue() const
|
|---|
| 161 | {
|
|---|
| 162 | ASSERT (currentvalue != values.end(),
|
|---|
| 163 | "RefCountedObservedValues::getCurrentValue() - cannot get, no values present.");
|
|---|
| 164 | return *currentvalue;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | Value_t& getEraseCandidate() const
|
|---|
| 168 | {
|
|---|
| 169 | ASSERT (tobedestroyed != values.end(),
|
|---|
| 170 | "RefCountedObservedValues::getCurrentValue() - cannot get, no values present.");
|
|---|
| 171 | return *tobedestroyed;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | void eraseCurrentValue()
|
|---|
| 175 | {
|
|---|
| 176 | ASSERT (tobedestroyed != values.end(),
|
|---|
| 177 | "RefCountedObservedValues::eraseCurrentValue() - cannot erase, no values present.");
|
|---|
| 178 | if (tobedestroyed == currentvalue)
|
|---|
| 179 | ++currentvalue;
|
|---|
| 180 | values.erase(tobedestroyed);
|
|---|
| 181 | tobedestroyed = currentvalue;
|
|---|
| 182 | // removal of this instance when both iterators point to end is done inside QtObservedInstanceBoard
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | //!> set of values
|
|---|
| 186 | Values_t values;
|
|---|
| 187 | //!> iterator on the value that is next gonna be destroyed
|
|---|
| 188 | typename Values_t::iterator tobedestroyed;
|
|---|
| 189 | //!> iterator on the current value that is served on get
|
|---|
| 190 | typename Values_t::iterator currentvalue;
|
|---|
| 191 | };
|
|---|
| 192 |
|
|---|
| 193 | typedef std::map<id, RefCountedObserved > CountedObservedValues_t;
|
|---|
| 194 | //!> internal vector of observed values
|
|---|
| 195 | CountedObservedValues_t ObservedValues;
|
|---|
| 196 |
|
|---|
| 197 | //!> typedef for map with subjectKilledCounts for each instance
|
|---|
| 198 | typedef std::map<id, size_t> subjectKilledCount_t;
|
|---|
| 199 |
|
|---|
| 200 | //!> counts how many ObservedValues have already been subjectKilled()
|
|---|
| 201 | subjectKilledCount_t subjectKilledCount;
|
|---|
| 202 |
|
|---|
| 203 | //!> typedef for the set with ids to be erase
|
|---|
| 204 | typedef std::set<id> MarkedForErase_t;
|
|---|
| 205 |
|
|---|
| 206 | //!> marks ids marked for erase (i.e. all subjectKilled() received)
|
|---|
| 207 | MarkedForErase_t MarkedForErase;
|
|---|
| 208 |
|
|---|
| 209 | //!> name used in describing the instance type
|
|---|
| 210 | const std::string NameOfType;
|
|---|
| 211 |
|
|---|
| 212 | //!> reference to InstanceBoard for callbacks on subjectKilled()
|
|---|
| 213 | QtObservedInstanceBoard &board;
|
|---|
| 214 |
|
|---|
| 215 | //!> callback function when ObservedValues need to be destroyed
|
|---|
| 216 | const onDestroy_t onDestroy;
|
|---|
| 217 |
|
|---|
| 218 | private:
|
|---|
| 219 | /** Internal function to check whether an Observed instance identified by
|
|---|
| 220 | * \a _id is still signOn() to its associated World instance.
|
|---|
| 221 | *
|
|---|
| 222 | * \param _id identifier of instance
|
|---|
| 223 | * \return true - no more signOn()s, false - else
|
|---|
| 224 | */
|
|---|
| 225 | bool checkRefCount(const id _id) const;
|
|---|
| 226 |
|
|---|
| 227 | /** Internal function to check whether any ObservedValue identified by
|
|---|
| 228 | * \a _id is still signOn() to its associated World instance.
|
|---|
| 229 | *
|
|---|
| 230 | * \param _id identifier of instance
|
|---|
| 231 | * \return true - no more signOn()s, false - else
|
|---|
| 232 | */
|
|---|
| 233 | bool checksubjectKilled(const id _id) const;
|
|---|
| 234 |
|
|---|
| 235 | /** Internal function to check whether the vector of ObservedValue's
|
|---|
| 236 | * identified by \a _id has been marked for erase.
|
|---|
| 237 | *
|
|---|
| 238 | * Marked for erase means that it has received all subjectKilled()
|
|---|
| 239 | * (the container not the values themselves).
|
|---|
| 240 | *
|
|---|
| 241 | * \param _id identifier of instance
|
|---|
| 242 | * \return true - marked for erase, false - else
|
|---|
| 243 | */
|
|---|
| 244 | bool checkMarkedForErase(const id _id) const;
|
|---|
| 245 |
|
|---|
| 246 | private:
|
|---|
| 247 | //!> QtObservedInstanceBoard may access anything
|
|---|
| 248 | friend class QtObservedInstanceBoard;
|
|---|
| 249 |
|
|---|
| 250 | /** Inserts a new ObservedValue vector into the container.
|
|---|
| 251 | *
|
|---|
| 252 | * \param _id identifier of instance associated with observed values
|
|---|
| 253 | * \param _obsvalues vector of observed values of instance
|
|---|
| 254 | */
|
|---|
| 255 | void insert(const id _id, const typename T::ptr &_obsvalues);
|
|---|
| 256 |
|
|---|
| 257 | /** Use to change the identifier associated with a vector of observed values.
|
|---|
| 258 | *
|
|---|
| 259 | * \param _oldid old identifier
|
|---|
| 260 | * \param _newid new identifier
|
|---|
| 261 | * \return true - change successful, false - else
|
|---|
| 262 | */
|
|---|
| 263 | bool changeIdentifier(const id _oldid, const id _newid);
|
|---|
| 264 |
|
|---|
| 265 | /** Checks whether a vector of observed values of an instance identified by \a _id
|
|---|
| 266 | * is present.
|
|---|
| 267 | *
|
|---|
| 268 | * \param _id identifier of instance
|
|---|
| 269 | * \return true - present, false - else
|
|---|
| 270 | */
|
|---|
| 271 | bool isPresent(const id _id) const;
|
|---|
| 272 | };
|
|---|
| 273 |
|
|---|
| 274 | #endif /* OBSERVEDVALUESCONTAINER_HPP_ */
|
|---|