C++11 std::bind std::function 高级用法weakcallback

关于std::function<>模板的使用
1、如何给其赋值
https://blog.csdn.net/eclipser1987/article/details/24406203?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.add_param_isCf&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.add_param_isCf

2、在读muduo多线程编程的时候,发现了一个比较有趣的用法,下面看源码:

template <typename CLASS, typename... ARGS> class WeakCallback {
public:
  WeakCallback(const std::weak_ptr<CLASS> &object,
               const std::function<void(CLASS *, ARGS...)> &function)
      : object_(object), function_(function) {}

  // Default dtor, copy ctor and assignment are okay

  void operator()(ARGS &&... args) const {
    std::shared_ptr<CLASS> ptr(object_.lock());
    if (ptr) {
      function_(ptr.get(), std::forward<ARGS>(args)...);
    }
    // else
    // {
    //   LOG_TRACE << "expired";
    // }
  }

private:
  std::weak_ptr<CLASS> object_;
  std::function<void(CLASS *, ARGS...)> function_;
};

template <typename CLASS, typename... ARGS>
WeakCallback<CLASS, ARGS...>
makeWeakCallback(const std::shared_ptr<CLASS> &object,
                 void (CLASS::*function)(ARGS...)) {
    return WeakCallback<CLASS, ARGS...>(object, function);
}

template <typename CLASS, typename... ARGS>
WeakCallback<CLASS, ARGS...>
makeWeakCallback(const std::shared_ptr<CLASS> &object,
                 void (CLASS::*function)(ARGS...) const) {
                 //这里函数指针和std::function<void(CLASS *, ARGS...)
  return WeakCallback<CLASS, ARGS...>(object, function);
}

下面是具体用法:

class String
{
 public:
  String(const char* str)
  {
    printf("String ctor this %p\n", this);
  }

  String(const String& rhs)
  {
    printf("String copy ctor this %p, rhs %p\n", this, &rhs);
  }

  String(String&& rhs)
  {
    printf("String move ctor this %p, rhs %p\n", this, &rhs);
  }
};

class Foo : boost::noncopyable
{
 public:
  void zero();
  void zeroc() const;
  void one(int);
  void oner(int&);
  void onec(int) const;
  void oneString(const String& str);
  void oneStringRR(String&& str);
};

BOOST_AUTO_TEST_CASE(testWeakCallback)
{
  printf("======== testWeakCallback \n");
  std::shared_ptr<Foo> foo(new Foo);
  muduo::WeakCallback<Foo> cb0 = muduo::makeWeakCallback(foo, &Foo::zero);
  muduo::WeakCallback<Foo> cb0c = muduo::makeWeakCallback(foo, &Foo::zeroc);
  cb0();
  cb0c();

  muduo::WeakCallback<Foo, int> cb1 = muduo::makeWeakCallback(foo, &Foo::one);
  auto cb1c = muduo::makeWeakCallback(foo, &Foo::onec);
  auto cb1r = muduo::makeWeakCallback(foo, &Foo::oner);
  cb1(123);
  cb1c(234);
  int i = 345;
  cb1r(i);
  BOOST_CHECK_EQUAL(i, 1000);

  auto cb2 = muduo::makeWeakCallback(foo, &Foo::oneString);
  auto cb2r = muduo::makeWeakCallback(foo, &Foo::oneStringRR);
  printf("_Z%s\n", typeid(cb2).name());
  printf("_Z%s\n", typeid(cb2r).name());
  cb2(String("xx"));
  cb2r(String("yy"));

  muduo::WeakCallback<Foo> cb3(foo, std::bind(&Foo::oneString, std::placeholders::_1, "zz"));

  cb3();

  printf("======== reset \n");
  foo.reset();
  cb0();
  cb0c();
  cb1(123);
  cb1c(234);
  cb2(String("xx"));
  cb2r(String("yy"));
  cb3();
}
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页