| 1 | /*
 | 
|---|
| 2 |  * Notification.hpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Dec 1, 2011
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #ifndef NOTIFICATION_HPP_
 | 
|---|
| 9 | #define NOTIFICATION_HPP_
 | 
|---|
| 10 | 
 | 
|---|
| 11 | // include config.h
 | 
|---|
| 12 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 13 | #include <config.h>
 | 
|---|
| 14 | #endif
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include <set>
 | 
|---|
| 17 | 
 | 
|---|
| 18 | class Channels;
 | 
|---|
| 19 | class Observable;
 | 
|---|
| 20 | class Observer;
 | 
|---|
| 21 | class Relay;
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /** Notifications are sort of news channels of an Observable.
 | 
|---|
| 24 |  * Via the NOTIFY() macro updates can be transmitted in a specific channel.
 | 
|---|
| 25 |  * Observers can subscribe to Notification in much the same way as they can to
 | 
|---|
| 26 |  * the Observable itself. Usually, Notifications are used along
 | 
|---|
| 27 |  * with the usual OBSERVE() macro to generate both the specific and
 | 
|---|
| 28 |  * the global message of change.
 | 
|---|
| 29 |  *
 | 
|---|
| 30 |  * \note Notification::subjectKilled() needs to be called by owning Channels.
 | 
|---|
| 31 |  * We use the passed on Observable instance to let Observers that have only 
 | 
|---|
| 32 |  * signed on to single channel know when their observable has died.
 | 
|---|
| 33 |  */
 | 
|---|
| 34 | class Notification {
 | 
|---|
| 35 | public:
 | 
|---|
| 36 |   Notification(size_t _channelno);
 | 
|---|
| 37 |   virtual ~Notification();
 | 
|---|
| 38 | 
 | 
|---|
| 39 |   size_t getChannelNo() const { return channelno; }
 | 
|---|
| 40 | 
 | 
|---|
| 41 | protected:
 | 
|---|
| 42 | 
 | 
|---|
| 43 |   void addObserver(Observer *target);
 | 
|---|
| 44 |   void removeObserver(Observer *target);
 | 
|---|
| 45 | 
 | 
|---|
| 46 | private:
 | 
|---|
| 47 |   //!> grant Observable access to notifyAll() and subjectKilled()
 | 
|---|
| 48 |   friend class Observable;
 | 
|---|
| 49 |   //!> grant Channels access to notifyAll() and subjectKilled()
 | 
|---|
| 50 |   friend class Channels;
 | 
|---|
| 51 |   //!> grant Relay access to notifyAll()
 | 
|---|
| 52 |   friend class Relay;
 | 
|---|
| 53 | 
 | 
|---|
| 54 |   void notifyAll(Observable * const publisher);
 | 
|---|
| 55 | 
 | 
|---|
| 56 |   /** Informs channel subscribers about dstor call.
 | 
|---|
| 57 |    *
 | 
|---|
| 58 |    * This is meant to be called from Observable only.
 | 
|---|
| 59 |    * Channels and Notifications are strictly attached to an Observable. Hence,
 | 
|---|
| 60 |    * it makes no sense to inform them on their own. Especially, neither has
 | 
|---|
| 61 |    * any knowledge on the publisher.
 | 
|---|
| 62 |    *
 | 
|---|
| 63 |    * \param *publisher Observable about to be destroyed
 | 
|---|
| 64 |    */
 | 
|---|
| 65 |   void subjectKilled(Observable * const publisher);
 | 
|---|
| 66 | 
 | 
|---|
| 67 |   /** Getter for the number of Observer for this notification (channel).
 | 
|---|
| 68 |    *
 | 
|---|
| 69 |    * \return number of added observers
 | 
|---|
| 70 |    */
 | 
|---|
| 71 |   size_t getNumberOfObservers() const
 | 
|---|
| 72 |   { return targets.size(); }
 | 
|---|
| 73 | 
 | 
|---|
| 74 | private:
 | 
|---|
| 75 |   typedef std::set<Observer*> targets_t;
 | 
|---|
| 76 |   targets_t targets;
 | 
|---|
| 77 |   const size_t channelno;
 | 
|---|
| 78 | };
 | 
|---|
| 79 | 
 | 
|---|
| 80 | 
 | 
|---|
| 81 | 
 | 
|---|
| 82 | #endif /* NOTIFICATION_HPP_ */
 | 
|---|