[084729c] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2010 University of Bonn. All rights reserved.
|
---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | /*
|
---|
| 9 | * Observable.cpp
|
---|
| 10 | *
|
---|
| 11 | * Created on: Dec 1, 2011
|
---|
| 12 | * Author: heber
|
---|
| 13 | */
|
---|
| 14 |
|
---|
| 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
| 20 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 21 |
|
---|
| 22 | #include "CodePatterns/Observer/Observable.hpp"
|
---|
| 23 |
|
---|
| 24 | #include "CodePatterns/Assert.hpp"
|
---|
| 25 | #include "CodePatterns/Observer/Channels.hpp"
|
---|
| 26 | #include "CodePatterns/Observer/defs.hpp"
|
---|
| 27 | #include "CodePatterns/Observer/Notification.hpp"
|
---|
| 28 |
|
---|
| 29 | #include <algorithm>
|
---|
| 30 |
|
---|
| 31 | #include <boost/thread/locks.hpp>
|
---|
| 32 | #include <boost/thread/recursive_mutex.hpp>
|
---|
| 33 |
|
---|
| 34 | //!> This function does nothing with the given Observable
|
---|
| 35 | void NoOp_informer(const Observable *)
|
---|
| 36 | {}
|
---|
| 37 |
|
---|
| 38 | Observable::graveyard_informer_t Observable::noop_informer(&NoOp_informer);
|
---|
| 39 |
|
---|
| 40 | Observable::ChannelMap Observable::NotificationChannels;
|
---|
| 41 |
|
---|
| 42 | /** Attaching Sub-observables to Observables.
|
---|
| 43 | * Increases entry in Observable::(GlobalObservableInfo::getInstance().getdepth()) for this \a *publisher by one.
|
---|
| 44 | *
|
---|
| 45 | * The two functions \sa start_observer_internal() and \sa finish_observer_internal()
|
---|
| 46 | * have to be used together at all time. Never use these functions directly
|
---|
| 47 | * START_OBSERVER and FINISH_OBSERVER also construct a bogus while(0) loop
|
---|
| 48 | * thus producing compiler-errors whenever only one is used.
|
---|
| 49 | * \param *publisher reference of sub-observable
|
---|
| 50 | */
|
---|
| 51 | void Observable::start_observer_internal(Observable *publisher)
|
---|
| 52 | {
|
---|
| 53 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());
|
---|
| 54 | // increase the count for this observable by one
|
---|
| 55 | // if no entry for this observable is found, an new one is created
|
---|
| 56 | // by the STL and initialized to 0 (see STL documentation)
|
---|
| 57 | #ifdef LOG_OBSERVER
|
---|
| 58 | observerLog().addMessage((GlobalObservableInfo::getInstance().getdepth())[publisher]) << ">> Locking " << observerLog().getName(publisher);
|
---|
| 59 | #endif
|
---|
| 60 | (GlobalObservableInfo::getInstance().getdepth())[publisher]++;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /** Detaching Sub-observables from Observables.
|
---|
| 64 | * Decreases entry in Observable::(GlobalObservableInfo::getInstance().getdepth()) for this \a *publisher by one. If zero, we
|
---|
| 65 | * start notifying all our Observers.
|
---|
| 66 | *
|
---|
| 67 | * The two functions start_observer_internal() and finish_observer_internal()
|
---|
| 68 | * have to be used together at all time. Never use these functions directly
|
---|
| 69 | * START_OBSERVER and FINISH_OBSERVER also construct a bogus while(0) loop
|
---|
| 70 | * thus producing compiler-errors whenever only one is used.
|
---|
| 71 | * \param *publisher reference of sub-observable
|
---|
| 72 | */
|
---|
| 73 | void Observable::finish_observer_internal(Observable *publisher)
|
---|
| 74 | {
|
---|
| 75 | // decrease the count for this observable
|
---|
| 76 | // if zero is reached all observed blocks are done and we can
|
---|
| 77 | // start to notify our observers
|
---|
| 78 | int depth_publisher = 0;
|
---|
| 79 | {
|
---|
| 80 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());
|
---|
| 81 | --(GlobalObservableInfo::getInstance().getdepth())[publisher];
|
---|
| 82 | #ifdef LOG_OBSERVER
|
---|
| 83 | observerLog().addMessage((GlobalObservableInfo::getInstance().getdepth())[publisher]) << "<< Unlocking " << observerLog().getName(publisher);
|
---|
| 84 | #endif
|
---|
| 85 | depth_publisher = (GlobalObservableInfo::getInstance().getdepth())[publisher];
|
---|
| 86 | }
|
---|
| 87 | if(depth_publisher){}
|
---|
| 88 | else{
|
---|
| 89 | publisher->notifyAll();
|
---|
| 90 | // this item is done, so we don't have to keep the count with us
|
---|
| 91 | // save some memory by erasing it
|
---|
| 92 | {
|
---|
| 93 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());
|
---|
| 94 | (GlobalObservableInfo::getInstance().getdepth()).erase(publisher);
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | void Observable::enque_notification_internal(Observable *publisher, Notification_ptr notification)
|
---|
| 100 | {
|
---|
| 101 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());
|
---|
| 102 | (GlobalObservableInfo::getInstance().getnotifications())[publisher].insert(notification);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /** Constructor for Observable Protector.
|
---|
| 106 | * Basically, calls start_observer_internal(). Hence use this class instead of
|
---|
| 107 | * calling the function directly.
|
---|
| 108 | *
|
---|
| 109 | * \param *protege Observable to be protected.
|
---|
| 110 | */
|
---|
| 111 | Observable::_Observable_protector::_Observable_protector(Observable *_protege) :
|
---|
| 112 | protege(_protege)
|
---|
| 113 | {
|
---|
| 114 | start_observer_internal(protege);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | Observable::_Observable_protector::_Observable_protector(const _Observable_protector &dest) :
|
---|
| 118 | protege(dest.protege)
|
---|
| 119 | {
|
---|
| 120 | start_observer_internal(protege);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | /** Destructor for Observable Protector.
|
---|
| 124 | * Basically, calls finish_observer_internal(). Hence use this class instead of
|
---|
| 125 | * calling the function directly.
|
---|
| 126 | *
|
---|
| 127 | * \param *protege Observable to be protected.
|
---|
| 128 | */
|
---|
| 129 | Observable::_Observable_protector::~_Observable_protector()
|
---|
| 130 | {
|
---|
| 131 | finish_observer_internal(protege);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | /************* Notification mechanism for observables **************/
|
---|
| 135 |
|
---|
| 136 | /** Notify all Observers of changes.
|
---|
| 137 | * Puts \a *this into Observable::(GlobalObservableInfo::getInstance().getbusyObservables()), calls Observer::update() for all in callee_t
|
---|
| 138 | * and removes from busy list.
|
---|
| 139 | */
|
---|
| 140 | void Observable::notifyAll() {
|
---|
| 141 | #ifdef LOG_OBSERVER
|
---|
| 142 | observerLog().addMessage() << "--> " << observerLog().getName(this)
|
---|
| 143 | << " is about to inform all its Observers.";
|
---|
| 144 | #endif
|
---|
| 145 | // we are busy notifying others right now
|
---|
| 146 | // add ourselves to the list of busy subjects to enable circle detection
|
---|
| 147 | {
|
---|
| 148 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());
|
---|
| 149 | (GlobalObservableInfo::getInstance().getbusyObservables()).insert(this);
|
---|
| 150 | }
|
---|
| 151 | // see if anyone has signed up for observation
|
---|
| 152 | // and call all observers
|
---|
| 153 | try {
|
---|
| 154 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());
|
---|
| 155 | GlobalObservableInfo::calltable_t& callTable = GlobalObservableInfo::getInstance().getcallTable();
|
---|
| 156 | const bool callTable_contains = callTable.find(this) != callTable.end();
|
---|
| 157 | if (callTable_contains) {
|
---|
| 158 | // elements are stored sorted by keys in the multimap
|
---|
| 159 | // so iterating over it gives us a the callees sorted by
|
---|
| 160 | // the priorities
|
---|
| 161 | // copy such that signOff() within receiving update() does not affect iterating
|
---|
| 162 | // this is because within the same thread and with the updateKilled() signOff() may be
|
---|
| 163 | // called and when executed it modifies targets
|
---|
| 164 | GlobalObservableInfo::callees_t callees = callTable[this];
|
---|
| 165 | GlobalObservableInfo::callees_t::iterator iter;
|
---|
| 166 | for(iter=callees.begin();iter!=callees.end();++iter){
|
---|
| 167 | #ifdef LOG_OBSERVER
|
---|
| 168 | observerLog().addMessage() << "-> Sending update from " << observerLog().getName(this)
|
---|
| 169 | << " to " << observerLog().getName((*iter).second)
|
---|
| 170 | << " (priority=" << (*iter).first << ")";
|
---|
| 171 | #endif
|
---|
| 172 | (*iter).second->update(this);
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 | ASSERT_NOCATCH("Exception thrown from Observer Update");
|
---|
| 177 |
|
---|
| 178 | // send out all notifications that need to be done
|
---|
| 179 | {
|
---|
| 180 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());
|
---|
| 181 | GlobalObservableInfo::notificationSet currentNotifications =
|
---|
| 182 | (GlobalObservableInfo::getInstance().getnotifications())[this];
|
---|
| 183 | for(GlobalObservableInfo::notificationSet::iterator it = currentNotifications.begin();
|
---|
| 184 | it != currentNotifications.end();++it){
|
---|
| 185 | (*it)->notifyAll(this);
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | {
|
---|
| 190 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());
|
---|
| 191 | (GlobalObservableInfo::getInstance().getnotifications()).erase(this);
|
---|
| 192 |
|
---|
| 193 | // done with notification, we can leave the set of busy subjects
|
---|
| 194 | (GlobalObservableInfo::getInstance().getbusyObservables()).erase(this);
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | #ifdef LOG_OBSERVER
|
---|
| 198 | observerLog().addMessage() << "--> " << observerLog().getName(this)
|
---|
| 199 | << " is done informing all its Observers.";
|
---|
| 200 | #endif
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 |
|
---|
| 204 | /** Handles passing on updates from sub-Observables.
|
---|
| 205 | * Mimicks basically the Observer::update() function.
|
---|
| 206 | *
|
---|
| 207 | * \param *publisher The \a *this we observe.
|
---|
| 208 | */
|
---|
| 209 | void Observable::update(Observable *publisher) {
|
---|
| 210 | // circle detection
|
---|
| 211 | bool presentCircle = false;
|
---|
| 212 | {
|
---|
| 213 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());
|
---|
| 214 | presentCircle = (GlobalObservableInfo::getInstance().getbusyObservables()).find(this)!=(GlobalObservableInfo::getInstance().getbusyObservables()).end();
|
---|
| 215 | }
|
---|
| 216 | if(presentCircle) {
|
---|
| 217 | // somehow a circle was introduced... we were busy notifying our
|
---|
| 218 | // observers, but still we are called by one of our sub-Observables
|
---|
| 219 | // we cannot be sure observation will still work at this point
|
---|
| 220 | ASSERT(0,"Circle detected in observation-graph.\n"
|
---|
| 221 | "Observation-graph always needs to be a DAG to work correctly!\n"
|
---|
| 222 | "Please check your observation code and fix this!\n");
|
---|
| 223 | return;
|
---|
| 224 | }
|
---|
| 225 | else {
|
---|
| 226 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());
|
---|
| 227 | // see if we are in the process of changing ourselves
|
---|
| 228 | // if we are changing ourselves at the same time our sub-observables change
|
---|
| 229 | // we do not need to publish all the changes at each time we are called
|
---|
| 230 | std::map<Observable*, int>& depth = GlobalObservableInfo::getInstance().getdepth();
|
---|
| 231 | const bool depth_contains = depth.find(this)==depth.end();
|
---|
| 232 | if(depth_contains) {
|
---|
| 233 | #ifdef LOG_OBSERVER
|
---|
| 234 | observerLog().addMessage() << "-* Update from " << observerLog().getName(publisher)
|
---|
| 235 | << " propagated by " << observerLog().getName(this);
|
---|
| 236 | #endif
|
---|
| 237 | notifyAll();
|
---|
| 238 | }
|
---|
| 239 | else{
|
---|
| 240 | #ifdef LOG_OBSERVER
|
---|
| 241 | observerLog().addMessage() << "-| Update from " << observerLog().getName(publisher)
|
---|
| 242 | << " not propagated by " << observerLog().getName(this);
|
---|
| 243 | #endif
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | /** Sign on an Observer to this Observable.
|
---|
| 249 | * Puts \a *target into Observable::(GlobalObservableInfo::getInstance().getcallTable()) list.
|
---|
| 250 | * \param *target Observer
|
---|
| 251 | * \param priority number in [-20,20]
|
---|
| 252 | */
|
---|
| 253 | void Observable::signOn(Observer *target, GlobalObservableInfo::PriorityLevel priority) const
|
---|
| 254 | {
|
---|
| 255 | #ifdef LOG_OBSERVER
|
---|
| 256 | observerLog().addMessage() << "@@ Signing on " << observerLog().getName(target) << " to " << observerLog().getName(const_cast<Observable *>(this));
|
---|
| 257 | #endif
|
---|
| 258 | bool res = false;
|
---|
| 259 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());
|
---|
| 260 | GlobalObservableInfo::callees_t &callees = (GlobalObservableInfo::getInstance().getcallTable())[const_cast<Observable *>(this)];
|
---|
| 261 |
|
---|
| 262 | GlobalObservableInfo::callees_t::iterator iter;
|
---|
| 263 | for(iter=callees.begin();iter!=callees.end();++iter){
|
---|
| 264 | res |= ((*iter).second == target);
|
---|
| 265 | }
|
---|
| 266 | if(!res)
|
---|
| 267 | callees.insert(std::pair<int,Observer*>(priority.level,target));
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | /** Sign off an Observer from this Observable.
|
---|
| 271 | * Removes \a *target from Observable::(GlobalObservableInfo::getInstance().getcallTable()) list.
|
---|
| 272 | * \param *target Observer
|
---|
| 273 | */
|
---|
| 274 | void Observable::signOff(Observer *target) const
|
---|
| 275 | {
|
---|
| 276 | {
|
---|
| 277 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());
|
---|
| 278 | GlobalObservableInfo::calltable_t &callTable = GlobalObservableInfo::getInstance().getcallTable();
|
---|
| 279 | ASSERT(callTable.count(const_cast<Observable *>(this)),
|
---|
| 280 | "SignOff called for an Observable without Observers.");
|
---|
| 281 | #ifdef LOG_OBSERVER
|
---|
| 282 | observerLog().addMessage() << "** Signing off " << observerLog().getName(target) << " from " << observerLog().getName(const_cast<Observable *>(this));
|
---|
| 283 | #endif
|
---|
| 284 | GlobalObservableInfo::callees_t &callees = callTable[const_cast<Observable *>(this)];
|
---|
| 285 |
|
---|
| 286 | GlobalObservableInfo::callees_t::iterator iter;
|
---|
| 287 | GlobalObservableInfo::callees_t::iterator deliter;
|
---|
| 288 | for(iter=callees.begin();iter!=callees.end();) {
|
---|
| 289 | if((*iter).second == target) {
|
---|
| 290 | callees.erase(iter++);
|
---|
| 291 | }
|
---|
| 292 | else {
|
---|
| 293 | ++iter;
|
---|
| 294 | }
|
---|
| 295 | }
|
---|
| 296 | if(callees.empty()){
|
---|
| 297 | callTable.erase(const_cast<Observable *>(this));
|
---|
| 298 | }
|
---|
| 299 | }
|
---|
| 300 | (*graveyard_informer)(this);
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | void Observable::signOn(
|
---|
| 304 | Observer *target,
|
---|
| 305 | size_t channelno,
|
---|
| 306 | GlobalObservableInfo::PriorityLevel priority) const
|
---|
| 307 | {
|
---|
| 308 | Notification_ptr notification = getChannel(channelno);
|
---|
| 309 | #ifdef LOG_OBSERVER
|
---|
| 310 | observerLog().addMessage() << "@@ Signing on " << observerLog().getName(target)
|
---|
| 311 | << " to " << observerLog().getName(const_cast<Observable *>(this))
|
---|
| 312 | << "'s channel no." << channelno << ".";
|
---|
| 313 | #endif
|
---|
| 314 | notification->addObserver(target, priority.level);
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | void Observable::signOff(Observer *target, size_t channelno) const
|
---|
| 318 | {
|
---|
| 319 | Notification_ptr notification = getChannel(channelno);
|
---|
| 320 | #ifdef LOG_OBSERVER
|
---|
| 321 | observerLog().addMessage() << "** Signing off " << observerLog().getName(target)
|
---|
| 322 | << " from " << observerLog().getName(const_cast<Observable *>(this))
|
---|
| 323 | << "'s channel no." << channelno << ".";
|
---|
| 324 | #endif
|
---|
| 325 | notification->removeObserver(target);
|
---|
| 326 | (*graveyard_informer)(this);
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | bool Observable::isBlocked() const
|
---|
| 330 | {
|
---|
| 331 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());
|
---|
| 332 | return (GlobalObservableInfo::getInstance().getdepth()).count(const_cast<Observable *>(this)) > 0;
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | Notification_ptr Observable::getChannel(size_t no) const
|
---|
| 336 | {
|
---|
| 337 | return getNotificationChannel(this, no);
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | size_t Observable::getNumberOfObservers() const
|
---|
| 341 | {
|
---|
| 342 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());
|
---|
| 343 | size_t ObserverCount = 0;
|
---|
| 344 | {
|
---|
| 345 | GlobalObservableInfo::calltable_t &callTable = GlobalObservableInfo::getInstance().getcallTable();
|
---|
| 346 | GlobalObservableInfo::calltable_t::const_iterator callees_t_iter =
|
---|
| 347 | callTable.find(const_cast<Observable *>(this));
|
---|
| 348 | // if not present, then we have zero observers
|
---|
| 349 | if (callees_t_iter != callTable.end())
|
---|
| 350 | ObserverCount += callees_t_iter->second.size();
|
---|
| 351 | }
|
---|
| 352 | {
|
---|
| 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();
|
---|
| 358 | ++channeliter)
|
---|
| 359 | ObserverCount += (channeliter->second)->getNumberOfObservers();
|
---|
| 360 | }
|
---|
| 361 | return ObserverCount;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 | /** Handles sub-observables that just got killed
|
---|
| 365 | * when an sub-observerable dies we usually don't need to do anything
|
---|
| 366 | * \param *publisher Sub-Observable.
|
---|
| 367 | */
|
---|
| 368 | void Observable::subjectKilled(Observable *publisher)
|
---|
| 369 | {
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | /** Constructor for class Observable.
|
---|
| 373 | */
|
---|
| 374 | Observable::Observable(
|
---|
| 375 | std::string name,
|
---|
| 376 | const channels_t &_channels) :
|
---|
| 377 | Observer(Observer::BaseConstructor()),
|
---|
| 378 | graveyard_informer(&noop_informer)
|
---|
| 379 | {
|
---|
| 380 | #ifdef LOG_OBSERVER
|
---|
| 381 | observerLog().addName(this,name);
|
---|
| 382 | observerLog().addMessage() << "++ Creating Observable "
|
---|
| 383 | << observerLog().getName(static_cast<Observable *>(this));
|
---|
| 384 | #endif
|
---|
| 385 |
|
---|
| 386 | if (!_channels.empty()) {
|
---|
| 387 | Channels *OurChannel = new Channels;
|
---|
| 388 | // add instance for each notification type
|
---|
| 389 | for (channels_t::const_iterator iter = _channels.begin();
|
---|
| 390 | iter != _channels.end(); ++iter)
|
---|
| 391 | OurChannel->addChannel(*iter);
|
---|
| 392 | insertNotificationChannel( std::make_pair(static_cast<Observable *>(this), OurChannel) );
|
---|
| 393 | }
|
---|
| 394 | }
|
---|
| 395 |
|
---|
| 396 | /** Destructor for class Observable.
|
---|
| 397 | * When an observable is deleted, we let all our observers know. \sa Observable::subjectKilled().
|
---|
| 398 | */
|
---|
| 399 | Observable::~Observable()
|
---|
| 400 | {
|
---|
| 401 | #ifdef LOG_OBSERVER
|
---|
| 402 | observerLog().addMessage() << "-- Destroying Observable "
|
---|
| 403 | << observerLog().getName(static_cast<Observable *>(this));
|
---|
| 404 | #endif
|
---|
| 405 | bool CallTable_contains = false;
|
---|
| 406 | {
|
---|
| 407 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());
|
---|
| 408 | CallTable_contains = (GlobalObservableInfo::getInstance().getcallTable()).count(this);
|
---|
| 409 | }
|
---|
| 410 | if(CallTable_contains) {
|
---|
| 411 | // copy the list from the map
|
---|
| 412 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());
|
---|
| 413 | // copy such that signOff() within receiving subjectKilled() does not affect iterating
|
---|
| 414 | // this is because within the same thread and with the subjectKilled() signOff() may be
|
---|
| 415 | // called and when executed it modifies targets
|
---|
| 416 | GlobalObservableInfo::callees_t callees = (GlobalObservableInfo::getInstance().getcallTable())[this];
|
---|
| 417 | // delete all entries for this observable
|
---|
| 418 | GlobalObservableInfo::callees_t::iterator iter;
|
---|
| 419 | for(iter=callees.begin();iter!=callees.end();++iter)
|
---|
| 420 | (*iter).second->subjectKilled(this);
|
---|
| 421 | // erase the list in the map
|
---|
| 422 | (GlobalObservableInfo::getInstance().getcallTable()).erase(this);
|
---|
| 423 | }
|
---|
| 424 |
|
---|
| 425 | // also kill instance in static Channels map if present
|
---|
| 426 | eraseNotificationChannel(this);
|
---|
| 427 | }
|
---|
| 428 |
|
---|
| 429 | Observable::channels_t Observable::getChannelList(const size_t max)
|
---|
| 430 | {
|
---|
| 431 | channels_t channels(max);
|
---|
| 432 | std::generate(channels.begin(), channels.end(), UniqueNumber());
|
---|
| 433 | return channels;
|
---|
| 434 | }
|
---|
| 435 |
|
---|
| 436 | void Observable::insertNotificationChannel(std::pair<Observable*, Channels *> _pair)
|
---|
| 437 | {
|
---|
| 438 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex());
|
---|
| 439 | NotificationChannels.insert(_pair);
|
---|
| 440 | }
|
---|
| 441 |
|
---|
| 442 | void 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 |
|
---|
| 453 | bool 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 |
|
---|
| 462 | const 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 |
|
---|
| 473 | Notification_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 | }
|
---|