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