comparison Framework/Messages/IObserver.h @ 267:89d02de83c03 am-2

added declaretion of messages handled/emitted
author am@osimis.io
date Wed, 22 Aug 2018 14:59:20 +0200
parents c9cf95b49a86
children 5bd4161bf11b
comparison
equal deleted inserted replaced
266:c9cf95b49a86 267:89d02de83c03
21 21
22 #pragma once 22 #pragma once
23 23
24 #include "MessageBroker.h" 24 #include "MessageBroker.h"
25 #include "IMessage.h" 25 #include "IMessage.h"
26 #include "IObservable.h" 26 #include <set>
27 #include <assert.h>
27 28
28 namespace OrthancStone { 29 namespace OrthancStone {
29 30
30 class IObservable; 31 class IObservable;
31 32
32 class IObserver : public boost::noncopyable 33 class IObserver : public boost::noncopyable
33 { 34 {
34 protected: 35 protected:
35 MessageBroker& broker_; 36 MessageBroker& broker_;
37 std::set<MessageType> handledMessages_;
38 std::set<MessageType> ignoredMessages_;
36 39
37 public: 40 public:
38 IObserver(MessageBroker& broker) 41 IObserver(MessageBroker& broker)
39 : broker_(broker) 42 : broker_(broker)
40 { 43 {
44 virtual ~IObserver() 47 virtual ~IObserver()
45 { 48 {
46 broker_.Unregister(*this); 49 broker_.Unregister(*this);
47 } 50 }
48 51
52 void HandleMessage_(const IObservable &from, const IMessage &message)
53 {
54 assert(handledMessages_.find(message.GetType()) != handledMessages_.end()); // please declare the messages that you're handling
55
56 HandleMessage(from, message);
57 }
58
49 virtual void HandleMessage(const IObservable& from, const IMessage& message) = 0; 59 virtual void HandleMessage(const IObservable& from, const IMessage& message) = 0;
60
61
62 const std::set<MessageType>& GetHandledMessages() const
63 {
64 return handledMessages_;
65 }
66
67 const std::set<MessageType>& GetIgnoredMessages() const
68 {
69 return ignoredMessages_;
70 }
71
72 protected:
73
74 // when you connect an IObserver to an IObservable, the observer must handle all observable messages (this is checked during the registration)
75 // so, all messages that may be emitted by the observable must be declared "handled" or "ignored" by the observer
76 void DeclareHandledMessage(MessageType messageType)
77 {
78 handledMessages_.insert(messageType);
79 }
80
81 void DeclareIgnoredMessage(MessageType messageType)
82 {
83 ignoredMessages_.insert(messageType);
84 }
85
50 }; 86 };
51 87
52 } 88 }