[C ++ 11] On std :: function and std :: bind

std :: function callable wrapper

C ++ callables (Callable Objects) is defined as follows:

  1. Function pointers: consistent with C language;
  2. Class member function pointer;
  3. Functor (functor): also as a function of the object, overload operator()classes / objects operator structure;
  4. lambda expressions.

std :: function is Callable Objects wrapper (Wrapper), Callable Objects may be received in addition to any class member function pointers. std :: function can be used to process the callback function, similar to the C language function pointer, allowing to save more Callable Objects, and to delay the execution of them, but it may save Callable Objects in addition to the function pointers, so that it is more powerful than the C language.

When we std :: function instantiated a function signature (function types, including the list of parameters and return values), it becomes a package that can accommodate all of these functions call.

std :: function basic usage

#include <iostream>
#include <functional> // std::function

// global function
void func(void) {
  std::cout << __FUNCTION__ << std::endl;
}

class Foo {
public:
  // class static function
  static int foo_func(int a) {
    std::cout << __FUNCTION__ << "(" << a << "):";
    return a;
  }

  // class non-static member function
  int foo_func_nonstatic(int a) {
    std::cout << __FUNCTION__ << "(" << a  << "):";
    return a;
  }
};

class Bar {
public:
  // functor
  int operator()(int a) {
    std::cout << __FUNCTION__ << "(" << a << "):";
    return a;
  }
};

int main() {
  // 传入合适函数签名给std::function模板参数即可绑定对应签名的
  // 普通函数或
  // 类静态成员函数或
  // 借助std::bind绑定类非静态成员函数
  std::function<void(void)> func1 = func;
  std::function<int(int)> func2   = Foo::foo_func;
  Foo foo;
  std::function<int(int)> func3   = std::bind(&Foo::foo_func_nonstatic, &foo,
                                              std::placeholders::_1);

  // 然后,直接像函数一样调用
  func1(); // func
  std::cout << func2(1)  << std::endl; // foo_func(1):1
  std::cout << func3(11) << std::endl; // foo_func_nonstatic(11):11

  // 当函数签名一致时,func2可像一个变量一样复用
  // Bar重载了operator()即成为functor,可直接包装到std::function
  Bar bar;
  func2 = bar;
  std::cout << func2(2) << std::endl; // operator()(2):2

  // 也可绑定lambda表达式
  auto func_lambda = [](int a){
    std::cout << "bind lambda sample(" << a << ")" << std::endl;
  };
  func_lambda(3); // bind lambda sample(3)

  return 0;
}

Since std :: function as a left function values ​​received object, it can function as a parameter; std :: function and a function pointer can be bound, to achieve a delay execution of the function, it may be substituted std :: function std :: function as a callback function.

Examples of std :: function callback mechanism to achieve the following:

#include <iostream>
#include <functional> // std::function

// 任意可调用对象,如普通全局函数
void func_callback(int a) {
  std::cout << __FUNCTION__ << ":Output, a=" << a << std::endl;
}

class Foo {
public:
  explicit Foo(std::function<void(int)> cb)
    :cb_(cb), a_(0) {
  }
  ~Foo() = default;

  // setter
  void set_a(int a) {
    a_ = a;
  }

  void OutputCallback() {
    cb_(a_);
  }

private:
  std::function<void(int)> cb_;
  int a_;
};

int main() {
  // 实例化Foo,并参数注入回调函数
  // 处理
  // 回调输出处理结果
  Foo foo(func_callback);
  foo.set_a(1);
  foo.OutputCallback(); // func_callback:Output, a=1

  return 0;
}

Above, describes the use std :: function of binding Callable Objects, and to demonstrate an instance of std :: function as an example callback function; which, func_callback callback parameter to an int, in practical applications such as face intelligent analysis , structure analysis information face as a callback function pointer argument, the function may output a recognition result; class Foo human face Related category, OutputCallback class may be called a thread facial analysis, the analysis of the results obtained you can call a callback OutputCallback output to provide user func_callback's.

NOTE: C ++ 11 provides std :: function alternatively in C language function pointer as the callback, the former is the C ++ programming style, which is the C programming style.

C / C ++ callback mechanism concurrent server-side application programming and gaming wide, well-known widely used 2d game framework Cocos2dx callback mechanism, provides a common macro below callback, the callback mechanism may be more open Cocos2dx reference source.

// Cocos2dx new callbacks based on C++11
//
// __selector__:回调函数指针
// __target__:回调对象指针
// ##__VA_ARGS__:可变参数列表 
// std::placeholders::_1:不定参数1,调用时由调用函数的参数传入
// std::placeholders::_2:不定参数2,调用时由调用函数的参数传入
// std::placeholders::_3:不定参数3,调用时由调用函数的参数传入
#define CC_CALLBACK_0(__selector__,__target__, ...) std::bind(&__selector__,__target__, ##__VA_ARGS__)
#define CC_CALLBACK_1(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, ##__VA_ARGS__)
#define CC_CALLBACK_2(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, ##__VA_ARGS__)
#define CC_CALLBACK_3(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, ##__VA_ARGS__)

Reflections std :: function / std :: bind and abstract factory, factory method

TODO...

Guess you like

Origin www.cnblogs.com/bigosprite/p/11294264.html