blob: ead92eadee345477046a5d1d95b40956d0e7ebe0 [file] [log] [blame]
[email protected]243576f2013-09-25 13:56:231// This file was GENERATED by command:
[email protected]2a7cac02013-09-26 19:20:182// pump.py callback_list.h.pump
[email protected]243576f2013-09-25 13:56:233// DO NOT EDIT BY HAND!!!
4
5
[email protected]893c8162013-09-11 15:16:336// Copyright 2013 The Chromium Authors. All rights reserved.
7// Use of this source code is governed by a BSD-style license that can be
8// found in the LICENSE file.
9
[email protected]2a7cac02013-09-26 19:20:1810#ifndef BASE_CALLBACK_LIST_H_
11#define BASE_CALLBACK_LIST_H_
[email protected]893c8162013-09-11 15:16:3312
13#include <list>
14
15#include "base/basictypes.h"
16#include "base/callback.h"
[email protected]243576f2013-09-25 13:56:2317#include "base/callback_internal.h"
[email protected]893c8162013-09-11 15:16:3318#include "base/compiler_specific.h"
19#include "base/logging.h"
20#include "base/memory/scoped_ptr.h"
21
22// OVERVIEW:
23//
24// A container for a list of callbacks. Unlike a normal STL vector or list,
25// this container can be modified during iteration without invalidating the
26// iterator. It safely handles the case of a callback removing itself
27// or another callback from the list while callbacks are being run.
28//
29// TYPICAL USAGE:
30//
31// class MyWidget {
32// public:
33// ...
34//
35// typedef base::Callback<void(const Foo&)> OnFooCallback;
36//
[email protected]2a7cac02013-09-26 19:20:1837// scoped_ptr<base::CallbackList<void(const Foo&)>::Subscription>
[email protected]243576f2013-09-25 13:56:2338// RegisterCallback(const OnFooCallback& cb) {
[email protected]2a7cac02013-09-26 19:20:1839// return callback_list_.Add(cb);
[email protected]893c8162013-09-11 15:16:3340// }
41//
42// private:
43// void NotifyFoo(const Foo& foo) {
[email protected]2a7cac02013-09-26 19:20:1844// callback_list_.Notify(foo);
[email protected]893c8162013-09-11 15:16:3345// }
46//
[email protected]2a7cac02013-09-26 19:20:1847// base::CallbackList<void(const Foo&)> callback_list_;
[email protected]893c8162013-09-11 15:16:3348// };
49//
50//
51// class MyWidgetListener {
52// public:
53// MyWidgetListener::MyWidgetListener() {
54// foo_subscription_ = MyWidget::GetCurrent()->RegisterCallback(
55// base::Bind(&MyWidgetListener::OnFoo, this)));
56// }
57//
58// MyWidgetListener::~MyWidgetListener() {
59// // Subscription gets deleted automatically and will deregister
60// // the callback in the process.
61// }
62//
63// private:
64// void OnFoo(const Foo& foo) {
65// // Do something.
66// }
67//
[email protected]2a7cac02013-09-26 19:20:1868// scoped_ptr<base::CallbackList<void(const Foo&)>::Subscription>
[email protected]243576f2013-09-25 13:56:2369// foo_subscription_;
[email protected]893c8162013-09-11 15:16:3370// };
71
72namespace base {
73
74namespace internal {
75
76template <typename CallbackType>
[email protected]2a7cac02013-09-26 19:20:1877class CallbackListBase {
[email protected]893c8162013-09-11 15:16:3378 public:
79 class Subscription {
80 public:
[email protected]2a7cac02013-09-26 19:20:1881 Subscription(CallbackListBase<CallbackType>* list,
[email protected]893c8162013-09-11 15:16:3382 typename std::list<CallbackType>::iterator iter)
83 : list_(list),
84 iter_(iter) {}
85
86 ~Subscription() {
87 if (list_->active_iterator_count_)
88 (*iter_).Reset();
89 else
90 list_->callbacks_.erase(iter_);
91 }
92
93 private:
[email protected]2a7cac02013-09-26 19:20:1894 CallbackListBase<CallbackType>* list_;
[email protected]893c8162013-09-11 15:16:3395 typename std::list<CallbackType>::iterator iter_;
96
97 DISALLOW_COPY_AND_ASSIGN(Subscription);
98 };
99
100 // Add a callback to the list. The callback will remain registered until the
101 // returned Subscription is destroyed, which must occur before the
[email protected]2a7cac02013-09-26 19:20:18102 // CallbackList is destroyed.
[email protected]893c8162013-09-11 15:16:33103 scoped_ptr<Subscription> Add(const CallbackType& cb) {
104 DCHECK(!cb.is_null());
105 return scoped_ptr<Subscription>(
106 new Subscription(this, callbacks_.insert(callbacks_.end(), cb)));
107 }
108
109 protected:
110 // An iterator class that can be used to access the list of callbacks.
111 class Iterator {
112 public:
[email protected]2a7cac02013-09-26 19:20:18113 explicit Iterator(CallbackListBase<CallbackType>* list)
[email protected]893c8162013-09-11 15:16:33114 : list_(list),
115 list_iter_(list_->callbacks_.begin()) {
116 ++list_->active_iterator_count_;
117 }
118
119 Iterator(const Iterator& iter)
120 : list_(iter.list_),
121 list_iter_(iter.list_iter_) {
122 ++list_->active_iterator_count_;
123 }
124
125 ~Iterator() {
126 if (list_ && --list_->active_iterator_count_ == 0) {
127 list_->Compact();
128 }
129 }
130
131 CallbackType* GetNext() {
132 while ((list_iter_ != list_->callbacks_.end()) && list_iter_->is_null())
133 ++list_iter_;
134
[email protected]da04fbf2013-09-11 16:44:56135 CallbackType* cb = NULL;
136 if (list_iter_ != list_->callbacks_.end()) {
137 cb = &(*list_iter_);
138 ++list_iter_;
139 }
[email protected]893c8162013-09-11 15:16:33140 return cb;
141 }
142
143 private:
[email protected]2a7cac02013-09-26 19:20:18144 CallbackListBase<CallbackType>* list_;
[email protected]893c8162013-09-11 15:16:33145 typename std::list<CallbackType>::iterator list_iter_;
146 };
147
[email protected]2a7cac02013-09-26 19:20:18148 CallbackListBase()
[email protected]893c8162013-09-11 15:16:33149 : active_iterator_count_(0) {}
150
[email protected]2a7cac02013-09-26 19:20:18151 ~CallbackListBase() {
[email protected]893c8162013-09-11 15:16:33152 DCHECK_EQ(0, active_iterator_count_);
153 DCHECK_EQ(0U, callbacks_.size());
154 }
155
[email protected]2a7cac02013-09-26 19:20:18156 // Returns an instance of a CallbackListBase::Iterator which can be used
[email protected]893c8162013-09-11 15:16:33157 // to run callbacks.
158 Iterator GetIterator() {
159 return Iterator(this);
160 }
161
162 // Compact the list: remove any entries which were NULLed out during
163 // iteration.
164 void Compact() {
165 typename std::list<CallbackType>::iterator it = callbacks_.begin();
166 while (it != callbacks_.end()) {
167 if ((*it).is_null())
168 it = callbacks_.erase(it);
169 else
170 ++it;
171 }
172 }
173
174 private:
175 std::list<CallbackType> callbacks_;
176 int active_iterator_count_;
177
[email protected]2a7cac02013-09-26 19:20:18178 DISALLOW_COPY_AND_ASSIGN(CallbackListBase);
[email protected]893c8162013-09-11 15:16:33179};
180
181} // namespace internal
182
[email protected]2a7cac02013-09-26 19:20:18183template <typename Sig> class CallbackList;
[email protected]243576f2013-09-25 13:56:23184
185template <>
[email protected]2a7cac02013-09-26 19:20:18186class CallbackList<void(void)>
187 : public internal::CallbackListBase<Callback<void(void)> > {
[email protected]893c8162013-09-11 15:16:33188 public:
[email protected]243576f2013-09-25 13:56:23189 typedef Callback<void(void)> CallbackType;
190
[email protected]2a7cac02013-09-26 19:20:18191 CallbackList() {}
[email protected]893c8162013-09-11 15:16:33192
[email protected]893c8162013-09-11 15:16:33193 void Notify() {
[email protected]2a7cac02013-09-26 19:20:18194 internal::CallbackListBase<CallbackType>::Iterator it =
[email protected]243576f2013-09-25 13:56:23195 this->GetIterator();
196 CallbackType* cb;
[email protected]893c8162013-09-11 15:16:33197 while((cb = it.GetNext()) != NULL) {
198 cb->Run();
199 }
200 }
201
202 private:
[email protected]2a7cac02013-09-26 19:20:18203 DISALLOW_COPY_AND_ASSIGN(CallbackList);
[email protected]893c8162013-09-11 15:16:33204};
205
[email protected]243576f2013-09-25 13:56:23206template <typename A1>
[email protected]2a7cac02013-09-26 19:20:18207class CallbackList<void(A1)>
208 : public internal::CallbackListBase<
209 Callback<void(A1)> > {
[email protected]243576f2013-09-25 13:56:23210 public:
211 typedef Callback<void(A1)> CallbackType;
212
[email protected]2a7cac02013-09-26 19:20:18213 CallbackList() {}
[email protected]243576f2013-09-25 13:56:23214
215 void Notify(typename internal::CallbackParamTraits<A1>::ForwardType a1) {
[email protected]2a7cac02013-09-26 19:20:18216 typename internal::CallbackListBase<CallbackType>::Iterator it =
[email protected]243576f2013-09-25 13:56:23217 this->GetIterator();
218 CallbackType* cb;
219 while((cb = it.GetNext()) != NULL) {
220 cb->Run(a1);
221 }
222 }
223
224 private:
[email protected]2a7cac02013-09-26 19:20:18225 DISALLOW_COPY_AND_ASSIGN(CallbackList);
[email protected]243576f2013-09-25 13:56:23226};
227
228template <typename A1, typename A2>
[email protected]2a7cac02013-09-26 19:20:18229class CallbackList<void(A1, A2)>
230 : public internal::CallbackListBase<
231 Callback<void(A1, A2)> > {
[email protected]243576f2013-09-25 13:56:23232 public:
233 typedef Callback<void(A1, A2)> CallbackType;
234
[email protected]2a7cac02013-09-26 19:20:18235 CallbackList() {}
[email protected]243576f2013-09-25 13:56:23236
237 void Notify(typename internal::CallbackParamTraits<A1>::ForwardType a1,
238 typename internal::CallbackParamTraits<A2>::ForwardType a2) {
[email protected]2a7cac02013-09-26 19:20:18239 typename internal::CallbackListBase<CallbackType>::Iterator it =
[email protected]243576f2013-09-25 13:56:23240 this->GetIterator();
241 CallbackType* cb;
242 while((cb = it.GetNext()) != NULL) {
243 cb->Run(a1, a2);
244 }
245 }
246
247 private:
[email protected]2a7cac02013-09-26 19:20:18248 DISALLOW_COPY_AND_ASSIGN(CallbackList);
[email protected]243576f2013-09-25 13:56:23249};
250
251template <typename A1, typename A2, typename A3>
[email protected]2a7cac02013-09-26 19:20:18252class CallbackList<void(A1, A2, A3)>
253 : public internal::CallbackListBase<
254 Callback<void(A1, A2, A3)> > {
[email protected]243576f2013-09-25 13:56:23255 public:
256 typedef Callback<void(A1, A2, A3)> CallbackType;
257
[email protected]2a7cac02013-09-26 19:20:18258 CallbackList() {}
[email protected]243576f2013-09-25 13:56:23259
260 void Notify(typename internal::CallbackParamTraits<A1>::ForwardType a1,
261 typename internal::CallbackParamTraits<A2>::ForwardType a2,
262 typename internal::CallbackParamTraits<A3>::ForwardType a3) {
[email protected]2a7cac02013-09-26 19:20:18263 typename internal::CallbackListBase<CallbackType>::Iterator it =
[email protected]243576f2013-09-25 13:56:23264 this->GetIterator();
265 CallbackType* cb;
266 while((cb = it.GetNext()) != NULL) {
267 cb->Run(a1, a2, a3);
268 }
269 }
270
271 private:
[email protected]2a7cac02013-09-26 19:20:18272 DISALLOW_COPY_AND_ASSIGN(CallbackList);
[email protected]243576f2013-09-25 13:56:23273};
274
275template <typename A1, typename A2, typename A3, typename A4>
[email protected]2a7cac02013-09-26 19:20:18276class CallbackList<void(A1, A2, A3, A4)>
277 : public internal::CallbackListBase<
278 Callback<void(A1, A2, A3, A4)> > {
[email protected]243576f2013-09-25 13:56:23279 public:
280 typedef Callback<void(A1, A2, A3, A4)> CallbackType;
281
[email protected]2a7cac02013-09-26 19:20:18282 CallbackList() {}
[email protected]243576f2013-09-25 13:56:23283
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) {
[email protected]2a7cac02013-09-26 19:20:18288 typename internal::CallbackListBase<CallbackType>::Iterator it =
[email protected]243576f2013-09-25 13:56:23289 this->GetIterator();
290 CallbackType* cb;
291 while((cb = it.GetNext()) != NULL) {
292 cb->Run(a1, a2, a3, a4);
293 }
294 }
295
296 private:
[email protected]2a7cac02013-09-26 19:20:18297 DISALLOW_COPY_AND_ASSIGN(CallbackList);
[email protected]243576f2013-09-25 13:56:23298};
299
300template <typename A1, typename A2, typename A3, typename A4, typename A5>
[email protected]2a7cac02013-09-26 19:20:18301class CallbackList<void(A1, A2, A3, A4, A5)>
302 : public internal::CallbackListBase<
303 Callback<void(A1, A2, A3, A4, A5)> > {
[email protected]243576f2013-09-25 13:56:23304 public:
305 typedef Callback<void(A1, A2, A3, A4, A5)> CallbackType;
306
[email protected]2a7cac02013-09-26 19:20:18307 CallbackList() {}
[email protected]243576f2013-09-25 13:56:23308
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) {
[email protected]2a7cac02013-09-26 19:20:18314 typename internal::CallbackListBase<CallbackType>::Iterator it =
[email protected]243576f2013-09-25 13:56:23315 this->GetIterator();
316 CallbackType* cb;
317 while((cb = it.GetNext()) != NULL) {
318 cb->Run(a1, a2, a3, a4, a5);
319 }
320 }
321
322 private:
[email protected]2a7cac02013-09-26 19:20:18323 DISALLOW_COPY_AND_ASSIGN(CallbackList);
[email protected]243576f2013-09-25 13:56:23324};
325
326template <typename A1, typename A2, typename A3, typename A4, typename A5,
327 typename A6>
[email protected]2a7cac02013-09-26 19:20:18328class CallbackList<void(A1, A2, A3, A4, A5, A6)>
329 : public internal::CallbackListBase<
330 Callback<void(A1, A2, A3, A4, A5, A6)> > {
[email protected]243576f2013-09-25 13:56:23331 public:
332 typedef Callback<void(A1, A2, A3, A4, A5, A6)> CallbackType;
333
[email protected]2a7cac02013-09-26 19:20:18334 CallbackList() {}
[email protected]243576f2013-09-25 13:56:23335
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) {
[email protected]2a7cac02013-09-26 19:20:18342 typename internal::CallbackListBase<CallbackType>::Iterator it =
[email protected]243576f2013-09-25 13:56:23343 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:
[email protected]2a7cac02013-09-26 19:20:18351 DISALLOW_COPY_AND_ASSIGN(CallbackList);
[email protected]243576f2013-09-25 13:56:23352};
353
354template <typename A1, typename A2, typename A3, typename A4, typename A5,
355 typename A6, typename A7>
[email protected]2a7cac02013-09-26 19:20:18356class CallbackList<void(A1, A2, A3, A4, A5, A6, A7)>
357 : public internal::CallbackListBase<
358 Callback<void(A1, A2, A3, A4, A5, A6, A7)> > {
[email protected]243576f2013-09-25 13:56:23359 public:
360 typedef Callback<void(A1, A2, A3, A4, A5, A6, A7)> CallbackType;
361
[email protected]2a7cac02013-09-26 19:20:18362 CallbackList() {}
[email protected]243576f2013-09-25 13:56:23363
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) {
[email protected]2a7cac02013-09-26 19:20:18371 typename internal::CallbackListBase<CallbackType>::Iterator it =
[email protected]243576f2013-09-25 13:56:23372 this->GetIterator();
373 CallbackType* cb;
374 while((cb = it.GetNext()) != NULL) {
375 cb->Run(a1, a2, a3, a4, a5, a6, a7);
376 }
377 }
378
379 private:
[email protected]2a7cac02013-09-26 19:20:18380 DISALLOW_COPY_AND_ASSIGN(CallbackList);
[email protected]243576f2013-09-25 13:56:23381};
382
[email protected]893c8162013-09-11 15:16:33383} // namespace base
384
[email protected]2a7cac02013-09-26 19:20:18385#endif // BASE_CALLBACK_LIST_H_