zoukankan      html  css  js  c++  java
  • C++ in Chromium 编程开始

     

    实现回调三步走:

    1,我这个函数需要传入一个回调,我调用这个回调用run。这个是模板抽象。

    void MyFunction1(base::OnceCallback<int(int32, double)> my_callback) {
      // OnceCallback
      int result1 = std::move(my_callback).Run(10, 1.0);
    
      // After running a OnceCallback, it's consumed and nulled out.
      DCHECK(!my_callback)
      ...
    }

    2,这是个回调函数:

    void MyFunction(int32 a, double b){
    
    xxxx
    }

    MyFunction通过bind后变成一个回调函数声明。my_callback1就可以传入1,作为实体参数。

    base::OnceCallback<void(double)> my_callback1 = base::BindOnce(&MyFunction, 10); //这个10相当于第一个参数是10,第二个参数run时再指定:如std::move(my_callback1).Run(3.5);

    3, 调用

    MyFunction1(my_callback1)

    ok


    C++ in Chromium 101 - Codelab

    This tutorial will guide you through the creation of various example C++ applications, highlighting important Chromium C++ concepts. This tutorial assumes robust knowledge of C++ (the language) but does not assume you know how to write an application specific to Chromium's style and architecture. This tutorial does assume that you know how to check files out of Chromium's repository.
     
    As always, consider the following resources as of primary importance:

    This tutorial does not assume you have read any of the above, though you should feel free to peruse them when necessary. This tutorial will cover information across all of those guides.

    Exercise 0: "Hello World!"

    This exercise demonstrates the use of the ninja build system to build a simple C++ binary and demonstrates how typical C++ builds are organized within Chromium.

    Create a new target in base/BUILD.gn for a new executable named hello_world. Then write the classic "Hello, world!" program in C++. You should be able to build it with ninja -C out/Default hello_world and execute it directly by finding the binary within out/Default.

    Sample execution:

     
    $ cd /path/to/chromium/src
    $ gclient runhooks
    $ ninja -C out/Default hello_world
    $ out/Default/hello_world
    Hello, world!
    AMI: might be confusing how rietveld shows the new file as a copy/diff against an unrelated file.
    AMI: output doesn't actually match what's in the solution.  Might be confusing.

    Part 1: Using command-line arguments

    We will augment our hello_world binary to parse command-line flags and use those values to print messages to the user.

    Command-line arguments within Chromium are processed by the CommandLine::Init() function, which takes command line flags from the argc and argv (argument count & vector) variables of the main() method. A typical invocation of CommandLine::Init() looks like the following:
     
    int main(int argc, char** argv) {
      CommandLine::Init(argc, argv);
      // Main program execution ...
      return 0;
    }

    Flags are not explicitly defined in Chromium. Instead, we use GetSwitchValueAsASCII() and friends to retrieve values passed in.

    Important include files

    Exercise 1: Using command-line arguments

    Change hello_world to take a --greeting and a --name switch. The greeting, if not specified, should default to "Hello", and the name, if not specified, should default to "World".

    Exercise solution

    Part 2: Callbacks and Bind

    C++, unlike other languages such as Python, Javascript, or Lisp, has only rudimentary support for callbacks and no support for partial application. However, Chromium has the base::OnceCallback<Sig> and  base::RepeatingCallback<Sig>  class, whose instances can be freely passed around, returned, and generally be treated as first-class values. base::OnceCallback<Sig> is the move-only, single-call variant, and base::RepeatingCallback<Sig> is the copyable, multiple-call variant.

    AMI: last sentence might benefit from a pointer to refcounting (without which it might be less clear how binding works with copying of bound closures).

    The Sig template parameter is a function signature type:

    // The type of a callback that:
    //  - Can run only once.
    //  - Is move-only and non-copyable.
    //  - Takes no arguments and does not return anything.
    // base::OnceClosure is an alias of this type.
    base::OnceCallback<void()>

    // The type of a callback that:
    //  - Can run more than once.
    //  - Is copyable.
    //  - Takes no arguments and does not return anything.
    // base::RepeatingClosure is an alias of this type.
    base::RepeatingCallback<void()>


    // The types of a callback that takes two arguments (a string and a double)
    // and returns an int.
    base::OnceCallback<int(std::string, double)>
    base::RepeatingCallback<int(std::string, double)>

    Callbacks are executed by invoking the Run() member function. base::OnceCallback<Sig> needs to be rvalue to run.

    void MyFunction1(base::OnceCallback<int(std::string, double)> my_callback) {
      // OnceCallback
      int result1 = std::move(my_callback).Run("my string 1", 1.0);


      // After running a OnceCallback, it's consumed and nulled out.
      DCHECK(!my_callback)

      ...
    }

    void MyFunction2(base::RepeatingCallback<int(std::string, double)> my_callback) {

      int result1 = my_callback.Run("my string 1", 1.0);

      // Run() can be called as many times as you wish for RepeatingCallback.
      int result2 = my_callback.Run("my string 2", 2);
      ...
    }

    Callbacks are constructed using the base::BindOnce() or base::BindRepeating() function, which handles partial application:

    // Declare a function.
    void MyFunction(int32 a, double b);

    base::OnceCallback<void(double)> my_callback1 = base::BindOnce(&MyFunction, 10);
    base::RepeatingCallback<void(double)> my_callback2 = base::BindRepeating(&MyFunction, 10);


    // Equivalent to:
    //
    // MyFunction(10, 3.5);
    //
    std::move(my_callback1).Run(3.5);
    my_callback2.Run(3.5);

    base::BindOnce() and base::BindRepeating can do a lot more, including binding class member functions and binding additional arguments to an existing base::OnceCallback or base::RepeatingCallback. See the comments in docs/callback.md for details.

    Important Include Files

    #include "base/bind.h"
    #include "base/callback.h"

    Exercise 2: Fibonacci closures

    Implement a function that returns a callback that takes no arguments and returns successive Fibonacci numbers. That is, a function that can be used like this:

    base::RepeatingCallback<int()> fibonacci_closure = MakeFibonacciClosure();
    printf("%d ", fibonacci_closure.Run()); // Prints "1"
    printf("%d ", fibonacci_closure.Run()); // Prints "1"
    printf("%d ", fibonacci_closure.Run()); // Prints "2"
    ...

    Each returned Fibonacci callback should be independent; running one callback shouldn't affect the result of running another callback. Write a fibonacci executable that takes an integer argument n and uses your function to print out the first n Fibonacci numbers.

    (This exercise was inspired by this Go exercise: Function closures.)

    Exercise solution

    Part 3: Message loops

    Chromium's abstraction for event loops is base::MessageLoopbase::MessageLoop handles running tasks (which are instances of base::Closure) on the current thread. Given a pointer to the message loop for a thread, you can post tasks on it with base::MessageLoop::PostTask (or base::MessageLoop::PostDelayedTask if you want to add a delay).
     
    Normally you wouldn't have to worry about setting up a base::MessageLoop and keeping it running, since that is automatically done by Chromium's thread classes. However, since the main thread doesn't automatically start off with a base::MessageLoop, you have to create, pump, and shutdown one yourself if you're writing a new executable. The base::RunLoop class is the current recommended way of doing that.

    Important header files

    Exercise 3: Sleep

    Implement the Unix command-line utility sleep using only a base::MessageLoop (i.e., without using the sleep function or base::PlatformThread::Sleep).

    Exercise solution

    Part 4: Threads and task runners

    Chromium's platform-independent abstraction for threads is base::Thread. Once you call base::Thread::Start() (and it succeeds), you can post tasks on its message loop.

    Chromium's abstraction for asynchronously running posted tasks is base::TaskRunner. You can get a base::TaskRunner for various things, including threads, message loops, and thread pools. base::TaskRunner allows you to write code that posts tasks without depending on what exactly will run those tasks.

    An important member function of base::TaskRunner is base::TaskRunner::PostTaskAndReplyPostTaskAndReply lets you post a task to a base::TaskRunner which will post a task back to your current thread when its done. This sounds simple, but it's quite tricky to do this in a correct, thread-safe manner!

    Important header files

    More information

    Exercise 4: Integer factorization

    Take the given (slow) function to find a non-trivial factor of a given integer:

    bool FindNonTrivialFactor(int n, int* factor) {
      // Really naive algorithm.
      for (int i = n-1; i >= 2; --i) {
        
    if (n % i == 0) {
          
    *factor = i;
          return true;
        }
      }
      return false;
    }

    Write a command-line utility factor that takes a number, spawns a thread to factor it in the background using FindNonTrivialFactor, and prints a status update on the main thread every second while the worker thread is busy.

    Exercise solution


    Callback<> and Bind()

    [TOC]

    Introduction

    The templated base::Callback<> class is a generalized function object. Together with the base::Bind() function in base/bind.h, they provide a type-safe method for performing partial application of functions.

    Partial application is the process of binding a subset of a function's arguments to produce another function that takes fewer arguments. This can be used to pass around a unit of delayed execution, much like lexical closures are used in other languages. For example, it is used in Chromium code to schedule tasks on different MessageLoops.

    A callback with no unbound input parameters (base::Callback<void()>) is called a base::Closure. Note that this is NOT the same as what other languages refer to as a closure -- it does not retain a reference to its enclosing environment.

    OnceCallback<> And RepeatingCallback<>

    base::OnceCallback<> and base::RepeatingCallback<> are next gen callback classes, which are under development.

    base::OnceCallback<> is created by base::BindOnce(). This is a callback variant that is a move-only type and can be run only once. This moves out bound parameters from its internal storage to the bound function by default, so it's easier to use with movable types. This should be the preferred callback type: since the lifetime of the callback is clear, it's simpler to reason about when a callback that is passed between threads is destroyed.

    base::RepeatingCallback<> is created by base::BindRepeating(). This is a callback variant that is copyable that can be run multiple times. It uses internal ref-counting to make copies cheap. However, since ownership is shared, it is harder to reason about when the callback and the bound state are destroyed, especially when the callback is passed between threads.

    The legacy base::Callback<> is currently aliased to base::RepeatingCallback<>. In new code, prefer base::OnceCallback<> where possible, and use base::RepeatingCallback<> otherwise. Once the migration is complete, the type alias will be removed and base::OnceCallback<> will be renamed to base::Callback<> to emphasize that it should be preferred.

    base::RepeatingCallback<> is convertible to base::OnceCallback<> by the implicit conversion.

    Memory Management And Passing

    Pass base::{Once,Repeating}Callback objects by value if ownership is transferred; otherwise, pass it by const-reference.

    // |Foo| just refers to |cb| but doesn't store it nor consume it.
    bool Foo(const base::OnceCallback<void(int)>& cb) {
      return cb.is_null();
    }
    
    // |Bar| takes the ownership of |cb| and stores |cb| into |g_cb|.
    base::RepeatingCallback<void(int)> g_cb;
    void Bar(base::RepeatingCallback<void(int)> cb) {
      g_cb = std::move(cb);
    }
    
    // |Baz| takes the ownership of |cb| and consumes |cb| by Run().
    void Baz(base::OnceCallback<void(int)> cb) {
      std::move(cb).Run(42);
    }
    
    // |Qux| takes the ownership of |cb| and transfers ownership to PostTask(),
    // which also takes the ownership of |cb|.
    void Qux(base::RepeatingCallback<void(int)> cb) {
      PostTask(FROM_HERE, base::BindOnce(cb, 42));
      PostTask(FROM_HERE, base::BindOnce(std::move(cb), 43));
    }
    

    When you pass a base::{Once,Repeating}Callback object to a function parameter, use std::move() if you don't need to keep a reference to it, otherwise, pass the object directly. You may see a compile error when the function requires the exclusive ownership, and you didn't pass the callback by move. Note that the moved-from base::{Once,Repeating}Callback becomes null, as if its Reset() method had been called. Afterward, its is_null() method will return true and its operator bool() will return false.

    Quick reference for basic stuff

    Binding A Bare Function

    int Return5() { return 5; }
    base::OnceCallback<int()> func_cb = base::BindOnce(&Return5);
    LOG(INFO) << std::move(func_cb).Run();  // Prints 5.
    
    int Return5() { return 5; }
    base::RepeatingCallback<int()> func_cb = base::BindRepeating(&Return5);
    LOG(INFO) << func_cb.Run();  // Prints 5.
    

    Binding A Captureless Lambda

    base::Callback<int()> lambda_cb = base::Bind([] { return 4; });
    LOG(INFO) << lambda_cb.Run();  // Print 4.
    
    base::OnceCallback<int()> lambda_cb2 = base::BindOnce([] { return 3; });
    LOG(INFO) << std::move(lambda_cb2).Run();  // Print 3.
    

    Binding A Capturing Lambda (In Tests)

    When writing tests, it is often useful to capture arguments that need to be modified in a callback.

    #include "base/test/bind_test_util.h"
    
    int i = 2;
    base::Callback<void()> lambda_cb = base::BindLambdaForTesting([&]() { i++; });
    lambda_cb.Run();
    LOG(INFO) << i;  // Print 3;
    

    Binding A Class Method

    The first argument to bind is the member function to call, the second is the object on which to call it.

    class Ref : public base::RefCountedThreadSafe<Ref> {
     public:
      int Foo() { return 3; }
    };
    scoped_refptr<Ref> ref = new Ref();
    base::Callback<void()> ref_cb = base::Bind(&Ref::Foo, ref);
    LOG(INFO) << ref_cb.Run();  // Prints out 3.
    

    By default the object must support RefCounted or you will get a compiler error. If you're passing between threads, be sure it's RefCountedThreadSafe! See "Advanced binding of member functions" below if you don't want to use reference counting.

    Running A Callback

    Callbacks can be run with their Run method, which has the same signature as the template argument to the callback. Note that base::OnceCallback::Run consumes the callback object and can only be invoked on a callback rvalue.

    void DoSomething(const base::Callback<void(int, std::string)>& callback) {
      callback.Run(5, "hello");
    }
    
    void DoSomethingOther(base::OnceCallback<void(int, std::string)> callback) {
      std::move(callback).Run(5, "hello");
    }
    

    RepeatingCallbacks can be run more than once (they don't get deleted or marked when run). However, this precludes using base::Passed (see below).

    void DoSomething(const base::RepeatingCallback<double(double)>& callback) {
      double myresult = callback.Run(3.14159);
      myresult += callback.Run(2.71828);
    }
    

    If running a callback could result in its own destruction (e.g., if the callback recipient deletes the object the callback is a member of), the callback should be moved before it can be safely invoked. (Note that this is only an issue for RepeatingCallbacks, because a OnceCallback always has to be moved for execution.)

    void Foo::RunCallback() {
      std::move(&foo_deleter_callback_).Run();
    }
    

    Creating a Callback That Does Nothing

    Sometimes you need a callback that does nothing when run (e.g. test code that doesn't care to be notified about certain types of events). It may be tempting to pass a default-constructed callback of the right type:

    using MyCallback = base::OnceCallback<void(bool arg)>;
    void MyFunction(MyCallback callback) {
      std::move(callback).Run(true);  // Uh oh...
    }
    ...
    MyFunction(MyCallback());  // ...this will crash when Run()!
    

    Default-constructed callbacks are null, and thus cannot be Run(). Instead, use base::DoNothing():

    ...
    MyFunction(base::DoNothing());  // Can be Run(), will no-op
    

    base::DoNothing() can be passed for any OnceCallback or RepeatingCallback that returns void.

    Implementation-wise, base::DoNothing() is actually a functor which produces a callback from operator(). This makes it unusable when trying to bind other arguments to it. Normally, the only reason to bind arguments to DoNothing() is to manage object lifetimes, and in these cases, you should strive to use idioms like DeleteSoon(), ReleaseSoon(), or RefCountedDeleteOnSequence instead. If you truly need to bind an argument to DoNothing(), or if you need to explicitly create a callback object (because implicit conversion through operator()() won't compile), you can instantiate directly:

    // Binds |foo_ptr| to a no-op OnceCallback takes a scoped_refptr<Foo>.
    // ANTIPATTERN WARNING: This should likely be changed to ReleaseSoon()!
    base::Bind(base::DoNothing::Once<scoped_refptr<Foo>>(), foo_ptr);
    

    Passing Unbound Input Parameters

    Unbound parameters are specified at the time a callback is Run(). They are specified in the base::Callback template type:

    void MyFunc(int i, const std::string& str) {}
    base::Callback<void(int, const std::string&)> cb = base::Bind(&MyFunc);
    cb.Run(23, "hello, world");
    

    Passing Bound Input Parameters

    Bound parameters are specified when you create the callback as arguments to base::Bind(). They will be passed to the function and the Run()ner of the callback doesn't see those values or even know that the function it's calling.

    void MyFunc(int i, const std::string& str) {}
    base::Callback<void()> cb = base::Bind(&MyFunc, 23, "hello world");
    cb.Run();
    

    A callback with no unbound input parameters (base::Callback<void()>) is called a base::Closure. So we could have also written:

    base::Closure cb = base::Bind(&MyFunc, 23, "hello world");
    

    When calling member functions, bound parameters just go after the object pointer.

    base::Closure cb = base::Bind(&MyClass::MyFunc, this, 23, "hello world");
    

    Partial Binding Of Parameters

    You can specify some parameters when you create the callback, and specify the rest when you execute the callback.

    When calling a function bound parameters are first, followed by unbound parameters.

    void ReadIntFromFile(const std::string& filename,
                         base::OnceCallback<void(int)> on_read);
    
    void DisplayIntWithPrefix(const std::string& prefix, int result) {
      LOG(INFO) << prefix << result;
    }
    
    void AnotherFunc(const std::string& file) {
      ReadIntFromFile(file, base::BindOnce(&DisplayIntWithPrefix, "MyPrefix: "));
    };
    

    This technique is known as partial application. It should be used in lieu of creating an adapter class that holds the bound arguments. Notice also that the "MyPrefix: " argument is actually a const char*, while DisplayIntWithPrefix actually wants a const std::string&. Like normal function dispatch, base::Bind, will coerce parameter types if possible.

    Avoiding Copies With Callback Parameters

    A parameter of base::BindRepeating() or base::BindOnce() is moved into its internal storage if it is passed as a rvalue.

    std::vector<int> v = {1, 2, 3};
    // |v| is moved into the internal storage without copy.
    base::Bind(&Foo, std::move(v));
    
    // The vector is moved into the internal storage without copy.
    base::Bind(&Foo, std::vector<int>({1, 2, 3}));
    

    Arguments bound with base::BindOnce() are always moved, if possible, to the target function. A function parameter that is passed by value and has a move constructor will be moved instead of copied. This makes it easy to use move-only types with base::BindOnce().

    In contrast, arguments bound with base::BindRepeating() are only moved to the target function if the argument is bound with base::Passed().

    DANGER: A base::RepeatingCallback can only be run once if arguments were bound with base::Passed(). For this reason, avoid base::Passed(). If you know a callback will only be called once, prefer to refactor code to work with base::OnceCallback instead.

    Avoid using base::Passed() with base::BindOnce(), as std::move() does the same thing and is more familiar.

    void Foo(std::unique_ptr<int>) {}
    auto p = std::make_unique<int>(42);
    
    // |p| is moved into the internal storage of Bind(), and moved out to |Foo|.
    base::BindOnce(&Foo, std::move(p));
    base::BindRepeating(&Foo, base::Passed(&p)); // Ok, but subtle.
    base::BindRepeating(&Foo, base::Passed(std::move(p))); // Ok, but subtle.
    

    Quick reference for advanced binding

    Binding A Class Method With Weak Pointers

    If MyClass has a base::WeakPtr<MyClass> weak_this_ member (see below) then a class method can be bound with:

    base::Bind(&MyClass::Foo, weak_this_);
    

    The callback will not be run if the object has already been destroyed.

    Note that class method callbacks bound to base::WeakPtrs may only be run on the same sequence on which the object will be destroyed, since otherwise execution of the callback might race with the object's deletion.

    To use base::WeakPtr with base::Bind()MyClass will typically look like:

    class MyClass {
    public:
      MyClass() {
        weak_this_ = weak_factory_.GetWeakPtr();
      }
    private:
      base::WeakPtr<MyClass> weak_this_;
      // MyClass member variables go here.
      base::WeakPtrFactory<MyClass> weak_factory_{this};
    };
    

    weak_factory_ is the last member variable in MyClass so that it is destroyed first. This ensures that if any class methods bound to weak_this_ are Run() during teardown, then they will not actually be executed.

    If MyClass only ever base::Bind()s and executes callbacks on the same sequence, then it is generally safe to call weak_factory_.GetWeakPtr() at the base::Bind() call, rather than taking a separate weak_this_ during construction.

    Binding A Class Method With Manual Lifetime Management

    base::Bind(&MyClass::Foo, base::Unretained(this));
    

    This disables all lifetime management on the object. You're responsible for making sure the object is alive at the time of the call. You break it, you own it!

    Binding A Class Method And Having The Callback Own The Class

    MyClass* myclass = new MyClass;
    base::Bind(&MyClass::Foo, base::Owned(myclass));
    

    The object will be deleted when the callback is destroyed, even if it's not run (like if you post a task during shutdown). Potentially useful for "fire and forget" cases.

    Smart pointers (e.g. std::unique_ptr<>) are also supported as the receiver.

    std::unique_ptr<MyClass> myclass(new MyClass);
    base::Bind(&MyClass::Foo, std::move(myclass));
    

    Ignoring Return Values

    Sometimes you want to call a function that returns a value in a callback that doesn't expect a return value.

    int DoSomething(int arg) { cout << arg << endl; }
    base::Callback<void(int)> cb =
        base::Bind(IgnoreResult(&DoSomething));
    

    Quick reference for binding parameters to Bind()

    Bound parameters are specified as arguments to base::Bind() and are passed to the function. A callback with no parameters or no unbound parameters is called a base::Closure (base::Callback<void()> and base::Closure are the same thing).

    Passing Parameters Owned By The Callback

    void Foo(int* arg) { cout << *arg << endl; }
    int* pn = new int(1);
    base::Closure foo_callback = base::Bind(&foo, base::Owned(pn));
    

    The parameter will be deleted when the callback is destroyed, even if it's not run (like if you post a task during shutdown).

    Passing Parameters As A unique_ptr

    void TakesOwnership(std::unique_ptr<Foo> arg) {}
    auto f = std::make_unique<Foo>();
    // f becomes null during the following call.
    base::OnceClosure cb = base::BindOnce(&TakesOwnership, std::move(f));
    

    Ownership of the parameter will be with the callback until the callback is run, and then ownership is passed to the callback function. This means the callback can only be run once. If the callback is never run, it will delete the object when it's destroyed.

    Passing Parameters As A scoped_refptr

    void TakesOneRef(scoped_refptr<Foo> arg) {}
    scoped_refptr<Foo> f(new Foo);
    base::Closure cb = base::Bind(&TakesOneRef, f);
    

    This should "just work." The closure will take a reference as long as it is alive, and another reference will be taken for the called function.

    void DontTakeRef(Foo* arg) {}
    scoped_refptr<Foo> f(new Foo);
    base::Closure cb = base::Bind(&DontTakeRef, base::RetainedRef(f));
    

    base::RetainedRef holds a reference to the object and passes a raw pointer to the object when the Callback is run.

    Passing Parameters By Reference

    References are copied unless std::ref or std::cref is used. Example:

    void foo(const int& arg) { printf("%d %p
    ", arg, &arg); }
    int n = 1;
    base::Closure has_copy = base::Bind(&foo, n);
    base::Closure has_ref = base::Bind(&foo, std::cref(n));
    n = 2;
    foo(n);                        // Prints "2 0xaaaaaaaaaaaa"
    has_copy.Run();                // Prints "1 0xbbbbbbbbbbbb"
    has_ref.Run();                 // Prints "2 0xaaaaaaaaaaaa"
    

    Normally parameters are copied in the closure. DANGER: std::ref and std::cref store a (const) reference instead, referencing the original parameter. This means that you must ensure the object outlives the callback!

    Implementation notes

    Where Is This Design From:

    The design of base::Callback and base::Bind is heavily influenced by C++'s tr1::function / tr1::bind, and by the "Google Callback" system used inside Google.

    Customizing the behavior

    There are several injection points that controls binding behavior from outside of its implementation.

    namespace base {
    
    template <typename Receiver>
    struct IsWeakReceiver {
      static constexpr bool value = false;
    };
    
    template <typename Obj>
    struct UnwrapTraits {
      template <typename T>
      T&& Unwrap(T&& obj) {
        return std::forward<T>(obj);
      }
    };
    
    }  // namespace base
    

    If base::IsWeakReceiver<Receiver>::value is true on a receiver of a method, base::Bind checks if the receiver is evaluated to true and cancels the invocation if it's evaluated to false. You can specialize base::IsWeakReceiver to make an external smart pointer as a weak pointer.

    base::UnwrapTraits<BoundObject>::Unwrap() is called for each bound arguments right before base::Callback calls the target function. You can specialize this to define an argument wrapper such as base::Unretainedbase::Ownedbase::RetainedRef and base::Passed.

    How The Implementation Works:

    There are three main components to the system:

    1. The base::Callback<> classes.
    2. The base::Bind() functions.
    3. The arguments wrappers (e.g., base::Unretained() and base::Owned()).

    The Callback classes represent a generic function pointer. Internally, it stores a refcounted piece of state that represents the target function and all its bound parameters. The base::Callback constructor takes a base::BindStateBase*, which is upcasted from a base::BindState<>. In the context of the constructor, the static type of this base::BindState<> pointer uniquely identifies the function it is representing, all its bound parameters, and a Run() method that is capable of invoking the target.

    base::Bind() creates the base::BindState<> that has the full static type, and erases the target function type as well as the types of the bound parameters. It does this by storing a pointer to the specific Run() function, and upcasting the state of base::BindState<>* to a base::BindStateBase*. This is safe as long as this BindStateBase pointer is only used with the stored Run() pointer.

    To base::BindState<> objects are created inside the base::Bind() functions. These functions, along with a set of internal templates, are responsible for

    • Unwrapping the function signature into return type, and parameters
    • Determining the number of parameters that are bound
    • Creating the BindState storing the bound parameters
    • Performing compile-time asserts to avoid error-prone behavior
    • Returning a Callback<> with an arity matching the number of unbound parameters and that knows the correct refcounting semantics for the target object if we are binding a method.

    The base::Bind functions do the above using type-inference and variadic templates.

    By default base::Bind() will store copies of all bound parameters, and attempt to refcount a target object if the function being bound is a class method. These copies are created even if the function takes parameters as const references. (Binding to non-const references is forbidden, see bind.h.)

    To change this behavior, we introduce a set of argument wrappers (e.g., base::Unretained()). These are simple container templates that are passed by value, and wrap a pointer to argument. See the file-level comment in base/bind_helpers.h for more info.

    These types are passed to the Unwrap() functions to modify the behavior of base::Bind(). The Unwrap() functions change behavior by doing partial specialization based on whether or not a parameter is a wrapper type.

    base::Unretained() is specific to Chromium.

    Missing Functionality

    • Binding arrays to functions that take a non-const pointer. Example:
    void Foo(const char* ptr);
    void Bar(char* ptr);
    base::Bind(&Foo, "test");
    base::Bind(&Bar, "test");  // This fails because ptr is not const.
    
    • In case of partial binding of parameters a possibility of having unbound parameters before bound parameters. Example:
    void Foo(int x, bool y);
    base::Bind(&Foo, _1, false); // _1 is a placeholder.
    

    If you are thinking of forward declaring base::Callback in your own header file, please include "base/callback_forward.h" instead.


    Partial application

    From Wikipedia, the free encyclopedia
     
     
    Jump to navigationJump to search

    In computer sciencepartial application (or partial function application) refers to the process of fixing a number of arguments to a function, producing another function of smaller arity. Given a function {displaystyle fcolon (X imes Y imes Z) o N}{displaystyle fcolon (X	imes Y	imes Z)	o N}, we might fix (or 'bind') the first argument, producing a function of type {displaystyle { ext{partial}}(f)colon (Y imes Z) o N}{displaystyle {	ext{partial}}(f)colon (Y	imes Z)	o N}. Evaluation of this function might be represented as {displaystyle f_{partial}(2,3)}f_{partial}(2,3). Note that the result of partial function application in this case is a function that takes two arguments. Partial application is sometimes incorrectly called currying, which is a related, but distinct concept.

    Motivation[edit]

    Intuitively, partial function application says "if you fix the first arguments of the function, you get a function of the remaining arguments". For example, if function div(x,y) = x/y, then div with the parameter x fixed at 1 is another function: div1(y) = div(1,y) = 1/y. This is the same as the function inv that returns the multiplicative inverse of its argument, defined by inv(y) = 1/y.

    The practical motivation for partial application is that very often the functions obtained by supplying some but not all of the arguments to a function are useful; for example, many languages have a function or operator similar to plus_one. Partial application makes it easy to define these functions, for example by creating a function that represents the addition operator with 1 bound as its first argument.

    Implementations[edit]

    In languages such as MLHaskell and F#, functions are defined in curried form by default. Supplying fewer than the total number of arguments is referred to as partial application.

    In languages with first-class functions one can define curryuncurry and papply to perform currying and partial application explicitly. This might incur a greater run-time overhead due to the creation of additional closures, while Haskell can use more efficient techniques.[1]

    Scala implements optional partial application with placeholder, e.g. def add(xIntyInt{x+y}; add(1_: Int) returns an incrementing function. Scala also support multiple parameter lists as currying, e.g. def add(xInt)(yInt{x+y}; add(1_.

    Clojure implements partial application using the partial function defined in its core library.

    The C++ standard library provides bind(function, args..) to return a function object that is the result of partial application of the given arguments to the given function. Alternatively, lambdas can be used.

    int f(int a, int b);
    auto f_partial = [](int a) { return f(a, 123); };
    assert(f_partial(456) == f(456, 123) );
    

    In JavaMethodHandle.bindTo partially applies a function to its first argument.[2]

    In Raku, the assuming method creates a new function with fewer parameters.[3]

    The Python standard library module functools includes the partial function, allowing positional and named argument bindings, returning a new function.[4]

    In XQuery, an argument placeholder (?) is used for each non-fixed argument in a partial function application.[5]

    Definitions[edit]

    In the simply-typed lambda calculus with function and product types (λ→,×) partial application, currying and uncurrying can be defined as:

    papply
    (((a × b) → c) × a) → (b → c) = λ(fx). λyf (xy)
    curry
    ((a × b) → c) → (a → (b → c)) = λfλxλyf (xy)
    uncurry
    (a → (b → c)) → ((a × b) → c) = λfλ(xy). f x y

    Note that curry papply = curry.

  • 相关阅读:
    MySQL第七课
    MySQL第六课
    mysql第五课
    MySQL第四课
    MySQL第三课
    MYSQL第一课
    MYSQL第二课
    char、vchar、nvarchar 的区别
    SSRS Reporting Service安装与部署
    存储过程用法
  • 原文地址:https://www.cnblogs.com/bigben0123/p/13253924.html
Copyright © 2011-2022 走看看