gpt4 book ai didi

c++ - 使用 boost::bind 将回调发布到任务队列

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:14:41 24 4
gpt4 key购买 nike

假设我有一个名为 subscribe() 的函数,它接受一个回调处理程序,它会在事件被触发时被调用。

现在,我有另一个版本,称为 subscribe2()。一切都一样,只是在触发时需要将其发布到事件队列。它是使用原始的 subscribe() 实现的,带有一个名为 helper() 的辅助函数。它所做的只是将原始处理程序和任何其他参数绑定(bind)到仿函数中,然后调用 postToEventQueue()

现在,我想知道是否有一种方法可以消除辅助函数,以便在 subsribe2() 中,我可以以某种方式将 postToTaskQueue() 函数和原始函数打包直接回调处理程序,并将其传递给 subscribe()。原因是我有很多不同的处理程序类型,到处引入辅助函数很乏味和累人。毕竟,boost::bind 应该返回给定原始函数的新函数,对吗?我正在尝试使用 boost::bind 直接生成辅助函数。

一个尝试是说

subscribe(boost::bind(boost::bind(postToTaskQueue, boost::bind(_1, _2)), cb, _1)); 

subscribe2() 中,但它不起作用。有可能吗?

请参阅下面的详细示例代码。谢谢!

#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <iostream>

typedef boost::function<void(int)> SomeCallback;
typedef boost::function<void()> Task;

void handler(int i){
std::cout << "i=" << i <<std::endl;
}

void subscribe(SomeCallback cb)
{
cb(100); //just invoke the callback for simplicity
}

void postToTaskQueue(Task t)
{
t(); // just invoke the task for simplicity
}

void helper(SomeCallback cb, int i)
{
Task t = boost::bind(cb, i);
postToTaskQueue(t);
}

void subscribe2(SomeCallback cb)
{
subscribe(boost::bind(helper, cb, _1));

// this does not work..
// subscribe(boost::bind(boost::bind(postToTaskQueue, boost::bind(_1, _2)), cb, _1));
}
int main()
{
subscribe(boost::bind(handler, _1));
subscribe2(boost::bind(handler, _1));
}

最佳答案

我没有答案。但是,我已经玩了一个多小时了:

  • boost::bind
  • boost::apply<>
  • boost::protect

也许,只是也许,更有经验的 boost 开发人员可以从这里开始:

void subscribe2(SomeCallback cb)
{
using boost::bind;
using boost::protect;
using boost::apply;

bind(cb, 41)(); // OK of course
postToTaskQueue(bind(cb, 46)); // also fine
bind(postToTaskQueue, protect(bind(cb, 146)))(); // boost::protect to the rescue

postToTaskQueue(bind(apply<void>(), cb, 47));
bind(postToTaskQueue, protect(bind(apply<void>(), cb, 147)))();

上面的打印

i=41
i=46
i=146
i=47
i=147

但是,遗憾的是,我似乎无法使这个东西参数化(正如建议应该在 the documentation on composition using Nested Binds 中工作):

    // but sadly, this appears to not work ...
auto hmm = bind(postToTaskQueue, bind(apply<void>(), cb, _1));
hmm(997); // FAIL
}

这是一个完整编译的演示,显示了事态: Live on Coliru

#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/bind/protect.hpp>
#include <boost/bind/apply.hpp>
#include <iostream>

typedef boost::function<void(int)> SomeCallback;
typedef boost::function<void()> Task;

void handler(int i){
std::cout << "i=" << i <<std::endl;
}

void subscribe(SomeCallback cb)
{
cb(100); //just invoke the callback for simplicity
}

void postToTaskQueue(Task t)
{
t(); // just invoke the task for simplicity
}

void helper(SomeCallback cb, int i)
{
postToTaskQueue(boost::bind(cb, i));
}

void subscribe2(SomeCallback cb)
{
using boost::bind;
using boost::protect;
using boost::apply;

bind(cb, 41)(); // OK of course
postToTaskQueue(bind(cb, 46)); // also find
bind(postToTaskQueue, protect(bind(cb, 146)))(); // boost::protect to the rescue

postToTaskQueue(bind(apply<void>(), cb, 47));
bind(postToTaskQueue, protect(bind(apply<void>(), cb, 147)))();

// but sadly, this appears to not work ...
auto hmm = bind(postToTaskQueue, bind(apply<void>(), cb, _1));
//hmm(997); // FAIL
}
int main()
{
subscribe (boost::bind(handler, _1));
subscribe2(boost::bind(handler, _1));
}

关于c++ - 使用 boost::bind 将回调发布到任务队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18680114/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com