| [a80f419] | 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 | /* | 
|---|
| [e2e035e] | 9 | * Observable.cpp | 
|---|
| [a80f419] | 10 | * | 
|---|
| [e2e035e] | 11 | *  Created on: Dec 1, 2011 | 
|---|
|  | 12 | *      Author: heber | 
|---|
| [a80f419] | 13 | */ | 
|---|
|  | 14 |  | 
|---|
|  | 15 | // include config.h | 
|---|
|  | 16 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 17 | #include <config.h> | 
|---|
|  | 18 | #endif | 
|---|
|  | 19 |  | 
|---|
| [9b8fa4] | 20 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| [a80f419] | 21 |  | 
|---|
| [9b8fa4] | 22 | #include "CodePatterns/Observer/Observable.hpp" | 
|---|
| [8fe1e2] | 23 |  | 
|---|
| [9b8fa4] | 24 | #include "CodePatterns/Assert.hpp" | 
|---|
|  | 25 | #include "CodePatterns/Observer/Channels.hpp" | 
|---|
|  | 26 | #include "CodePatterns/Observer/defs.hpp" | 
|---|
|  | 27 | #include "CodePatterns/Observer/Notification.hpp" | 
|---|
| [a80f419] | 28 |  | 
|---|
| [454bc54] | 29 | #include <algorithm> | 
|---|
|  | 30 |  | 
|---|
| [959c82] | 31 | #include <boost/thread/locks.hpp> | 
|---|
|  | 32 | #include <boost/thread/recursive_mutex.hpp> | 
|---|
|  | 33 |  | 
|---|
| [b324a3] | 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); | 
|---|
| [a80f419] | 39 |  | 
|---|
| [959c82] | 40 | Observable::ChannelMap Observable::NotificationChannels; | 
|---|
| [8fe1e2] | 41 |  | 
|---|
| [a80f419] | 42 | /** Attaching Sub-observables to Observables. | 
|---|
| [959c82] | 43 | * Increases entry in Observable::(GlobalObservableInfo::getInstance().getdepth()) for this \a *publisher by one. | 
|---|
| [a80f419] | 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 | */ | 
|---|
| [1b5188] | 51 | void Observable::start_observer_internal(Observable *publisher) | 
|---|
|  | 52 | { | 
|---|
| [959c82] | 53 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex()); | 
|---|
| [a80f419] | 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 | 
|---|
| [959c82] | 58 | observerLog().addMessage((GlobalObservableInfo::getInstance().getdepth())[publisher]) << ">> Locking " << observerLog().getName(publisher); | 
|---|
| [a80f419] | 59 | #endif | 
|---|
| [959c82] | 60 | (GlobalObservableInfo::getInstance().getdepth())[publisher]++; | 
|---|
| [a80f419] | 61 | } | 
|---|
|  | 62 |  | 
|---|
|  | 63 | /** Detaching Sub-observables from Observables. | 
|---|
| [959c82] | 64 | * Decreases entry in Observable::(GlobalObservableInfo::getInstance().getdepth()) for this \a *publisher by one. If zero, we | 
|---|
| [a80f419] | 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 | */ | 
|---|
| [1b5188] | 73 | void Observable::finish_observer_internal(Observable *publisher) | 
|---|
|  | 74 | { | 
|---|
| [a80f419] | 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 | 
|---|
| [959c82] | 78 | int depth_publisher = 0; | 
|---|
|  | 79 | { | 
|---|
|  | 80 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex()); | 
|---|
|  | 81 | --(GlobalObservableInfo::getInstance().getdepth())[publisher]; | 
|---|
| [a80f419] | 82 | #ifdef LOG_OBSERVER | 
|---|
| [959c82] | 83 | observerLog().addMessage((GlobalObservableInfo::getInstance().getdepth())[publisher]) << "<< Unlocking " << observerLog().getName(publisher); | 
|---|
| [a80f419] | 84 | #endif | 
|---|
| [959c82] | 85 | depth_publisher = (GlobalObservableInfo::getInstance().getdepth())[publisher]; | 
|---|
|  | 86 | } | 
|---|
| [1b5188] | 87 | if(depth_publisher){} | 
|---|
| [a80f419] | 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 | 
|---|
| [959c82] | 92 | { | 
|---|
|  | 93 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex()); | 
|---|
|  | 94 | (GlobalObservableInfo::getInstance().getdepth()).erase(publisher); | 
|---|
|  | 95 | } | 
|---|
| [a80f419] | 96 | } | 
|---|
|  | 97 | } | 
|---|
|  | 98 |  | 
|---|
| [1b5188] | 99 | void Observable::enque_notification_internal(Observable *publisher, Notification_ptr notification) | 
|---|
|  | 100 | { | 
|---|
| [959c82] | 101 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex()); | 
|---|
|  | 102 | (GlobalObservableInfo::getInstance().getnotifications())[publisher].insert(notification); | 
|---|
| [a80f419] | 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. | 
|---|
| [959c82] | 137 | * Puts \a *this into Observable::(GlobalObservableInfo::getInstance().getbusyObservables()), calls Observer::update() for all in callee_t | 
|---|
| [a80f419] | 138 | * and removes from busy list. | 
|---|
|  | 139 | */ | 
|---|
|  | 140 | void Observable::notifyAll() { | 
|---|
| [3d87df] | 141 | #ifdef LOG_OBSERVER | 
|---|
|  | 142 | observerLog().addMessage() << "--> " << observerLog().getName(this) | 
|---|
|  | 143 | << " is about to inform all its Observers."; | 
|---|
|  | 144 | #endif | 
|---|
| [a80f419] | 145 | // we are busy notifying others right now | 
|---|
|  | 146 | // add ourselves to the list of busy subjects to enable circle detection | 
|---|
| [959c82] | 147 | { | 
|---|
|  | 148 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex()); | 
|---|
|  | 149 | (GlobalObservableInfo::getInstance().getbusyObservables()).insert(this); | 
|---|
|  | 150 | } | 
|---|
| [a80f419] | 151 | // see if anyone has signed up for observation | 
|---|
|  | 152 | // and call all observers | 
|---|
|  | 153 | try { | 
|---|
| [959c82] | 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) { | 
|---|
| [a80f419] | 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 | 
|---|
| [959c82] | 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; | 
|---|
| [a80f419] | 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) | 
|---|
| [2c11c1] | 170 | << " (priority=" << (*iter).first << ")"; | 
|---|
| [a80f419] | 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 | 
|---|
| [959c82] | 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 | } | 
|---|
| [a80f419] | 187 | } | 
|---|
|  | 188 |  | 
|---|
| [959c82] | 189 | { | 
|---|
|  | 190 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex()); | 
|---|
|  | 191 | (GlobalObservableInfo::getInstance().getnotifications()).erase(this); | 
|---|
| [a80f419] | 192 |  | 
|---|
| [959c82] | 193 | // done with notification, we can leave the set of busy subjects | 
|---|
|  | 194 | (GlobalObservableInfo::getInstance().getbusyObservables()).erase(this); | 
|---|
|  | 195 | } | 
|---|
| [3d87df] | 196 |  | 
|---|
|  | 197 | #ifdef LOG_OBSERVER | 
|---|
|  | 198 | observerLog().addMessage() << "--> " << observerLog().getName(this) | 
|---|
|  | 199 | << " is done informing all its Observers."; | 
|---|
|  | 200 | #endif | 
|---|
| [a80f419] | 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 | 
|---|
| [959c82] | 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 | } | 
|---|
| [1b5188] | 216 | if(presentCircle) { | 
|---|
| [a80f419] | 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 { | 
|---|
| [959c82] | 226 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex()); | 
|---|
| [a80f419] | 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 | 
|---|
| [959c82] | 230 | std::map<Observable*, int>& depth = GlobalObservableInfo::getInstance().getdepth(); | 
|---|
|  | 231 | const bool depth_contains = depth.find(this)==depth.end(); | 
|---|
|  | 232 | if(depth_contains) { | 
|---|
| [a80f419] | 233 | #ifdef LOG_OBSERVER | 
|---|
|  | 234 | observerLog().addMessage() << "-* Update from " << observerLog().getName(publisher) | 
|---|
| [2c11c1] | 235 | << " propagated by " << observerLog().getName(this); | 
|---|
| [a80f419] | 236 | #endif | 
|---|
|  | 237 | notifyAll(); | 
|---|
|  | 238 | } | 
|---|
|  | 239 | else{ | 
|---|
|  | 240 | #ifdef LOG_OBSERVER | 
|---|
|  | 241 | observerLog().addMessage() << "-| Update from " <<  observerLog().getName(publisher) | 
|---|
| [2c11c1] | 242 | << " not propagated by " << observerLog().getName(this); | 
|---|
| [a80f419] | 243 | #endif | 
|---|
|  | 244 | } | 
|---|
|  | 245 | } | 
|---|
|  | 246 | } | 
|---|
|  | 247 |  | 
|---|
|  | 248 | /** Sign on an Observer to this Observable. | 
|---|
| [959c82] | 249 | * Puts \a *target into Observable::(GlobalObservableInfo::getInstance().getcallTable()) list. | 
|---|
| [a80f419] | 250 | * \param *target Observer | 
|---|
|  | 251 | * \param priority number in [-20,20] | 
|---|
|  | 252 | */ | 
|---|
| [959c82] | 253 | void Observable::signOn(Observer *target, GlobalObservableInfo::PriorityLevel priority) const | 
|---|
| [9e776f] | 254 | { | 
|---|
| [a80f419] | 255 | #ifdef LOG_OBSERVER | 
|---|
| [2c11c1] | 256 | observerLog().addMessage() << "@@ Signing on " << observerLog().getName(target) << " to " << observerLog().getName(const_cast<Observable *>(this)); | 
|---|
| [a80f419] | 257 | #endif | 
|---|
|  | 258 | bool res = false; | 
|---|
| [959c82] | 259 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex()); | 
|---|
|  | 260 | GlobalObservableInfo::callees_t &callees = (GlobalObservableInfo::getInstance().getcallTable())[const_cast<Observable *>(this)]; | 
|---|
| [a80f419] | 261 |  | 
|---|
| [959c82] | 262 | GlobalObservableInfo::callees_t::iterator iter; | 
|---|
| [a80f419] | 263 | for(iter=callees.begin();iter!=callees.end();++iter){ | 
|---|
|  | 264 | res |= ((*iter).second == target); | 
|---|
|  | 265 | } | 
|---|
|  | 266 | if(!res) | 
|---|
| [8fe1e2] | 267 | callees.insert(std::pair<int,Observer*>(priority.level,target)); | 
|---|
| [a80f419] | 268 | } | 
|---|
|  | 269 |  | 
|---|
|  | 270 | /** Sign off an Observer from this Observable. | 
|---|
| [959c82] | 271 | * Removes \a *target from Observable::(GlobalObservableInfo::getInstance().getcallTable()) list. | 
|---|
| [a80f419] | 272 | * \param *target Observer | 
|---|
|  | 273 | */ | 
|---|
| [9e776f] | 274 | void Observable::signOff(Observer *target) const | 
|---|
|  | 275 | { | 
|---|
| [959c82] | 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."); | 
|---|
| [a80f419] | 281 | #ifdef LOG_OBSERVER | 
|---|
| [959c82] | 282 | observerLog().addMessage() << "** Signing off " << observerLog().getName(target) << " from " << observerLog().getName(const_cast<Observable *>(this)); | 
|---|
| [a80f419] | 283 | #endif | 
|---|
| [959c82] | 284 | GlobalObservableInfo::callees_t &callees = callTable[const_cast<Observable *>(this)]; | 
|---|
| [a80f419] | 285 |  | 
|---|
| [959c82] | 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 | } | 
|---|
| [a80f419] | 295 | } | 
|---|
| [959c82] | 296 | if(callees.empty()){ | 
|---|
|  | 297 | callTable.erase(const_cast<Observable *>(this)); | 
|---|
| [a80f419] | 298 | } | 
|---|
|  | 299 | } | 
|---|
| [f3d16a] | 300 | (*graveyard_informer)(this); | 
|---|
| [a80f419] | 301 | } | 
|---|
|  | 302 |  | 
|---|
| [454bc54] | 303 | void Observable::signOn( | 
|---|
|  | 304 | Observer *target, | 
|---|
|  | 305 | size_t channelno, | 
|---|
| [959c82] | 306 | GlobalObservableInfo::PriorityLevel priority) const | 
|---|
| [9e776f] | 307 | { | 
|---|
| [40f2e6] | 308 | Notification_ptr notification = getChannel(channelno); | 
|---|
| [1c291d] | 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 | 
|---|
| [454bc54] | 314 | notification->addObserver(target, priority.level); | 
|---|
| [a80f419] | 315 | } | 
|---|
|  | 316 |  | 
|---|
| [40f2e6] | 317 | void Observable::signOff(Observer *target, size_t channelno) const | 
|---|
| [9e776f] | 318 | { | 
|---|
| [40f2e6] | 319 | Notification_ptr notification = getChannel(channelno); | 
|---|
| [1c291d] | 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 | 
|---|
| [a80f419] | 325 | notification->removeObserver(target); | 
|---|
| [f3d16a] | 326 | (*graveyard_informer)(this); | 
|---|
| [a80f419] | 327 | } | 
|---|
|  | 328 |  | 
|---|
| [9e776f] | 329 | bool Observable::isBlocked() const | 
|---|
|  | 330 | { | 
|---|
| [959c82] | 331 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex()); | 
|---|
|  | 332 | return (GlobalObservableInfo::getInstance().getdepth()).count(const_cast<Observable *>(this)) > 0; | 
|---|
| [a80f419] | 333 | } | 
|---|
|  | 334 |  | 
|---|
| [74e0f7] | 335 | Notification_ptr Observable::getChannel(size_t no) const | 
|---|
|  | 336 | { | 
|---|
| [dd7c44] | 337 | return getNotificationChannel(this, no); | 
|---|
| [74e0f7] | 338 | } | 
|---|
|  | 339 |  | 
|---|
| [b324a3] | 340 | size_t Observable::getNumberOfObservers() const | 
|---|
|  | 341 | { | 
|---|
| [959c82] | 342 | boost::recursive_mutex::scoped_lock lock(GlobalObservableInfo::getInstance().getObservablesMapMutex()); | 
|---|
| [b324a3] | 343 | size_t ObserverCount = 0; | 
|---|
|  | 344 | { | 
|---|
| [959c82] | 345 | GlobalObservableInfo::calltable_t &callTable = GlobalObservableInfo::getInstance().getcallTable(); | 
|---|
|  | 346 | GlobalObservableInfo::calltable_t::const_iterator callees_t_iter = | 
|---|
| [b324a3] | 347 | callTable.find(const_cast<Observable *>(this)); | 
|---|
|  | 348 | // if not present, then we have zero observers | 
|---|
| [959c82] | 349 | if (callees_t_iter != callTable.end()) | 
|---|
|  | 350 | ObserverCount += callees_t_iter->second.size(); | 
|---|
| [b324a3] | 351 | } | 
|---|
|  | 352 | { | 
|---|
| [dd7c44] | 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(); | 
|---|
| [b324a3] | 358 | ++channeliter) | 
|---|
|  | 359 | ObserverCount += (channeliter->second)->getNumberOfObservers(); | 
|---|
|  | 360 | } | 
|---|
|  | 361 | return ObserverCount; | 
|---|
|  | 362 | } | 
|---|
|  | 363 |  | 
|---|
| [a80f419] | 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 | */ | 
|---|
| [1b5188] | 368 | void Observable::subjectKilled(Observable *publisher) | 
|---|
|  | 369 | { | 
|---|
| [a80f419] | 370 | } | 
|---|
|  | 371 |  | 
|---|
|  | 372 | /** Constructor for class Observable. | 
|---|
|  | 373 | */ | 
|---|
| [959c82] | 374 | Observable::Observable( | 
|---|
|  | 375 | std::string name, | 
|---|
|  | 376 | const channels_t &_channels) : | 
|---|
| [b324a3] | 377 | Observer(Observer::BaseConstructor()), | 
|---|
| [f3d16a] | 378 | graveyard_informer(&noop_informer) | 
|---|
| [a80f419] | 379 | { | 
|---|
|  | 380 | #ifdef LOG_OBSERVER | 
|---|
|  | 381 | observerLog().addName(this,name); | 
|---|
| [1c291d] | 382 | observerLog().addMessage() << "++ Creating Observable " | 
|---|
|  | 383 | << observerLog().getName(static_cast<Observable *>(this)); | 
|---|
| [a80f419] | 384 | #endif | 
|---|
| [454bc54] | 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); | 
|---|
| [dd7c44] | 392 | insertNotificationChannel( std::make_pair(static_cast<Observable *>(this), OurChannel) ); | 
|---|
| [454bc54] | 393 | } | 
|---|
| [a80f419] | 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 | 
|---|
| [1c291d] | 402 | observerLog().addMessage() << "-- Destroying Observable " | 
|---|
|  | 403 | << observerLog().getName(static_cast<Observable *>(this)); | 
|---|
| [a80f419] | 404 | #endif | 
|---|
| [959c82] | 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]; | 
|---|
| [a80f419] | 417 | // delete all entries for this observable | 
|---|
| [959c82] | 418 | GlobalObservableInfo::callees_t::iterator iter; | 
|---|
| [1c291d] | 419 | for(iter=callees.begin();iter!=callees.end();++iter) | 
|---|
| [a80f419] | 420 | (*iter).second->subjectKilled(this); | 
|---|
| [959c82] | 421 | // erase the list in the map | 
|---|
|  | 422 | (GlobalObservableInfo::getInstance().getcallTable()).erase(this); | 
|---|
| [a80f419] | 423 | } | 
|---|
| [e429dc] | 424 |  | 
|---|
|  | 425 | // also kill instance in static Channels map if present | 
|---|
| [dd7c44] | 426 | eraseNotificationChannel(this); | 
|---|
| [a80f419] | 427 | } | 
|---|
| [454bc54] | 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 | } | 
|---|
| [dd7c44] | 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 | } | 
|---|