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_LIST_H_ | |
6 #define BASE_CALLBACK_LIST_H_ | |
7 | |
8 #include <algorithm> | |
9 #include <limits> | |
10 #include <vector> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/bind.h" | |
14 #include "base/callback.h" | |
15 #include "base/callback_list_internal.h" | |
16 #include "base/memory/scoped_ptr.h" | |
17 #include "base/memory/weak_ptr.h" | |
18 | |
19 // OVERVIEW: | |
20 // | |
21 // A container for a list of callbacks. Unlike a normal STL vector or list, | |
22 // this container can be modified during iteration without invalidating the | |
23 // iterator. So, it safely handles the case of an callback removing itself | |
erikwright (departed)
2013/09/04 19:35:55
an->a
Cait (Slow)
2013/09/04 22:09:25
Done.
| |
24 // or other callback from the list while callbacks are being run. | |
erikwright (departed)
2013/09/04 19:35:55
either 'other'->'another' or 'callback'->'callback
Cait (Slow)
2013/09/04 22:09:25
Done.
| |
25 // | |
26 // TYPICAL USAGE: | |
27 // | |
28 // class MyWidget { | |
29 // public: | |
30 // ... | |
31 // | |
32 // typedef base::Callback<void(const Foo&)> OnFooCallback; | |
33 // | |
34 // base::Closure RegisterCallback(const OnFooCallback& cb) { | |
35 // return callback_list_.Add(cb); | |
36 // } | |
37 // | |
38 // private: | |
39 // void NotifyFoo(const Foo& foo) { | |
40 // callback_list_.Run(foo); | |
41 // } | |
42 // | |
43 // CallbackList<Foo> callback_list_(CallbackList<Foo>::NOTIFY_ALL); | |
44 // }; | |
45 // | |
46 // | |
47 // class MyWidgetListener { | |
48 // public: | |
49 // MyWidgetListener::MyWidgetListener() { | |
50 // remove_foo_callback_.Reset( | |
51 // MyWidget::GetCurrent()->RegisterCallback( | |
52 // base::Bind(&MyWidgetListener::OnFoo, this))); | |
53 // } | |
54 // | |
55 // MyWidgetListener::~MyWidgetListener() { | |
56 // // ScopedClosureRunner runs its closure automatically on deletion. | |
57 // } | |
58 // | |
59 // void OnFoo(const Foo& foo) { | |
60 // // Do something. | |
61 // } | |
62 // | |
63 // private: | |
64 // base::ScopedClosureRunner remove_foo_callback_; | |
65 // }; | |
66 | |
67 namespace base { | |
68 | |
69 template <typename T> | |
70 struct CallbackHelper { | |
awong
2013/09/04 18:48:21
Anything that isn't in the public API should be in
Cait (Slow)
2013/09/04 22:09:25
Done.
| |
71 typedef base::Callback<void(const T&)> Type; | |
72 }; | |
73 | |
74 template <> | |
75 struct CallbackHelper<void> { | |
76 typedef base::Closure Type; | |
77 }; | |
78 | |
79 template <typename Details> | |
80 class CallbackListBase { | |
awong
2013/09/04 18:48:21
As discussed on IM, this feels like it should be s
Cait (Slow)
2013/09/04 22:09:25
Done.
| |
81 public: | |
82 typedef typename CallbackHelper<Details>::Type CallbackType; | |
awong
2013/09/04 18:48:21
I think you may need to typedef CallbackNotificati
Cait (Slow)
2013/09/04 22:09:25
Done.
| |
83 | |
84 explicit CallbackListBase(CallbackNotificationType type) | |
erikwright (departed)
2013/09/04 19:35:55
Should be protected.
Cait (Slow)
2013/09/04 22:09:25
Done.
| |
85 : list_impl_(new base::internal::CallbackListImpl(type)) {} | |
86 | |
87 // Add a callback to the list. A callback should not be added to | |
88 // the same list more than once. The returned closure (used to remove the | |
89 // callback from the list) is guaranteed to be safe to run. | |
90 base::Closure Add(const CallbackType& cb) { | |
erikwright (departed)
2013/09/04 19:35:55
It seems like, in almost all cases, it would proba
Cait (Slow)
2013/09/04 22:09:25
Going with WARN_UNUSED_RESULT for now. Will look i
| |
91 DCHECK(!cb.is_null()); | |
92 return list_impl_->AddCallback(new CallbackType(cb)); | |
93 } | |
94 | |
95 // Delete (or nullify, if called during iteration), all callbacks in the list. | |
erikwright (departed)
2013/09/04 19:35:55
I don't think that the parenthetical is relevant c
Cait (Slow)
2013/09/04 22:09:25
Done.
| |
96 void Clear() { | |
97 list_impl_->Clear(); | |
98 } | |
99 | |
100 bool might_have_callbacks() { | |
awong
2013/09/04 18:48:21
One more thought...if this is necessary to expose,
erikwright (departed)
2013/09/04 19:35:55
What client would call this?
awong
2013/09/04 19:50:41
IIRC, the discussion with Cait is that some client
Cait (Slow)
2013/09/04 22:09:25
Switched to an AssertEmpty, which checks 1. no act
| |
101 return list_impl_->might_have_callbacks(); | |
102 } | |
103 | |
104 protected: | |
105 // Execute all active (non-null) callbacks with |details| parameter. | |
erikwright (departed)
2013/09/04 19:35:55
ditto on the parenthetical. And the rest (about |d
Cait (Slow)
2013/09/04 22:09:25
Done.
| |
106 template <typename Visitor> | |
107 void VisitCallbacks(Visitor visitor) { | |
awong
2013/09/04 18:48:21
The visitor is a nifty way to reuse code, but sinc
Cait (Slow)
2013/09/04 22:09:25
Done.
| |
108 if (might_have_callbacks()) { | |
109 internal::CallbackListImpl::Iterator it(list_impl_->GetWeakPtr()); | |
110 CallbackType* cb; | |
111 while((cb = static_cast<CallbackType*>(it.GetNext())) != NULL) { | |
112 visitor.Visit(*cb); | |
113 } | |
114 } | |
115 } | |
116 | |
117 private: | |
118 scoped_ptr<base::internal::CallbackListImpl> list_impl_; | |
119 | |
120 DISALLOW_COPY_AND_ASSIGN(CallbackListBase); | |
121 }; | |
122 | |
123 template <typename Details> | |
124 class CallbackList : public CallbackListBase<Details> { | |
125 public: | |
126 typedef base::Callback<void(const Details&)> CallbackType; | |
127 | |
128 class Visitor { | |
erikwright (departed)
2013/09/04 19:35:55
can be private
Cait (Slow)
2013/09/04 22:09:25
Done.
| |
129 public: | |
130 explicit Visitor(const Details& details) : details_(details) {} | |
131 void Visit(const CallbackType& callback) { | |
132 callback.Run(details_); | |
133 } | |
134 private: | |
135 const Details& details_; | |
136 }; | |
137 | |
138 explicit CallbackList(CallbackNotificationType type) | |
139 : CallbackListBase<Details>(type) {} | |
140 | |
141 // Execute all active (non-null) callbacks with |details| parameter. | |
erikwright (departed)
2013/09/04 19:35:55
ditto on the parenthetical.
Cait (Slow)
2013/09/04 22:09:25
Done.
| |
142 void Run(const Details& details) { | |
143 this->template VisitCallbacks<Visitor>(Visitor(details)); | |
144 } | |
145 | |
146 private: | |
147 DISALLOW_COPY_AND_ASSIGN(CallbackList); | |
148 }; | |
149 | |
150 template <> class CallbackList<void> : public CallbackListBase<void> { | |
151 public: | |
152 typedef Closure CallbackType; | |
153 | |
154 class Visitor { | |
erikwright (departed)
2013/09/04 19:35:55
private
Cait (Slow)
2013/09/04 22:09:25
Done.
| |
155 public: | |
156 void Visit(const CallbackType& callback) { | |
157 callback.Run(); | |
158 } | |
159 }; | |
160 | |
161 explicit CallbackList(CallbackNotificationType type) | |
162 : CallbackListBase<void>(type) {} | |
163 | |
164 // Execute all active (non-null) callbacks. | |
erikwright (departed)
2013/09/04 19:35:55
parenthetical
Cait (Slow)
2013/09/04 22:09:25
Done.
| |
165 void Run() { | |
166 CallbackListBase<void>::VisitCallbacks<Visitor>(Visitor()); | |
167 } | |
168 | |
169 private: | |
170 DISALLOW_COPY_AND_ASSIGN(CallbackList); | |
171 }; | |
172 | |
173 } // namespace base | |
awong
2013/09/04 18:48:21
nit:
} // namespace base
Cait (Slow)
2013/09/04 22:09:25
Done.
| |
174 | |
175 #endif // BASE_CALLBACK_LIST_H_ | |
OLD | NEW |