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

Side by Side Diff: base/callback_list.h

Issue 22877038: Add a CallbackRegistry class to base/ to manage callbacks (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 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
OLDNEW
(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/callback.h"
14 #include "base/callback_list_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 // base::Closure RegisterCallback(const OnFooCallback& cb) {
34 // return callback_list_.Add(cb);
35 // }
36 //
37 // private:
38 // void NotifyFoo(const Foo& foo) {
39 // callback_list_.Run(foo);
40 // }
41 //
42 // CallbackList<Foo> callback_list_(CallbackList<Foo>::NOTIFY_ALL);
43 // };
44 //
45 //
46 // class MyWidgetListener {
47 // public:
48 // MyWidgetListener::MyWidgetListener() {
49 // remove_foo_callback_.Reset(
50 // MyWidget::GetCurrent()->RegisterCallback(
51 // base::Bind(&MyWidgetListener::OnFoo, this)));
52 // }
53 //
54 // MyWidgetListener::~MyWidgetListener() {
55 // // ScopedClosureRunner runs its closure automatically on deletion.
56 // }
57 //
58 // void OnFoo(const Foo& foo) {
59 // // Do something.
60 // }
61 //
62 // private:
63 // base::ScopedClosureRunner remove_foo_callback_;
64 // };
65
66 namespace base {
67
68 template <typename Details>
69 class CallbackList
70 : public base::internal::CallbackListBase<Callback<void(const Details&)> > {
71 public:
72 typedef base::Callback<void(const Details&)> CallbackType;
73
74 CallbackList() {}
75
76 // Execute all active callbacks with |details| parameter.
77 void Run(const Details& details) {
78 internal::CallbackListImpl::Iterator it = this->GetIterator();
79 CallbackType* cb;
80 // static_cast is safe here. All callbacks were added via
81 // CallbackListBase::Add() and so must be of type
82 // Callback<void(const Details&)>.
83 while((cb = static_cast<CallbackType*>(it.GetNext())) != NULL) {
84 cb->Run(details);
85 }
86 }
87
88 private:
89 DISALLOW_COPY_AND_ASSIGN(CallbackList);
90 };
91
92 template <> class CallbackList<void>
93 : public base::internal::CallbackListBase<Closure> {
94 public:
95 CallbackList() {}
96
97 // Execute all active callbacks.
98 void Run() {
99 internal::CallbackListImpl::Iterator it = this->GetIterator();
100 Closure* cb;
101 // static_cast is safe here. All callbacks were added via
102 // CallbackListBase::Add() and so must be Closures.
103 while((cb = static_cast<Closure*>(it.GetNext())) != NULL) {
104 cb->Run();
105 }
106 }
107
108 private:
109 DISALLOW_COPY_AND_ASSIGN(CallbackList);
110 };
111
112 } // namespace base
113
114 #endif // BASE_CALLBACK_LIST_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698