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