| 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 | 
 | 
|---|
| 90 |   typedef std::map<id, typename T::ptr > CountedObservedValues_t;
 | 
|---|
| 91 |   //!> internal vector of observed values
 | 
|---|
| 92 |   CountedObservedValues_t ObservedValues;
 | 
|---|
| 93 | 
 | 
|---|
| 94 |   //!> typedef for the set with ids to be erase
 | 
|---|
| 95 |   typedef std::set<id> MarkedSet_t;
 | 
|---|
| 96 | 
 | 
|---|
| 97 |   //!> marks ids marked for erase (i.e. all subjectKilled() received)
 | 
|---|
| 98 |   MarkedSet_t MarkedForErase;
 | 
|---|
| 99 | 
 | 
|---|
| 100 |   //!> marks ids marked as connected (i.e. for later subjectKilled() received)
 | 
|---|
| 101 |   MarkedSet_t MarkedForConnected;
 | 
|---|
| 102 | 
 | 
|---|
| 103 |   //!> name used in describing the instance type
 | 
|---|
| 104 |   const std::string NameOfType;
 | 
|---|
| 105 | 
 | 
|---|
| 106 |   //!> reference to InstanceBoard for callbacks on subjectKilled()
 | 
|---|
| 107 |   QtObservedInstanceBoard &board;
 | 
|---|
| 108 | 
 | 
|---|
| 109 |   //!> callback function when ObservedValues need to be destroyed
 | 
|---|
| 110 |   const onDestroy_t onDestroy;
 | 
|---|
| 111 | 
 | 
|---|
| 112 |   //!> internal mutex to have atomic access to control maps
 | 
|---|
| 113 |   mutable boost::recursive_mutex atomic_mutex;
 | 
|---|
| 114 | 
 | 
|---|
| 115 | private:
 | 
|---|
| 116 |   /** Internal function to check whether an Observed instance identified by
 | 
|---|
| 117 |    * \a _id is still signOn() to its associated World instance.
 | 
|---|
| 118 |    *
 | 
|---|
| 119 |    * \param _id identifier of instance
 | 
|---|
| 120 |    * \return true - no more signOn()s, false - else
 | 
|---|
| 121 |    */
 | 
|---|
| 122 |   bool checkMarkedForConnected(const id _id) const;
 | 
|---|
| 123 | 
 | 
|---|
| 124 |   /** Internal function to check whether the vector of ObservedValue's
 | 
|---|
| 125 |    *  identified by \a _id has been marked for erase.
 | 
|---|
| 126 |    *
 | 
|---|
| 127 |    *  Marked for erase means that it has received all subjectKilled()
 | 
|---|
| 128 |    *  (the container not the values themselves).
 | 
|---|
| 129 |    *
 | 
|---|
| 130 |    * \param _id identifier of instance
 | 
|---|
| 131 |    * \return true - marked for erase, false - else
 | 
|---|
| 132 |    */
 | 
|---|
| 133 |   bool checkMarkedForErase(const id _id) const;
 | 
|---|
| 134 | 
 | 
|---|
| 135 |   /** Combines all of the above checks with call to removeObservedValues() on true.
 | 
|---|
| 136 |    *
 | 
|---|
| 137 |    * \param _id identifier of instance
 | 
|---|
| 138 |    */
 | 
|---|
| 139 |   void checkRemoval(const id _id);
 | 
|---|
| 140 | 
 | 
|---|
| 141 | private:
 | 
|---|
| 142 |   //!> QtObservedInstanceBoard may access anything
 | 
|---|
| 143 |   friend class QtObservedInstanceBoard;
 | 
|---|
| 144 | 
 | 
|---|
| 145 |   /** Inserts a new ObservedValue vector into the container.
 | 
|---|
| 146 |    *
 | 
|---|
| 147 |    * \param _id identifier of instance associated with observed values
 | 
|---|
| 148 |    * \param _obsvalues vector of observed values of instance
 | 
|---|
| 149 |    */
 | 
|---|
| 150 |   void insert(const id _id, const typename T::ptr &_obsvalues);
 | 
|---|
| 151 | 
 | 
|---|
| 152 |   /** Use to change the identifier associated with a vector of observed values.
 | 
|---|
| 153 |    *
 | 
|---|
| 154 |    * \param _oldid old identifier
 | 
|---|
| 155 |    * \param _newid new identifier
 | 
|---|
| 156 |    * \return true - change successful, false - else
 | 
|---|
| 157 |    */
 | 
|---|
| 158 |   bool changeIdentifier(const id _oldid, const id _newid);
 | 
|---|
| 159 | 
 | 
|---|
| 160 |   /** Checks whether a vector of observed values of an instance  identified by \a _id
 | 
|---|
| 161 |    * is present.
 | 
|---|
| 162 |    *
 | 
|---|
| 163 |    * \param _id identifier of instance
 | 
|---|
| 164 |    * \return true - present, false - else
 | 
|---|
| 165 |    */
 | 
|---|
| 166 |   bool isPresent(const id _id) const;
 | 
|---|
| 167 | };
 | 
|---|
| 168 | 
 | 
|---|
| 169 | #endif /* OBSERVEDVALUESCONTAINER_HPP_ */
 | 
|---|