Ignore:
Timestamp:
Oct 30, 2015, 11:43:01 AM (10 years ago)
Author:
Frederik Heber <heber@…>
Children:
e24dde
Parents:
1b5188
git-author:
Frederik Heber <heber@…> (06/19/15 22:28:24)
git-committer:
Frederik Heber <heber@…> (10/30/15 11:43:01)
Message:

Added new pattern ObservedValue, contrary to Cacheable.

  • essentially updates immediately and caches a value for off-line use, safe- guard with mutexes.
  • added unit test for ObservedValue.
  • factored threeNumbers stub out of CacheableUnitTest, also used the stub in ObservedValueUnitTest.
  • we may now signOn() to Notification with a priority.
  • Cacheable can also update now via notification channels not just for global updates.
  • Observable can now also instantiate the channels directly if given a list. This was necessary as Cacheable or ObservedValue are instantiated in the constructor, too, and if listening to channels, these must already be present.
  • FIX: Channels::addChannels() used NDEBUG wrong way round.
  • ObservedValue::get() returns reference.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Observer/Observable.cpp

    r1b5188 r454bc54  
    2626#include "CodePatterns/Observer/defs.hpp"
    2727#include "CodePatterns/Observer/Notification.hpp"
     28
     29#include <algorithm>
    2830
    2931//!> This function does nothing with the given Observable
     
    107109  ObservablesMapLock.lock();
    108110  --depth[publisher];
    109   ObservablesMapLock.unlock();
    110111#ifdef LOG_OBSERVER
    111112  observerLog().addMessage(depth[publisher]) << "<< Unlocking " << observerLog().getName(publisher);
    112113#endif
    113   ObservablesMapLock.lock();
    114114  int depth_publisher = depth[publisher];
    115115  ObservablesMapLock.unlock();
     
    203203  // send out all notifications that need to be done
    204204
     205  ObservablesMapLock.lock();
    205206  notificationSet currentNotifications = notifications[this];
     207  ObservablesMapLock.unlock();
    206208  for(notificationSet::iterator it = currentNotifications.begin();
    207209      it != currentNotifications.end();++it){
     
    317319}
    318320
    319 void Observable::signOn(Observer *target, size_t channelno) const
     321void Observable::signOn(
     322    Observer *target,
     323    size_t channelno,
     324    PriorityLevel priority) const
    320325{
    321326  boost::recursive_mutex::scoped_lock guard(ObservablesMapLock);
     
    326331      << "'s channel no." << channelno << ".";
    327332#endif
    328   notification->addObserver(target);
     333  notification->addObserver(target, priority.level);
    329334}
    330335
     
    398403/** Constructor for class Observable.
    399404 */
    400 Observable::Observable(std::string name) :
     405Observable::Observable(std::string name, const channels_t &_channels) :
    401406  Observer(Observer::BaseConstructor()),
    402407  graveyard_informer(&noop_informer)
     
    407412      << observerLog().getName(static_cast<Observable *>(this));
    408413#endif
     414
     415  if (!_channels.empty()) {
     416    Channels *OurChannel = new Channels;
     417    NotificationChannels.insert( std::make_pair(static_cast<Observable *>(this), OurChannel) );
     418    // add instance for each notification type
     419    for (channels_t::const_iterator iter = _channels.begin();
     420        iter != _channels.end(); ++iter)
     421      OurChannel->addChannel(*iter);
     422  }
    409423}
    410424
     
    442456  }
    443457}
     458
     459Observable::channels_t Observable::getChannelList(const size_t max)
     460{
     461  channels_t channels(max);
     462  std::generate(channels.begin(), channels.end(), UniqueNumber());
     463  return channels;
     464}
Note: See TracChangeset for help on using the changeset viewer.