OLD | NEW |
| 1 // This file was GENERATED by command: |
| 2 // pump.py callback_registry.h.pump |
| 3 // DO NOT EDIT BY HAND!!! |
| 4 |
| 5 |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 6 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 7 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 8 // found in the LICENSE file. |
4 | 9 |
5 #ifndef BASE_CALLBACK_REGISTRY_H_ | 10 #ifndef BASE_CALLBACK_REGISTRY_H_ |
6 #define BASE_CALLBACK_REGISTRY_H_ | 11 #define BASE_CALLBACK_REGISTRY_H_ |
7 | 12 |
8 #include <list> | 13 #include <list> |
9 | 14 |
10 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
11 #include "base/callback.h" | 16 #include "base/callback.h" |
| 17 #include "base/callback_internal.h" |
12 #include "base/compiler_specific.h" | 18 #include "base/compiler_specific.h" |
13 #include "base/logging.h" | 19 #include "base/logging.h" |
14 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
15 | 21 |
16 // OVERVIEW: | 22 // OVERVIEW: |
17 // | 23 // |
18 // A container for a list of callbacks. Unlike a normal STL vector or list, | 24 // 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 | 25 // this container can be modified during iteration without invalidating the |
20 // iterator. It safely handles the case of a callback removing itself | 26 // iterator. It safely handles the case of a callback removing itself |
21 // or another callback from the list while callbacks are being run. | 27 // or another callback from the list while callbacks are being run. |
22 // | 28 // |
23 // TYPICAL USAGE: | 29 // TYPICAL USAGE: |
24 // | 30 // |
25 // class MyWidget { | 31 // class MyWidget { |
26 // public: | 32 // public: |
27 // ... | 33 // ... |
28 // | 34 // |
29 // typedef base::Callback<void(const Foo&)> OnFooCallback; | 35 // typedef base::Callback<void(const Foo&)> OnFooCallback; |
30 // | 36 // |
31 // scoped_ptr<base::CallbackRegistry<Foo>::Subscription> RegisterCallback( | 37 // scoped_ptr<base::CallbackRegistry<void(const Foo&)>::Subscription> |
32 // const OnFooCallback& cb) { | 38 // RegisterCallback(const OnFooCallback& cb) { |
33 // return callback_registry_.Add(cb); | 39 // return callback_registry_.Add(cb); |
34 // } | 40 // } |
35 // | 41 // |
36 // private: | 42 // private: |
37 // void NotifyFoo(const Foo& foo) { | 43 // void NotifyFoo(const Foo& foo) { |
38 // callback_registry_.Notify(foo); | 44 // callback_registry_.Notify(foo); |
39 // } | 45 // } |
40 // | 46 // |
41 // base::CallbackRegistry<Foo> callback_registry_; | 47 // base::CallbackRegistry<void(const Foo&)> callback_registry_; |
42 // }; | 48 // }; |
43 // | 49 // |
44 // | 50 // |
45 // class MyWidgetListener { | 51 // class MyWidgetListener { |
46 // public: | 52 // public: |
47 // MyWidgetListener::MyWidgetListener() { | 53 // MyWidgetListener::MyWidgetListener() { |
48 // foo_subscription_ = MyWidget::GetCurrent()->RegisterCallback( | 54 // foo_subscription_ = MyWidget::GetCurrent()->RegisterCallback( |
49 // base::Bind(&MyWidgetListener::OnFoo, this))); | 55 // base::Bind(&MyWidgetListener::OnFoo, this))); |
50 // } | 56 // } |
51 // | 57 // |
52 // MyWidgetListener::~MyWidgetListener() { | 58 // MyWidgetListener::~MyWidgetListener() { |
53 // // Subscription gets deleted automatically and will deregister | 59 // // Subscription gets deleted automatically and will deregister |
54 // // the callback in the process. | 60 // // the callback in the process. |
55 // } | 61 // } |
56 // | 62 // |
57 // private: | 63 // private: |
58 // void OnFoo(const Foo& foo) { | 64 // void OnFoo(const Foo& foo) { |
59 // // Do something. | 65 // // Do something. |
60 // } | 66 // } |
61 // | 67 // |
62 // scoped_ptr<base::CallbackRegistry<Foo>::Subscription> foo_subscription_; | 68 // scoped_ptr<base::CallbackRegistry<void(const Foo&)>::Subscription> |
| 69 // foo_subscription_; |
63 // }; | 70 // }; |
64 | 71 |
65 namespace base { | 72 namespace base { |
66 | 73 |
67 namespace internal { | 74 namespace internal { |
68 | 75 |
69 template <typename CallbackType> | 76 template <typename CallbackType> |
70 class CallbackRegistryBase { | 77 class CallbackRegistryBase { |
71 public: | 78 public: |
72 class Subscription { | 79 class Subscription { |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 | 173 |
167 private: | 174 private: |
168 std::list<CallbackType> callbacks_; | 175 std::list<CallbackType> callbacks_; |
169 int active_iterator_count_; | 176 int active_iterator_count_; |
170 | 177 |
171 DISALLOW_COPY_AND_ASSIGN(CallbackRegistryBase); | 178 DISALLOW_COPY_AND_ASSIGN(CallbackRegistryBase); |
172 }; | 179 }; |
173 | 180 |
174 } // namespace internal | 181 } // namespace internal |
175 | 182 |
176 template <typename Details> | 183 template <typename Sig> class CallbackRegistry; |
177 class CallbackRegistry | 184 |
178 : public internal::CallbackRegistryBase<Callback<void(const Details&)> > { | 185 template <> |
| 186 class CallbackRegistry<void(void)> |
| 187 : public internal::CallbackRegistryBase<Callback<void(void)> > { |
179 public: | 188 public: |
| 189 typedef Callback<void(void)> CallbackType; |
| 190 |
180 CallbackRegistry() {} | 191 CallbackRegistry() {} |
181 | 192 |
182 // Execute all active callbacks with |details| parameter. | |
183 void Notify(const Details& details) { | |
184 typename internal::CallbackRegistryBase< | |
185 Callback<void(const Details&)> >::Iterator it = this->GetIterator(); | |
186 Callback<void(const Details&)>* cb; | |
187 while((cb = it.GetNext()) != NULL) { | |
188 cb->Run(details); | |
189 } | |
190 } | |
191 | |
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() { | 193 void Notify() { |
203 Iterator it = this->GetIterator(); | 194 internal::CallbackRegistryBase<CallbackType>::Iterator it = |
204 Closure* cb; | 195 this->GetIterator(); |
| 196 CallbackType* cb; |
205 while((cb = it.GetNext()) != NULL) { | 197 while((cb = it.GetNext()) != NULL) { |
206 cb->Run(); | 198 cb->Run(); |
207 } | 199 } |
208 } | 200 } |
209 | 201 |
| 202 private: |
| 203 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry); |
| 204 }; |
| 205 |
| 206 template <typename A1> |
| 207 class CallbackRegistry<void(A1)> |
| 208 : public internal::CallbackRegistryBase< |
| 209 Callback<void(A1)> > { |
| 210 public: |
| 211 typedef Callback<void(A1)> CallbackType; |
| 212 |
| 213 CallbackRegistry() {} |
| 214 |
| 215 void Notify(typename internal::CallbackParamTraits<A1>::ForwardType a1) { |
| 216 typename internal::CallbackRegistryBase<CallbackType>::Iterator it = |
| 217 this->GetIterator(); |
| 218 CallbackType* cb; |
| 219 while((cb = it.GetNext()) != NULL) { |
| 220 cb->Run(a1); |
| 221 } |
| 222 } |
| 223 |
| 224 private: |
| 225 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry); |
| 226 }; |
| 227 |
| 228 template <typename A1, typename A2> |
| 229 class CallbackRegistry<void(A1, A2)> |
| 230 : public internal::CallbackRegistryBase< |
| 231 Callback<void(A1, A2)> > { |
| 232 public: |
| 233 typedef Callback<void(A1, A2)> CallbackType; |
| 234 |
| 235 CallbackRegistry() {} |
| 236 |
| 237 void Notify(typename internal::CallbackParamTraits<A1>::ForwardType a1, |
| 238 typename internal::CallbackParamTraits<A2>::ForwardType a2) { |
| 239 typename internal::CallbackRegistryBase<CallbackType>::Iterator it = |
| 240 this->GetIterator(); |
| 241 CallbackType* cb; |
| 242 while((cb = it.GetNext()) != NULL) { |
| 243 cb->Run(a1, a2); |
| 244 } |
| 245 } |
| 246 |
| 247 private: |
| 248 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry); |
| 249 }; |
| 250 |
| 251 template <typename A1, typename A2, typename A3> |
| 252 class CallbackRegistry<void(A1, A2, A3)> |
| 253 : public internal::CallbackRegistryBase< |
| 254 Callback<void(A1, A2, A3)> > { |
| 255 public: |
| 256 typedef Callback<void(A1, A2, A3)> CallbackType; |
| 257 |
| 258 CallbackRegistry() {} |
| 259 |
| 260 void Notify(typename internal::CallbackParamTraits<A1>::ForwardType a1, |
| 261 typename internal::CallbackParamTraits<A2>::ForwardType a2, |
| 262 typename internal::CallbackParamTraits<A3>::ForwardType a3) { |
| 263 typename internal::CallbackRegistryBase<CallbackType>::Iterator it = |
| 264 this->GetIterator(); |
| 265 CallbackType* cb; |
| 266 while((cb = it.GetNext()) != NULL) { |
| 267 cb->Run(a1, a2, a3); |
| 268 } |
| 269 } |
| 270 |
| 271 private: |
| 272 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry); |
| 273 }; |
| 274 |
| 275 template <typename A1, typename A2, typename A3, typename A4> |
| 276 class CallbackRegistry<void(A1, A2, A3, A4)> |
| 277 : public internal::CallbackRegistryBase< |
| 278 Callback<void(A1, A2, A3, A4)> > { |
| 279 public: |
| 280 typedef Callback<void(A1, A2, A3, A4)> CallbackType; |
| 281 |
| 282 CallbackRegistry() {} |
| 283 |
| 284 void Notify(typename internal::CallbackParamTraits<A1>::ForwardType a1, |
| 285 typename internal::CallbackParamTraits<A2>::ForwardType a2, |
| 286 typename internal::CallbackParamTraits<A3>::ForwardType a3, |
| 287 typename internal::CallbackParamTraits<A4>::ForwardType a4) { |
| 288 typename internal::CallbackRegistryBase<CallbackType>::Iterator it = |
| 289 this->GetIterator(); |
| 290 CallbackType* cb; |
| 291 while((cb = it.GetNext()) != NULL) { |
| 292 cb->Run(a1, a2, a3, a4); |
| 293 } |
| 294 } |
| 295 |
| 296 private: |
| 297 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry); |
| 298 }; |
| 299 |
| 300 template <typename A1, typename A2, typename A3, typename A4, typename A5> |
| 301 class CallbackRegistry<void(A1, A2, A3, A4, A5)> |
| 302 : public internal::CallbackRegistryBase< |
| 303 Callback<void(A1, A2, A3, A4, A5)> > { |
| 304 public: |
| 305 typedef Callback<void(A1, A2, A3, A4, A5)> CallbackType; |
| 306 |
| 307 CallbackRegistry() {} |
| 308 |
| 309 void Notify(typename internal::CallbackParamTraits<A1>::ForwardType a1, |
| 310 typename internal::CallbackParamTraits<A2>::ForwardType a2, |
| 311 typename internal::CallbackParamTraits<A3>::ForwardType a3, |
| 312 typename internal::CallbackParamTraits<A4>::ForwardType a4, |
| 313 typename internal::CallbackParamTraits<A5>::ForwardType a5) { |
| 314 typename internal::CallbackRegistryBase<CallbackType>::Iterator it = |
| 315 this->GetIterator(); |
| 316 CallbackType* cb; |
| 317 while((cb = it.GetNext()) != NULL) { |
| 318 cb->Run(a1, a2, a3, a4, a5); |
| 319 } |
| 320 } |
| 321 |
| 322 private: |
| 323 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry); |
| 324 }; |
| 325 |
| 326 template <typename A1, typename A2, typename A3, typename A4, typename A5, |
| 327 typename A6> |
| 328 class CallbackRegistry<void(A1, A2, A3, A4, A5, A6)> |
| 329 : public internal::CallbackRegistryBase< |
| 330 Callback<void(A1, A2, A3, A4, A5, A6)> > { |
| 331 public: |
| 332 typedef Callback<void(A1, A2, A3, A4, A5, A6)> CallbackType; |
| 333 |
| 334 CallbackRegistry() {} |
| 335 |
| 336 void Notify(typename internal::CallbackParamTraits<A1>::ForwardType a1, |
| 337 typename internal::CallbackParamTraits<A2>::ForwardType a2, |
| 338 typename internal::CallbackParamTraits<A3>::ForwardType a3, |
| 339 typename internal::CallbackParamTraits<A4>::ForwardType a4, |
| 340 typename internal::CallbackParamTraits<A5>::ForwardType a5, |
| 341 typename internal::CallbackParamTraits<A6>::ForwardType a6) { |
| 342 typename internal::CallbackRegistryBase<CallbackType>::Iterator it = |
| 343 this->GetIterator(); |
| 344 CallbackType* cb; |
| 345 while((cb = it.GetNext()) != NULL) { |
| 346 cb->Run(a1, a2, a3, a4, a5, a6); |
| 347 } |
| 348 } |
| 349 |
| 350 private: |
| 351 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry); |
| 352 }; |
| 353 |
| 354 template <typename A1, typename A2, typename A3, typename A4, typename A5, |
| 355 typename A6, typename A7> |
| 356 class CallbackRegistry<void(A1, A2, A3, A4, A5, A6, A7)> |
| 357 : public internal::CallbackRegistryBase< |
| 358 Callback<void(A1, A2, A3, A4, A5, A6, A7)> > { |
| 359 public: |
| 360 typedef Callback<void(A1, A2, A3, A4, A5, A6, A7)> CallbackType; |
| 361 |
| 362 CallbackRegistry() {} |
| 363 |
| 364 void Notify(typename internal::CallbackParamTraits<A1>::ForwardType a1, |
| 365 typename internal::CallbackParamTraits<A2>::ForwardType a2, |
| 366 typename internal::CallbackParamTraits<A3>::ForwardType a3, |
| 367 typename internal::CallbackParamTraits<A4>::ForwardType a4, |
| 368 typename internal::CallbackParamTraits<A5>::ForwardType a5, |
| 369 typename internal::CallbackParamTraits<A6>::ForwardType a6, |
| 370 typename internal::CallbackParamTraits<A7>::ForwardType a7) { |
| 371 typename internal::CallbackRegistryBase<CallbackType>::Iterator it = |
| 372 this->GetIterator(); |
| 373 CallbackType* cb; |
| 374 while((cb = it.GetNext()) != NULL) { |
| 375 cb->Run(a1, a2, a3, a4, a5, a6, a7); |
| 376 } |
| 377 } |
| 378 |
210 private: | 379 private: |
211 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry); | 380 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry); |
212 }; | 381 }; |
213 | 382 |
214 } // namespace base | 383 } // namespace base |
215 | 384 |
216 #endif // BASE_CALLBACK_REGISTRY_H_ | 385 #endif // BASE_CALLBACK_REGISTRY_H |
OLD | NEW |