| 1 | /*
 | 
|---|
| 2 |  * Channels.hpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Dec 1, 2011
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #ifndef CHANNELS_HPP_
 | 
|---|
| 9 | #define CHANNELS_HPP_
 | 
|---|
| 10 | 
 | 
|---|
| 11 | // include config.h
 | 
|---|
| 12 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 13 | #include <config.h>
 | 
|---|
| 14 | #endif
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include <map>
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include <boost/thread/recursive_mutex.hpp>
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "CodePatterns/Observer/defs.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | class Observable;
 | 
|---|
| 23 | class Relay;
 | 
|---|
| 24 | 
 | 
|---|
| 25 | /** Channels aggregate all possible Notifications of an Observable.
 | 
|---|
| 26 |  *
 | 
|---|
| 27 |  * Usually, one implements an enumeration of the channel number which is
 | 
|---|
| 28 |  * visible to the outside only.
 | 
|---|
| 29 |  *
 | 
|---|
| 30 |  * \note Channels::subjectKilled() needs to be called by owning Observable.
 | 
|---|
| 31 |  * It is passed on to Notification such that Observer that have only signed
 | 
|---|
| 32 |  * on to single channel still know when their observable has died.
 | 
|---|
| 33 |  */
 | 
|---|
| 34 | class Channels {
 | 
|---|
| 35 | public:
 | 
|---|
| 36 |   Channels();
 | 
|---|
| 37 |   virtual ~Channels();
 | 
|---|
| 38 | 
 | 
|---|
| 39 |   void addChannel(size_t no);
 | 
|---|
| 40 | 
 | 
|---|
| 41 |   Notification_ptr getChannel(size_t no) const;
 | 
|---|
| 42 |   size_t getType(Notification_ptr channel) const;
 | 
|---|
| 43 | 
 | 
|---|
| 44 | protected:
 | 
|---|
| 45 |   void removeChannel(size_t no);
 | 
|---|
| 46 | 
 | 
|---|
| 47 | private:
 | 
|---|
| 48 |   //! grant Observable access to notifyAll() and subjectKilled()
 | 
|---|
| 49 |   friend class Observable;
 | 
|---|
| 50 |   //!> grant Relay access to notifyAll()
 | 
|---|
| 51 |   friend class Relay;
 | 
|---|
| 52 | 
 | 
|---|
| 53 |   /** Informs channel subscribers about imminent dstor call.
 | 
|---|
| 54 |    *
 | 
|---|
| 55 |    * This is meant to be called from Observable only.
 | 
|---|
| 56 |    * Channels and Notifications are strictly attached to an Observable. Hence,
 | 
|---|
| 57 |    * it makes no sense to inform them on their own. Especially, neither has
 | 
|---|
| 58 |    * any knowledge on the publisher.
 | 
|---|
| 59 |    *
 | 
|---|
| 60 |    * \param *publisher Observable about to be destroyed
 | 
|---|
| 61 |    */
 | 
|---|
| 62 |   void subjectKilled(Observable * const publisher);
 | 
|---|
| 63 | 
 | 
|---|
| 64 | private:
 | 
|---|
| 65 |   typedef std::map< size_t, Notification_ptr> NotificationTypetoRefMap;
 | 
|---|
| 66 | 
 | 
|---|
| 67 |   NotificationTypetoRefMap ChannelMap;
 | 
|---|
| 68 | 
 | 
|---|
| 69 |   mutable boost::recursive_mutex ChannelLock; //!< a lock for the pointer of the instance
 | 
|---|
| 70 | };
 | 
|---|
| 71 | 
 | 
|---|
| 72 | #endif /* CHANNELS_HPP_ */
 | 
|---|