Changeset 454bc54 for src/Observer/Notification.cpp
- Timestamp:
- Oct 30, 2015, 11:43:01 AM (10 years ago)
- 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)
- File:
-
- 1 edited
-
src/Observer/Notification.cpp (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Observer/Notification.cpp
r1b5188 r454bc54 20 20 #include "CodePatterns/MemDebug.hpp" 21 21 22 #include <boost/thread/locks.hpp> 23 22 24 #include "CodePatterns/Observer/Notification.hpp" 23 25 #include "CodePatterns/Observer/Observer.hpp" … … 30 32 Notification::~Notification(){} 31 33 32 void Notification::addObserver(Observer *target )34 void Notification::addObserver(Observer *target, const int priority) 33 35 { 34 targets.insert(target); 36 boost::recursive_mutex::scoped_lock guard(TargetsLock); 37 targets.insert( std::make_pair(priority, target) ); 35 38 } 36 39 37 40 void Notification::removeObserver(Observer *target) 38 41 { 39 targets.erase(target); 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 } 40 50 } 41 51 42 52 void Notification::notifyAll(Observable * const publisher) 43 53 { 44 for(std::set<Observer*>::iterator it=targets.begin(); 45 it!=targets.end();++it){ 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){ 46 61 #ifdef LOG_OBSERVER 47 62 observerLog().addMessage() << "-> Sending update from " << observerLog().getName(publisher) 48 63 << " for channel " << channelno 49 << " to " << observerLog().getName( *it);64 << " to " << observerLog().getName((*it).second); 50 65 #endif 51 (*it) ->recieveNotification(publisher,this);66 (*it).second->recieveNotification(publisher,this); 52 67 } 53 68 } … … 55 70 void Notification::subjectKilled(Observable * const publisher) 56 71 { 57 for(std::set<Observer*>::iterator it=targets.begin(); 58 it!=targets.end();++it){ 59 (*it)->subjectKilled(publisher); 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); 60 83 } 61 84 }
Note:
See TracChangeset
for help on using the changeset viewer.
