Mercurial > hg > orthanc-stone
comparison Framework/Toolbox/ObserversRegistry.h @ 53:c2dc924f1a63 wasm
removing threading out of the framework
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 27 Apr 2017 16:57:49 +0200 |
parents | 28956ed68280 |
children | 64e60018943f |
comparison
equal
deleted
inserted
replaced
52:37e504582af6 | 53:c2dc924f1a63 |
---|---|
22 #pragma once | 22 #pragma once |
23 | 23 |
24 #include "../../Resources/Orthanc/Core/OrthancException.h" | 24 #include "../../Resources/Orthanc/Core/OrthancException.h" |
25 | 25 |
26 #include <boost/noncopyable.hpp> | 26 #include <boost/noncopyable.hpp> |
27 #include <boost/thread/mutex.hpp> | |
28 #include <set> | 27 #include <set> |
29 | 28 |
30 namespace OrthancStone | 29 namespace OrthancStone |
31 { | 30 { |
32 template < | 31 template < |
45 } | 44 } |
46 }; | 45 }; |
47 | 46 |
48 typedef std::set<Observer*> Observers; | 47 typedef std::set<Observer*> Observers; |
49 | 48 |
50 boost::mutex mutex_; | 49 Observers observers_; |
51 Observers observers_; | |
52 bool empty_; | |
53 | 50 |
54 public: | 51 public: |
55 ObserversRegistry() : empty_(true) | |
56 { | |
57 } | |
58 | |
59 template <typename Functor> | 52 template <typename Functor> |
60 void Notify(const Source* source, | 53 void Notify(const Source* source, |
61 Functor& functor) | 54 Functor& functor) |
62 { | 55 { |
63 if (empty_) | |
64 { | |
65 // Optimization to avoid the unnecessary locking of the mutex | |
66 return; | |
67 } | |
68 | |
69 if (source == NULL) | 56 if (source == NULL) |
70 { | 57 { |
71 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | 58 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); |
72 } | 59 } |
73 | |
74 boost::mutex::scoped_lock lock(mutex_); | |
75 | 60 |
76 for (typename Observers::const_iterator observer = observers_.begin(); | 61 for (typename Observers::const_iterator observer = observers_.begin(); |
77 observer != observers_.end(); ++observer) | 62 observer != observers_.end(); ++observer) |
78 { | 63 { |
79 functor(**observer, *source); | 64 functor(**observer, *source); |
93 Notify<ChangeFunctor>(source); | 78 Notify<ChangeFunctor>(source); |
94 } | 79 } |
95 | 80 |
96 void Register(Observer& observer) | 81 void Register(Observer& observer) |
97 { | 82 { |
98 empty_ = false; | |
99 | |
100 boost::mutex::scoped_lock lock(mutex_); | |
101 observers_.insert(&observer); | 83 observers_.insert(&observer); |
102 } | 84 } |
103 | 85 |
104 void Unregister(Observer& observer) | 86 void Unregister(Observer& observer) |
105 { | 87 { |
106 boost::mutex::scoped_lock lock(mutex_); | |
107 observers_.erase(&observer); | 88 observers_.erase(&observer); |
108 | |
109 if (observers_.empty()) | |
110 { | |
111 empty_ = true; | |
112 } | |
113 } | 89 } |
114 | 90 |
115 bool IsEmpty() | 91 bool IsEmpty() |
116 { | 92 { |
117 #if 0 | |
118 boost::mutex::scoped_lock lock(mutex_); | |
119 return observers_.empty(); | 93 return observers_.empty(); |
120 #else | |
121 return empty_; | |
122 #endif | |
123 } | 94 } |
124 }; | 95 }; |
125 } | 96 } |