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 |
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<void<int>) cb = |
| 170 // base::Bind(base::IgnoreResult(&DoSomething)); |
| 171 // |
| 172 // |
| 173 // ----------------------------------------------------------------------------- |
| 174 // Quick reference for binding parameters to Bind() |
| 175 // ----------------------------------------------------------------------------- |
| 176 // |
| 177 // Bound parameters are specified as arguments to Bind() and are passed to the |
| 178 // function. A callback with no parameters or no unbound parameters is called a |
| 179 // Closure (base::Callback<void(void)> and base::Closure are the same thing). |
| 180 // |
| 181 // PASSING PARAMETERS OWNED BY THE CALLBACK |
| 182 // |
| 183 // void Foo(int* arg) { cout << *arg << endl; } |
| 184 // int* pn = new int(1); |
| 185 // base::Closure foo_callback = base::Bind(&foo, base::Owned(pn)); |
| 186 // |
| 187 // The parameter will be deleted when the callback is destroyed, even if it's |
| 188 // not run (like if you post a task during shutdown). |
| 189 // |
| 190 // PASSING PARAMETERS AS A scoped_ptr |
| 191 // |
| 192 // void TakesOwnership(scoped_ptr<Foo> arg) {} |
| 193 // scoped_ptr<Foo> f(new Foo); |
| 194 // // f becomes null during the following call. |
| 195 // base::Closure cb = base::Bind(&TakesOwnership, base::Passed(&f)); |
| 196 // |
| 197 // Ownership of the parameter will be with the callback until the it is run, |
| 198 // when ownership is passed to the callback function. This means the callback |
| 199 // can only be run once. If the callback is never run, it will delete the |
| 200 // object when it's destroyed. |
| 201 // |
| 202 // PASSING PARAMETERS AS A scoped_refptr |
| 203 // |
| 204 // void TakesOneRef(scoped_refptr<Foo> arg) {} |
| 205 // scoped_refptr<Foo> f(new Foo) |
| 206 // base::Closure cb = base::Bind(&TakesOneRef, f); |
| 207 // |
| 208 // This should "just work." The closure will take a reference as long as it |
| 209 // is alive, and another reference will be taken for the called function. |
| 210 // |
| 211 // PASSING PARAMETERS BY REFERENCE |
| 212 // |
| 213 // void foo(int arg) { cout << arg << endl } |
| 214 // int n = 1; |
| 215 // base::Closure has_ref = base::Bind(&foo, base::ConstRef(n)); |
| 216 // n = 2; |
| 217 // has_ref.Run(); // Prints "2" |
| 218 // |
| 219 // Normally parameters are copied in the closure. DANGER: ConstRef stores a |
| 220 // const reference instead, referencing the original parameter. This means |
| 221 // that you must ensure the object outlives the callback! |
| 222 // |
| 223 // |
| 224 // ----------------------------------------------------------------------------- |
| 225 // Implementation notes |
| 226 // ----------------------------------------------------------------------------- |
110 // | 227 // |
111 // WHERE IS THIS DESIGN FROM: | 228 // WHERE IS THIS DESIGN FROM: |
112 // | 229 // |
113 // The design Callback and Bind is heavily influenced by C++'s | 230 // The design Callback and Bind is heavily influenced by C++'s |
114 // tr1::function/tr1::bind, and by the "Google Callback" system used inside | 231 // tr1::function/tr1::bind, and by the "Google Callback" system used inside |
115 // Google. | 232 // Google. |
116 // | 233 // |
117 // | 234 // |
118 // HOW THE IMPLEMENTATION WORKS: | 235 // HOW THE IMPLEMENTATION WORKS: |
119 // | 236 // |
(...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
625 }; | 742 }; |
626 | 743 |
627 | 744 |
628 // Syntactic sugar to make Callbacks<void(void)> easier to declare since it | 745 // Syntactic sugar to make Callbacks<void(void)> easier to declare since it |
629 // will be used in a lot of APIs with delayed execution. | 746 // will be used in a lot of APIs with delayed execution. |
630 typedef Callback<void(void)> Closure; | 747 typedef Callback<void(void)> Closure; |
631 | 748 |
632 } // namespace base | 749 } // namespace base |
633 | 750 |
634 #endif // BASE_CALLBACK_H | 751 #endif // BASE_CALLBACK_H |
OLD | NEW |