OLD | NEW |
(Empty) | |
| 1 // This file was GENERATED by command: |
| 2 // pump.py uber_callback.h.pump |
| 3 // DO NOT EDIT BY HAND!!! |
| 4 |
| 5 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 6 // Use of this source code is governed by a BSD-style license that can be |
| 7 // found in the LICENSE file. |
| 8 |
| 9 #ifndef BASE_UBER_CALLBACK_H_ |
| 10 #define BASE_UBER_CALLBACK_H_ |
| 11 #pragma once |
| 12 |
| 13 #include "base/uber_callback_helpers.h" |
| 14 |
| 15 // New, super-duper, unified Callback system. This will eventually replace |
| 16 // NewRunnableMethod, NewRunnableFunction, CreateFunctor, and CreateCallback |
| 17 // systems currently in the Chromium code base. |
| 18 // |
| 19 // WHAT IS THIS: |
| 20 // |
| 21 // The templated Callback class is a generalized funciton object. Together |
| 22 // with the Prebind() function in prebind.h, they provide a type-safe method |
| 23 // for performing currying of arguments, and createing a "closure." |
| 24 // |
| 25 // In programing languages, a closure is a first-class function where all its |
| 26 // parameters have been bound (usually via currying). Closures are well |
| 27 // suited for representing, and passing around a unit of delayed execution. |
| 28 // They are used in Chromium code to schedule tasks on different MessageLoops. |
| 29 // |
| 30 // EXAMPLE USAGE: |
| 31 // |
| 32 // /* Binding a class member. */ |
| 33 // class Ref : public RefCountedThreadSafe<Ref> { |
| 34 // int Foo() { return 3; } |
| 35 // }; |
| 36 // scoped_refptr<Ref> ref = new Ref(); |
| 37 // Callback<int(void)> ref_cb = Prebind(&Ref::Foo, ref.get()); |
| 38 // LOG(INFO) << ref_cb.Run(); // Prints out 3. |
| 39 // |
| 40 // /* Binding a class member for a non-refcounted class. */ |
| 41 // class NoRef { |
| 42 // int Foo() { return 4; } |
| 43 // }; |
| 44 // NoRef no_ref; |
| 45 // Callback<int(void)> no_ref_cb = Prebind(&NoRef::Foo, Unretained(&no_ref)); |
| 46 // LOG(INFO) << ref_cb.Run(); // Prints out 4. |
| 47 // |
| 48 // /* Binding a normal function. */ |
| 49 // int Return5() { return 5; } |
| 50 // Callback<int(int)> func_cb = Prebind(&Return5); |
| 51 // LOG(INFO) << func_cb.Run(5); // Prints 5. |
| 52 // |
| 53 // /* Binding a reference. */ |
| 54 // int Identity(int n) { return n; } |
| 55 // int value = 1; |
| 56 // Callback<int(void)> bound_copy_cb = Prebind(&Identity, value); |
| 57 // Callback<int(void)> bound_ref_cb = Prebind(&Identity, ConstRef(value)); |
| 58 // LOG(INFO) << bound_copy_cb.Run(); // Prints 1. |
| 59 // LOG(INFO) << bound_ref_cb.Run(); // Prints 1. |
| 60 // value = 2; |
| 61 // LOG(INFO) << bound_copy_cb.Run(); // Prints 1. |
| 62 // LOG(INFO) << bound_ref_cb.Run(); // Prints 2. |
| 63 // |
| 64 // |
| 65 // WHERE IS THIS DESIGN FROM: |
| 66 // |
| 67 // The design Callback and Prebind is heavily influenced by C++'s |
| 68 // tr1::function/tr1::bind, and by the "Google Callback" system used inside |
| 69 // Google. |
| 70 // |
| 71 // |
| 72 // WHY NOT TR1 FUNCTION/BIND? |
| 73 // |
| 74 // Direct use of tr1::function and tr1::bind was considered, but ultimately |
| 75 // rejected because of the number of copy constructors invocations involved |
| 76 // in the binding of arguments during construction, and the forwarding of |
| 77 // arguments during invocation. These copies will no longer be an issue in |
| 78 // C++0x because C++0x will support rvalue reference allowing for the compiler |
| 79 // to avoid these copies. However, waiting for C++0x is not an option. |
| 80 // |
| 81 // Measured with valgrind on gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5), the |
| 82 // tr1::bind call itself will invoke a non-trivial copy constructor three times |
| 83 // for each bound parameter. Also, each when passing a tr1::function, each |
| 84 // bound argument will be copied again. |
| 85 // |
| 86 // In addition to the copies taken at binding and invocation, copying a |
| 87 // tr1::function causes a copy to be made of all the bound parameters and |
| 88 // state. |
| 89 // |
| 90 // Furthermore, in Chromium, it is desirable for the Callback to take a |
| 91 // reference on a target object when representing a class method call. This |
| 92 // is not supported by tr1. |
| 93 // |
| 94 // Lastly, tr1::function and tr1::bind has a more general and flexible API. |
| 95 // This includes things like argument reordering by use of |
| 96 // tr1::bind::placeholder, support for non-const reference parameters, and some |
| 97 // limited amount of subtyping of the tr1::function object (eg., |
| 98 // tr1::function<int(int)> is convertable to tr1::function<void(int)>). |
| 99 // |
| 100 // These are not features that are required in Chromium. Some of them, such as |
| 101 // allowing for reference parameters, and subtyping of functions, may actually |
| 102 // because a source of errors. Removing support for these features actually |
| 103 // allows for a simpler implementation, and a terser Currying API. |
| 104 |
| 105 namespace base { |
| 106 |
| 107 // First, we forward declare the Callback class template. This informs the |
| 108 // compiler that ther template only have 1 type parameter: the function |
| 109 // signature that the Callback is abstracting. |
| 110 // |
| 111 // After this, create template specializations for 0-6 parameters. Note that |
| 112 // even though the template typelist grows, that the specialization still |
| 113 // only has one type: the function signature. |
| 114 // |
| 115 // Also, note that the templated constructor should *not* be explicit. This is |
| 116 // to allow the natural assignment syntax from the result of Prebind(), which |
| 117 // is not the same type as Callback(). See the description of Prebind for |
| 118 // details. |
| 119 template <typename Sig> |
| 120 class Callback; |
| 121 |
| 122 template <typename R> |
| 123 class Callback<R(void)> { |
| 124 public: |
| 125 Callback() : polymorphic_invoke_(NULL) { } |
| 126 |
| 127 typedef R(*PolymorphicInvoke)(internal::InvokerStorageBase*); |
| 128 |
| 129 template <typename T> |
| 130 Callback(const internal::InvokerStorageHolder<T>& invoker_holder) |
| 131 : polymorphic_invoke_(&T::FunctionTraits::DoInvoke) { |
| 132 invoker_storage_.swap(invoker_holder.invoker_storage_); |
| 133 } |
| 134 |
| 135 R Run(void) { |
| 136 return polymorphic_invoke_(invoker_storage_.get()); |
| 137 } |
| 138 |
| 139 private: |
| 140 scoped_refptr<internal::InvokerStorageBase> invoker_storage_; |
| 141 PolymorphicInvoke polymorphic_invoke_; |
| 142 }; |
| 143 |
| 144 template <typename R, typename A1> |
| 145 class Callback<R(A1)> { |
| 146 public: |
| 147 Callback() : polymorphic_invoke_(NULL) { } |
| 148 |
| 149 typedef R(*PolymorphicInvoke)(internal::InvokerStorageBase*, const A1&); |
| 150 |
| 151 template <typename T> |
| 152 Callback(const internal::InvokerStorageHolder<T>& invoker_holder) |
| 153 : polymorphic_invoke_(&T::FunctionTraits::DoInvoke) { |
| 154 invoker_storage_.swap(invoker_holder.invoker_storage_); |
| 155 } |
| 156 |
| 157 R Run(const A1& a1) { |
| 158 return polymorphic_invoke_(invoker_storage_.get(), a1); |
| 159 } |
| 160 |
| 161 private: |
| 162 scoped_refptr<internal::InvokerStorageBase> invoker_storage_; |
| 163 PolymorphicInvoke polymorphic_invoke_; |
| 164 }; |
| 165 |
| 166 template <typename R, typename A1, typename A2> |
| 167 class Callback<R(A1, A2)> { |
| 168 public: |
| 169 Callback() : polymorphic_invoke_(NULL) { } |
| 170 |
| 171 typedef R(*PolymorphicInvoke)(internal::InvokerStorageBase*, const A1&, |
| 172 const A2&); |
| 173 |
| 174 template <typename T> |
| 175 Callback(const internal::InvokerStorageHolder<T>& invoker_holder) |
| 176 : polymorphic_invoke_(&T::FunctionTraits::DoInvoke) { |
| 177 invoker_storage_.swap(invoker_holder.invoker_storage_); |
| 178 } |
| 179 |
| 180 R Run(const A1& a1, const A2& a2) { |
| 181 return polymorphic_invoke_(invoker_storage_.get(), a1, a2); |
| 182 } |
| 183 |
| 184 private: |
| 185 scoped_refptr<internal::InvokerStorageBase> invoker_storage_; |
| 186 PolymorphicInvoke polymorphic_invoke_; |
| 187 }; |
| 188 |
| 189 template <typename R, typename A1, typename A2, typename A3> |
| 190 class Callback<R(A1, A2, A3)> { |
| 191 public: |
| 192 Callback() : polymorphic_invoke_(NULL) { } |
| 193 |
| 194 typedef R(*PolymorphicInvoke)(internal::InvokerStorageBase*, const A1&, |
| 195 const A2&, const A3&); |
| 196 |
| 197 template <typename T> |
| 198 Callback(const internal::InvokerStorageHolder<T>& invoker_holder) |
| 199 : polymorphic_invoke_(&T::FunctionTraits::DoInvoke) { |
| 200 invoker_storage_.swap(invoker_holder.invoker_storage_); |
| 201 } |
| 202 |
| 203 R Run(const A1& a1, const A2& a2, const A3& a3) { |
| 204 return polymorphic_invoke_(invoker_storage_.get(), a1, a2, a3); |
| 205 } |
| 206 |
| 207 private: |
| 208 scoped_refptr<internal::InvokerStorageBase> invoker_storage_; |
| 209 PolymorphicInvoke polymorphic_invoke_; |
| 210 }; |
| 211 |
| 212 template <typename R, typename A1, typename A2, typename A3, typename A4> |
| 213 class Callback<R(A1, A2, A3, A4)> { |
| 214 public: |
| 215 Callback() : polymorphic_invoke_(NULL) { } |
| 216 |
| 217 typedef R(*PolymorphicInvoke)(internal::InvokerStorageBase*, const A1&, |
| 218 const A2&, const A3&, const A4&); |
| 219 |
| 220 template <typename T> |
| 221 Callback(const internal::InvokerStorageHolder<T>& invoker_holder) |
| 222 : polymorphic_invoke_(&T::FunctionTraits::DoInvoke) { |
| 223 invoker_storage_.swap(invoker_holder.invoker_storage_); |
| 224 } |
| 225 |
| 226 R Run(const A1& a1, const A2& a2, const A3& a3, const A4& a4) { |
| 227 return polymorphic_invoke_(invoker_storage_.get(), a1, a2, a3, a4); |
| 228 } |
| 229 |
| 230 private: |
| 231 scoped_refptr<internal::InvokerStorageBase> invoker_storage_; |
| 232 PolymorphicInvoke polymorphic_invoke_; |
| 233 }; |
| 234 |
| 235 template <typename R, typename A1, typename A2, typename A3, typename A4, |
| 236 typename A5> |
| 237 class Callback<R(A1, A2, A3, A4, A5)> { |
| 238 public: |
| 239 Callback() : polymorphic_invoke_(NULL) { } |
| 240 |
| 241 typedef R(*PolymorphicInvoke)(internal::InvokerStorageBase*, const A1&, |
| 242 const A2&, const A3&, const A4&, const A5&); |
| 243 |
| 244 template <typename T> |
| 245 Callback(const internal::InvokerStorageHolder<T>& invoker_holder) |
| 246 : polymorphic_invoke_(&T::FunctionTraits::DoInvoke) { |
| 247 invoker_storage_.swap(invoker_holder.invoker_storage_); |
| 248 } |
| 249 |
| 250 R Run(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5) { |
| 251 return polymorphic_invoke_(invoker_storage_.get(), a1, a2, a3, a4, a5); |
| 252 } |
| 253 |
| 254 private: |
| 255 scoped_refptr<internal::InvokerStorageBase> invoker_storage_; |
| 256 PolymorphicInvoke polymorphic_invoke_; |
| 257 }; |
| 258 |
| 259 template <typename R, typename A1, typename A2, typename A3, typename A4, |
| 260 typename A5, typename A6> |
| 261 class Callback<R(A1, A2, A3, A4, A5, A6)> { |
| 262 public: |
| 263 Callback() : polymorphic_invoke_(NULL) { } |
| 264 |
| 265 typedef R(*PolymorphicInvoke)(internal::InvokerStorageBase*, const A1&, |
| 266 const A2&, const A3&, const A4&, const A5&, const A6&); |
| 267 |
| 268 template <typename T> |
| 269 Callback(const internal::InvokerStorageHolder<T>& invoker_holder) |
| 270 : polymorphic_invoke_(&T::FunctionTraits::DoInvoke) { |
| 271 invoker_storage_.swap(invoker_holder.invoker_storage_); |
| 272 } |
| 273 |
| 274 R Run(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, |
| 275 const A6& a6) { |
| 276 return polymorphic_invoke_(invoker_storage_.get(), a1, a2, a3, a4, a5, a6); |
| 277 } |
| 278 |
| 279 private: |
| 280 scoped_refptr<internal::InvokerStorageBase> invoker_storage_; |
| 281 PolymorphicInvoke polymorphic_invoke_; |
| 282 }; |
| 283 |
| 284 |
| 285 // Syntactic sugar to make Callbacks<void(void)> easier to read since it will |
| 286 // be used in a lot of APIs with delayed execution. |
| 287 typedef Callback<void(void)> Closure; |
| 288 |
| 289 } // namespace base |
| 290 |
| 291 #endif // BASE_UBER_CALLBACK_H |
OLD | NEW |