【发布时间】:2015-11-24 10:51:54
【问题描述】:
我使用std::function 编写了一个超级简单的事件系统。
相当于
std::vector<Delegate<Args ...>*> _delegates;
其中Delegate 是std::function 的别名
template <typename ... Args>
using Delegate = std::function<void(Args ...)>;
事件的operator()、operator+= 和operator-= 过载。
operator() calls all of the listening functions.
operator+= adds a listener.
operator-= removes a listener.
将它们连接起来看起来像这样......
Foo::Foo(Bar& bar)
{
...
m_bar_ptr = &bar;
using namespace std::placeholders;
event_receiver =
Delegate<const Bar&, const SomeBarData>
(std::bind(&Foo::OnEventReceived, this, _1, _2));
bar.BarEvent += event_receiver;
...
}
一切都按预期工作,但是当我移动 Delegate 的所有者时,我最终不得不取消挂钩并复制初始挂钩的代码(如预期的那样)。
看起来像这样……
Foo& Foo::operator=(Foo&& other)
{
...
m_bar_ptr = other.m_bar_ptr;
other.m_bar_ptr = nullptr;
m_bar_ptr->BarEvent -= other.event_receiver;
using namespace std::place_holders;
event_receiver =
Delegate<const Bar&, const SomeBarData>
(std::bind(&Foo::OnEventReceived, this, _1, _2));
bar.BarEvent += event_receiver;
...
}
除了必须保留 Bar 的句柄(这是可以接受的)之外,还有很多代码可以重新定位 Delegate...并留下很大的错误空间。
我喜欢这些事件的简单性(尽管我愿意接受建议),但我真正想要的是一种保持这个事件系统并简化动作的方法。
有什么建议吗?
谢谢
【问题讨论】:
-
你为什么使用指向
std::function的原始指针向量? -
因为
std::functions无法比较是否相等。我将它们存储在std::vector中,并且需要能够找到它们以进行删除并避免添加重复项。
标签: c++ c++11 std move std-function