Mercurial > hg > orthanc-stone
annotate Framework/Toolbox/ObserversRegistry.h @ 47:28956ed68280
agpl license
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 27 Apr 2017 11:00:15 +0200 |
parents | 7207a407bcd8 |
children | c2dc924f1a63 4cff7b1ed31d |
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 <boost/thread/mutex.hpp> | |
28 #include <set> | |
29 | |
30 namespace OrthancStone | |
31 { | |
32 template < | |
33 typename Source, | |
34 typename Observer = typename Source::IChangeObserver | |
35 > | |
36 class ObserversRegistry : public boost::noncopyable | |
37 { | |
38 private: | |
39 struct ChangeFunctor : public boost::noncopyable | |
40 { | |
41 void operator() (Observer& observer, | |
42 const Source& source) | |
43 { | |
44 observer.NotifyChange(source); | |
45 } | |
46 }; | |
47 | |
48 typedef std::set<Observer*> Observers; | |
49 | |
50 boost::mutex mutex_; | |
51 Observers observers_; | |
52 bool empty_; | |
53 | |
54 public: | |
55 ObserversRegistry() : empty_(true) | |
56 { | |
57 } | |
58 | |
59 template <typename Functor> | |
60 void Notify(const Source* source, | |
61 Functor& functor) | |
62 { | |
63 if (empty_) | |
64 { | |
65 // Optimization to avoid the unnecessary locking of the mutex | |
66 return; | |
67 } | |
68 | |
69 if (source == NULL) | |
70 { | |
71 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
72 } | |
73 | |
74 boost::mutex::scoped_lock lock(mutex_); | |
75 | |
76 for (typename Observers::const_iterator observer = observers_.begin(); | |
77 observer != observers_.end(); ++observer) | |
78 { | |
79 functor(**observer, *source); | |
80 } | |
81 } | |
82 | |
83 template <typename Functor> | |
84 void Notify(const Source* source) | |
85 { | |
86 // Use the default functor | |
87 Functor functor; | |
88 Notify(source, functor); | |
89 } | |
90 | |
91 void NotifyChange(const Source* source) | |
92 { | |
93 Notify<ChangeFunctor>(source); | |
94 } | |
95 | |
96 void Register(Observer& observer) | |
97 { | |
98 empty_ = false; | |
99 | |
100 boost::mutex::scoped_lock lock(mutex_); | |
101 observers_.insert(&observer); | |
102 } | |
103 | |
104 void Unregister(Observer& observer) | |
105 { | |
106 boost::mutex::scoped_lock lock(mutex_); | |
107 observers_.erase(&observer); | |
108 | |
109 if (observers_.empty()) | |
110 { | |
111 empty_ = true; | |
112 } | |
113 } | |
114 | |
115 bool IsEmpty() | |
116 { | |
117 #if 0 | |
118 boost::mutex::scoped_lock lock(mutex_); | |
119 return observers_.empty(); | |
120 #else | |
121 return empty_; | |
122 #endif | |
123 } | |
124 }; | |
125 } |