用户类中的多签名信号管理
multi-signatures signal management in user classes
我非常熟悉Qt,我知道我们不能有一个类似的语法,因为我们没有MOC部分在这里。然而,我试图有一个信号创建管理,以简化信号的声明和连接到它,在我的类。
这是我现在所做的示意图
class Foo
{
public:
void connectMove(boost::signal<void(int)>::slot_type slot)
void connectRotate(boost::signal<void(double)>::slot_type slot)
private:
boost::signal<void(int)> m_signalMove;
boost::signal<void(double)> m_signalRotate;
};
这就是我想要做的(大写=缺失部分)
<>之前
class SignalManager
{
public:
typedef boost::unrodered_map<std::string, GENERIC_SIGNAL *> MapSignal;
public:
template <typename Sig>
bool connect(const std::string& strSignalName, boost::signal<Sig>::slot_type slot)
{
// simplyfied... :
(*m_mapSignal.find(strSignalName))->connect(slot);
}
template <typename Sig>
bool disconnect(const std::string& strSignalName, boost::signal<Sig>::slot_type slot)
{
// simplyfied... :
(*m_mapSignal.find(strSignalName))->disconnect(slot);
}
protected:
bool call(const std::string& strSignalName, SIGNAL_ARGS)
{
(*m_mapSignal.find(strSignalName))(SIGNAL_ARGS);
}
template <typename Sig>
void delareSignal(const std::string& strSignalName)
{
m_mapSignals.insert(MapSignal::value_type(strSignalName, new boost::signal<Sig>()));
}
void destroySignal(const std::string& strSignalName)
{
// simplyfied... :
auto it = m_mapSignal.find(strSignalName);
delete *it;
m_mapSignal.erase(it);
}
private:
MapSignal m_mapSignals;
};
class Foo : public SignalManager
{
public:
Foo(void)
{
this->declareSignal<void(int)>("Move");
this->declareSignal<void(double)>("Rotate");
}
};
class Other : public boost::signals::trackable
{
public:
Other(Foo *p)
{
p->connect("Move", &Other::onMove);
p->connect("Rotate", &Other::onRotate);
}
void onMove(int i)
{
/* ... */
}
void onRotate(double d)
{
/* ... */
}
};
之前我想我可以用boost::functions_traits<>解决"SIGNAL_ARGS"部分,但我不知道如何绕过抽象信号类型。
1/我想要的可能吗?
2/这是个好方法吗?(我知道由于unordered_map,我将有一些开销。查找,特别是当我使用这个->调用("signalname",…),但我认为它不应该太重要)
3/如果这是不可能的或不是一个好的方法,你有什么其他的建议吗?
我通过包装boost::signals
并使用boost::shared_ptr<IWrapperSignal>
代替我的GENERIC_SIGNAL
来解决我的问题。
使用boost::function_traits<T>::arg_type
也解决了参数问题。
我不知道这是不是最好的方法,但它工作得很好,它是更简单的用户在类中声明的信号,继承这个SignalManager
相关文章:
- Qt VTK交互风格的信号到小部件
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 当vector是tje全局变量时,c++中vector的内存管理
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 如何在没有信号的情况下从C++执行QML插槽
- 线程之间的布尔停止信号
- 如何维护资源管理器项目视图中当前可见的项目列表
- 如何在信号处理程序和普通函数中对全局变量进行互斥读写操作
- 如何在GTK程序运行时禁用屏幕保护程序/电源管理/屏幕消隐
- 当用户在qtablewidget中输入单元格时,如何获得信号?C++
- 有可能在信号处理程序中设置promise吗
- 资源管理设计模式
- 代码在我的计算机上运行良好,但是在将其提交给coursera时遇到未知的信号11问题
- 使用"Task"函数指针队列定义作业管理器
- 在条件变量中触发错误信号的频率是多少
- 用户类中的多签名信号管理
- 使用信号和线程的事件管理器
- Boost信号连接管理
- 你如何处理内存管理和信号/插槽
- Qt:等待具有超时管理的信号