| 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 "CodePatterns/Observer/defs.hpp" | 
|---|
| 19 |  | 
|---|
| 20 | class Observable; | 
|---|
| 21 | class Relay; | 
|---|
| 22 |  | 
|---|
| 23 | /** Channels aggregate all possible Notifications of an Observable. | 
|---|
| 24 | * | 
|---|
| 25 | * Usually, one implements an enumeration of the channel number which is | 
|---|
| 26 | * visible to the outside only. | 
|---|
| 27 | * | 
|---|
| 28 | * \note Channels::subjectKilled() needs to be called by owning Observable. | 
|---|
| 29 | * It is passed on to Notification such that Observer that have only signed | 
|---|
| 30 | * on to single channel still know when their observable has died. | 
|---|
| 31 | */ | 
|---|
| 32 | class Channels { | 
|---|
| 33 | public: | 
|---|
| 34 | Channels(); | 
|---|
| 35 | virtual ~Channels(); | 
|---|
| 36 |  | 
|---|
| 37 | void addChannel(size_t no); | 
|---|
| 38 |  | 
|---|
| 39 | Notification_ptr getChannel(size_t no) const; | 
|---|
| 40 | size_t getType(Notification_ptr channel) const; | 
|---|
| 41 |  | 
|---|
| 42 | protected: | 
|---|
| 43 | void removeChannel(size_t no); | 
|---|
| 44 |  | 
|---|
| 45 | private: | 
|---|
| 46 | //! grant Observable access to notifyAll() and subjectKilled() | 
|---|
| 47 | friend class Observable; | 
|---|
| 48 | //!> grant Relay access to notifyAll() | 
|---|
| 49 | friend class Relay; | 
|---|
| 50 |  | 
|---|
| 51 | /** Informs channel subscribers about imminent dstor call. | 
|---|
| 52 | * | 
|---|
| 53 | * This is meant to be called from Observable only. | 
|---|
| 54 | * Channels and Notifications are strictly attached to an Observable. Hence, | 
|---|
| 55 | * it makes no sense to inform them on their own. Especially, neither has | 
|---|
| 56 | * any knowledge on the publisher. | 
|---|
| 57 | * | 
|---|
| 58 | * \param *publisher Observable about to be destroyed | 
|---|
| 59 | */ | 
|---|
| 60 | void subjectKilled(Observable * const publisher); | 
|---|
| 61 |  | 
|---|
| 62 | private: | 
|---|
| 63 | typedef std::map< size_t, Notification_ptr> NotificationTypetoRefMap; | 
|---|
| 64 |  | 
|---|
| 65 | NotificationTypetoRefMap ChannelMap; | 
|---|
| 66 | }; | 
|---|
| 67 |  | 
|---|
| 68 | #endif /* CHANNELS_HPP_ */ | 
|---|