comparison Framework/Messages/IObservable.cpp @ 403:99e31898910e

IObservable.cpp
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sun, 11 Nov 2018 12:13:31 +0100
parents
children 751fb354149e b70e9be013e4
comparison
equal deleted inserted replaced
402:72355b637945 403:99e31898910e
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include "IObservable.h"
23
24 #include <Core/OrthancException.h>
25
26 #include <cassert>
27
28 namespace OrthancStone
29 {
30 IObservable::~IObservable()
31 {
32 // delete all callables (this will also unregister them from the broker)
33 for (Callables::const_iterator it = callables_.begin();
34 it != callables_.end(); ++it)
35 {
36 for (std::set<ICallable*>::const_iterator
37 it2 = it->second.begin(); it2 != it->second.end(); ++it2)
38 {
39 delete *it2;
40 }
41 }
42
43 // unregister the forwarders but don't delete them (they'll be
44 // deleted by the observable they are observing as any other
45 // callable)
46 for (Forwarders::iterator it = forwarders_.begin();
47 it != forwarders_.end(); ++it)
48 {
49 IMessageForwarder* fw = *it;
50 broker_.Unregister(dynamic_cast<IObserver&>(*fw));
51 }
52 }
53
54
55 void IObservable::RegisterObserverCallback(ICallable* callable)
56 {
57 if (callable == NULL)
58 {
59 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
60 }
61
62 MessageType messageType = callable->GetMessageType();
63
64 callables_[messageType].insert(callable);
65 }
66
67
68 void IObservable::EmitMessage(const IMessage& message)
69 {
70 Callables::const_iterator found = callables_.find(message.GetType());
71
72 if (found != callables_.end())
73 {
74 for (std::set<ICallable*>::const_iterator
75 it = found->second.begin(); it != found->second.end(); ++it)
76 {
77 assert(*it != NULL);
78 if (broker_.IsActive(*(*it)->GetObserver()))
79 {
80 (*it)->Apply(message);
81 }
82 }
83 }
84 }
85
86
87 void IObservable::RegisterForwarder(IMessageForwarder* forwarder)
88 {
89 if (forwarder == NULL)
90 {
91 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
92 }
93
94 forwarders_.insert(forwarder);
95 }
96 }