Ignore:
Timestamp:
Oct 30, 2015, 11:43:20 AM (10 years ago)
Author:
Frederik Heber <heber@…>
Children:
6e2f3b
Parents:
1f96ec
git-author:
Frederik Heber <heber@…> (07/10/15 09:44:07)
git-committer:
Frederik Heber <heber@…> (10/30/15 11:43:20)
Message:

Added static functions to control access to static NotificationChannels.

  • Relay is friend as it uses the mutex when accessing.
  • this is to protect changes in NotificationChannels from changes outside the scope of this library.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Observer/Observable.cpp

    r1f96ec rdd7c44  
    335335Notification_ptr Observable::getChannel(size_t no) const
    336336{
    337   boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());
    338   const ChannelMap::const_iterator iter = NotificationChannels.find(const_cast<Observable *>(this));
    339   const bool status = iter != NotificationChannels.end();
    340   Channels *OurChannel = NULL;
    341   if (status)
    342     OurChannel = iter->second;
    343   ASSERT(status,
    344       "Observable::getChannel() - we do not have a channel "+toString(no)+" in NotificationChannels.");
    345   ASSERT(OurChannel != NULL,
    346       "Observable::getChannel() - observable has no channels.");
    347   return OurChannel->getChannel(no);
     337  return getNotificationChannel(this, no);
    348338}
    349339
     
    361351  }
    362352  {
    363     const ChannelMap::const_iterator iter =
    364         NotificationChannels.find(const_cast<Observable *>(this));
    365     // if not present, then we have zero observers
    366     if (iter != NotificationChannels.end())
    367       for (Channels::NotificationTypetoRefMap::const_iterator channeliter = iter->second->ChannelMap.begin();
    368           channeliter != iter->second->ChannelMap.end();
     353    boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());
     354    const Channels *OurChannels = getNotificationChannels(this);
     355    if (OurChannels != NULL)
     356      for (Channels::NotificationTypetoRefMap::const_iterator channeliter = OurChannels->ChannelMap.begin();
     357          channeliter != OurChannels->ChannelMap.end();
    369358          ++channeliter)
    370359        ObserverCount += (channeliter->second)->getNumberOfObservers();
     
    396385
    397386  if (!_channels.empty()) {
    398     boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());
    399387    Channels *OurChannel = new Channels;
    400     NotificationChannels.insert( std::make_pair(static_cast<Observable *>(this), OurChannel) );
    401388    // add instance for each notification type
    402389    for (channels_t::const_iterator iter = _channels.begin();
    403390        iter != _channels.end(); ++iter)
    404391      OurChannel->addChannel(*iter);
     392    insertNotificationChannel( std::make_pair(static_cast<Observable *>(this), OurChannel) );
    405393  }
    406394}
     
    436424
    437425  // also kill instance in static Channels map if present
    438   {
    439     boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());
    440     ChannelMap::iterator iter = NotificationChannels.find(static_cast<Observable *>(this));
    441     if (iter != NotificationChannels.end()) {
    442       iter->second->subjectKilled(static_cast<Observable *>(this));
    443       delete iter->second;
    444       NotificationChannels.erase(iter);
    445     }
    446   }
     426  eraseNotificationChannel(this);
    447427}
    448428
     
    453433  return channels;
    454434}
     435
     436void Observable::insertNotificationChannel(std::pair<Observable*, Channels *> _pair)
     437{
     438  boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());
     439  NotificationChannels.insert(_pair);
     440}
     441
     442void Observable::eraseNotificationChannel(Observable * const _target)
     443{
     444  boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());
     445  ChannelMap::iterator iter = NotificationChannels.find(static_cast<Observable *>(_target));
     446  if (iter != NotificationChannels.end()) {
     447    iter->second->subjectKilled(static_cast<Observable *>(_target));
     448    delete iter->second;
     449    NotificationChannels.erase(iter);
     450  }
     451}
     452
     453bool Observable::isNotificationChannelPresent(const Observable * const _target)
     454{
     455  boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());
     456  ChannelMap::const_iterator iter =
     457      NotificationChannels.find(const_cast<Observable * const>(_target));
     458  return iter != NotificationChannels.end();
     459}
     460
     461
     462const Channels* Observable::getNotificationChannels(const Observable * const _target)
     463{
     464  boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());
     465  ChannelMap::const_iterator iter =
     466      NotificationChannels.find(const_cast<Observable * const>(_target));
     467  if (iter != NotificationChannels.end())
     468    return iter->second;
     469  else
     470    return NULL;
     471}
     472
     473Notification_ptr Observable::getNotificationChannel(const Observable * const _target, const size_t _no)
     474{
     475  boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());
     476  ChannelMap::const_iterator iter =
     477      NotificationChannels.find(const_cast<Observable * const>(_target));
     478  ASSERT(iter != NotificationChannels.end(),
     479      "Observable::getNotificationChannel() - could not find channel for target "
     480      +toString(_target)+".");
     481  return iter->second->getChannel(_no);
     482}
Note: See TracChangeset for help on using the changeset viewer.