Mercurial > hg > orthanc-stone
annotate Framework/Messages/IObservable.cpp @ 460:4d8ac609fc33
fix link
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 31 Jan 2019 19:23:12 +0100 |
parents | a750f11892ec |
children | 42dadae61fa9 |
rev | line source |
---|---|
403 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
439 | 5 * Copyright (C) 2017-2019 Osimis S.A., Belgium |
403 | 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 | |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
67 void IObservable::Unregister(IObserver *observer) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
68 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
69 // delete all callables from this observer |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
70 for (Callables::iterator itCallableSet = callables_.begin(); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
71 itCallableSet != callables_.end(); ++itCallableSet) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
72 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
73 for (std::set<ICallable*>::const_iterator |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
74 itCallable = itCallableSet->second.begin(); itCallable != itCallableSet->second.end(); ) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
75 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
76 if ((*itCallable)->GetObserver() == observer) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
77 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
78 delete *itCallable; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
79 itCallableSet->second.erase(itCallable++); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
80 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
81 else |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
82 ++itCallable; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
83 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
84 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
85 } |
403 | 86 |
87 void IObservable::EmitMessage(const IMessage& message) | |
88 { | |
89 Callables::const_iterator found = callables_.find(message.GetType()); | |
90 | |
91 if (found != callables_.end()) | |
92 { | |
93 for (std::set<ICallable*>::const_iterator | |
94 it = found->second.begin(); it != found->second.end(); ++it) | |
95 { | |
96 assert(*it != NULL); | |
97 if (broker_.IsActive(*(*it)->GetObserver())) | |
98 { | |
99 (*it)->Apply(message); | |
100 } | |
101 } | |
102 } | |
103 } | |
104 | |
105 | |
106 void IObservable::RegisterForwarder(IMessageForwarder* forwarder) | |
107 { | |
108 if (forwarder == NULL) | |
109 { | |
110 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
111 } | |
112 | |
113 forwarders_.insert(forwarder); | |
114 } | |
115 } |