PNaCl: Remove friendly reminder about new manifest format for optlevel.
[chromium-blink-merge.git] / base / callback_registry.h
blobdcdeb81d01eb9b260baf9cddbc72760d57a84131
1 // This file was GENERATED by command:
2 // pump.py callback_registry.h.pump
3 // DO NOT EDIT BY HAND!!!
6 // 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.
10 #ifndef BASE_CALLBACK_REGISTRY_H_
11 #define BASE_CALLBACK_REGISTRY_H_
13 #include <list>
15 #include "base/basictypes.h"
16 #include "base/callback.h"
17 #include "base/callback_internal.h"
18 #include "base/compiler_specific.h"
19 #include "base/logging.h"
20 #include "base/memory/scoped_ptr.h"
22 // OVERVIEW:
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.
29 // TYPICAL USAGE:
31 // class MyWidget {
32 // public:
33 // ...
35 // typedef base::Callback<void(const Foo&)> OnFooCallback;
37 // scoped_ptr<base::CallbackRegistry<void(const Foo&)>::Subscription>
38 // RegisterCallback(const OnFooCallback& cb) {
39 // return callback_registry_.Add(cb);
40 // }
42 // private:
43 // void NotifyFoo(const Foo& foo) {
44 // callback_registry_.Notify(foo);
45 // }
47 // base::CallbackRegistry<void(const Foo&)> callback_registry_;
48 // };
51 // class MyWidgetListener {
52 // public:
53 // MyWidgetListener::MyWidgetListener() {
54 // foo_subscription_ = MyWidget::GetCurrent()->RegisterCallback(
55 // base::Bind(&MyWidgetListener::OnFoo, this)));
56 // }
58 // MyWidgetListener::~MyWidgetListener() {
59 // // Subscription gets deleted automatically and will deregister
60 // // the callback in the process.
61 // }
63 // private:
64 // void OnFoo(const Foo& foo) {
65 // // Do something.
66 // }
68 // scoped_ptr<base::CallbackRegistry<void(const Foo&)>::Subscription>
69 // foo_subscription_;
70 // };
72 namespace base {
74 namespace internal {
76 template <typename CallbackType>
77 class CallbackRegistryBase {
78 public:
79 class Subscription {
80 public:
81 Subscription(CallbackRegistryBase<CallbackType>* list,
82 typename std::list<CallbackType>::iterator iter)
83 : list_(list),
84 iter_(iter) {}
86 ~Subscription() {
87 if (list_->active_iterator_count_)
88 (*iter_).Reset();
89 else
90 list_->callbacks_.erase(iter_);
93 private:
94 CallbackRegistryBase<CallbackType>* list_;
95 typename std::list<CallbackType>::iterator iter_;
97 DISALLOW_COPY_AND_ASSIGN(Subscription);
100 // Add a callback to the list. The callback will remain registered until the
101 // returned Subscription is destroyed, which must occur before the
102 // CallbackRegistry is destroyed.
103 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)));
109 protected:
110 // An iterator class that can be used to access the list of callbacks.
111 class Iterator {
112 public:
113 explicit Iterator(CallbackRegistryBase<CallbackType>* list)
114 : list_(list),
115 list_iter_(list_->callbacks_.begin()) {
116 ++list_->active_iterator_count_;
119 Iterator(const Iterator& iter)
120 : list_(iter.list_),
121 list_iter_(iter.list_iter_) {
122 ++list_->active_iterator_count_;
125 ~Iterator() {
126 if (list_ && --list_->active_iterator_count_ == 0) {
127 list_->Compact();
131 CallbackType* GetNext() {
132 while ((list_iter_ != list_->callbacks_.end()) && list_iter_->is_null())
133 ++list_iter_;
135 CallbackType* cb = NULL;
136 if (list_iter_ != list_->callbacks_.end()) {
137 cb = &(*list_iter_);
138 ++list_iter_;
140 return cb;
143 private:
144 CallbackRegistryBase<CallbackType>* list_;
145 typename std::list<CallbackType>::iterator list_iter_;
148 CallbackRegistryBase()
149 : active_iterator_count_(0) {}
151 ~CallbackRegistryBase() {
152 DCHECK_EQ(0, active_iterator_count_);
153 DCHECK_EQ(0U, callbacks_.size());
156 // Returns an instance of a CallbackRegistryBase::Iterator which can be used
157 // to run callbacks.
158 Iterator GetIterator() {
159 return Iterator(this);
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;
174 private:
175 std::list<CallbackType> callbacks_;
176 int active_iterator_count_;
178 DISALLOW_COPY_AND_ASSIGN(CallbackRegistryBase);
181 } // namespace internal
183 template <typename Sig> class CallbackRegistry;
185 template <>
186 class CallbackRegistry<void(void)>
187 : public internal::CallbackRegistryBase<Callback<void(void)> > {
188 public:
189 typedef Callback<void(void)> CallbackType;
191 CallbackRegistry() {}
193 void Notify() {
194 internal::CallbackRegistryBase<CallbackType>::Iterator it =
195 this->GetIterator();
196 CallbackType* cb;
197 while((cb = it.GetNext()) != NULL) {
198 cb->Run();
202 private:
203 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry);
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;
213 CallbackRegistry() {}
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);
224 private:
225 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry);
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;
235 CallbackRegistry() {}
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);
247 private:
248 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry);
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;
258 CallbackRegistry() {}
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);
271 private:
272 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry);
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;
282 CallbackRegistry() {}
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);
296 private:
297 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry);
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;
307 CallbackRegistry() {}
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);
322 private:
323 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry);
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;
334 CallbackRegistry() {}
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);
350 private:
351 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry);
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;
362 CallbackRegistry() {}
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);
379 private:
380 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry);
383 } // namespace base
385 #endif // BASE_CALLBACK_REGISTRY_H