| 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 |  * Notification.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 <boost/thread/locks.hpp>
 | 
|---|
| 23 | 
 | 
|---|
| 24 | #include "CodePatterns/Observer/Notification.hpp"
 | 
|---|
| 25 | #include "CodePatterns/Observer/Observer.hpp"
 | 
|---|
| 26 | #include "CodePatterns/Observer/ObserverLog.hpp"
 | 
|---|
| 27 | 
 | 
|---|
| 28 | Notification::Notification(size_t _channelno) :
 | 
|---|
| 29 |     channelno(_channelno)
 | 
|---|
| 30 | {}
 | 
|---|
| 31 | 
 | 
|---|
| 32 | Notification::~Notification(){}
 | 
|---|
| 33 | 
 | 
|---|
| 34 | void Notification::addObserver(Observer *target, const int priority)
 | 
|---|
| 35 | {
 | 
|---|
| 36 |   boost::recursive_mutex::scoped_lock guard(TargetsLock);
 | 
|---|
| 37 |   targets.insert( std::make_pair(priority, target) );
 | 
|---|
| 38 | }
 | 
|---|
| 39 | 
 | 
|---|
| 40 | void Notification::removeObserver(Observer *target)
 | 
|---|
| 41 | {
 | 
|---|
| 42 |   boost::recursive_mutex::scoped_lock guard(TargetsLock);
 | 
|---|
| 43 |   for(targets_t::iterator iter=targets.begin();iter!=targets.end();) {
 | 
|---|
| 44 |     if((*iter).second == target) {
 | 
|---|
| 45 |       targets.erase(iter++);
 | 
|---|
| 46 |     } else {
 | 
|---|
| 47 |       ++iter;
 | 
|---|
| 48 |     }
 | 
|---|
| 49 |   }
 | 
|---|
| 50 | }
 | 
|---|
| 51 | 
 | 
|---|
| 52 | void Notification::notifyAll(Observable * const publisher)
 | 
|---|
| 53 | {
 | 
|---|
| 54 |   boost::recursive_mutex::scoped_lock guard(TargetsLock);
 | 
|---|
| 55 |   // copy such that signOff() within receiving update() does not affect iterating
 | 
|---|
| 56 |   // this is because within the same thread and with the update() signOff() may be
 | 
|---|
| 57 |   // called and when executed it modifies targets
 | 
|---|
| 58 |   targets_t temp_targets = targets;
 | 
|---|
| 59 |   for(targets_t::iterator it=temp_targets.begin();
 | 
|---|
| 60 |       it!=temp_targets.end();++it){
 | 
|---|
| 61 | #ifdef LOG_OBSERVER
 | 
|---|
| 62 |     observerLog().addMessage() << "-> Sending update from " << observerLog().getName(publisher)
 | 
|---|
| 63 |                                << " for channel " << channelno
 | 
|---|
| 64 |                                << " to " << observerLog().getName((*it).second);
 | 
|---|
| 65 | #endif
 | 
|---|
| 66 |     (*it).second->recieveNotification(publisher,this);
 | 
|---|
| 67 |   }
 | 
|---|
| 68 | }
 | 
|---|
| 69 | 
 | 
|---|
| 70 | void Notification::subjectKilled(Observable * const publisher)
 | 
|---|
| 71 | {
 | 
|---|
| 72 |   boost::recursive_mutex::scoped_lock guard(TargetsLock);
 | 
|---|
| 73 |   // copy such that signOff() within receiving subjectKilled() does not affect iterating
 | 
|---|
| 74 |   // this is because within the same thread and with the subjectKilled() signOff() may be
 | 
|---|
| 75 |   // called and when executed it modifies targets
 | 
|---|
| 76 |   for(targets_t::iterator it=targets.begin();
 | 
|---|
| 77 |       !targets.empty();it=targets.begin()){
 | 
|---|
| 78 |     Observer *target = (*it).second;
 | 
|---|
| 79 |     const size_t prior_size = targets.size();
 | 
|---|
| 80 |     target->subjectKilled(publisher);
 | 
|---|
| 81 |     if (prior_size == targets.size())
 | 
|---|
| 82 |       targets.erase(it);
 | 
|---|
| 83 |   }
 | 
|---|
| 84 | }
 | 
|---|