OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef BASE_CALLBACK_REGISTRY_H_ | |
6 #define BASE_CALLBACK_REGISTRY_H_ | |
7 | |
8 #include <algorithm> | |
9 #include <limits> | |
10 #include <vector> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/callback.h" | |
14 #include "base/callback_registry_internal.h" | |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/memory/weak_ptr.h" | |
17 | |
18 // OVERVIEW: | |
19 // | |
20 // A container for a list of callbacks. Unlike a normal STL vector or list, | |
21 // this container can be modified during iteration without invalidating the | |
22 // iterator. It safely handles the case of a callback removing itself | |
23 // or another callback from the list while callbacks are being run. | |
24 // | |
25 // TYPICAL USAGE: | |
26 // | |
27 // class MyWidget { | |
28 // public: | |
29 // ... | |
30 // | |
31 // typedef base::Callback<void(const Foo&)> OnFooCallback; | |
32 // | |
33 // scoped_ptr<base::CallbackHandle> RegisterCallback( | |
34 // const OnFooCallback& cb) { | |
35 // return callback_registry_.Add(cb); | |
36 // } | |
37 // | |
38 // private: | |
39 // void NotifyFoo(const Foo& foo) { | |
40 // callback_registry_.Run(foo); | |
awong
2013/09/06 19:03:24
I'd rename this to Notify() now. Seems more sensib
Cait (Slow)
2013/09/06 22:17:11
Done.
| |
41 // } | |
42 // | |
43 // CallbackRegistry<Foo> callback_registry_(); | |
44 // }; | |
45 // | |
46 // | |
47 // class MyWidgetListener { | |
48 // public: | |
49 // MyWidgetListener::MyWidgetListener() { | |
50 // foo_callback_handle_ = MyWidget::GetCurrent()->RegisterCallback( | |
51 // base::Bind(&MyWidgetListener::OnFoo, this))); | |
52 // } | |
53 // | |
54 // MyWidgetListener::~MyWidgetListener() { | |
55 // // CallbackHandle gets deleted automatically and will deregister | |
56 // // the callback in the process. | |
57 // } | |
58 // | |
59 // void OnFoo(const Foo& foo) { | |
60 // // Do something. | |
61 // } | |
62 // | |
63 // private: | |
64 // scoped_ptr<base::CallbackHandle> foo_callback_handle_; | |
65 // }; | |
66 | |
67 namespace base { | |
68 | |
69 template <typename Details> | |
70 class CallbackRegistry | |
71 : public base::internal::CallbackRegistryBase< | |
72 Callback<void(const Details&)> > { | |
73 public: | |
74 typedef base::Callback<void(const Details&)> CallbackType; | |
awong
2013/09/06 19:03:24
I don't think you need this CallbackType either do
Cait (Slow)
2013/09/06 22:17:11
Done.
| |
75 typedef typename base::internal::CallbackRegistryBase<CallbackType>::Iterator | |
awong
2013/09/06 19:03:24
You shouldn't need this typedef since Iterator sho
Cait (Slow)
2013/09/06 22:17:11
I either need to typedef it or refer to it in full
| |
76 Iterator; | |
77 CallbackRegistry() {} | |
78 | |
79 // Execute all active callbacks with |details| parameter. | |
80 void Run(const Details& details) { | |
81 Iterator it = this->GetIterator(); | |
82 CallbackType* cb; | |
83 while((cb = it.GetNext()) != NULL) { | |
84 cb->Run(details); | |
85 } | |
86 } | |
87 | |
88 private: | |
89 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry); | |
90 }; | |
91 | |
92 template <> class CallbackRegistry<void> | |
93 : public base::internal::CallbackRegistryBase<Closure> { | |
94 public: | |
95 CallbackRegistry() {} | |
96 typedef typename base::internal::CallbackRegistryBase<Closure>::Iterator | |
97 Iterator; | |
98 // Execute all active callbacks. | |
99 void Run() { | |
100 Iterator it = this->GetIterator(); | |
101 Closure* cb; | |
102 while((cb = it.GetNext()) != NULL) { | |
103 cb->Run(); | |
104 } | |
105 } | |
106 | |
107 private: | |
108 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry); | |
109 }; | |
110 | |
111 } // namespace base | |
112 | |
113 #endif // BASE_CALLBACK_REGISTRY_H_ | |
OLD | NEW |