| 1 | /*
 | 
|---|
| 2 |  * GlobalObservableInfo.hpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Jul 4, 2015
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #ifndef GLOBALOBSERVABLEINFO_HPP_
 | 
|---|
| 9 | #define GLOBALOBSERVABLEINFO_HPP_
 | 
|---|
| 10 | 
 | 
|---|
| 11 | // include config.h
 | 
|---|
| 12 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 13 | #include <config.h>
 | 
|---|
| 14 | #endif
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include <map>
 | 
|---|
| 17 | #include <set>
 | 
|---|
| 18 | 
 | 
|---|
| 19 | #include "CodePatterns/Range.hpp"
 | 
|---|
| 20 | #include "CodePatterns/Singleton.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include <boost/thread/recursive_mutex.hpp>
 | 
|---|
| 23 | 
 | 
|---|
| 24 | class Channels;
 | 
|---|
| 25 | class Observable;
 | 
|---|
| 26 | class Observer;
 | 
|---|
| 27 | class Notification;
 | 
|---|
| 28 | 
 | 
|---|
| 29 | /** This class contains all global maps storing information about observers
 | 
|---|
| 30 |  * and which is signed on to which Observable.
 | 
|---|
| 31 |  *
 | 
|---|
| 32 |  * All infrastructure for the observer-pattern is bundled at a central place
 | 
|---|
| 33 |  * this is more efficient if many objects can be observed (inherit from observable)
 | 
|---|
| 34 |  * but only few are actually coupled with observers. E.g. TMV has over 500.000 Atoms,
 | 
|---|
| 35 |  * which might become observable. Handling Observable infrastructure in each of
 | 
|---|
| 36 |  * these would use memory for each atom. By handling Observer-infrastructure
 | 
|---|
| 37 |  * here we only need memory for objects that actually are observed.
 | 
|---|
| 38 |  * See [Gamma et al, 1995] p. 297
 | 
|---|
| 39 |  *
 | 
|---|
| 40 |  * We have transfered this information into a single singleton as we need to
 | 
|---|
| 41 |  * control the time when the information is destroyed. Objects in here must
 | 
|---|
| 42 |  * remain valid and accessible till the very last Observable in some other
 | 
|---|
| 43 |  * module (i.e. not in this library) is destroyed.
 | 
|---|
| 44 |  */
 | 
|---|
| 45 | class GlobalObservableInfo : public Singleton<GlobalObservableInfo>
 | 
|---|
| 46 | {
 | 
|---|
| 47 |   //!> singleton pattern is friend to access private cstor
 | 
|---|
| 48 |   friend class Singleton<GlobalObservableInfo>;
 | 
|---|
| 49 | 
 | 
|---|
| 50 | private:
 | 
|---|
| 51 |   /** Private cstor for GlobalObservableInfo.
 | 
|---|
| 52 |    *
 | 
|---|
| 53 |    * Must be private as is singleton.
 | 
|---|
| 54 |    *
 | 
|---|
| 55 |    */
 | 
|---|
| 56 |   GlobalObservableInfo();
 | 
|---|
| 57 | 
 | 
|---|
| 58 |   /** Dstor for GlobalObservableInfo.
 | 
|---|
| 59 |    *
 | 
|---|
| 60 |    */
 | 
|---|
| 61 |   ~GlobalObservableInfo();
 | 
|---|
| 62 | 
 | 
|---|
| 63 | public:
 | 
|---|
| 64 |   /** This class is only used to distinguish from size_t in the overload.
 | 
|---|
| 65 |    *
 | 
|---|
| 66 |    * It encapsulates a const int (the priority level) and checks valid bounds
 | 
|---|
| 67 |    * in constructor.
 | 
|---|
| 68 |    *
 | 
|---|
| 69 |    */
 | 
|---|
| 70 |   class PriorityLevel {
 | 
|---|
| 71 |   public:
 | 
|---|
| 72 |     /** Constructor of PriorityLevel.
 | 
|---|
| 73 |      *
 | 
|---|
| 74 |      * \note We check whether the level is within Observable::PriorityLevel::ValidRange.
 | 
|---|
| 75 |      *
 | 
|---|
| 76 |      * @param i priority level encapsulated in this class.
 | 
|---|
| 77 |      */
 | 
|---|
| 78 |     explicit PriorityLevel(const int i);
 | 
|---|
| 79 |     ~PriorityLevel();
 | 
|---|
| 80 | 
 | 
|---|
| 81 |     const int level;
 | 
|---|
| 82 |   private:
 | 
|---|
| 83 |     friend class GlobalObservableInfo;
 | 
|---|
| 84 | 
 | 
|---|
| 85 |     static range<int> ValidRange;
 | 
|---|
| 86 |   };
 | 
|---|
| 87 | 
 | 
|---|
| 88 | public:
 | 
|---|
| 89 |   boost::recursive_mutex& getObservablesMapMutex()
 | 
|---|
| 90 |   { return ObservablesMapLock; }
 | 
|---|
| 91 | 
 | 
|---|
| 92 |   typedef std::multimap<int,Observer*> callees_t;
 | 
|---|
| 93 |   typedef std::set<Notification*> notificationSet;
 | 
|---|
| 94 | 
 | 
|---|
| 95 |   std::map<Observable*, int>& getdepth()
 | 
|---|
| 96 |   { return depth; }
 | 
|---|
| 97 |   typedef std::map<Observable*,callees_t > calltable_t;
 | 
|---|
| 98 |   calltable_t& getcallTable()
 | 
|---|
| 99 |   { return callTable; }
 | 
|---|
| 100 |   std::map<Observable*,std::set<Notification*> >& getnotifications()
 | 
|---|
| 101 |   { return notifications; }
 | 
|---|
| 102 |   std::set<Observable*>& getbusyObservables()
 | 
|---|
| 103 |   { return busyObservables; }
 | 
|---|
| 104 | 
 | 
|---|
| 105 |   const range<int>& getValidRange()
 | 
|---|
| 106 |   { return PriorityLevel::ValidRange; }
 | 
|---|
| 107 | 
 | 
|---|
| 108 |   static const PriorityLevel PriorityDefault;
 | 
|---|
| 109 | 
 | 
|---|
| 110 | private:
 | 
|---|
| 111 |   std::map<Observable*, int> depth;
 | 
|---|
| 112 |   std::map<Observable*,callees_t> callTable;
 | 
|---|
| 113 |   std::map<Observable*,notificationSet> notifications;
 | 
|---|
| 114 |   std::set<Observable*> busyObservables;
 | 
|---|
| 115 | 
 | 
|---|
| 116 |   boost::recursive_mutex ObservablesMapLock; //!< a lock for the pointer of the instance
 | 
|---|
| 117 | };
 | 
|---|
| 118 | 
 | 
|---|
| 119 | 
 | 
|---|
| 120 | #endif /* GLOBALOBSERVABLEINFO_HPP_ */
 | 
|---|