Changeset dd7c44


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.
Location:
src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/CodePatterns/Observer/Observable.hpp

    r1f96ec rdd7c44  
    4545 */
    4646class Observable : public Observer {
     47  //!> grant Relay access to NotificationChannels as we know it uses the mutex as well
     48  friend class Relay;
    4749public:
    4850  //!> typedef for a vector of channels
     
    134136
    135137protected:
     138
     139  static void insertNotificationChannel( std::pair<Observable*, Channels *> _pair);
     140  static void eraseNotificationChannel(Observable * const _target);
     141  static bool isNotificationChannelPresent(const Observable * const _target);
     142  static const Channels *getNotificationChannels(const Observable * const _target);
     143  static Notification_ptr getNotificationChannel(const Observable * const _target, const size_t _no);
     144
     145private:
    136146
    137147  typedef std::map<Observable*, Channels *> ChannelMap;
     
    188198#define PASTE_HELPER(a,b) a ## b
    189199#define OBSERVE Observable::_Observable_protector PASTE(_scope_obs_protector_,__LINE__)(this)
    190 #define NOTIFY(channelno) do{Observable::enque_notification_internal(this,NotificationChannels[this]->getChannel(channelno));}while(0)
     200#define NOTIFY(channelno) do{Observable::enque_notification_internal(this,Observable::getNotificationChannel(this,channelno));}while(0)
    191201#define LOCK_OBSERVABLE(observable) Observable::_Observable_protector PASTE(_scope_obs_protector_,__LINE__)(&(observable))
    192202
  • 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}
  • src/Observer/unittests/stubs/ObserverStub.cpp

    r1f96ec rdd7c44  
    136136{
    137137  Channels *OurChannel = new Channels();
    138   NotificationChannels.insert( std::make_pair(this, OurChannel) );
     138  Observable::insertNotificationChannel( std::make_pair(this, OurChannel) );
    139139  OurChannel->addChannel(Operation1Notify);
    140140  OurChannel->addChannel(Operation2Notify);
     
    143143NotificationObservable::~NotificationObservable()
    144144{
    145   NotificationChannels.erase(this);
     145  Observable::eraseNotificationChannel(this);
    146146}
    147147
     
    245245{
    246246  Channels *OurChannel = new Channels();
    247   NotificationChannels.insert( std::make_pair(this, OurChannel) );
     247  Observable::insertNotificationChannel( std::make_pair(this, OurChannel) );
    248248  OurChannel->addChannel(NotificationObservable::Operation1Notify);
    249249  OurChannel->addChannel(NotificationObservable::Operation2Notify);
Note: See TracChangeset for help on using the changeset viewer.