comparison Framework/Messages/MessageBroker.h @ 403:99e31898910e

IObservable.cpp
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sun, 11 Nov 2018 12:13:31 +0100
parents 3897f9f28cfa
children b70e9be013e4
comparison
equal deleted inserted replaced
402:72355b637945 403:99e31898910e
20 20
21 21
22 #pragma once 22 #pragma once
23 23
24 #include "boost/noncopyable.hpp" 24 #include "boost/noncopyable.hpp"
25
25 #include <set> 26 #include <set>
26 27
27 namespace OrthancStone 28 namespace OrthancStone
28 { 29 {
29 class IObserver; 30 class IObserver;
30 class IObservable;
31 31
32 /* 32 /*
33 * This is a central message broker. It keeps track of all observers and knows 33 * This is a central message broker. It keeps track of all observers and knows
34 * when an observer is deleted. 34 * when an observer is deleted.
35 * This way, it can prevent an observable to send a message to a deleted observer. 35 * This way, it can prevent an observable to send a message to a deleted observer.
36 */ 36 */
37 class MessageBroker : public boost::noncopyable 37 class MessageBroker : public boost::noncopyable
38 { 38 {
39 39 private:
40 std::set<IObserver*> activeObservers_; // the list of observers that are currently alive (that have not been deleted) 40 std::set<const IObserver*> activeObservers_; // the list of observers that are currently alive (that have not been deleted)
41 41
42 public: 42 public:
43 43 void Register(const IObserver& observer)
44 void Register(IObserver& observer)
45 { 44 {
46 activeObservers_.insert(&observer); 45 activeObservers_.insert(&observer);
47 } 46 }
48 47
49 void Unregister(IObserver& observer) 48 void Unregister(const IObserver& observer)
50 { 49 {
51 activeObservers_.erase(&observer); 50 activeObservers_.erase(&observer);
52 } 51 }
53 52
54 bool IsActive(IObserver* observer) 53 bool IsActive(const IObserver& observer)
55 { 54 {
56 return activeObservers_.find(observer) != activeObservers_.end(); 55 return activeObservers_.find(&observer) != activeObservers_.end();
57 } 56 }
58 }; 57 };
59
60 } 58 }