Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1143)

Side by Side Diff: base/callback_registry.h

Issue 23514056: Function-type templated CallbackRegistry (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: CallbackParamTraits Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | base/callback_registry.h.pump » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 //
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 172
167 private: 173 private:
168 std::list<CallbackType> callbacks_; 174 std::list<CallbackType> callbacks_;
169 int active_iterator_count_; 175 int active_iterator_count_;
170 176
171 DISALLOW_COPY_AND_ASSIGN(CallbackRegistryBase); 177 DISALLOW_COPY_AND_ASSIGN(CallbackRegistryBase);
172 }; 178 };
173 179
174 } // namespace internal 180 } // namespace internal
175 181
176 template <typename Details> 182 template <typename Sig> class CallbackRegistry;
erikwright (departed) 2013/09/17 18:21:49 blank between decl and specialization.
Cait (Slow) 2013/09/19 17:29:57 Done.
177 class CallbackRegistry 183 template <>
178 : public internal::CallbackRegistryBase<Callback<void(const Details&)> > { 184 class CallbackRegistry<void(void)>
185 : public internal::CallbackRegistryBase<Callback<void(void)> > {
179 public: 186 public:
187 typedef Callback<void(void)> CallbackType;
188
180 CallbackRegistry() {} 189 CallbackRegistry() {}
181 190
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() { 191 void Notify() {
203 Iterator it = this->GetIterator(); 192 typename internal::CallbackRegistryBase<CallbackType>::Iterator it =
204 Closure* cb; 193 this->GetIterator();
194 CallbackType* cb;
205 while((cb = it.GetNext()) != NULL) { 195 while((cb = it.GetNext()) != NULL) {
206 cb->Run(); 196 cb->Run();
207 } 197 }
208 } 198 }
209 199
200 private:
201 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry);
202 };
203
204 template <typename A1>
205 class CallbackRegistry<void(A1)>
206 : public internal::CallbackRegistryBase<
207 Callback<void(A1)> > {
208 public:
209 typedef Callback<void(A1)> CallbackType;
210
211 CallbackRegistry() {}
212
213 void Notify(typename internal::CallbackParamTraits<A1>::ForwardType a1) {
erikwright (departed) 2013/09/17 18:21:49 It will surprise no one to hear that I'm not a fan
awong 2013/09/17 18:31:00 I'm slammed right now between perf and a some outs
214 typename internal::CallbackRegistryBase<CallbackType>::Iterator it =
215 this->GetIterator();
216 CallbackType* cb;
217 while((cb = it.GetNext()) != NULL) {
218 cb->Run(a1);
219 }
220 }
221
222 private:
223 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry);
224 };
225
226 template <typename A1, typename A2>
227 class CallbackRegistry<void(A1, A2)>
228 : public internal::CallbackRegistryBase<
229 Callback<void(A1, A2)> > {
230 public:
231 typedef Callback<void(A1, A2)> CallbackType;
232
233 CallbackRegistry() {}
234
235 void Notify(typename internal::CallbackParamTraits<A1>::ForwardType a1,
236 typename internal::CallbackParamTraits<A2>::ForwardType a2) {
237 typename internal::CallbackRegistryBase<CallbackType>::Iterator it =
238 this->GetIterator();
239 CallbackType* cb;
240 while((cb = it.GetNext()) != NULL) {
241 cb->Run(a1, a2);
242 }
243 }
244
245 private:
246 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry);
247 };
248
249 template <typename A1, typename A2, typename A3>
250 class CallbackRegistry<void(A1, A2, A3)>
251 : public internal::CallbackRegistryBase<
252 Callback<void(A1, A2, A3)> > {
253 public:
254 typedef Callback<void(A1, A2, A3)> CallbackType;
255
256 CallbackRegistry() {}
257
258 void Notify(typename internal::CallbackParamTraits<A1>::ForwardType a1,
259 typename internal::CallbackParamTraits<A2>::ForwardType a2,
260 typename internal::CallbackParamTraits<A3>::ForwardType a3) {
261 typename internal::CallbackRegistryBase<CallbackType>::Iterator it =
262 this->GetIterator();
263 CallbackType* cb;
264 while((cb = it.GetNext()) != NULL) {
265 cb->Run(a1, a2, a3);
266 }
267 }
268
269 private:
270 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry);
271 };
272
273 template <typename A1, typename A2, typename A3, typename A4>
274 class CallbackRegistry<void(A1, A2, A3, A4)>
275 : public internal::CallbackRegistryBase<
276 Callback<void(A1, A2, A3, A4)> > {
277 public:
278 typedef Callback<void(A1, A2, A3, A4)> CallbackType;
279
280 CallbackRegistry() {}
281
282 void Notify(typename internal::CallbackParamTraits<A1>::ForwardType a1,
283 typename internal::CallbackParamTraits<A2>::ForwardType a2,
284 typename internal::CallbackParamTraits<A3>::ForwardType a3,
285 typename internal::CallbackParamTraits<A4>::ForwardType a4) {
286 typename internal::CallbackRegistryBase<CallbackType>::Iterator it =
287 this->GetIterator();
288 CallbackType* cb;
289 while((cb = it.GetNext()) != NULL) {
290 cb->Run(a1, a2, a3, a4);
291 }
292 }
293
294 private:
295 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry);
296 };
297
298 template <typename A1, typename A2, typename A3, typename A4, typename A5>
299 class CallbackRegistry<void(A1, A2, A3, A4, A5)>
300 : public internal::CallbackRegistryBase<
301 Callback<void(A1, A2, A3, A4, A5)> > {
302 public:
303 typedef Callback<void(A1, A2, A3, A4, A5)> CallbackType;
304
305 CallbackRegistry() {}
306
307 void Notify(typename internal::CallbackParamTraits<A1>::ForwardType a1,
308 typename internal::CallbackParamTraits<A2>::ForwardType a2,
309 typename internal::CallbackParamTraits<A3>::ForwardType a3,
310 typename internal::CallbackParamTraits<A4>::ForwardType a4,
311 typename internal::CallbackParamTraits<A5>::ForwardType a5) {
312 typename internal::CallbackRegistryBase<CallbackType>::Iterator it =
313 this->GetIterator();
314 CallbackType* cb;
315 while((cb = it.GetNext()) != NULL) {
316 cb->Run(a1, a2, a3, a4, a5);
317 }
318 }
319
320 private:
321 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry);
322 };
323
324 template <typename A1, typename A2, typename A3, typename A4, typename A5,
325 typename A6>
326 class CallbackRegistry<void(A1, A2, A3, A4, A5, A6)>
327 : public internal::CallbackRegistryBase<
328 Callback<void(A1, A2, A3, A4, A5, A6)> > {
329 public:
330 typedef Callback<void(A1, A2, A3, A4, A5, A6)> CallbackType;
331
332 CallbackRegistry() {}
333
334 void Notify(typename internal::CallbackParamTraits<A1>::ForwardType a1,
335 typename internal::CallbackParamTraits<A2>::ForwardType a2,
336 typename internal::CallbackParamTraits<A3>::ForwardType a3,
337 typename internal::CallbackParamTraits<A4>::ForwardType a4,
338 typename internal::CallbackParamTraits<A5>::ForwardType a5,
339 typename internal::CallbackParamTraits<A6>::ForwardType a6) {
340 typename internal::CallbackRegistryBase<CallbackType>::Iterator it =
341 this->GetIterator();
342 CallbackType* cb;
343 while((cb = it.GetNext()) != NULL) {
344 cb->Run(a1, a2, a3, a4, a5, a6);
345 }
346 }
347
348 private:
349 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry);
350 };
351
352 template <typename A1, typename A2, typename A3, typename A4, typename A5,
353 typename A6, typename A7>
354 class CallbackRegistry<void(A1, A2, A3, A4, A5, A6, A7)>
355 : public internal::CallbackRegistryBase<
356 Callback<void(A1, A2, A3, A4, A5, A6, A7)> > {
357 public:
358 typedef Callback<void(A1, A2, A3, A4, A5, A6, A7)> CallbackType;
359
360 CallbackRegistry() {}
361
362 void Notify(typename internal::CallbackParamTraits<A1>::ForwardType a1,
363 typename internal::CallbackParamTraits<A2>::ForwardType a2,
364 typename internal::CallbackParamTraits<A3>::ForwardType a3,
365 typename internal::CallbackParamTraits<A4>::ForwardType a4,
366 typename internal::CallbackParamTraits<A5>::ForwardType a5,
367 typename internal::CallbackParamTraits<A6>::ForwardType a6,
368 typename internal::CallbackParamTraits<A7>::ForwardType a7) {
369 typename internal::CallbackRegistryBase<CallbackType>::Iterator it =
370 this->GetIterator();
371 CallbackType* cb;
372 while((cb = it.GetNext()) != NULL) {
373 cb->Run(a1, a2, a3, a4, a5, a6, a7);
374 }
375 }
376
210 private: 377 private:
211 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry); 378 DISALLOW_COPY_AND_ASSIGN(CallbackRegistry);
212 }; 379 };
213 380
214 } // namespace base 381 } // namespace base
215 382
216 #endif // BASE_CALLBACK_REGISTRY_H_ 383 #endif // BASE_CALLBACK_REGISTRY_H
OLDNEW
« no previous file with comments | « no previous file | base/callback_registry.h.pump » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698