Mercurial > hg > orthanc-stone
annotate Framework/Toolbox/ObserversRegistry.h @ 90:64e60018943f wasm
fix and observer refactoring
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 29 May 2017 11:04:18 +0200 |
parents | c2dc924f1a63 |
children | 81f73efd81a1 |
rev | line source |
---|---|
0 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
40
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
5 * Copyright (C) 2017 Osimis, Belgium |
0 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
47 | 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. | |
0 | 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 | |
47 | 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 | |
0 | 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 **/ | |
20 | |
21 | |
22 #pragma once | |
23 | |
16 | 24 #include "../../Resources/Orthanc/Core/OrthancException.h" |
0 | 25 |
26 #include <boost/noncopyable.hpp> | |
27 #include <set> | |
28 | |
29 namespace OrthancStone | |
30 { | |
31 template < | |
32 typename Source, | |
33 typename Observer = typename Source::IChangeObserver | |
34 > | |
35 class ObserversRegistry : public boost::noncopyable | |
36 { | |
37 private: | |
38 struct ChangeFunctor : public boost::noncopyable | |
39 { | |
40 void operator() (Observer& observer, | |
41 const Source& source) | |
42 { | |
43 observer.NotifyChange(source); | |
44 } | |
45 }; | |
46 | |
47 typedef std::set<Observer*> Observers; | |
48 | |
53
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
47
diff
changeset
|
49 Observers observers_; |
0 | 50 |
51 public: | |
52 template <typename Functor> | |
53 void Notify(const Source* source, | |
54 Functor& functor) | |
55 { | |
56 if (source == NULL) | |
57 { | |
58 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
59 } | |
60 | |
61 for (typename Observers::const_iterator observer = observers_.begin(); | |
62 observer != observers_.end(); ++observer) | |
63 { | |
64 functor(**observer, *source); | |
65 } | |
66 } | |
67 | |
68 template <typename Functor> | |
69 void Notify(const Source* source) | |
70 { | |
71 // Use the default functor | |
72 Functor functor; | |
73 Notify(source, functor); | |
74 } | |
75 | |
76 void NotifyChange(const Source* source) | |
77 { | |
78 Notify<ChangeFunctor>(source); | |
79 } | |
80 | |
81 void Register(Observer& observer) | |
82 { | |
83 observers_.insert(&observer); | |
84 } | |
85 | |
86 void Unregister(Observer& observer) | |
87 { | |
88 observers_.erase(&observer); | |
89 } | |
90 | |
91 bool IsEmpty() | |
92 { | |
93 return observers_.empty(); | |
94 } | |
90
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
95 |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
96 void Apply(const Source& source, |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
97 void (Observer::*method) (const Source&)) |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
98 { |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
99 for (typename Observers::const_iterator it = observers_.begin(); |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
100 it != observers_.end(); ++it) |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
101 { |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
102 ((*it)->*method) (source); |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
103 } |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
104 } |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
105 |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
106 template <typename Argument0> |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
107 void Apply(const Source& source, |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
108 void (Observer::*method) (const Source&, const Argument0&), |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
109 const Argument0& argument0) |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
110 { |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
111 for (typename Observers::const_iterator it = observers_.begin(); |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
112 it != observers_.end(); ++it) |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
113 { |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
114 ((*it)->*method) (source, argument0); |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
115 } |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
116 } |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
117 |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
118 template <typename Argument0, |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
119 typename Argument1> |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
120 void Apply(const Source& source, |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
121 void (Observer::*method) (const Source&, const Argument0&, const Argument1&), |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
122 const Argument0& argument0, |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
123 const Argument1& argument1) |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
124 { |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
125 for (typename Observers::const_iterator it = observers_.begin(); |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
126 it != observers_.end(); ++it) |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
127 { |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
128 ((*it)->*method) (source, argument0, argument1); |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
129 } |
64e60018943f
fix and observer refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
130 } |
0 | 131 }; |
132 } |