TR1功能组播 [英] TR1 function multicast

查看:97
本文介绍了TR1功能组播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将如何为TR1函数实现多播?我已将回调插槽实现为

How would you implement multicast for TR1 functors? I have my callback slots implemented like

void setCallback(std::tr1::function<void (std::string)> cb)
{
    this->callback = cb;
}

,但需要在其中之一中传递多个回调.我不想进入更复杂的解决方案,例如观察者,因为这是到目前为止我唯一需要多播的情况.我也无法使用Boost.Signals(如建议使用此处),因为我不能使用Boost.当订户不再存在时,我不需要显式处理禁用回调.

but need to pass more than one callback in one of them. I don't want to go into more complex solutions like observer, as this is the only case I need multicast so far. I also cannot use Boost.Signals (as suggested here), because I cannot use Boost. I don't need to explicitly handle disabling callback when subscriber no longer exist.

推荐答案

您最可能想要:

void registerCallback(std::tr1::function<void (std::string)> cb)
{
    this->callbacks.push_back(cb);
}

带有callbacks

容器(任意一个)包含std::tr1::function对象,而不是单个容器.调度时,遍历回调.

with callbacks a container (whichever you like) of std::tr1::function objects instead of a single one. When dispatching, iterate over the callbacks.

此外,如果您希望以后能够删除回调,则可以按照以下步骤进行操作:

Also, if you want to be able to remove callbacks later, you can do something along these lines:

// I use list because I don't want the iterators to be invalid
// after I add / remove elements
std::list<std::function<void(std::string)>> callbacks;

...
typedef std::list<std::function<void(std::string)>>::iterator callback_id;

callback_id register_callback(std::function<void(std::string)> f)
{
    return callbacks.insert(callbacks.end(), f);
}

void unregister_callback(callback_id id)
{
    callbacks.erase(id);
}

这篇关于TR1功能组播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆