michael@0: // This file was GENERATED by command: michael@0: // pump.py callback.h.pump michael@0: // DO NOT EDIT BY HAND!!! michael@0: michael@0: michael@0: // Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_CALLBACK_H_ michael@0: #define BASE_CALLBACK_H_ michael@0: michael@0: #include "base/callback_forward.h" michael@0: #include "base/callback_internal.h" michael@0: #include "base/template_util.h" michael@0: michael@0: // NOTE: Header files that do not require the full definition of Callback or michael@0: // Closure should #include "base/callback_forward.h" instead of this file. michael@0: michael@0: // ----------------------------------------------------------------------------- michael@0: // Introduction michael@0: // ----------------------------------------------------------------------------- michael@0: // michael@0: // The templated Callback class is a generalized function object. Together michael@0: // with the Bind() function in bind.h, they provide a type-safe method for michael@0: // performing partial application of functions. michael@0: // michael@0: // Partial application (or "currying") is the process of binding a subset of michael@0: // a function's arguments to produce another function that takes fewer michael@0: // arguments. This can be used to pass around a unit of delayed execution, michael@0: // much like lexical closures are used in other languages. For example, it michael@0: // is used in Chromium code to schedule tasks on different MessageLoops. michael@0: // michael@0: // A callback with no unbound input parameters (base::Callback) michael@0: // is called a base::Closure. Note that this is NOT the same as what other michael@0: // languages refer to as a closure -- it does not retain a reference to its michael@0: // enclosing environment. michael@0: // michael@0: // MEMORY MANAGEMENT AND PASSING michael@0: // michael@0: // The Callback objects themselves should be passed by const-reference, and michael@0: // stored by copy. They internally store their state via a refcounted class michael@0: // and thus do not need to be deleted. michael@0: // michael@0: // The reason to pass via a const-reference is to avoid unnecessary michael@0: // AddRef/Release pairs to the internal state. michael@0: // michael@0: // michael@0: // ----------------------------------------------------------------------------- michael@0: // Quick reference for basic stuff michael@0: // ----------------------------------------------------------------------------- michael@0: // michael@0: // BINDING A BARE FUNCTION michael@0: // michael@0: // int Return5() { return 5; } michael@0: // base::Callback func_cb = base::Bind(&Return5); michael@0: // LOG(INFO) << func_cb.Run(); // Prints 5. michael@0: // michael@0: // BINDING A CLASS METHOD michael@0: // michael@0: // The first argument to bind is the member function to call, the second is michael@0: // the object on which to call it. michael@0: // michael@0: // class Ref : public base::RefCountedThreadSafe { michael@0: // public: michael@0: // int Foo() { return 3; } michael@0: // void PrintBye() { LOG(INFO) << "bye."; } michael@0: // }; michael@0: // scoped_refptr ref = new Ref(); michael@0: // base::Callback ref_cb = base::Bind(&Ref::Foo, ref); michael@0: // LOG(INFO) << ref_cb.Run(); // Prints out 3. michael@0: // michael@0: // By default the object must support RefCounted or you will get a compiler michael@0: // error. If you're passing between threads, be sure it's michael@0: // RefCountedThreadSafe! See "Advanced binding of member functions" below if michael@0: // you don't want to use reference counting. michael@0: // michael@0: // RUNNING A CALLBACK michael@0: // michael@0: // Callbacks can be run with their "Run" method, which has the same michael@0: // signature as the template argument to the callback. michael@0: // michael@0: // void DoSomething(const base::Callback& callback) { michael@0: // callback.Run(5, "hello"); michael@0: // } michael@0: // michael@0: // Callbacks can be run more than once (they don't get deleted or marked when michael@0: // run). However, this precludes using base::Passed (see below). michael@0: // michael@0: // void DoSomething(const base::Callback& callback) { michael@0: // double myresult = callback.Run(3.14159); michael@0: // myresult += callback.Run(2.71828); michael@0: // } michael@0: // michael@0: // PASSING UNBOUND INPUT PARAMETERS michael@0: // michael@0: // Unbound parameters are specified at the time a callback is Run(). They are michael@0: // specified in the Callback template type: michael@0: // michael@0: // void MyFunc(int i, const std::string& str) {} michael@0: // base::Callback cb = base::Bind(&MyFunc); michael@0: // cb.Run(23, "hello, world"); michael@0: // michael@0: // PASSING BOUND INPUT PARAMETERS michael@0: // michael@0: // Bound parameters are specified when you create thee callback as arguments michael@0: // to Bind(). They will be passed to the function and the Run()ner of the michael@0: // callback doesn't see those values or even know that the function it's michael@0: // calling. michael@0: // michael@0: // void MyFunc(int i, const std::string& str) {} michael@0: // base::Callback cb = base::Bind(&MyFunc, 23, "hello world"); michael@0: // cb.Run(); michael@0: // michael@0: // A callback with no unbound input parameters (base::Callback) michael@0: // is called a base::Closure. So we could have also written: michael@0: // michael@0: // base::Closure cb = base::Bind(&MyFunc, 23, "hello world"); michael@0: // michael@0: // When calling member functions, bound parameters just go after the object michael@0: // pointer. michael@0: // michael@0: // base::Closure cb = base::Bind(&MyClass::MyFunc, this, 23, "hello world"); michael@0: // michael@0: // PARTIAL BINDING OF PARAMETERS michael@0: // michael@0: // You can specify some parameters when you create the callback, and specify michael@0: // the rest when you execute the callback. michael@0: // michael@0: // void MyFunc(int i, const std::string& str) {} michael@0: // base::Callback cb = base::Bind(&MyFunc, 23); michael@0: // cb.Run("hello world"); michael@0: // michael@0: // When calling a function bound parameters are first, followed by unbound michael@0: // parameters. michael@0: // michael@0: // michael@0: // ----------------------------------------------------------------------------- michael@0: // Quick reference for advanced binding michael@0: // ----------------------------------------------------------------------------- michael@0: // michael@0: // BINDING A CLASS METHOD WITH WEAK POINTERS michael@0: // michael@0: // base::Bind(&MyClass::Foo, GetWeakPtr()); michael@0: // michael@0: // The callback will not be issued if the object is destroyed at the time michael@0: // it's issued. DANGER: weak pointers are not threadsafe, so don't use this michael@0: // when passing between threads! michael@0: // michael@0: // BINDING A CLASS METHOD WITH MANUAL LIFETIME MANAGEMENT michael@0: // michael@0: // base::Bind(&MyClass::Foo, base::Unretained(this)); michael@0: // michael@0: // This disables all lifetime management on the object. You're responsible michael@0: // for making sure the object is alive at the time of the call. You break it, michael@0: // you own it! michael@0: // michael@0: // BINDING A CLASS METHOD AND HAVING THE CALLBACK OWN THE CLASS michael@0: // michael@0: // MyClass* myclass = new MyClass; michael@0: // base::Bind(&MyClass::Foo, base::Owned(myclass)); michael@0: // michael@0: // The object will be deleted when the callback is destroyed, even if it's michael@0: // not run (like if you post a task during shutdown). Potentially useful for michael@0: // "fire and forget" cases. michael@0: // michael@0: // IGNORING RETURN VALUES michael@0: // michael@0: // Sometimes you want to call a function that returns a value in a callback michael@0: // that doesn't expect a return value. michael@0: // michael@0: // int DoSomething(int arg) { cout << arg << endl; } michael@0: // base::Callback) cb = michael@0: // base::Bind(base::IgnoreResult(&DoSomething)); michael@0: // michael@0: // michael@0: // ----------------------------------------------------------------------------- michael@0: // Quick reference for binding parameters to Bind() michael@0: // ----------------------------------------------------------------------------- michael@0: // michael@0: // Bound parameters are specified as arguments to Bind() and are passed to the michael@0: // function. A callback with no parameters or no unbound parameters is called a michael@0: // Closure (base::Callback and base::Closure are the same thing). michael@0: // michael@0: // PASSING PARAMETERS OWNED BY THE CALLBACK michael@0: // michael@0: // void Foo(int* arg) { cout << *arg << endl; } michael@0: // int* pn = new int(1); michael@0: // base::Closure foo_callback = base::Bind(&foo, base::Owned(pn)); michael@0: // michael@0: // The parameter will be deleted when the callback is destroyed, even if it's michael@0: // not run (like if you post a task during shutdown). michael@0: // michael@0: // PASSING PARAMETERS AS A scoped_ptr michael@0: // michael@0: // void TakesOwnership(scoped_ptr arg) {} michael@0: // scoped_ptr f(new Foo); michael@0: // // f becomes null during the following call. michael@0: // base::Closure cb = base::Bind(&TakesOwnership, base::Passed(&f)); michael@0: // michael@0: // Ownership of the parameter will be with the callback until the it is run, michael@0: // when ownership is passed to the callback function. This means the callback michael@0: // can only be run once. If the callback is never run, it will delete the michael@0: // object when it's destroyed. michael@0: // michael@0: // PASSING PARAMETERS AS A scoped_refptr michael@0: // michael@0: // void TakesOneRef(scoped_refptr arg) {} michael@0: // scoped_refptr f(new Foo) michael@0: // base::Closure cb = base::Bind(&TakesOneRef, f); michael@0: // michael@0: // This should "just work." The closure will take a reference as long as it michael@0: // is alive, and another reference will be taken for the called function. michael@0: // michael@0: // PASSING PARAMETERS BY REFERENCE michael@0: // michael@0: // void foo(int arg) { cout << arg << endl } michael@0: // int n = 1; michael@0: // base::Closure has_ref = base::Bind(&foo, base::ConstRef(n)); michael@0: // n = 2; michael@0: // has_ref.Run(); // Prints "2" michael@0: // michael@0: // Normally parameters are copied in the closure. DANGER: ConstRef stores a michael@0: // const reference instead, referencing the original parameter. This means michael@0: // that you must ensure the object outlives the callback! michael@0: // michael@0: // michael@0: // ----------------------------------------------------------------------------- michael@0: // Implementation notes michael@0: // ----------------------------------------------------------------------------- michael@0: // michael@0: // WHERE IS THIS DESIGN FROM: michael@0: // michael@0: // The design Callback and Bind is heavily influenced by C++'s michael@0: // tr1::function/tr1::bind, and by the "Google Callback" system used inside michael@0: // Google. michael@0: // michael@0: // michael@0: // HOW THE IMPLEMENTATION WORKS: michael@0: // michael@0: // There are three main components to the system: michael@0: // 1) The Callback classes. michael@0: // 2) The Bind() functions. michael@0: // 3) The arguments wrappers (e.g., Unretained() and ConstRef()). michael@0: // michael@0: // The Callback classes represent a generic function pointer. Internally, michael@0: // it stores a refcounted piece of state that represents the target function michael@0: // and all its bound parameters. Each Callback specialization has a templated michael@0: // constructor that takes an BindState<>*. In the context of the constructor, michael@0: // the static type of this BindState<> pointer uniquely identifies the michael@0: // function it is representing, all its bound parameters, and a Run() method michael@0: // that is capable of invoking the target. michael@0: // michael@0: // Callback's constructor takes the BindState<>* that has the full static type michael@0: // and erases the target function type as well as the types of the bound michael@0: // parameters. It does this by storing a pointer to the specific Run() michael@0: // function, and upcasting the state of BindState<>* to a michael@0: // BindStateBase*. This is safe as long as this BindStateBase pointer michael@0: // is only used with the stored Run() pointer. michael@0: // michael@0: // To BindState<> objects are created inside the Bind() functions. michael@0: // These functions, along with a set of internal templates, are responsible for michael@0: // michael@0: // - Unwrapping the function signature into return type, and parameters michael@0: // - Determining the number of parameters that are bound michael@0: // - Creating the BindState storing the bound parameters michael@0: // - Performing compile-time asserts to avoid error-prone behavior michael@0: // - Returning an Callback<> with an arity matching the number of unbound michael@0: // parameters and that knows the correct refcounting semantics for the michael@0: // target object if we are binding a method. michael@0: // michael@0: // The Bind functions do the above using type-inference, and template michael@0: // specializations. michael@0: // michael@0: // By default Bind() will store copies of all bound parameters, and attempt michael@0: // to refcount a target object if the function being bound is a class method. michael@0: // These copies are created even if the function takes parameters as const michael@0: // references. (Binding to non-const references is forbidden, see bind.h.) michael@0: // michael@0: // To change this behavior, we introduce a set of argument wrappers michael@0: // (e.g., Unretained(), and ConstRef()). These are simple container templates michael@0: // that are passed by value, and wrap a pointer to argument. See the michael@0: // file-level comment in base/bind_helpers.h for more info. michael@0: // michael@0: // These types are passed to the Unwrap() functions, and the MaybeRefcount() michael@0: // functions respectively to modify the behavior of Bind(). The Unwrap() michael@0: // and MaybeRefcount() functions change behavior by doing partial michael@0: // specialization based on whether or not a parameter is a wrapper type. michael@0: // michael@0: // ConstRef() is similar to tr1::cref. Unretained() is specific to Chromium. michael@0: // michael@0: // michael@0: // WHY NOT TR1 FUNCTION/BIND? michael@0: // michael@0: // Direct use of tr1::function and tr1::bind was considered, but ultimately michael@0: // rejected because of the number of copy constructors invocations involved michael@0: // in the binding of arguments during construction, and the forwarding of michael@0: // arguments during invocation. These copies will no longer be an issue in michael@0: // C++0x because C++0x will support rvalue reference allowing for the compiler michael@0: // to avoid these copies. However, waiting for C++0x is not an option. michael@0: // michael@0: // Measured with valgrind on gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5), the michael@0: // tr1::bind call itself will invoke a non-trivial copy constructor three times michael@0: // for each bound parameter. Also, each when passing a tr1::function, each michael@0: // bound argument will be copied again. michael@0: // michael@0: // In addition to the copies taken at binding and invocation, copying a michael@0: // tr1::function causes a copy to be made of all the bound parameters and michael@0: // state. michael@0: // michael@0: // Furthermore, in Chromium, it is desirable for the Callback to take a michael@0: // reference on a target object when representing a class method call. This michael@0: // is not supported by tr1. michael@0: // michael@0: // Lastly, tr1::function and tr1::bind has a more general and flexible API. michael@0: // This includes things like argument reordering by use of michael@0: // tr1::bind::placeholder, support for non-const reference parameters, and some michael@0: // limited amount of subtyping of the tr1::function object (e.g., michael@0: // tr1::function is convertible to tr1::function). michael@0: // michael@0: // These are not features that are required in Chromium. Some of them, such as michael@0: // allowing for reference parameters, and subtyping of functions, may actually michael@0: // become a source of errors. Removing support for these features actually michael@0: // allows for a simpler implementation, and a terser Currying API. michael@0: // michael@0: // michael@0: // WHY NOT GOOGLE CALLBACKS? michael@0: // michael@0: // The Google callback system also does not support refcounting. Furthermore, michael@0: // its implementation has a number of strange edge cases with respect to type michael@0: // conversion of its arguments. In particular, the argument's constness must michael@0: // at times match exactly the function signature, or the type-inference might michael@0: // break. Given the above, writing a custom solution was easier. michael@0: // michael@0: // michael@0: // MISSING FUNCTIONALITY michael@0: // - Invoking the return of Bind. Bind(&foo).Run() does not work; michael@0: // - Binding arrays to functions that take a non-const pointer. michael@0: // Example: michael@0: // void Foo(const char* ptr); michael@0: // void Bar(char* ptr); michael@0: // Bind(&Foo, "test"); michael@0: // Bind(&Bar, "test"); // This fails because ptr is not const. michael@0: michael@0: namespace base { michael@0: michael@0: // First, we forward declare the Callback class template. This informs the michael@0: // compiler that the template only has 1 type parameter which is the function michael@0: // signature that the Callback is representing. michael@0: // michael@0: // After this, create template specializations for 0-7 parameters. Note that michael@0: // even though the template typelist grows, the specialization still michael@0: // only has one type: the function signature. michael@0: // michael@0: // If you are thinking of forward declaring Callback in your own header file, michael@0: // please include "base/callback_forward.h" instead. michael@0: template michael@0: class Callback; michael@0: michael@0: namespace internal { michael@0: template michael@0: struct BindState; michael@0: } // namespace internal michael@0: michael@0: template michael@0: class Callback : public internal::CallbackBase { michael@0: public: michael@0: typedef R(RunType)(); michael@0: michael@0: Callback() : CallbackBase(NULL) { } michael@0: michael@0: // Note that this constructor CANNOT be explicit, and that Bind() CANNOT michael@0: // return the exact Callback<> type. See base/bind.h for details. michael@0: template michael@0: Callback(internal::BindState* bind_state) michael@0: : CallbackBase(bind_state) { michael@0: michael@0: // Force the assignment to a local variable of PolymorphicInvoke michael@0: // so the compiler will typecheck that the passed in Run() method has michael@0: // the correct type. michael@0: PolymorphicInvoke invoke_func = michael@0: &internal::BindState michael@0: ::InvokerType::Run; michael@0: polymorphic_invoke_ = reinterpret_cast(invoke_func); michael@0: } michael@0: michael@0: bool Equals(const Callback& other) const { michael@0: return CallbackBase::Equals(other); michael@0: } michael@0: michael@0: R Run() const { michael@0: PolymorphicInvoke f = michael@0: reinterpret_cast(polymorphic_invoke_); michael@0: michael@0: return f(bind_state_.get()); michael@0: } michael@0: michael@0: private: michael@0: typedef R(*PolymorphicInvoke)( michael@0: internal::BindStateBase*); michael@0: michael@0: }; michael@0: michael@0: template michael@0: class Callback : public internal::CallbackBase { michael@0: public: michael@0: typedef R(RunType)(A1); michael@0: michael@0: Callback() : CallbackBase(NULL) { } michael@0: michael@0: // Note that this constructor CANNOT be explicit, and that Bind() CANNOT michael@0: // return the exact Callback<> type. See base/bind.h for details. michael@0: template michael@0: Callback(internal::BindState* bind_state) michael@0: : CallbackBase(bind_state) { michael@0: michael@0: // Force the assignment to a local variable of PolymorphicInvoke michael@0: // so the compiler will typecheck that the passed in Run() method has michael@0: // the correct type. michael@0: PolymorphicInvoke invoke_func = michael@0: &internal::BindState michael@0: ::InvokerType::Run; michael@0: polymorphic_invoke_ = reinterpret_cast(invoke_func); michael@0: } michael@0: michael@0: bool Equals(const Callback& other) const { michael@0: return CallbackBase::Equals(other); michael@0: } michael@0: michael@0: R Run(typename internal::CallbackParamTraits::ForwardType a1) const { michael@0: PolymorphicInvoke f = michael@0: reinterpret_cast(polymorphic_invoke_); michael@0: michael@0: return f(bind_state_.get(), internal::CallbackForward(a1)); michael@0: } michael@0: michael@0: private: michael@0: typedef R(*PolymorphicInvoke)( michael@0: internal::BindStateBase*, michael@0: typename internal::CallbackParamTraits::ForwardType); michael@0: michael@0: }; michael@0: michael@0: template michael@0: class Callback : public internal::CallbackBase { michael@0: public: michael@0: typedef R(RunType)(A1, A2); michael@0: michael@0: Callback() : CallbackBase(NULL) { } michael@0: michael@0: // Note that this constructor CANNOT be explicit, and that Bind() CANNOT michael@0: // return the exact Callback<> type. See base/bind.h for details. michael@0: template michael@0: Callback(internal::BindState* bind_state) michael@0: : CallbackBase(bind_state) { michael@0: michael@0: // Force the assignment to a local variable of PolymorphicInvoke michael@0: // so the compiler will typecheck that the passed in Run() method has michael@0: // the correct type. michael@0: PolymorphicInvoke invoke_func = michael@0: &internal::BindState michael@0: ::InvokerType::Run; michael@0: polymorphic_invoke_ = reinterpret_cast(invoke_func); michael@0: } michael@0: michael@0: bool Equals(const Callback& other) const { michael@0: return CallbackBase::Equals(other); michael@0: } michael@0: michael@0: R Run(typename internal::CallbackParamTraits::ForwardType a1, michael@0: typename internal::CallbackParamTraits::ForwardType a2) const { michael@0: PolymorphicInvoke f = michael@0: reinterpret_cast(polymorphic_invoke_); michael@0: michael@0: return f(bind_state_.get(), internal::CallbackForward(a1), michael@0: internal::CallbackForward(a2)); michael@0: } michael@0: michael@0: private: michael@0: typedef R(*PolymorphicInvoke)( michael@0: internal::BindStateBase*, michael@0: typename internal::CallbackParamTraits::ForwardType, michael@0: typename internal::CallbackParamTraits::ForwardType); michael@0: michael@0: }; michael@0: michael@0: template michael@0: class Callback : public internal::CallbackBase { michael@0: public: michael@0: typedef R(RunType)(A1, A2, A3); michael@0: michael@0: Callback() : CallbackBase(NULL) { } michael@0: michael@0: // Note that this constructor CANNOT be explicit, and that Bind() CANNOT michael@0: // return the exact Callback<> type. See base/bind.h for details. michael@0: template michael@0: Callback(internal::BindState* bind_state) michael@0: : CallbackBase(bind_state) { michael@0: michael@0: // Force the assignment to a local variable of PolymorphicInvoke michael@0: // so the compiler will typecheck that the passed in Run() method has michael@0: // the correct type. michael@0: PolymorphicInvoke invoke_func = michael@0: &internal::BindState michael@0: ::InvokerType::Run; michael@0: polymorphic_invoke_ = reinterpret_cast(invoke_func); michael@0: } michael@0: michael@0: bool Equals(const Callback& other) const { michael@0: return CallbackBase::Equals(other); michael@0: } michael@0: michael@0: R Run(typename internal::CallbackParamTraits::ForwardType a1, michael@0: typename internal::CallbackParamTraits::ForwardType a2, michael@0: typename internal::CallbackParamTraits::ForwardType a3) const { michael@0: PolymorphicInvoke f = michael@0: reinterpret_cast(polymorphic_invoke_); michael@0: michael@0: return f(bind_state_.get(), internal::CallbackForward(a1), michael@0: internal::CallbackForward(a2), michael@0: internal::CallbackForward(a3)); michael@0: } michael@0: michael@0: private: michael@0: typedef R(*PolymorphicInvoke)( michael@0: internal::BindStateBase*, michael@0: typename internal::CallbackParamTraits::ForwardType, michael@0: typename internal::CallbackParamTraits::ForwardType, michael@0: typename internal::CallbackParamTraits::ForwardType); michael@0: michael@0: }; michael@0: michael@0: template michael@0: class Callback : public internal::CallbackBase { michael@0: public: michael@0: typedef R(RunType)(A1, A2, A3, A4); michael@0: michael@0: Callback() : CallbackBase(NULL) { } michael@0: michael@0: // Note that this constructor CANNOT be explicit, and that Bind() CANNOT michael@0: // return the exact Callback<> type. See base/bind.h for details. michael@0: template michael@0: Callback(internal::BindState* bind_state) michael@0: : CallbackBase(bind_state) { michael@0: michael@0: // Force the assignment to a local variable of PolymorphicInvoke michael@0: // so the compiler will typecheck that the passed in Run() method has michael@0: // the correct type. michael@0: PolymorphicInvoke invoke_func = michael@0: &internal::BindState michael@0: ::InvokerType::Run; michael@0: polymorphic_invoke_ = reinterpret_cast(invoke_func); michael@0: } michael@0: michael@0: bool Equals(const Callback& other) const { michael@0: return CallbackBase::Equals(other); michael@0: } michael@0: michael@0: R Run(typename internal::CallbackParamTraits::ForwardType a1, michael@0: typename internal::CallbackParamTraits::ForwardType a2, michael@0: typename internal::CallbackParamTraits::ForwardType a3, michael@0: typename internal::CallbackParamTraits::ForwardType a4) const { michael@0: PolymorphicInvoke f = michael@0: reinterpret_cast(polymorphic_invoke_); michael@0: michael@0: return f(bind_state_.get(), internal::CallbackForward(a1), michael@0: internal::CallbackForward(a2), michael@0: internal::CallbackForward(a3), michael@0: internal::CallbackForward(a4)); michael@0: } michael@0: michael@0: private: michael@0: typedef R(*PolymorphicInvoke)( michael@0: internal::BindStateBase*, michael@0: typename internal::CallbackParamTraits::ForwardType, michael@0: typename internal::CallbackParamTraits::ForwardType, michael@0: typename internal::CallbackParamTraits::ForwardType, michael@0: typename internal::CallbackParamTraits::ForwardType); michael@0: michael@0: }; michael@0: michael@0: template michael@0: class Callback : public internal::CallbackBase { michael@0: public: michael@0: typedef R(RunType)(A1, A2, A3, A4, A5); michael@0: michael@0: Callback() : CallbackBase(NULL) { } michael@0: michael@0: // Note that this constructor CANNOT be explicit, and that Bind() CANNOT michael@0: // return the exact Callback<> type. See base/bind.h for details. michael@0: template michael@0: Callback(internal::BindState* bind_state) michael@0: : CallbackBase(bind_state) { michael@0: michael@0: // Force the assignment to a local variable of PolymorphicInvoke michael@0: // so the compiler will typecheck that the passed in Run() method has michael@0: // the correct type. michael@0: PolymorphicInvoke invoke_func = michael@0: &internal::BindState michael@0: ::InvokerType::Run; michael@0: polymorphic_invoke_ = reinterpret_cast(invoke_func); michael@0: } michael@0: michael@0: bool Equals(const Callback& other) const { michael@0: return CallbackBase::Equals(other); michael@0: } michael@0: michael@0: R Run(typename internal::CallbackParamTraits::ForwardType a1, michael@0: typename internal::CallbackParamTraits::ForwardType a2, michael@0: typename internal::CallbackParamTraits::ForwardType a3, michael@0: typename internal::CallbackParamTraits::ForwardType a4, michael@0: typename internal::CallbackParamTraits::ForwardType a5) const { michael@0: PolymorphicInvoke f = michael@0: reinterpret_cast(polymorphic_invoke_); michael@0: michael@0: return f(bind_state_.get(), internal::CallbackForward(a1), michael@0: internal::CallbackForward(a2), michael@0: internal::CallbackForward(a3), michael@0: internal::CallbackForward(a4), michael@0: internal::CallbackForward(a5)); michael@0: } michael@0: michael@0: private: michael@0: typedef R(*PolymorphicInvoke)( michael@0: internal::BindStateBase*, michael@0: typename internal::CallbackParamTraits::ForwardType, michael@0: typename internal::CallbackParamTraits::ForwardType, michael@0: typename internal::CallbackParamTraits::ForwardType, michael@0: typename internal::CallbackParamTraits::ForwardType, michael@0: typename internal::CallbackParamTraits::ForwardType); michael@0: michael@0: }; michael@0: michael@0: template michael@0: class Callback : public internal::CallbackBase { michael@0: public: michael@0: typedef R(RunType)(A1, A2, A3, A4, A5, A6); michael@0: michael@0: Callback() : CallbackBase(NULL) { } michael@0: michael@0: // Note that this constructor CANNOT be explicit, and that Bind() CANNOT michael@0: // return the exact Callback<> type. See base/bind.h for details. michael@0: template michael@0: Callback(internal::BindState* bind_state) michael@0: : CallbackBase(bind_state) { michael@0: michael@0: // Force the assignment to a local variable of PolymorphicInvoke michael@0: // so the compiler will typecheck that the passed in Run() method has michael@0: // the correct type. michael@0: PolymorphicInvoke invoke_func = michael@0: &internal::BindState michael@0: ::InvokerType::Run; michael@0: polymorphic_invoke_ = reinterpret_cast(invoke_func); michael@0: } michael@0: michael@0: bool Equals(const Callback& other) const { michael@0: return CallbackBase::Equals(other); michael@0: } michael@0: michael@0: R Run(typename internal::CallbackParamTraits::ForwardType a1, michael@0: typename internal::CallbackParamTraits::ForwardType a2, michael@0: typename internal::CallbackParamTraits::ForwardType a3, michael@0: typename internal::CallbackParamTraits::ForwardType a4, michael@0: typename internal::CallbackParamTraits::ForwardType a5, michael@0: typename internal::CallbackParamTraits::ForwardType a6) const { michael@0: PolymorphicInvoke f = michael@0: reinterpret_cast(polymorphic_invoke_); michael@0: michael@0: return f(bind_state_.get(), internal::CallbackForward(a1), michael@0: internal::CallbackForward(a2), michael@0: internal::CallbackForward(a3), michael@0: internal::CallbackForward(a4), michael@0: internal::CallbackForward(a5), michael@0: internal::CallbackForward(a6)); michael@0: } michael@0: michael@0: private: michael@0: typedef R(*PolymorphicInvoke)( michael@0: internal::BindStateBase*, michael@0: typename internal::CallbackParamTraits::ForwardType, michael@0: typename internal::CallbackParamTraits::ForwardType, michael@0: typename internal::CallbackParamTraits::ForwardType, michael@0: typename internal::CallbackParamTraits::ForwardType, michael@0: typename internal::CallbackParamTraits::ForwardType, michael@0: typename internal::CallbackParamTraits::ForwardType); michael@0: michael@0: }; michael@0: michael@0: template michael@0: class Callback : public internal::CallbackBase { michael@0: public: michael@0: typedef R(RunType)(A1, A2, A3, A4, A5, A6, A7); michael@0: michael@0: Callback() : CallbackBase(NULL) { } michael@0: michael@0: // Note that this constructor CANNOT be explicit, and that Bind() CANNOT michael@0: // return the exact Callback<> type. See base/bind.h for details. michael@0: template michael@0: Callback(internal::BindState* bind_state) michael@0: : CallbackBase(bind_state) { michael@0: michael@0: // Force the assignment to a local variable of PolymorphicInvoke michael@0: // so the compiler will typecheck that the passed in Run() method has michael@0: // the correct type. michael@0: PolymorphicInvoke invoke_func = michael@0: &internal::BindState michael@0: ::InvokerType::Run; michael@0: polymorphic_invoke_ = reinterpret_cast(invoke_func); michael@0: } michael@0: michael@0: bool Equals(const Callback& other) const { michael@0: return CallbackBase::Equals(other); michael@0: } michael@0: michael@0: R Run(typename internal::CallbackParamTraits::ForwardType a1, michael@0: typename internal::CallbackParamTraits::ForwardType a2, michael@0: typename internal::CallbackParamTraits::ForwardType a3, michael@0: typename internal::CallbackParamTraits::ForwardType a4, michael@0: typename internal::CallbackParamTraits::ForwardType a5, michael@0: typename internal::CallbackParamTraits::ForwardType a6, michael@0: typename internal::CallbackParamTraits::ForwardType a7) const { michael@0: PolymorphicInvoke f = michael@0: reinterpret_cast(polymorphic_invoke_); michael@0: michael@0: return f(bind_state_.get(), internal::CallbackForward(a1), michael@0: internal::CallbackForward(a2), michael@0: internal::CallbackForward(a3), michael@0: internal::CallbackForward(a4), michael@0: internal::CallbackForward(a5), michael@0: internal::CallbackForward(a6), michael@0: internal::CallbackForward(a7)); michael@0: } michael@0: michael@0: private: michael@0: typedef R(*PolymorphicInvoke)( michael@0: internal::BindStateBase*, michael@0: typename internal::CallbackParamTraits::ForwardType, michael@0: typename internal::CallbackParamTraits::ForwardType, michael@0: typename internal::CallbackParamTraits::ForwardType, michael@0: typename internal::CallbackParamTraits::ForwardType, michael@0: typename internal::CallbackParamTraits::ForwardType, michael@0: typename internal::CallbackParamTraits::ForwardType, michael@0: typename internal::CallbackParamTraits::ForwardType); michael@0: michael@0: }; michael@0: michael@0: michael@0: // Syntactic sugar to make Callbacks easier to declare since it michael@0: // will be used in a lot of APIs with delayed execution. michael@0: typedef Callback Closure; michael@0: michael@0: } // namespace base michael@0: michael@0: #endif // BASE_CALLBACK_H