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

Side by Side Diff: base/callback.h.pump

Issue 10692147: Add quick reference for bind/callback usage. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 5 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 | « base/callback.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 $$ This is a pump file for generating file templates. Pump is a python 1 $$ This is a pump file for generating file templates. Pump is a python
2 $$ script that is part of the Google Test suite of utilities. Description 2 $$ script that is part of the Google Test suite of utilities. Description
3 $$ can be found here: 3 $$ can be found here:
4 $$ 4 $$
5 $$ http://code.google.com/p/googletest/wiki/PumpManual 5 $$ http://code.google.com/p/googletest/wiki/PumpManual
6 $$ 6 $$
7 7
8 $$ See comment for MAX_ARITY in base/bind.h.pump. 8 $$ See comment for MAX_ARITY in base/bind.h.pump.
9 $var MAX_ARITY = 7 9 $var MAX_ARITY = 7
10 10
11 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 11 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
12 // Use of this source code is governed by a BSD-style license that can be 12 // Use of this source code is governed by a BSD-style license that can be
13 // found in the LICENSE file. 13 // found in the LICENSE file.
14 14
15 #ifndef BASE_CALLBACK_H_ 15 #ifndef BASE_CALLBACK_H_
16 #define BASE_CALLBACK_H_ 16 #define BASE_CALLBACK_H_
17 #pragma once 17 #pragma once
18 18
19 #include "base/callback_forward.h" 19 #include "base/callback_forward.h"
20 #include "base/callback_internal.h" 20 #include "base/callback_internal.h"
21 #include "base/template_util.h" 21 #include "base/template_util.h"
22 22
23 // NOTE: Header files that do not require the full definition of Callback or 23 // NOTE: Header files that do not require the full definition of Callback or
24 // Closure should #include "base/callback_forward.h" instead of this file. 24 // Closure should #include "base/callback_forward.h" instead of this file.
25 25
26 // WHAT IS THIS: 26 // -----------------------------------------------------------------------------
27 // Introduction
28 // -----------------------------------------------------------------------------
27 // 29 //
28 // The templated Callback class is a generalized function object. Together 30 // The templated Callback class is a generalized function object. Together
29 // with the Bind() function in bind.h, they provide a type-safe method for 31 // with the Bind() function in bind.h, they provide a type-safe method for
30 // performing currying of arguments, and creating a "closure." 32 // performing currying of arguments, and creating a "closure."
31 // 33 //
32 // In programming languages, a closure is a first-class function where all its 34 // In programming languages, a closure is a first-class function where all its
33 // parameters have been bound (usually via currying). Closures are well 35 // parameters have been bound (usually via currying). Closures are well
34 // suited for representing, and passing around a unit of delayed execution. 36 // suited for representing, and passing around a unit of delayed execution.
35 // They are used in Chromium code to schedule tasks on different MessageLoops. 37 // They are used in Chromium code to schedule tasks on different MessageLoops.
36 // 38 //
37 // 39 //
38 // MEMORY MANAGEMENT AND PASSING 40 // MEMORY MANAGEMENT AND PASSING
39 // 41 //
40 // The Callback objects themselves should be passed by const-reference, and 42 // The Callback objects themselves should be passed by const-reference, and
41 // stored by copy. They internally store their state via a refcounted class 43 // stored by copy. They internally store their state via a refcounted class
42 // and thus do not need to be deleted. 44 // and thus do not need to be deleted.
43 // 45 //
44 // The reason to pass via a const-reference is to avoid unnecessary 46 // The reason to pass via a const-reference is to avoid unnecessary
45 // AddRef/Release pairs to the internal state. 47 // AddRef/Release pairs to the internal state.
46 // 48 //
47 // 49 //
48 // EXAMPLE USAGE: 50 // -----------------------------------------------------------------------------
51 // Quick reference for basic stuff
52 // -----------------------------------------------------------------------------
49 // 53 //
50 // /* Binding a normal function. */ 54 // BINDING A BARE FUNCTION
51 // int Return5() { return 5; }
52 // base::Callback<int(void)> func_cb = base::Bind(&Return5);
53 // LOG(INFO) << func_cb.Run(); // Prints 5.
54 // 55 //
55 // void PrintHi() { LOG(INFO) << "hi."; } 56 // int Return5() { return 5; }
56 // base::Closure void_func_cb = base::Bind(&PrintHi); 57 // base::Callback<int(void)> func_cb = base::Bind(&Return5);
57 // void_func_cb.Run(); // Prints: hi. 58 // LOG(INFO) << func_cb.Run(); // Prints 5.
58 // 59 //
59 // /* Binding a class method. */ 60 // BINDING A CLASS METHOD
60 // class Ref : public RefCountedThreadSafe<Ref> {
61 // public:
62 // int Foo() { return 3; }
63 // void PrintBye() { LOG(INFO) << "bye."; }
64 // };
65 // scoped_refptr<Ref> ref = new Ref();
66 // base::Callback<int(void)> ref_cb = base::Bind(&Ref::Foo, ref.get());
67 // LOG(INFO) << ref_cb.Run(); // Prints out 3.
68 // 61 //
69 // base::Closure void_ref_cb = base::Bind(&Ref::PrintBye, ref.get()); 62 // The first argument to bind is the member function to call, the second is
70 // void_ref_cb.Run(); // Prints: bye. 63 // the object on which to call it.
71 // 64 //
72 // /* Binding a class method in a non-refcounted class. 65 // class Ref : public base::RefCountedThreadSafe<Ref> {
73 // * 66 // public:
74 // * WARNING: You must be sure the referee outlives the callback! 67 // int Foo() { return 3; }
75 // * This is particularly important if you post a closure to a 68 // void PrintBye() { LOG(INFO) << "bye."; }
76 // * MessageLoop because then it becomes hard to know what the 69 // };
77 // * lifetime of the referee needs to be. 70 // scoped_refptr<Ref> ref = new Ref();
78 // */ 71 // base::Callback<void(void)> ref_cb = base::Bind(&Ref::Foo, ref);
79 // class NoRef { 72 // LOG(INFO) << ref_cb.Run(); // Prints out 3.
80 // public:
81 // int Foo() { return 4; }
82 // void PrintWhy() { LOG(INFO) << "why???"; }
83 // };
84 // NoRef no_ref;
85 // base::Callback<int(void)> base::no_ref_cb =
86 // base::Bind(&NoRef::Foo, base::Unretained(&no_ref));
87 // LOG(INFO) << ref_cb.Run(); // Prints out 4.
88 // 73 //
89 // base::Closure void_no_ref_cb = 74 // By default the object must support RefCounted or you will get a compiler
90 // base::Bind(&NoRef::PrintWhy, base::Unretained(no_ref)); 75 // error. If you’re passing between threads, be sure it’s
91 // void_no_ref_cb.Run(); // Prints: why??? 76 // RefCountedThreadSafe! See "Advanced binding of member functions" below if
77 // you don't want to use reference counting.
92 // 78 //
93 // /* Binding a reference. */ 79 // RUNNING A CALLBACK
94 // int Identity(int n) { return n; }
95 // int value = 1;
96 // base::Callback<int(void)> bound_copy_cb = base::Bind(&Identity, value);
97 // base::Callback<int(void)> bound_ref_cb =
98 // base::Bind(&Identity, base::ConstRef(value));
99 // LOG(INFO) << bound_copy_cb.Run(); // Prints 1.
100 // LOG(INFO) << bound_ref_cb.Run(); // Prints 1.
101 // value = 2;
102 // LOG(INFO) << bound_copy_cb.Run(); // Prints 1.
103 // LOG(INFO) << bound_ref_cb.Run(); // Prints 2.
104 // 80 //
105 // /* Currying parameters. This also works for methods. */ 81 // Callbacks can be run with their "Run" method, which has the same
106 // int Sum(int a, int b, int c) { 82 // signature as the template argument to the callback.
107 // return a + b + c;
108 // }
109 // base::Callback<int(int, int)> sum3_cb = base::Bind(&Sum, 3);
110 // LOG(INFO) << sum3_cb.Run(4, 5); // Prints 12.
111 // 83 //
112 // base::Callback<int(int)> sum7_cb = base::Bind(&Sum, 3, 4); 84 // void DoSomething(const base::Callback<void(int, std::string)>& callback) {
113 // LOG(INFO) << sum7_cb.Run(10); // Prints 17. 85 // callback.Run(5, "hello");
86 // }
114 // 87 //
88 // Callbacks can be run more than once (they don't get deleted or marked when
89 // run). However, this precludes using base::Passed (see below).
90 //
91 // void DoSomething(const base::Callback<double(double)>& callback) {
92 // double myresult = callback.Run(3.14159);
93 // myresult += callback.Run(2.71828);
94 // }
95 //
96 // PASSING UNBOUND INPUT PARAMETERS
97 //
98 // Unbound parameters are specified at the time a callback is Run(). They are
99 // specified in the Callback template type:
100 //
101 // void MyFunc(int i, const std::string& str) {}
102 // base::Callback<void(int, const std::string&)> cb = base::Bind(&MyFunc);
103 // cb.Run(23, "hello, world");
104 //
105 // PASSING BOUND INPUT PARAMETERS
106 //
107 // Bound parameters are specified when you create thee callback as arguments
108 // to Bind(). They will be passed to the function and the Run()ner of the
109 // callback doesn't see those values or even know that the function it's
110 // calling.
111 //
112 // void MyFunc(int i, const std::string& str) {}
113 // base::Callback<void(void)> cb = base::Bind(&MyFunc, 23, "hello world");
114 // cb.Run();
115 //
116 // A callback with no unbound input parameters (base::Callback<void(void)>)
117 // is called a base::Closure. So we could have also written:
118 //
119 // base::Closure cb = base::Bind(&MyFunc, 23, "hello world");
120 //
121 // When calling member functions, bound parameters just go after the object
122 // pointer.
123 //
124 // base::Closure cb = base::Bind(&MyClass::MyFunc, this, 23, "hello world");
125 //
126 // PARTIAL BINDING OF PARAMETERS
127 //
128 // You can specify some parameters when you create the callback, and specify
129 // the rest when you execute the callback.
130 //
131 // void MyFunc(int i, const std::string& str) {}
132 // base::Callback<void(int)> cb = base::Bind(&MyFunc, 23);
133 // cb.Run("hello world");
134 //
135 // When calling a function bound parameters are first, followed by unbound
136 // parameters.
137 //
138 //
139 // -----------------------------------------------------------------------------
140 // Quick reference for advanced binding
141 // -----------------------------------------------------------------------------
142 //
143 // BINDING A CLASS METHOD WITH WEAK POINTERS
144 //
145 // base::Bind(&MyClass::Foo, GetWeakPtr());
146 //
147 // The callback will not be issued if the object is destroyed at the time
148 // it’s issued. DANGER: weak pointers are not threadsafe, so don’t use this
149 // when passing between threads!
150 //
151 // BINDING A CLASS METHOD WITH MANUAL LIFETIME MANAGEMENT
152 //
153 // base::Bind(&MyClass::Foo, base::Unretained(this));
154 //
155 // This disables all lifetime management on the object. You’re responsible
156 // for making sure the object is alive at the time of the call. You break it,
157 // you own it!
158 //
159 // BINDING A CLASS METHOD AND HAVING THE CALLBACK OWN THE CLASS
160 //
161 // MyClass* myclass = new MyClass;
162 // base::Bind(&MyClass::Foo, base::Owned(myclass));
163 //
164 // The object will be deleted when the callback is destroyed, even if it’s
165 // not run (like if you post a task during shutdown). Potentially useful for
166 // "fire and forget" cases.
167 //
168 // IGNORING RETURN VALUES
169 //
170 // Sometimes you want to call a function that returns a value in a callback
171 // that doesn’t expect a return value.
172 //
173 // int DoSomething(int arg) { cout << arg << endl; }
174 // base::Callback<void<int>) cb =
175 // base::Bind(base::IgnoreResult(&DoSomething));
176 //
177 //
178 // -----------------------------------------------------------------------------
179 // Quick reference for binding parameters to Bind()
180 // -----------------------------------------------------------------------------
181 //
182 // Bound parameters are specified as arguments to Bind() and are passed to the
183 // function. A callback with no parameters or no unbound parameters is called a
184 // Closure (base::Callback<void(void)> and base::Closure are the same thing).
185 //
186 // PASSING PARAMETERS OWNED BY THE CALLBACK
187 //
188 // void Foo(int* arg) { cout << *arg << endl; }
189 // int* pn = new int(1);
190 // base::Closure foo_callback = base::Bind(&foo, base::Owned(pn));
191 //
192 // The parameter will be deleted when the callback is destroyed, even if it’s
193 // not run (like if you post a task during shutdown).
194 //
195 // PASSING PARAMETERS AS A scoped_ptr
196 //
197 // void TakesOwnership(scoped_ptr<Foo> arg) {}
198 // scoped_ptr<Foo> f(new Foo);
199 // // f becomes null during the following call.
200 // base::Closure cb = base::Bind(&TakesOwnership, base::Passed(&f));
201 //
202 // Ownership of the parameter will be with the callback until the it is run,
203 // when ownership is passed to the callback function. This means the callback
204 // can only be run once. If the callback is never run, it will delete the
205 // object when it’s destroyed.
206 //
207 // PASSING PARAMETERS AS A scoped_refptr
208 //
209 // void TakesOneRef(scoped_refptr<Foo> arg) {}
210 // scoped_refptr<Foo> f(new Foo)
211 // base::Closure cb = base::Bind(&TakesOneRef, f);
212 //
213 // This should "just work." The closure will take a reference as long as it
214 // is alive, and another reference will be taken for the called function.
215 //
216 // PASSING PARAMETERS BY REFERENCE
217 //
218 // void foo(int arg) { cout << arg << endl }
219 // int n = 1;
220 // base::Closure has_ref = base::Bind(&foo, base::ConstRef(n));
221 // n = 2;
222 // has_ref.Run(); // Prints "2"
223 //
224 // Normally parameters are copied in the closure. DANGER: ConstRef stores a
225 // const reference instead, referencing the original parameter. This means
226 // that you must ensure the object outlives the callback!
227 //
228 //
229 // -----------------------------------------------------------------------------
230 // Implementation notes
231 // -----------------------------------------------------------------------------
115 // 232 //
116 // WHERE IS THIS DESIGN FROM: 233 // WHERE IS THIS DESIGN FROM:
117 // 234 //
118 // The design Callback and Bind is heavily influenced by C++'s 235 // The design Callback and Bind is heavily influenced by C++'s
119 // tr1::function/tr1::bind, and by the "Google Callback" system used inside 236 // tr1::function/tr1::bind, and by the "Google Callback" system used inside
120 // Google. 237 // Google.
121 // 238 //
122 // 239 //
123 // HOW THE IMPLEMENTATION WORKS: 240 // HOW THE IMPLEMENTATION WORKS:
124 // 241 //
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 420
304 ]] $$ for ARITY 421 ]] $$ for ARITY
305 422
306 // Syntactic sugar to make Callbacks<void(void)> easier to declare since it 423 // Syntactic sugar to make Callbacks<void(void)> easier to declare since it
307 // will be used in a lot of APIs with delayed execution. 424 // will be used in a lot of APIs with delayed execution.
308 typedef Callback<void(void)> Closure; 425 typedef Callback<void(void)> Closure;
309 426
310 } // namespace base 427 } // namespace base
311 428
312 #endif // BASE_CALLBACK_H 429 #endif // BASE_CALLBACK_H
OLDNEW
« no previous file with comments | « base/callback.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698