Ignore:
Timestamp:
Dec 13, 2011, 12:02:21 PM (14 years ago)
Author:
Frederik Heber <heber@…>
Children:
3324cf
Parents:
d85532
git-author:
Frederik Heber <heber@…> (12/02/11 13:13:44)
git-committer:
Frederik Heber <heber@…> (12/13/11 12:02:21)
Message:

Relay can now also relay notifications.

  • NOTE: We do not yet notify the Observables, whose update() we combine, when the Relay is destroyed. They have to signOff before by themselves (or by some other means, e.g. be destroyed before.).
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Patterns/Observer/Relay.cpp

    rd85532 rb760ac5  
    2424#include "Assert.hpp"
    2525#include "Channels.hpp"
    26 #include "defs.hpp"
    2726#include "Notification.hpp"
    2827
     
    4847  observerLog().addMessage() << "-- Destroying Relay " << observerLog().getName(this);
    4948#endif
    50 }
    51 
     49  // killing subjects is done by Observables' dstor
     50}
     51
     52
     53
     54/** Sign on an Observer to this Observable.
     55 * Puts \a *target into Observable::callTable list.
     56 * \param *target Observer
     57 * \param priority number in [-20,20]
     58 */
     59void Relay::signOn(Observer *target, PriorityLevel priority) const
     60{
     61#ifdef LOG_OBSERVER
     62  observerLog().addMessage() << "@@ Signing on " << observerLog().getName(target)
     63      << " to "
     64      << observerLog().getName(const_cast<Observable *>(static_cast<const Observable * const>(this)));
     65#endif
     66  bool res = false;
     67  callees_t &callees = callTable[const_cast<Observable *>(static_cast<const Observable * const>(this))];
     68
     69  callees_t::iterator iter;
     70  for(iter=callees.begin();iter!=callees.end();++iter){
     71    res |= ((*iter).second == target);
     72  }
     73  if(!res)
     74    callees.insert(std::pair<int,Observer*>(priority.level,target));
     75}
     76
     77/** Sign off an Observer from this Observable.
     78 * Removes \a *target from Observable::callTable list.
     79 * \param *target Observer
     80 */
     81void Relay::signOff(Observer *target) const
     82{
     83  ASSERT(callTable.count(const_cast<Observable *>(static_cast<const Observable * const>(this))),
     84      "Relay::signOff() - called for an Observable without Observers.");
     85#ifdef LOG_OBSERVER
     86  observerLog().addMessage() << "** Signing off " << observerLog().getName(target)
     87      << " from "
     88      << observerLog().getName(const_cast<Observable *>(static_cast<const Observable * const>(this)));
     89#endif
     90  callees_t &callees = callTable[const_cast<Observable *>(static_cast<const Observable * const>(this))];
     91
     92  callees_t::iterator iter;
     93  callees_t::iterator deliter;
     94  for(iter=callees.begin();iter!=callees.end();) {
     95    if((*iter).second == target) {
     96      callees.erase(iter++);
     97    }
     98    else {
     99      ++iter;
     100    }
     101  }
     102  if(callees.empty()){
     103    callTable.erase(const_cast<Observable *>(static_cast<const Observable * const>(this)));
     104  }
     105}
     106
     107void Relay::signOn(Observer *target, size_t channelno) const
     108{
     109  Notification_ptr notification = getChannel(channelno);
     110  notification->addObserver(target);
     111}
     112
     113void Relay::signOff(Observer *target, size_t channelno) const
     114{
     115  Notification_ptr notification = getChannel(channelno);
     116  notification->removeObserver(target);
     117}
    52118
    53119/** Notify all Observers of changes.
     
    125191      Updater = publisher;
    126192      notifyAll();
     193      Updater = NULL;
    127194    }
    128195    else{
     
    135202}
    136203
    137 //Notification_ptr Relay::getChannel(size_t no) const
    138 //{
    139 //  ASSERT(NotificationChannels != NULL,
    140 //      "Relay::getChannel() - observable has no channels.");
    141 //  return NotificationChannels->getChannel(no);
    142 //}
     204/** Method for receiving specialized notifications.
     205 *
     206 * \param *publisher The \a *this we observe.
     207 * \param notification type of notification
     208 */
     209void Relay::recieveNotification(Observable *publisher, Notification_ptr notification)
     210{
     211  Updater = publisher;
     212  const Channels *myChannels = NotificationChannels[const_cast<Observable *>(static_cast<const Observable * const>(this))];
     213  ASSERT(myChannels != NULL,
     214      "Relay::recieveNotification() - this relay does not have any channels.");
     215  const size_t channelno = notification->getChannelNo();
     216  Notification_ptr mynotification = myChannels->getChannel(channelno);
     217  ASSERT(mynotification != NULL,
     218      "Relay::recieveNotification() - this relay does not have a notification no "+toString(channelno)+".");
     219  mynotification->notifyAll(Updater);
     220  Updater = NULL;
     221}
    143222
    144223/** Handles sub-observables that just got killed
Note: See TracChangeset for help on using the changeset viewer.