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

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

Issue 6109007: Unified callback system. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/base
Patch Set: more bits of cleanup Created 9 years, 10 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 $$ 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
3 $$ can be found here:
4 $$
5 $$ http://code.google.com/p/googletest/wiki/PumpManual
6 $$
7
8 $var MAX_ARITY = 6
9
10 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
11 // Use of this source code is governed by a BSD-style license that can be
12 // found in the LICENSE file.
13
14 #ifndef BASE_UBER_CALLBACK_H_
15 #define BASE_UBER_CALLBACK_H_
16 #pragma once
17
18 #include "base/uber_callback_helpers.h"
19
20 // New, super-duper, unified Callback system. This will eventually replace
21 // NewRunnableMethod, NewRunnableFunction, CreateFunctor, and CreateCallback
22 // systems currently in the Chromium code base.
23 //
24 // WHAT IS THIS:
25 //
26 // The templated Callback class is a generalized funciton object. Together
27 // with the Prebind() function in prebind.h, they provide a type-safe method
28 // for performing currying of arguments, and createing a "closure."
29 //
30 // In programing languages, a closure is a first-class function where all its
31 // parameters have been bound (usually via currying). Closures are well
32 // suited for representing, and passing around a unit of delayed execution.
33 // They are used in Chromium code to schedule tasks on different MessageLoops.
34 //
35 // EXAMPLE USAGE:
36 //
37 // /* Binding a class method. */
38 // class Ref : public RefCountedThreadSafe<Ref> {
39 // int Foo() { return 3; }
40 // };
41 // scoped_refptr<Ref> ref = new Ref();
42 // Callback<int(void)> ref_cb = Prebind(&Ref::Foo, ref.get());
43 // LOG(INFO) << ref_cb.Run(); // Prints out 3.
44 //
45 // /* Binding a class method in a non-refcounted class. */
46 // class NoRef {
47 // int Foo() { return 4; }
48 // };
49 // NoRef no_ref;
50 // Callback<int(void)> no_ref_cb = Prebind(&NoRef::Foo, Unretained(&no_ref));
51 // LOG(INFO) << ref_cb.Run(); // Prints out 4.
52 //
53 // /* Binding a normal function. */
54 // int Return5() { return 5; }
55 // Callback<int(int)> func_cb = Prebind(&Return5);
56 // LOG(INFO) << func_cb.Run(5); // Prints 5.
57 //
58 // /* Binding a reference. */
59 // int Identity(int n) { return n; }
60 // int value = 1;
61 // Callback<int(void)> bound_copy_cb = Prebind(&Identity, value);
62 // Callback<int(void)> bound_ref_cb = Prebind(&Identity, ConstRef(value));
63 // LOG(INFO) << bound_copy_cb.Run(); // Prints 1.
64 // LOG(INFO) << bound_ref_cb.Run(); // Prints 1.
65 // value = 2;
66 // LOG(INFO) << bound_copy_cb.Run(); // Prints 1.
67 // LOG(INFO) << bound_ref_cb.Run(); // Prints 2.
68 //
69 //
70 // WHERE IS THIS DESIGN FROM:
71 //
72 // The design Callback and Prebind is heavily influenced by C++'s
73 // tr1::function/tr1::bind, and by the "Google Callback" system used inside
74 // Google.
75 //
76 //
77 // HOW THE IMPLEMENTATION WORKS:
78 //
79 // There are three main components to the system:
80 // 1) The Callback classes.
81 // 2) The Prebind() functions.
82 // 3) The arguments wrappers (eg., Unretained() and ConstRef()).
83 //
84 // The Callback classes represent a generic function pointer. Internally,
85 // it stores a refcounted piece of state that represents the target function
86 // and all its bound parameters. Each Callback specialization has a templated
87 // constructor that takes an InvokerStorageHolder<> object. In the context of
88 // the constructor, the static type of this InvokerStorageHolder<> object
89 // uniquely identifies the function it is representing, all its bound
90 // parameters, and a DoInvoke that is capable of invoking the target.
91 //
92 // Callback's constructor is takes the InvokerStorageHolder<> that has the
93 // full static type and erases the target function type, and the bound
94 // parameters. It does this by storing a pointer to the specific DoInvoke
95 // function, and upcasting the state of InvokerStorageHolder<> to a
96 // InvokerStorageBase. This is safe as long as this InvokerStorageBase pointer
97 // is only used with the stored DoInvoke pointer.
98 //
99 // To create InvokerStorageHolder<> objects, we use the Prebind() functions.
100 // These functions, along with a set of internal templates, are reponsible for
101 //
102 // - Unwrapping the function signature into return type, and parameters
103 // - Determining the number of parameters that are bound
104 // - Creating the storage for the bound parameters
105 // - Performing compile-time asserts to avoid error-prone behavior
106 // - Returning an InvokerStorageHolder<> with an DoInvoke that has an arity
107 // matching the number of unbound parameters, and knows the correct
108 // refcounting semantics for the target object if we are binding a class
109 // method.
110 //
111 // The Prebind functions do the above using type-inference, and template
112 // specializations.
113 //
114 // By default Prebind() will store copies of all bound parameters, and attempt
115 // to refcount a target object if the function being bound is a class method.
116 //
117 // To change this behavior, we introduce a set of argument wrappers
118 // (eg. Unretained(), and ConstRef()). These are simple container templates
119 // that are passed by value, and wrap a pointer to argument.
120 //
121 // ConstRef() allows Prebind()'s storage to preserve copy-semantics even if we
122 // wish to pass the invoked object a reference to the bound parameter.
123 //
124 // Unretained() allows us to tag an object for different refcounting semantics.
125 //
126 // These types are passed to the Unwrap() functions, and the MaybeRefcount()
127 // functions respectively to modify the behavior of Prebind(). The Unwrap()
128 // and MaybeRefcount() functions change behavior by doing partial
129 // specialization based on whether or not a parameter is a wrapper type.
130 //
131 // ConstRef() is similar to tr1::cref. Unretained() is specific to Chromium.
132 //
133 //
134 // WHY NOT TR1 FUNCTION/BIND?
135 //
136 // Direct use of tr1::function and tr1::bind was considered, but ultimately
137 // rejected because of the number of copy constructors invocations involved
138 // in the binding of arguments during construction, and the forwarding of
139 // arguments during invocation. These copies will no longer be an issue in
140 // C++0x because C++0x will support rvalue reference allowing for the compiler
141 // to avoid these copies. However, waiting for C++0x is not an option.
142 //
143 // Measured with valgrind on gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5), the
144 // tr1::bind call itself will invoke a non-trivial copy constructor three times
145 // for each bound parameter. Also, each when passing a tr1::function, each
146 // bound argument will be copied again.
147 //
148 // In addition to the copies taken at binding and invocation, copying a
149 // tr1::function causes a copy to be made of all the bound parameters and
150 // state.
151 //
152 // Furthermore, in Chromium, it is desirable for the Callback to take a
153 // reference on a target object when representing a class method call. This
154 // is not supported by tr1.
155 //
156 // Lastly, tr1::function and tr1::bind has a more general and flexible API.
157 // This includes things like argument reordering by use of
158 // tr1::bind::placeholder, support for non-const reference parameters, and some
159 // limited amount of subtyping of the tr1::function object (eg.,
160 // tr1::function<int(int)> is convertable to tr1::function<void(int)>).
161 //
162 // These are not features that are required in Chromium. Some of them, such as
163 // allowing for reference parameters, and subtyping of functions, may actually
164 // because a source of errors. Removing support for these features actually
165 // allows for a simpler implementation, and a terser Currying API.
166 //
167 //
168 // WHY NOT GOOGLE CALLBACKS?
169 //
170 // The Google callback system also does not support refcounting. Furthermore,
171 // its implementation has a number of strange edge cases with respect to type
172 // convesrion of its arguments. In particular, the argument's constness must
173 // at times match exactly the function signature, or the type-inference might
174 // break. Given the above, writing a custom solution was easier.
175 //
176 //
177 // MISSING FUNCTIONALITY
178 // - Invoking the return of Prebind. Prebind(&foo).Run() does not work;
179 // - Binding arrays to functions that take a non-const pointer.
180 // Example:
181 // void Foo(const char* ptr);
182 // void Bar(char* ptr);
183 // Prebind(&Foo, "test");
184 // Prebind(&Bar, "tesT"); // This fails because ptr is not const.
185
186 namespace base {
187
188 // First, we forward declare the Callback class template. This informs the
189 // compiler that the template only has 1 type parameter which is the function
190 // signature that the Callback is representing.
191 //
192 // After this, create template specializations for 0-$(MAX_ARITY) parameters. No te that
193 // even though the template typelist grows, the specialization still
194 // only has one type: the function signature.
195 //
196 // Also, note that the templated constructor should *not* be explicit. This is
197 // to allow for a natural assignment syntax from the result of Prebind(), which
198 // is not the same type as Callback(). See the description of Prebind for
199 // details.
200 template <typename Sig>
201 class Callback;
202
203
204 $range ARITY 0..MAX_ARITY
205 $for ARITY [[
206 $range ARG 1..ARITY
207
208 $if ARITY == 0 [[
209 template <typename R>
210 class Callback<R(void)> {
211 ]] $else [[
212 template <typename R, $for ARG , [[typename A$(ARG)]]>
213 class Callback<R($for ARG , [[A$(ARG)]])> {
214 ]]
215
216 public:
217 Callback() : polymorphic_invoke_(NULL) { }
218
219 typedef R(*PolymorphicInvoke)(internal::InvokerStorageBase*[[]]
220 $if ARITY != 0 [[, ]] $for ARG , [[const A$(ARG)&]]);
221
222 template <typename T>
223 Callback(const internal::InvokerStorageHolder<T>& invoker_holder)
224 : polymorphic_invoke_(&T::FunctionTraits::DoInvoke) {
225 invoker_storage_.swap(invoker_holder.invoker_storage_);
226 }
227
228
229 $if ARITY == 0 [[
230 R Run(void) {
231 ]] $else [[
232 R Run($for ARG , [[const A$(ARG)& a$(ARG)]]) {
233 ]]
234
235 return polymorphic_invoke_(invoker_storage_.get()[[]]
236 $if ARITY != 0 [[, ]] $for ARG , [[a$(ARG)]]);
237 }
238
239 private:
240 scoped_refptr<internal::InvokerStorageBase> invoker_storage_;
241 PolymorphicInvoke polymorphic_invoke_;
242 };
243
244
245 ]] $$ for ARITY
246
247 // Syntactic sugar to make Callbacks<void(void)> easier to declare since it
248 // will be used in a lot of APIs with delayed execution.
249 typedef Callback<void(void)> Closure;
250
251 } // namespace base
252
253 #endif // BASE_UBER_CALLBACK_H
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698