用户类中的多签名信号管理

multi-signatures signal management in user classes

本文关键字:信号 管理 用户      更新时间:2023-10-16

我非常熟悉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