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();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值