比较std :: tr1 :: function<>对象 [英] Comparing std::tr1::function<> objects

查看:192
本文介绍了比较std :: tr1 :: function<>对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在C ++中实现一个C#类的事件系统,使用tr1函数模板来存储处理事件的函数。

I've been trying to implement a C#-like event system in C++ with the tr1 function templates used to store a function that handles the event.

我创建了一个向量,以便可以将多个侦听器附加到此事件,例如:

I created a vector so that multiple listeners can be attached to this event, i.e.:

vector< function<void (int)> >  listenerList;

我想从列表中删除一个处理程序,以停止监听器接收事件。

I'd like to be able to remove a handler from the list to stop a listener receiving events.

那么,我如何找到这个列表中对应于给定监听器的条目?我可以测试列表中的'function'对象是否指向特定的函数?

So, how can I find the entry in this list that corresponds to a given listener? Can I test if a 'function' object in the list refers to a particular function?

谢谢!

编辑:看过boost :: signal方法,似乎它可能是使用令牌系统实现,如你所建议的一些。 这里有一些信息。观察器在附加到事件时保留连接对象,并且此连接对象用于在需要时断开连接。所以它看起来像是使用Boost还是自己滚动tr1,基本原理是一样的。也就是说它会有点笨重:)

Having looked into the boost::signal approach, it seems it's probably implemented using a token system as some of you have suggested. Here's some info on this. An observer retains a "Connection" object when they attach to an event, and this connection object is used to disconnect if needed. So it looks like whether you use Boost or roll your own with tr1, the basic principle's the same. i.e. it will be a bit clumsy :)

推荐答案

我不知道你是否锁定std C ++和tr1,但如果你不是,似乎你的问题可以完全避免,如果你只是使用像boost :: signal和boost :: bind来解决你的原始问题 - 创建一个事件系统,而不是试图自己滚动。

I don't know if you're locked into std C++ and tr1, but if you aren't, it seems like your problem could be completely avoided if you just used something like boost::signal and boost::bind to solve your original problem - creating an event system - instead of trying to roll your own.

这篇关于比较std :: tr1 :: function&lt;&gt;对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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