Ignore:
Timestamp:
Sep 1, 2011, 11:07:47 AM (14 years ago)
Author:
Frederik Heber <heber@…>
Children:
9e776f
Parents:
451f17
git-author:
Frederik Heber <heber@…> (09/01/11 11:02:49)
git-committer:
Frederik Heber <heber@…> (09/01/11 11:07:47)
Message:

Extended Observables by NotificationChannels member variable.

  • new class Channels that aggregates Notifications.
  • NOTIFY now just takes a channel number.
  • may or may not be instantiated for a given Observable.
  • Observable just needs to add enum of all desired channels and addChannel on each of these.
  • ObserverUnitTest has been adapted.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Patterns/Observer.hpp

    r451f17 r74e0f7  
    4040#endif
    4141
     42class Channels;
     43class Notification;
    4244class Observable;
    43 class Notification;
    4445
    4546// Pointers to notifications are used for unique identification
     
    145146  virtual bool isBlocked();
    146147
     148  Notification_ptr getChannel(size_t no) const;
     149
    147150protected:
    148151  virtual void update(Observable *publisher);
     
    164167
    165168  static void enque_notification_internal(Observable *publisher, Notification_ptr notification);
     169
     170  Channels *NotificationChannels;
    166171
    167172private:
     
    192197};
    193198
     199/** Notifications are sort of new channels of an Observable.
     200 * Via the NOTIFY() macro updates can be transmitted in a specific channel.
     201 * Observers can subscribe to Notification in much the same way as they can to
     202 * the Observable itself. Usually, Notifications
     203 */
    194204class Notification {
    195205  friend class Observable;
     206  friend class Channels;
    196207public:
    197208  Notification(Observable *_owner);
     209  Notification(Observable *_owner, size_t _channelno);
    198210  virtual ~Notification();
     211
     212  size_t getChannelNo() const { return channelno; }
    199213protected:
    200214
     
    206220  Observable * const owner;
    207221  std::set<Observer*> targets;
     222  size_t channelno;
     223};
     224
     225/** Channels aggregate all possible Notifications of an Observable.
     226 *
     227 * Usually, one implements an enumeration of the channel number which is
     228 * visible to the outside only.
     229 *
     230 */
     231class Channels {
     232public:
     233  Channels(Observable *_owner);
     234  virtual ~Channels();
     235
     236  void addChannel(size_t no);
     237
     238  Notification_ptr getChannel(size_t no) const;
     239  size_t getType(Notification_ptr channel) const;
     240
     241protected:
     242  void removeChannel(size_t no);
     243
     244private:
     245  Observable * const owner;
     246
     247  typedef std::map< size_t, Notification_ptr> NotificationTypetoRefMap;
     248
     249  NotificationTypetoRefMap ChannelMap;
    208250};
    209251
     
    245287#define PASTE_HELPER(a,b) a ## b
    246288#define OBSERVE Observable::_Observable_protector PASTE(_scope_obs_protector_,__LINE__)(this)
    247 #define NOTIFY(notification) do{Observable::enque_notification_internal(this,notification);}while(0)
     289#define NOTIFY(channelno) do{Observable::enque_notification_internal(this,NotificationChannels->getChannel(channelno));}while(0)
    248290#define LOCK_OBSERVABLE(observable) Observable::_Observable_protector PASTE(_scope_obs_protector_,__LINE__)(&(observable))
    249291
Note: See TracChangeset for help on using the changeset viewer.