OLD | NEW |
---|---|
(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 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
8 // Use of this source code is governed by a BSD-style license that can be | |
9 // found in the LICENSE file. | |
10 | |
11 #ifndef BASE_UBER_CALLBACK_H_ | |
12 #define BASE_UBER_CALLBACK_H_ | |
13 #pragma once | |
14 | |
15 #include "base/uber_callback_helpers.h" | |
16 | |
17 // New, super-duper, unified Callback system. This will eventually replace | |
willchan no longer on Chromium
2011/02/06 10:26:50
Change comments. This will look silly in 2-3 years
| |
18 // NewRunnableMethod, NewRunnableFunction, CreateFunctor, and CreateCallback | |
19 // systems currently in the Chromium code base. | |
20 // | |
21 // WHAT IS THIS: | |
22 // | |
23 // The templated Callback class is a generalized funciton object. Together | |
24 // with the Prebind() function in prebind.h, they provide a type-safe method | |
25 // for performing currying of arguments, and createing a "closure." | |
26 // | |
27 // In programing languages, a closure is a first-class function where all its | |
28 // parameters have been bound (usually via currying). Closures are well | |
29 // suited for representing, and passing around a unit of delayed execution. | |
30 // They are used in Chromium code to schedule tasks on different MessageLoops. | |
31 // | |
32 // EXAMPLE USAGE: | |
33 // | |
34 // /* Binding a class member. */ | |
35 // class Ref : public RefCountedThreadSafe<Ref> { | |
36 // int Foo() { return 3; } | |
37 // }; | |
38 // scoped_refptr<Ref> ref = new Ref(); | |
39 // Callback<int(void)> ref_cb = Prebind(&Ref::Foo, ref.get()); | |
40 // LOG(INFO) << ref_cb.Run(); // Prints out 3. | |
41 // | |
42 // /* Binding a class member for a non-refcounted class. */ | |
43 // class NoRef { | |
44 // int Foo() { return 4; } | |
45 // }; | |
46 // NoRef no_ref; | |
47 // Callback<int(void)> no_ref_cb = Prebind(&NoRef::Foo, Unretained(&no_ref)); | |
48 // LOG(INFO) << ref_cb.Run(); // Prints out 4. | |
49 // | |
50 // /* Binding a normal function. */ | |
51 // int Return5() { return 5; } | |
52 // Callback<int(int)> func_cb = Prebind(&Return5); | |
53 // LOG(INFO) << func_cb.Run(5); // Prints 5. | |
54 // | |
55 // /* Binding a reference. */ | |
56 // int Identity(int n) { return n; } | |
57 // int value = 1; | |
58 // Callback<int(void)> bound_copy_cb = Prebind(&Identity, value); | |
59 // Callback<int(void)> bound_ref_cb = Prebind(&Identity, ConstRef(value)); | |
60 // LOG(INFO) << bound_copy_cb.Run(); // Prints 1. | |
61 // LOG(INFO) << bound_ref_cb.Run(); // Prints 1. | |
62 // value = 2; | |
63 // LOG(INFO) << bound_copy_cb.Run(); // Prints 1. | |
64 // LOG(INFO) << bound_ref_cb.Run(); // Prints 2. | |
65 // | |
66 // | |
67 // WHERE IS THIS DESIGN FROM: | |
68 // | |
69 // The design Callback and Prebind is heavily influenced by C++'s | |
70 // tr1::function/tr1::bind, and by the "Google Callback" system used inside | |
71 // Google. | |
72 // | |
73 // | |
74 // WHY NOT TR1 FUNCTION/BIND? | |
75 // | |
76 // Direct use of tr1::function and tr1::bind was considered, but ultimately | |
77 // rejected because of the number of copy constructors invocations involved | |
78 // in the binding of arguments during construction, and the forwarding of | |
79 // arguments during invocation. These copies will no longer be an issue in | |
80 // C++0x because C++0x will support rvalue reference allowing for the compiler | |
81 // to avoid these copies. However, waiting for C++0x is not an option. | |
82 // | |
83 // Measured with valgrind on gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5), the | |
84 // tr1::bind call itself will invoke a non-trivial copy constructor three times | |
85 // for each bound parameter. Also, each when passing a tr1::function, each | |
86 // bound argument will be copied again. | |
87 // | |
88 // In addition to the copies taken at binding and invocation, copying a | |
89 // tr1::function causes a copy to be made of all the bound parameters and | |
90 // state. | |
91 // | |
92 // Furthermore, in Chromium, it is desirable for the Callback to take a | |
93 // reference on a target object when representing a class method call. This | |
94 // is not supported by tr1. | |
95 // | |
96 // Lastly, tr1::function and tr1::bind has a more general and flexible API. | |
97 // This includes things like argument reordering by use of | |
98 // tr1::bind::placeholder, support for non-const reference parameters, and some | |
99 // limited amount of subtyping of the tr1::function object (eg., | |
100 // tr1::function<int(int)> is convertable to tr1::function<void(int)>). | |
101 // | |
102 // These are not features that are required in Chromium. Some of them, such as | |
103 // allowing for reference parameters, and subtyping of functions, may actually | |
104 // because a source of errors. Removing support for these features actually | |
105 // allows for a simpler implementation, and a terser Currying API. | |
106 | |
107 namespace base { | |
108 | |
109 $var MAX_ARITY = 6 | |
110 | |
111 // First, we forward declare the Callback class template. This informs the | |
112 // compiler that ther template only have 1 type parameter: the function | |
113 // signature that the Callback is abstracting. | |
114 // | |
115 // After this, create template specializations for 0-$(MAX_ARITY) parameters. No te that | |
116 // even though the template typelist grows, that the specialization still | |
117 // only has one type: the function signature. | |
118 // | |
119 // Also, note that the templated constructor should *not* be explicit. This is | |
120 // to allow the natural assignment syntax from the result of Prebind(), which | |
121 // is not the same type as Callback(). See the description of Prebind for | |
122 // details. | |
123 template <typename Sig> | |
124 class Callback; | |
125 | |
126 | |
127 $range ARITY 0..MAX_ARITY | |
128 $for ARITY [[ | |
129 $range ARG 1..ARITY | |
130 | |
131 $if ARITY == 0 [[ | |
132 template <typename R> | |
133 class Callback<R(void)> { | |
134 ]] $else [[ | |
135 template <typename R, $for ARG , [[typename A$(ARG)]]> | |
136 class Callback<R($for ARG , [[A$(ARG)]])> { | |
137 ]] | |
138 | |
139 public: | |
140 Callback() : polymorphic_invoke_(NULL) { } | |
141 | |
142 typedef R(*PolymorphicInvoke)(internal::InvokerStorageBase*[[]] | |
143 $if ARITY != 0 [[, ]] $for ARG , [[const A$(ARG)&]]); | |
144 | |
145 template <typename T> | |
146 Callback(const internal::InvokerStorageHolder<T>& invoker_holder) | |
147 : polymorphic_invoke_(&T::FunctionTraits::DoInvoke) { | |
148 invoker_storage_.swap(invoker_holder.invoker_storage_); | |
149 } | |
150 | |
151 | |
152 $if ARITY == 0 [[ | |
153 R Run(void) { | |
154 ]] $else [[ | |
155 R Run($for ARG , [[const A$(ARG)& a$(ARG)]]) { | |
156 ]] | |
157 | |
158 return polymorphic_invoke_(invoker_storage_.get()[[]] | |
159 $if ARITY != 0 [[, ]] $for ARG , [[a$(ARG)]]); | |
160 } | |
161 | |
162 private: | |
163 scoped_refptr<internal::InvokerStorageBase> invoker_storage_; | |
164 PolymorphicInvoke polymorphic_invoke_; | |
165 }; | |
166 | |
167 | |
168 ]] $$ for ARITY | |
169 | |
170 // Syntactic sugar to make Callbacks<void(void)> easier to read since it will | |
171 // be used in a lot of APIs with delayed execution. | |
172 typedef Callback<void(void)> Closure; | |
173 | |
174 } // namespace base | |
175 | |
176 #endif // BASE_UBER_CALLBACK_H | |
OLD | NEW |