security/sandbox/chromium/base/callback.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // This file was GENERATED by command:
michael@0 2 // pump.py callback.h.pump
michael@0 3 // DO NOT EDIT BY HAND!!!
michael@0 4
michael@0 5
michael@0 6 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
michael@0 7 // Use of this source code is governed by a BSD-style license that can be
michael@0 8 // found in the LICENSE file.
michael@0 9
michael@0 10 #ifndef BASE_CALLBACK_H_
michael@0 11 #define BASE_CALLBACK_H_
michael@0 12
michael@0 13 #include "base/callback_forward.h"
michael@0 14 #include "base/callback_internal.h"
michael@0 15 #include "base/template_util.h"
michael@0 16
michael@0 17 // NOTE: Header files that do not require the full definition of Callback or
michael@0 18 // Closure should #include "base/callback_forward.h" instead of this file.
michael@0 19
michael@0 20 // -----------------------------------------------------------------------------
michael@0 21 // Introduction
michael@0 22 // -----------------------------------------------------------------------------
michael@0 23 //
michael@0 24 // The templated Callback class is a generalized function object. Together
michael@0 25 // with the Bind() function in bind.h, they provide a type-safe method for
michael@0 26 // performing partial application of functions.
michael@0 27 //
michael@0 28 // Partial application (or "currying") is the process of binding a subset of
michael@0 29 // a function's arguments to produce another function that takes fewer
michael@0 30 // arguments. This can be used to pass around a unit of delayed execution,
michael@0 31 // much like lexical closures are used in other languages. For example, it
michael@0 32 // is used in Chromium code to schedule tasks on different MessageLoops.
michael@0 33 //
michael@0 34 // A callback with no unbound input parameters (base::Callback<void(void)>)
michael@0 35 // is called a base::Closure. Note that this is NOT the same as what other
michael@0 36 // languages refer to as a closure -- it does not retain a reference to its
michael@0 37 // enclosing environment.
michael@0 38 //
michael@0 39 // MEMORY MANAGEMENT AND PASSING
michael@0 40 //
michael@0 41 // The Callback objects themselves should be passed by const-reference, and
michael@0 42 // stored by copy. They internally store their state via a refcounted class
michael@0 43 // and thus do not need to be deleted.
michael@0 44 //
michael@0 45 // The reason to pass via a const-reference is to avoid unnecessary
michael@0 46 // AddRef/Release pairs to the internal state.
michael@0 47 //
michael@0 48 //
michael@0 49 // -----------------------------------------------------------------------------
michael@0 50 // Quick reference for basic stuff
michael@0 51 // -----------------------------------------------------------------------------
michael@0 52 //
michael@0 53 // BINDING A BARE FUNCTION
michael@0 54 //
michael@0 55 // int Return5() { return 5; }
michael@0 56 // base::Callback<int(void)> func_cb = base::Bind(&Return5);
michael@0 57 // LOG(INFO) << func_cb.Run(); // Prints 5.
michael@0 58 //
michael@0 59 // BINDING A CLASS METHOD
michael@0 60 //
michael@0 61 // The first argument to bind is the member function to call, the second is
michael@0 62 // the object on which to call it.
michael@0 63 //
michael@0 64 // class Ref : public base::RefCountedThreadSafe<Ref> {
michael@0 65 // public:
michael@0 66 // int Foo() { return 3; }
michael@0 67 // void PrintBye() { LOG(INFO) << "bye."; }
michael@0 68 // };
michael@0 69 // scoped_refptr<Ref> ref = new Ref();
michael@0 70 // base::Callback<void(void)> ref_cb = base::Bind(&Ref::Foo, ref);
michael@0 71 // LOG(INFO) << ref_cb.Run(); // Prints out 3.
michael@0 72 //
michael@0 73 // By default the object must support RefCounted or you will get a compiler
michael@0 74 // error. If you're passing between threads, be sure it's
michael@0 75 // RefCountedThreadSafe! See "Advanced binding of member functions" below if
michael@0 76 // you don't want to use reference counting.
michael@0 77 //
michael@0 78 // RUNNING A CALLBACK
michael@0 79 //
michael@0 80 // Callbacks can be run with their "Run" method, which has the same
michael@0 81 // signature as the template argument to the callback.
michael@0 82 //
michael@0 83 // void DoSomething(const base::Callback<void(int, std::string)>& callback) {
michael@0 84 // callback.Run(5, "hello");
michael@0 85 // }
michael@0 86 //
michael@0 87 // Callbacks can be run more than once (they don't get deleted or marked when
michael@0 88 // run). However, this precludes using base::Passed (see below).
michael@0 89 //
michael@0 90 // void DoSomething(const base::Callback<double(double)>& callback) {
michael@0 91 // double myresult = callback.Run(3.14159);
michael@0 92 // myresult += callback.Run(2.71828);
michael@0 93 // }
michael@0 94 //
michael@0 95 // PASSING UNBOUND INPUT PARAMETERS
michael@0 96 //
michael@0 97 // Unbound parameters are specified at the time a callback is Run(). They are
michael@0 98 // specified in the Callback template type:
michael@0 99 //
michael@0 100 // void MyFunc(int i, const std::string& str) {}
michael@0 101 // base::Callback<void(int, const std::string&)> cb = base::Bind(&MyFunc);
michael@0 102 // cb.Run(23, "hello, world");
michael@0 103 //
michael@0 104 // PASSING BOUND INPUT PARAMETERS
michael@0 105 //
michael@0 106 // Bound parameters are specified when you create thee callback as arguments
michael@0 107 // to Bind(). They will be passed to the function and the Run()ner of the
michael@0 108 // callback doesn't see those values or even know that the function it's
michael@0 109 // calling.
michael@0 110 //
michael@0 111 // void MyFunc(int i, const std::string& str) {}
michael@0 112 // base::Callback<void(void)> cb = base::Bind(&MyFunc, 23, "hello world");
michael@0 113 // cb.Run();
michael@0 114 //
michael@0 115 // A callback with no unbound input parameters (base::Callback<void(void)>)
michael@0 116 // is called a base::Closure. So we could have also written:
michael@0 117 //
michael@0 118 // base::Closure cb = base::Bind(&MyFunc, 23, "hello world");
michael@0 119 //
michael@0 120 // When calling member functions, bound parameters just go after the object
michael@0 121 // pointer.
michael@0 122 //
michael@0 123 // base::Closure cb = base::Bind(&MyClass::MyFunc, this, 23, "hello world");
michael@0 124 //
michael@0 125 // PARTIAL BINDING OF PARAMETERS
michael@0 126 //
michael@0 127 // You can specify some parameters when you create the callback, and specify
michael@0 128 // the rest when you execute the callback.
michael@0 129 //
michael@0 130 // void MyFunc(int i, const std::string& str) {}
michael@0 131 // base::Callback<void(const std::string&)> cb = base::Bind(&MyFunc, 23);
michael@0 132 // cb.Run("hello world");
michael@0 133 //
michael@0 134 // When calling a function bound parameters are first, followed by unbound
michael@0 135 // parameters.
michael@0 136 //
michael@0 137 //
michael@0 138 // -----------------------------------------------------------------------------
michael@0 139 // Quick reference for advanced binding
michael@0 140 // -----------------------------------------------------------------------------
michael@0 141 //
michael@0 142 // BINDING A CLASS METHOD WITH WEAK POINTERS
michael@0 143 //
michael@0 144 // base::Bind(&MyClass::Foo, GetWeakPtr());
michael@0 145 //
michael@0 146 // The callback will not be issued if the object is destroyed at the time
michael@0 147 // it's issued. DANGER: weak pointers are not threadsafe, so don't use this
michael@0 148 // when passing between threads!
michael@0 149 //
michael@0 150 // BINDING A CLASS METHOD WITH MANUAL LIFETIME MANAGEMENT
michael@0 151 //
michael@0 152 // base::Bind(&MyClass::Foo, base::Unretained(this));
michael@0 153 //
michael@0 154 // This disables all lifetime management on the object. You're responsible
michael@0 155 // for making sure the object is alive at the time of the call. You break it,
michael@0 156 // you own it!
michael@0 157 //
michael@0 158 // BINDING A CLASS METHOD AND HAVING THE CALLBACK OWN THE CLASS
michael@0 159 //
michael@0 160 // MyClass* myclass = new MyClass;
michael@0 161 // base::Bind(&MyClass::Foo, base::Owned(myclass));
michael@0 162 //
michael@0 163 // The object will be deleted when the callback is destroyed, even if it's
michael@0 164 // not run (like if you post a task during shutdown). Potentially useful for
michael@0 165 // "fire and forget" cases.
michael@0 166 //
michael@0 167 // IGNORING RETURN VALUES
michael@0 168 //
michael@0 169 // Sometimes you want to call a function that returns a value in a callback
michael@0 170 // that doesn't expect a return value.
michael@0 171 //
michael@0 172 // int DoSomething(int arg) { cout << arg << endl; }
michael@0 173 // base::Callback<void<int>) cb =
michael@0 174 // base::Bind(base::IgnoreResult(&DoSomething));
michael@0 175 //
michael@0 176 //
michael@0 177 // -----------------------------------------------------------------------------
michael@0 178 // Quick reference for binding parameters to Bind()
michael@0 179 // -----------------------------------------------------------------------------
michael@0 180 //
michael@0 181 // Bound parameters are specified as arguments to Bind() and are passed to the
michael@0 182 // function. A callback with no parameters or no unbound parameters is called a
michael@0 183 // Closure (base::Callback<void(void)> and base::Closure are the same thing).
michael@0 184 //
michael@0 185 // PASSING PARAMETERS OWNED BY THE CALLBACK
michael@0 186 //
michael@0 187 // void Foo(int* arg) { cout << *arg << endl; }
michael@0 188 // int* pn = new int(1);
michael@0 189 // base::Closure foo_callback = base::Bind(&foo, base::Owned(pn));
michael@0 190 //
michael@0 191 // The parameter will be deleted when the callback is destroyed, even if it's
michael@0 192 // not run (like if you post a task during shutdown).
michael@0 193 //
michael@0 194 // PASSING PARAMETERS AS A scoped_ptr
michael@0 195 //
michael@0 196 // void TakesOwnership(scoped_ptr<Foo> arg) {}
michael@0 197 // scoped_ptr<Foo> f(new Foo);
michael@0 198 // // f becomes null during the following call.
michael@0 199 // base::Closure cb = base::Bind(&TakesOwnership, base::Passed(&f));
michael@0 200 //
michael@0 201 // Ownership of the parameter will be with the callback until the it is run,
michael@0 202 // when ownership is passed to the callback function. This means the callback
michael@0 203 // can only be run once. If the callback is never run, it will delete the
michael@0 204 // object when it's destroyed.
michael@0 205 //
michael@0 206 // PASSING PARAMETERS AS A scoped_refptr
michael@0 207 //
michael@0 208 // void TakesOneRef(scoped_refptr<Foo> arg) {}
michael@0 209 // scoped_refptr<Foo> f(new Foo)
michael@0 210 // base::Closure cb = base::Bind(&TakesOneRef, f);
michael@0 211 //
michael@0 212 // This should "just work." The closure will take a reference as long as it
michael@0 213 // is alive, and another reference will be taken for the called function.
michael@0 214 //
michael@0 215 // PASSING PARAMETERS BY REFERENCE
michael@0 216 //
michael@0 217 // void foo(int arg) { cout << arg << endl }
michael@0 218 // int n = 1;
michael@0 219 // base::Closure has_ref = base::Bind(&foo, base::ConstRef(n));
michael@0 220 // n = 2;
michael@0 221 // has_ref.Run(); // Prints "2"
michael@0 222 //
michael@0 223 // Normally parameters are copied in the closure. DANGER: ConstRef stores a
michael@0 224 // const reference instead, referencing the original parameter. This means
michael@0 225 // that you must ensure the object outlives the callback!
michael@0 226 //
michael@0 227 //
michael@0 228 // -----------------------------------------------------------------------------
michael@0 229 // Implementation notes
michael@0 230 // -----------------------------------------------------------------------------
michael@0 231 //
michael@0 232 // WHERE IS THIS DESIGN FROM:
michael@0 233 //
michael@0 234 // The design Callback and Bind is heavily influenced by C++'s
michael@0 235 // tr1::function/tr1::bind, and by the "Google Callback" system used inside
michael@0 236 // Google.
michael@0 237 //
michael@0 238 //
michael@0 239 // HOW THE IMPLEMENTATION WORKS:
michael@0 240 //
michael@0 241 // There are three main components to the system:
michael@0 242 // 1) The Callback classes.
michael@0 243 // 2) The Bind() functions.
michael@0 244 // 3) The arguments wrappers (e.g., Unretained() and ConstRef()).
michael@0 245 //
michael@0 246 // The Callback classes represent a generic function pointer. Internally,
michael@0 247 // it stores a refcounted piece of state that represents the target function
michael@0 248 // and all its bound parameters. Each Callback specialization has a templated
michael@0 249 // constructor that takes an BindState<>*. In the context of the constructor,
michael@0 250 // the static type of this BindState<> pointer uniquely identifies the
michael@0 251 // function it is representing, all its bound parameters, and a Run() method
michael@0 252 // that is capable of invoking the target.
michael@0 253 //
michael@0 254 // Callback's constructor takes the BindState<>* that has the full static type
michael@0 255 // and erases the target function type as well as the types of the bound
michael@0 256 // parameters. It does this by storing a pointer to the specific Run()
michael@0 257 // function, and upcasting the state of BindState<>* to a
michael@0 258 // BindStateBase*. This is safe as long as this BindStateBase pointer
michael@0 259 // is only used with the stored Run() pointer.
michael@0 260 //
michael@0 261 // To BindState<> objects are created inside the Bind() functions.
michael@0 262 // These functions, along with a set of internal templates, are responsible for
michael@0 263 //
michael@0 264 // - Unwrapping the function signature into return type, and parameters
michael@0 265 // - Determining the number of parameters that are bound
michael@0 266 // - Creating the BindState storing the bound parameters
michael@0 267 // - Performing compile-time asserts to avoid error-prone behavior
michael@0 268 // - Returning an Callback<> with an arity matching the number of unbound
michael@0 269 // parameters and that knows the correct refcounting semantics for the
michael@0 270 // target object if we are binding a method.
michael@0 271 //
michael@0 272 // The Bind functions do the above using type-inference, and template
michael@0 273 // specializations.
michael@0 274 //
michael@0 275 // By default Bind() will store copies of all bound parameters, and attempt
michael@0 276 // to refcount a target object if the function being bound is a class method.
michael@0 277 // These copies are created even if the function takes parameters as const
michael@0 278 // references. (Binding to non-const references is forbidden, see bind.h.)
michael@0 279 //
michael@0 280 // To change this behavior, we introduce a set of argument wrappers
michael@0 281 // (e.g., Unretained(), and ConstRef()). These are simple container templates
michael@0 282 // that are passed by value, and wrap a pointer to argument. See the
michael@0 283 // file-level comment in base/bind_helpers.h for more info.
michael@0 284 //
michael@0 285 // These types are passed to the Unwrap() functions, and the MaybeRefcount()
michael@0 286 // functions respectively to modify the behavior of Bind(). The Unwrap()
michael@0 287 // and MaybeRefcount() functions change behavior by doing partial
michael@0 288 // specialization based on whether or not a parameter is a wrapper type.
michael@0 289 //
michael@0 290 // ConstRef() is similar to tr1::cref. Unretained() is specific to Chromium.
michael@0 291 //
michael@0 292 //
michael@0 293 // WHY NOT TR1 FUNCTION/BIND?
michael@0 294 //
michael@0 295 // Direct use of tr1::function and tr1::bind was considered, but ultimately
michael@0 296 // rejected because of the number of copy constructors invocations involved
michael@0 297 // in the binding of arguments during construction, and the forwarding of
michael@0 298 // arguments during invocation. These copies will no longer be an issue in
michael@0 299 // C++0x because C++0x will support rvalue reference allowing for the compiler
michael@0 300 // to avoid these copies. However, waiting for C++0x is not an option.
michael@0 301 //
michael@0 302 // Measured with valgrind on gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5), the
michael@0 303 // tr1::bind call itself will invoke a non-trivial copy constructor three times
michael@0 304 // for each bound parameter. Also, each when passing a tr1::function, each
michael@0 305 // bound argument will be copied again.
michael@0 306 //
michael@0 307 // In addition to the copies taken at binding and invocation, copying a
michael@0 308 // tr1::function causes a copy to be made of all the bound parameters and
michael@0 309 // state.
michael@0 310 //
michael@0 311 // Furthermore, in Chromium, it is desirable for the Callback to take a
michael@0 312 // reference on a target object when representing a class method call. This
michael@0 313 // is not supported by tr1.
michael@0 314 //
michael@0 315 // Lastly, tr1::function and tr1::bind has a more general and flexible API.
michael@0 316 // This includes things like argument reordering by use of
michael@0 317 // tr1::bind::placeholder, support for non-const reference parameters, and some
michael@0 318 // limited amount of subtyping of the tr1::function object (e.g.,
michael@0 319 // tr1::function<int(int)> is convertible to tr1::function<void(int)>).
michael@0 320 //
michael@0 321 // These are not features that are required in Chromium. Some of them, such as
michael@0 322 // allowing for reference parameters, and subtyping of functions, may actually
michael@0 323 // become a source of errors. Removing support for these features actually
michael@0 324 // allows for a simpler implementation, and a terser Currying API.
michael@0 325 //
michael@0 326 //
michael@0 327 // WHY NOT GOOGLE CALLBACKS?
michael@0 328 //
michael@0 329 // The Google callback system also does not support refcounting. Furthermore,
michael@0 330 // its implementation has a number of strange edge cases with respect to type
michael@0 331 // conversion of its arguments. In particular, the argument's constness must
michael@0 332 // at times match exactly the function signature, or the type-inference might
michael@0 333 // break. Given the above, writing a custom solution was easier.
michael@0 334 //
michael@0 335 //
michael@0 336 // MISSING FUNCTIONALITY
michael@0 337 // - Invoking the return of Bind. Bind(&foo).Run() does not work;
michael@0 338 // - Binding arrays to functions that take a non-const pointer.
michael@0 339 // Example:
michael@0 340 // void Foo(const char* ptr);
michael@0 341 // void Bar(char* ptr);
michael@0 342 // Bind(&Foo, "test");
michael@0 343 // Bind(&Bar, "test"); // This fails because ptr is not const.
michael@0 344
michael@0 345 namespace base {
michael@0 346
michael@0 347 // First, we forward declare the Callback class template. This informs the
michael@0 348 // compiler that the template only has 1 type parameter which is the function
michael@0 349 // signature that the Callback is representing.
michael@0 350 //
michael@0 351 // After this, create template specializations for 0-7 parameters. Note that
michael@0 352 // even though the template typelist grows, the specialization still
michael@0 353 // only has one type: the function signature.
michael@0 354 //
michael@0 355 // If you are thinking of forward declaring Callback in your own header file,
michael@0 356 // please include "base/callback_forward.h" instead.
michael@0 357 template <typename Sig>
michael@0 358 class Callback;
michael@0 359
michael@0 360 namespace internal {
michael@0 361 template <typename Runnable, typename RunType, typename BoundArgsType>
michael@0 362 struct BindState;
michael@0 363 } // namespace internal
michael@0 364
michael@0 365 template <typename R>
michael@0 366 class Callback<R(void)> : public internal::CallbackBase {
michael@0 367 public:
michael@0 368 typedef R(RunType)();
michael@0 369
michael@0 370 Callback() : CallbackBase(NULL) { }
michael@0 371
michael@0 372 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
michael@0 373 // return the exact Callback<> type. See base/bind.h for details.
michael@0 374 template <typename Runnable, typename BindRunType, typename BoundArgsType>
michael@0 375 Callback(internal::BindState<Runnable, BindRunType,
michael@0 376 BoundArgsType>* bind_state)
michael@0 377 : CallbackBase(bind_state) {
michael@0 378
michael@0 379 // Force the assignment to a local variable of PolymorphicInvoke
michael@0 380 // so the compiler will typecheck that the passed in Run() method has
michael@0 381 // the correct type.
michael@0 382 PolymorphicInvoke invoke_func =
michael@0 383 &internal::BindState<Runnable, BindRunType, BoundArgsType>
michael@0 384 ::InvokerType::Run;
michael@0 385 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
michael@0 386 }
michael@0 387
michael@0 388 bool Equals(const Callback& other) const {
michael@0 389 return CallbackBase::Equals(other);
michael@0 390 }
michael@0 391
michael@0 392 R Run() const {
michael@0 393 PolymorphicInvoke f =
michael@0 394 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
michael@0 395
michael@0 396 return f(bind_state_.get());
michael@0 397 }
michael@0 398
michael@0 399 private:
michael@0 400 typedef R(*PolymorphicInvoke)(
michael@0 401 internal::BindStateBase*);
michael@0 402
michael@0 403 };
michael@0 404
michael@0 405 template <typename R, typename A1>
michael@0 406 class Callback<R(A1)> : public internal::CallbackBase {
michael@0 407 public:
michael@0 408 typedef R(RunType)(A1);
michael@0 409
michael@0 410 Callback() : CallbackBase(NULL) { }
michael@0 411
michael@0 412 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
michael@0 413 // return the exact Callback<> type. See base/bind.h for details.
michael@0 414 template <typename Runnable, typename BindRunType, typename BoundArgsType>
michael@0 415 Callback(internal::BindState<Runnable, BindRunType,
michael@0 416 BoundArgsType>* bind_state)
michael@0 417 : CallbackBase(bind_state) {
michael@0 418
michael@0 419 // Force the assignment to a local variable of PolymorphicInvoke
michael@0 420 // so the compiler will typecheck that the passed in Run() method has
michael@0 421 // the correct type.
michael@0 422 PolymorphicInvoke invoke_func =
michael@0 423 &internal::BindState<Runnable, BindRunType, BoundArgsType>
michael@0 424 ::InvokerType::Run;
michael@0 425 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
michael@0 426 }
michael@0 427
michael@0 428 bool Equals(const Callback& other) const {
michael@0 429 return CallbackBase::Equals(other);
michael@0 430 }
michael@0 431
michael@0 432 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1) const {
michael@0 433 PolymorphicInvoke f =
michael@0 434 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
michael@0 435
michael@0 436 return f(bind_state_.get(), internal::CallbackForward(a1));
michael@0 437 }
michael@0 438
michael@0 439 private:
michael@0 440 typedef R(*PolymorphicInvoke)(
michael@0 441 internal::BindStateBase*,
michael@0 442 typename internal::CallbackParamTraits<A1>::ForwardType);
michael@0 443
michael@0 444 };
michael@0 445
michael@0 446 template <typename R, typename A1, typename A2>
michael@0 447 class Callback<R(A1, A2)> : public internal::CallbackBase {
michael@0 448 public:
michael@0 449 typedef R(RunType)(A1, A2);
michael@0 450
michael@0 451 Callback() : CallbackBase(NULL) { }
michael@0 452
michael@0 453 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
michael@0 454 // return the exact Callback<> type. See base/bind.h for details.
michael@0 455 template <typename Runnable, typename BindRunType, typename BoundArgsType>
michael@0 456 Callback(internal::BindState<Runnable, BindRunType,
michael@0 457 BoundArgsType>* bind_state)
michael@0 458 : CallbackBase(bind_state) {
michael@0 459
michael@0 460 // Force the assignment to a local variable of PolymorphicInvoke
michael@0 461 // so the compiler will typecheck that the passed in Run() method has
michael@0 462 // the correct type.
michael@0 463 PolymorphicInvoke invoke_func =
michael@0 464 &internal::BindState<Runnable, BindRunType, BoundArgsType>
michael@0 465 ::InvokerType::Run;
michael@0 466 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
michael@0 467 }
michael@0 468
michael@0 469 bool Equals(const Callback& other) const {
michael@0 470 return CallbackBase::Equals(other);
michael@0 471 }
michael@0 472
michael@0 473 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
michael@0 474 typename internal::CallbackParamTraits<A2>::ForwardType a2) const {
michael@0 475 PolymorphicInvoke f =
michael@0 476 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
michael@0 477
michael@0 478 return f(bind_state_.get(), internal::CallbackForward(a1),
michael@0 479 internal::CallbackForward(a2));
michael@0 480 }
michael@0 481
michael@0 482 private:
michael@0 483 typedef R(*PolymorphicInvoke)(
michael@0 484 internal::BindStateBase*,
michael@0 485 typename internal::CallbackParamTraits<A1>::ForwardType,
michael@0 486 typename internal::CallbackParamTraits<A2>::ForwardType);
michael@0 487
michael@0 488 };
michael@0 489
michael@0 490 template <typename R, typename A1, typename A2, typename A3>
michael@0 491 class Callback<R(A1, A2, A3)> : public internal::CallbackBase {
michael@0 492 public:
michael@0 493 typedef R(RunType)(A1, A2, A3);
michael@0 494
michael@0 495 Callback() : CallbackBase(NULL) { }
michael@0 496
michael@0 497 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
michael@0 498 // return the exact Callback<> type. See base/bind.h for details.
michael@0 499 template <typename Runnable, typename BindRunType, typename BoundArgsType>
michael@0 500 Callback(internal::BindState<Runnable, BindRunType,
michael@0 501 BoundArgsType>* bind_state)
michael@0 502 : CallbackBase(bind_state) {
michael@0 503
michael@0 504 // Force the assignment to a local variable of PolymorphicInvoke
michael@0 505 // so the compiler will typecheck that the passed in Run() method has
michael@0 506 // the correct type.
michael@0 507 PolymorphicInvoke invoke_func =
michael@0 508 &internal::BindState<Runnable, BindRunType, BoundArgsType>
michael@0 509 ::InvokerType::Run;
michael@0 510 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
michael@0 511 }
michael@0 512
michael@0 513 bool Equals(const Callback& other) const {
michael@0 514 return CallbackBase::Equals(other);
michael@0 515 }
michael@0 516
michael@0 517 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
michael@0 518 typename internal::CallbackParamTraits<A2>::ForwardType a2,
michael@0 519 typename internal::CallbackParamTraits<A3>::ForwardType a3) const {
michael@0 520 PolymorphicInvoke f =
michael@0 521 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
michael@0 522
michael@0 523 return f(bind_state_.get(), internal::CallbackForward(a1),
michael@0 524 internal::CallbackForward(a2),
michael@0 525 internal::CallbackForward(a3));
michael@0 526 }
michael@0 527
michael@0 528 private:
michael@0 529 typedef R(*PolymorphicInvoke)(
michael@0 530 internal::BindStateBase*,
michael@0 531 typename internal::CallbackParamTraits<A1>::ForwardType,
michael@0 532 typename internal::CallbackParamTraits<A2>::ForwardType,
michael@0 533 typename internal::CallbackParamTraits<A3>::ForwardType);
michael@0 534
michael@0 535 };
michael@0 536
michael@0 537 template <typename R, typename A1, typename A2, typename A3, typename A4>
michael@0 538 class Callback<R(A1, A2, A3, A4)> : public internal::CallbackBase {
michael@0 539 public:
michael@0 540 typedef R(RunType)(A1, A2, A3, A4);
michael@0 541
michael@0 542 Callback() : CallbackBase(NULL) { }
michael@0 543
michael@0 544 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
michael@0 545 // return the exact Callback<> type. See base/bind.h for details.
michael@0 546 template <typename Runnable, typename BindRunType, typename BoundArgsType>
michael@0 547 Callback(internal::BindState<Runnable, BindRunType,
michael@0 548 BoundArgsType>* bind_state)
michael@0 549 : CallbackBase(bind_state) {
michael@0 550
michael@0 551 // Force the assignment to a local variable of PolymorphicInvoke
michael@0 552 // so the compiler will typecheck that the passed in Run() method has
michael@0 553 // the correct type.
michael@0 554 PolymorphicInvoke invoke_func =
michael@0 555 &internal::BindState<Runnable, BindRunType, BoundArgsType>
michael@0 556 ::InvokerType::Run;
michael@0 557 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
michael@0 558 }
michael@0 559
michael@0 560 bool Equals(const Callback& other) const {
michael@0 561 return CallbackBase::Equals(other);
michael@0 562 }
michael@0 563
michael@0 564 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
michael@0 565 typename internal::CallbackParamTraits<A2>::ForwardType a2,
michael@0 566 typename internal::CallbackParamTraits<A3>::ForwardType a3,
michael@0 567 typename internal::CallbackParamTraits<A4>::ForwardType a4) const {
michael@0 568 PolymorphicInvoke f =
michael@0 569 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
michael@0 570
michael@0 571 return f(bind_state_.get(), internal::CallbackForward(a1),
michael@0 572 internal::CallbackForward(a2),
michael@0 573 internal::CallbackForward(a3),
michael@0 574 internal::CallbackForward(a4));
michael@0 575 }
michael@0 576
michael@0 577 private:
michael@0 578 typedef R(*PolymorphicInvoke)(
michael@0 579 internal::BindStateBase*,
michael@0 580 typename internal::CallbackParamTraits<A1>::ForwardType,
michael@0 581 typename internal::CallbackParamTraits<A2>::ForwardType,
michael@0 582 typename internal::CallbackParamTraits<A3>::ForwardType,
michael@0 583 typename internal::CallbackParamTraits<A4>::ForwardType);
michael@0 584
michael@0 585 };
michael@0 586
michael@0 587 template <typename R, typename A1, typename A2, typename A3, typename A4,
michael@0 588 typename A5>
michael@0 589 class Callback<R(A1, A2, A3, A4, A5)> : public internal::CallbackBase {
michael@0 590 public:
michael@0 591 typedef R(RunType)(A1, A2, A3, A4, A5);
michael@0 592
michael@0 593 Callback() : CallbackBase(NULL) { }
michael@0 594
michael@0 595 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
michael@0 596 // return the exact Callback<> type. See base/bind.h for details.
michael@0 597 template <typename Runnable, typename BindRunType, typename BoundArgsType>
michael@0 598 Callback(internal::BindState<Runnable, BindRunType,
michael@0 599 BoundArgsType>* bind_state)
michael@0 600 : CallbackBase(bind_state) {
michael@0 601
michael@0 602 // Force the assignment to a local variable of PolymorphicInvoke
michael@0 603 // so the compiler will typecheck that the passed in Run() method has
michael@0 604 // the correct type.
michael@0 605 PolymorphicInvoke invoke_func =
michael@0 606 &internal::BindState<Runnable, BindRunType, BoundArgsType>
michael@0 607 ::InvokerType::Run;
michael@0 608 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
michael@0 609 }
michael@0 610
michael@0 611 bool Equals(const Callback& other) const {
michael@0 612 return CallbackBase::Equals(other);
michael@0 613 }
michael@0 614
michael@0 615 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
michael@0 616 typename internal::CallbackParamTraits<A2>::ForwardType a2,
michael@0 617 typename internal::CallbackParamTraits<A3>::ForwardType a3,
michael@0 618 typename internal::CallbackParamTraits<A4>::ForwardType a4,
michael@0 619 typename internal::CallbackParamTraits<A5>::ForwardType a5) const {
michael@0 620 PolymorphicInvoke f =
michael@0 621 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
michael@0 622
michael@0 623 return f(bind_state_.get(), internal::CallbackForward(a1),
michael@0 624 internal::CallbackForward(a2),
michael@0 625 internal::CallbackForward(a3),
michael@0 626 internal::CallbackForward(a4),
michael@0 627 internal::CallbackForward(a5));
michael@0 628 }
michael@0 629
michael@0 630 private:
michael@0 631 typedef R(*PolymorphicInvoke)(
michael@0 632 internal::BindStateBase*,
michael@0 633 typename internal::CallbackParamTraits<A1>::ForwardType,
michael@0 634 typename internal::CallbackParamTraits<A2>::ForwardType,
michael@0 635 typename internal::CallbackParamTraits<A3>::ForwardType,
michael@0 636 typename internal::CallbackParamTraits<A4>::ForwardType,
michael@0 637 typename internal::CallbackParamTraits<A5>::ForwardType);
michael@0 638
michael@0 639 };
michael@0 640
michael@0 641 template <typename R, typename A1, typename A2, typename A3, typename A4,
michael@0 642 typename A5, typename A6>
michael@0 643 class Callback<R(A1, A2, A3, A4, A5, A6)> : public internal::CallbackBase {
michael@0 644 public:
michael@0 645 typedef R(RunType)(A1, A2, A3, A4, A5, A6);
michael@0 646
michael@0 647 Callback() : CallbackBase(NULL) { }
michael@0 648
michael@0 649 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
michael@0 650 // return the exact Callback<> type. See base/bind.h for details.
michael@0 651 template <typename Runnable, typename BindRunType, typename BoundArgsType>
michael@0 652 Callback(internal::BindState<Runnable, BindRunType,
michael@0 653 BoundArgsType>* bind_state)
michael@0 654 : CallbackBase(bind_state) {
michael@0 655
michael@0 656 // Force the assignment to a local variable of PolymorphicInvoke
michael@0 657 // so the compiler will typecheck that the passed in Run() method has
michael@0 658 // the correct type.
michael@0 659 PolymorphicInvoke invoke_func =
michael@0 660 &internal::BindState<Runnable, BindRunType, BoundArgsType>
michael@0 661 ::InvokerType::Run;
michael@0 662 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
michael@0 663 }
michael@0 664
michael@0 665 bool Equals(const Callback& other) const {
michael@0 666 return CallbackBase::Equals(other);
michael@0 667 }
michael@0 668
michael@0 669 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
michael@0 670 typename internal::CallbackParamTraits<A2>::ForwardType a2,
michael@0 671 typename internal::CallbackParamTraits<A3>::ForwardType a3,
michael@0 672 typename internal::CallbackParamTraits<A4>::ForwardType a4,
michael@0 673 typename internal::CallbackParamTraits<A5>::ForwardType a5,
michael@0 674 typename internal::CallbackParamTraits<A6>::ForwardType a6) const {
michael@0 675 PolymorphicInvoke f =
michael@0 676 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
michael@0 677
michael@0 678 return f(bind_state_.get(), internal::CallbackForward(a1),
michael@0 679 internal::CallbackForward(a2),
michael@0 680 internal::CallbackForward(a3),
michael@0 681 internal::CallbackForward(a4),
michael@0 682 internal::CallbackForward(a5),
michael@0 683 internal::CallbackForward(a6));
michael@0 684 }
michael@0 685
michael@0 686 private:
michael@0 687 typedef R(*PolymorphicInvoke)(
michael@0 688 internal::BindStateBase*,
michael@0 689 typename internal::CallbackParamTraits<A1>::ForwardType,
michael@0 690 typename internal::CallbackParamTraits<A2>::ForwardType,
michael@0 691 typename internal::CallbackParamTraits<A3>::ForwardType,
michael@0 692 typename internal::CallbackParamTraits<A4>::ForwardType,
michael@0 693 typename internal::CallbackParamTraits<A5>::ForwardType,
michael@0 694 typename internal::CallbackParamTraits<A6>::ForwardType);
michael@0 695
michael@0 696 };
michael@0 697
michael@0 698 template <typename R, typename A1, typename A2, typename A3, typename A4,
michael@0 699 typename A5, typename A6, typename A7>
michael@0 700 class Callback<R(A1, A2, A3, A4, A5, A6, A7)> : public internal::CallbackBase {
michael@0 701 public:
michael@0 702 typedef R(RunType)(A1, A2, A3, A4, A5, A6, A7);
michael@0 703
michael@0 704 Callback() : CallbackBase(NULL) { }
michael@0 705
michael@0 706 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
michael@0 707 // return the exact Callback<> type. See base/bind.h for details.
michael@0 708 template <typename Runnable, typename BindRunType, typename BoundArgsType>
michael@0 709 Callback(internal::BindState<Runnable, BindRunType,
michael@0 710 BoundArgsType>* bind_state)
michael@0 711 : CallbackBase(bind_state) {
michael@0 712
michael@0 713 // Force the assignment to a local variable of PolymorphicInvoke
michael@0 714 // so the compiler will typecheck that the passed in Run() method has
michael@0 715 // the correct type.
michael@0 716 PolymorphicInvoke invoke_func =
michael@0 717 &internal::BindState<Runnable, BindRunType, BoundArgsType>
michael@0 718 ::InvokerType::Run;
michael@0 719 polymorphic_invoke_ = reinterpret_cast<InvokeFuncStorage>(invoke_func);
michael@0 720 }
michael@0 721
michael@0 722 bool Equals(const Callback& other) const {
michael@0 723 return CallbackBase::Equals(other);
michael@0 724 }
michael@0 725
michael@0 726 R Run(typename internal::CallbackParamTraits<A1>::ForwardType a1,
michael@0 727 typename internal::CallbackParamTraits<A2>::ForwardType a2,
michael@0 728 typename internal::CallbackParamTraits<A3>::ForwardType a3,
michael@0 729 typename internal::CallbackParamTraits<A4>::ForwardType a4,
michael@0 730 typename internal::CallbackParamTraits<A5>::ForwardType a5,
michael@0 731 typename internal::CallbackParamTraits<A6>::ForwardType a6,
michael@0 732 typename internal::CallbackParamTraits<A7>::ForwardType a7) const {
michael@0 733 PolymorphicInvoke f =
michael@0 734 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
michael@0 735
michael@0 736 return f(bind_state_.get(), internal::CallbackForward(a1),
michael@0 737 internal::CallbackForward(a2),
michael@0 738 internal::CallbackForward(a3),
michael@0 739 internal::CallbackForward(a4),
michael@0 740 internal::CallbackForward(a5),
michael@0 741 internal::CallbackForward(a6),
michael@0 742 internal::CallbackForward(a7));
michael@0 743 }
michael@0 744
michael@0 745 private:
michael@0 746 typedef R(*PolymorphicInvoke)(
michael@0 747 internal::BindStateBase*,
michael@0 748 typename internal::CallbackParamTraits<A1>::ForwardType,
michael@0 749 typename internal::CallbackParamTraits<A2>::ForwardType,
michael@0 750 typename internal::CallbackParamTraits<A3>::ForwardType,
michael@0 751 typename internal::CallbackParamTraits<A4>::ForwardType,
michael@0 752 typename internal::CallbackParamTraits<A5>::ForwardType,
michael@0 753 typename internal::CallbackParamTraits<A6>::ForwardType,
michael@0 754 typename internal::CallbackParamTraits<A7>::ForwardType);
michael@0 755
michael@0 756 };
michael@0 757
michael@0 758
michael@0 759 // Syntactic sugar to make Callbacks<void(void)> easier to declare since it
michael@0 760 // will be used in a lot of APIs with delayed execution.
michael@0 761 typedef Callback<void(void)> Closure;
michael@0 762
michael@0 763 } // namespace base
michael@0 764
michael@0 765 #endif // BASE_CALLBACK_H

mercurial