Ignore:
Timestamp:
Sep 1, 2011, 1:51:25 PM (14 years ago)
Author:
Frederik Heber <heber@…>
Children:
d45509
Parents:
74e0f7
Message:

Observable::signOn/Off functions are now on const instance possible.

  • maps of Observable are static anyway.
  • we need some const_casts as instances in maps (which we need as lookup key in sign on/off) are non-const.
  • whether there are more or fewer observers does not impact on the constness of a class but only affects a mutable part.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Patterns/Observer.cpp

    r74e0f7 r9e776f  
    211211 * \param priority number in [-20,20]
    212212 */
    213 void Observable::signOn(Observer *target,int priority) {
     213void Observable::signOn(Observer *target,int priority) const
     214{
    214215  ASSERT(priority>=-20 && priority<=+20, "Priority out of range [-20:+20] when signing on Observer");
    215216#ifdef LOG_OBSERVER
    216   observerLog().addMessage() << "@@ Signing on " << observerLog().getName(target) << " to " << observerLog().getName(this) << endl;
     217  observerLog().addMessage() << "@@ Signing on " << observerLog().getName(target) << " to " << observerLog().getName(const_cast<Observable *>(this)) << endl;
    217218#endif
    218219  bool res = false;
    219   callees_t &callees = callTable[this];
     220  callees_t &callees = callTable[const_cast<Observable *>(this)];
    220221
    221222  callees_t::iterator iter;
     
    231232 * \param *target Observer
    232233 */
    233 void Observable::signOff(Observer *target) {
    234   ASSERT(callTable.count(this),"SignOff called for an Observable without Observers.");
    235 #ifdef LOG_OBSERVER
    236   observerLog().addMessage() << "** Signing off " << observerLog().getName(target) << " from " << observerLog().getName(this) << endl;
    237 #endif
    238   callees_t &callees = callTable[this];
     234void Observable::signOff(Observer *target) const
     235{
     236  ASSERT(callTable.count(const_cast<Observable *>(this)),"SignOff called for an Observable without Observers.");
     237#ifdef LOG_OBSERVER
     238  observerLog().addMessage() << "** Signing off " << observerLog().getName(target) << " from " << observerLog().getName(const_cast<Observable *>(this)) << endl;
     239#endif
     240  callees_t &callees = callTable[const_cast<Observable *>(this)];
    239241
    240242  callees_t::iterator iter;
     
    249251  }
    250252  if(callees.empty()){
    251     callTable.erase(this);
    252   }
    253 }
    254 
    255 void Observable::signOn(Observer *target, Notification_ptr notification){
     253    callTable.erase(const_cast<Observable *>(this));
     254  }
     255}
     256
     257void Observable::signOn(Observer *target, Notification_ptr notification) const
     258{
    256259  ASSERT(notification->owner==this,
    257260         "Trying to sign on for a notification that is not provided by this object");
     
    260263}
    261264
    262 void Observable::signOff(Observer *target, Notification_ptr notification){
     265void Observable::signOff(Observer *target, Notification_ptr notification) const
     266{
    263267  ASSERT(notification->owner==this,
    264268         "Trying to sign off from a notification that is not provided by this object");
     
    267271}
    268272
    269 bool Observable::isBlocked(){
    270   return depth.count(this) > 0;
     273bool Observable::isBlocked() const
     274{
     275  return depth.count(const_cast<Observable *>(this)) > 0;
    271276}
    272277
Note: See TracChangeset for help on using the changeset viewer.