OLD | NEW |
| 1 $$ This is a pump file for generating file templates. Pump is a python |
| 2 $$ script that is part of the Google Test suite of utilities. Description |
| 3 $$ can be found here: |
| 4 $$ |
| 5 $$ http://code.google.com/p/googletest/wiki/PumpManual |
| 6 $$ |
| 7 |
| 8 $$ See comment for MAX_ARITY in base/bind.h.pump. |
| 9 $var MAX_ARITY = 7 |
| 10 |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 11 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 12 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 13 // found in the LICENSE file. |
4 | 14 |
5 #ifndef BASE_CALLBACK_REGISTRY_H_ | 15 #ifndef BASE_CALLBACK_REGISTRY_H_ |
6 #define BASE_CALLBACK_REGISTRY_H_ | 16 #define BASE_CALLBACK_REGISTRY_H_ |
7 | 17 |
8 #include <list> | 18 #include <list> |
9 | 19 |
10 #include "base/basictypes.h" | 20 #include "base/basictypes.h" |
11 #include "base/callback.h" | 21 #include "base/callback.h" |
| 22 #include "base/callback_internal.h" |
12 #include "base/compiler_specific.h" | 23 #include "base/compiler_specific.h" |
13 #include "base/logging.h" | 24 #include "base/logging.h" |
14 #include "base/memory/scoped_ptr.h" | 25 #include "base/memory/scoped_ptr.h" |
15 | 26 |
16 // OVERVIEW: | 27 // OVERVIEW: |
17 // | 28 // |
18 // A container for a list of callbacks. Unlike a normal STL vector or list, | 29 // A container for a list of callbacks. Unlike a normal STL vector or list, |
19 // this container can be modified during iteration without invalidating the | 30 // this container can be modified during iteration without invalidating the |
20 // iterator. It safely handles the case of a callback removing itself | 31 // iterator. It safely handles the case of a callback removing itself |
21 // or another callback from the list while callbacks are being run. | 32 // or another callback from the list while callbacks are being run. |
22 // | 33 // |
23 // TYPICAL USAGE: | 34 // TYPICAL USAGE: |
24 // | 35 // |
25 // class MyWidget { | 36 // class MyWidget { |
26 // public: | 37 // public: |
27 // ... | 38 // ... |
28 // | 39 // |
29 // typedef base::Callback<void(const Foo&)> OnFooCallback; | 40 // typedef base::Callback<void(const Foo&)> OnFooCallback; |
30 // | 41 // |
31 // scoped_ptr<base::CallbackRegistry<Foo>::Subscription> RegisterCallback( | 42 // scoped_ptr<base::CallbackRegistry<void(const Foo&)>::Subscription> |
32 // const OnFooCallback& cb) { | 43 // RegisterCallback(const OnFooCallback& cb) { |
33 // return callback_registry_.Add(cb); | 44 // return callback_registry_.Add(cb); |
34 // } | 45 // } |
35 // | 46 // |
36 // private: | 47 // private: |
37 // void NotifyFoo(const Foo& foo) { | 48 // void NotifyFoo(const Foo& foo) { |
38 // callback_registry_.Notify(foo); | 49 // callback_registry_.Notify(foo); |
39 // } | 50 // } |
40 // | 51 // |
41 // base::CallbackRegistry<Foo> callback_registry_; | 52 // base::CallbackRegistry<void(const Foo&)> callback_registry_; |
42 // }; | 53 // }; |
43 // | 54 // |
44 // | 55 // |
45 // class MyWidgetListener { | 56 // class MyWidgetListener { |
46 // public: | 57 // public: |
47 // MyWidgetListener::MyWidgetListener() { | 58 // MyWidgetListener::MyWidgetListener() { |
48 // foo_subscription_ = MyWidget::GetCurrent()->RegisterCallback( | 59 // foo_subscription_ = MyWidget::GetCurrent()->RegisterCallback( |
49 // base::Bind(&MyWidgetListener::OnFoo, this))); | 60 // base::Bind(&MyWidgetListener::OnFoo, this))); |
50 // } | 61 // } |
51 // | 62 // |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 | 177 |
167 private: | 178 private: |
168 std::list<CallbackType> callbacks_; | 179 std::list<CallbackType> callbacks_; |
169 int active_iterator_count_; | 180 int active_iterator_count_; |
170 | 181 |
171 DISALLOW_COPY_AND_ASSIGN(CallbackRegistryBase); | 182 DISALLOW_COPY_AND_ASSIGN(CallbackRegistryBase); |
172 }; | 183 }; |
173 | 184 |
174 } // namespace internal | 185 } // namespace internal |
175 | 186 |
176 template <typename Details> | 187 template <typename Sig> class CallbackRegistry; |
177 class CallbackRegistry | 188 |
178 : public internal::CallbackRegistryBase<Callback<void(const Details&)> > { | 189 |
| 190 $range ARITY 0..MAX_ARITY |
| 191 $for ARITY [[ |
| 192 $range ARG 1..ARITY |
| 193 |
| 194 $if ARITY == 0 [[ |
| 195 template <> |
| 196 class CallbackRegistry<void(void)> |
| 197 : public internal::CallbackRegistryBase<Callback<void(void)> > { |
| 198 ]] $else [[ |
| 199 template <$for ARG , [[typename A$(ARG)]]> |
| 200 class CallbackRegistry<void($for ARG , [[A$(ARG)]])> |
| 201 : public internal::CallbackRegistryBase< |
| 202 Callback<void($for ARG , [[A$(ARG)]])> > { |
| 203 ]] |
| 204 |
179 public: | 205 public: |
| 206 $if ARITY == 0 [[ |
| 207 |
| 208 typedef Callback<void(void)> CallbackType; |
| 209 ]] $else [[ |
| 210 |
| 211 typedef Callback<void($for ARG , [[A$(ARG)]])> CallbackType; |
| 212 ]] |
| 213 |
| 214 |
180 CallbackRegistry() {} | 215 CallbackRegistry() {} |
181 | 216 |
182 // Execute all active callbacks with |details| parameter. | 217 void Notify($for ARG , |
183 void Notify(const Details& details) { | 218 [[typename internal::CallbackParamTraits<A$(ARG)>::ForwardType a$(
ARG)]]) { |
184 typename internal::CallbackRegistryBase< | 219 $if ARITY == 0 [[ |
185 Callback<void(const Details&)> >::Iterator it = this->GetIterator(); | 220 |
186 Callback<void(const Details&)>* cb; | 221 internal::CallbackRegistryBase<CallbackType>::Iterator it = |
| 222 this->GetIterator(); |
| 223 ]] $else [[ |
| 224 |
| 225 typename internal::CallbackRegistryBase<CallbackType>::Iterator it = |
| 226 this->GetIterator(); |
| 227 ]] |
| 228 |
| 229 CallbackType* cb; |
187 while((cb = it.GetNext()) != NULL) { | 230 while((cb = it.GetNext()) != NULL) { |
188 cb->Run(details); | 231 cb->Run($for ARG , [[a$(ARG)]]); |
189 } | 232 } |
190 } | 233 } |
191 | 234 |
192 private: | |
193 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry); | |
194 }; | |
195 | |
196 template <> class CallbackRegistry<void> | |
197 : public internal::CallbackRegistryBase<Closure> { | |
198 public: | |
199 CallbackRegistry() {} | |
200 | |
201 // Execute all active callbacks. | |
202 void Notify() { | |
203 Iterator it = this->GetIterator(); | |
204 Closure* cb; | |
205 while((cb = it.GetNext()) != NULL) { | |
206 cb->Run(); | |
207 } | |
208 } | |
209 | |
210 private: | 235 private: |
211 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry); | 236 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry); |
212 }; | 237 }; |
213 | 238 |
| 239 |
| 240 ]] $$ for ARITY |
214 } // namespace base | 241 } // namespace base |
215 | 242 |
216 #endif // BASE_CALLBACK_REGISTRY_H_ | 243 #endif // BASE_CALLBACK_REGISTRY_H |
OLD | NEW |