c++ - C++ 中带有模板化事件类型的中央事件调度器

标签 c++ events

我一直在学习 C++ 并尝试从我熟悉的其他语言中实现各种习语。

最近我尝试实现一个中央 EventDispatcher,多个对象可以注册可以接受 N 个参数的事件,并且对 EventDispatcher::dispatch(event_name) 的单个调用将调用所有已注册的事件回调。

首先我为事件创建了一个模板类;

template <typename ..._args>
class Event
{
public:
    //Alias for template callback
    using _callback = std::function<void(_args...)>;

    //Ctor & Dtor
    explicit Event(std::string const& name, _callback const& cb) : _name(name), _cbFunc(cb) {}
    ~Event() {}

    //Accessors
    std::string const& getName() const { return this->_name; }

    //Methods
    void trigger(_args... a) { this->_cbFunc(a...); }
private:
    //Event identifier
    std::string _name;
    //Event callback - can't be changed inside event.
    _callback const _cbFunc;
};

使用此结构,我可以定义事件,如下例所示:

void testCallback(int a) {
    std::cout << a << std::endl;
}

Event<int> _event("TestEvent", std::bind(&testCallback, _1));
_event.trigger(20); //This will actually output 20 to the console.

我接下来要做的是,有一个 EventDispatcher 类,它应该定义如下:

class EventDispatcher
{
public:
    EventDispatcher();
    ~EventDispatcher();

    void registerEvent(**Event of any type**);
    void dispatchEvent(std::string const& eventName, ....args);
private:
    std::map<std::string, std::vector<**Event of any type**>> _eventList;
};

这样我就可以这样打电话了;

EventDispatcher _dispatcher;
_dispatcher.registerEvent(_event); //event defined in the previous example.

_dispatcher.dispatchEvent("TestEvent", 20); //this should call event's trigger() method with arguments passed to it actually.

但是我无法弄清楚如何实现中央调度程序并让它能够注册模板化事件实例,然后将可变数量的参数传递给 dispatchEvent(...) 并使其触发 vector 中的所有事件模板化事件。

我怎样才能实现这样的功能?我的思维过程是正确的还是与实现这种系统的 C++ 方式相去甚远?欢迎任何提示。

最佳答案

我建议为事件类 (Event) 使用抽象基类 (IEvent) 的解决方案 以及带有可变参数和 dynamic_cast 的模板调度程序方法。

具有抽象方法 getName 的 Apstrac 基类:

class IEvent
{
public:
    virtual const std::string & getName() const = 0;
};

您的事件类派生自 IEvent:

template <typename ..._args>
class Event : public IEvent
{
public:
    //Alias for template callback
    using _callback = std::function<void(_args...)>;

    //Ctor & Dtor
    //template< typename T_CB >
    explicit Event( const std::string & name, const _callback & cb ) : _name(name), _cbFunc(cb) {}
    ~Event() {}

    //Accessors
    virtual const std::string & getName() const override { return this->_name; }

    //Methods
    void trigger(_args... a) { this->_cbFunc(a...); }
private:
    //Event identifier
    std::string _name;
    //Event callback - can't be changed inside event.
    _callback const _cbFunc;
};

具有模板方法的调度器:

class EventDispatcher
{
public:
    EventDispatcher() {}
    ~EventDispatcher()
    {
      for ( auto el : _eventList )
      {
        for ( auto e : el.second )
          delete e;
      }
    }

    void registerEvent( IEvent *event )
    {
      if ( event )
          _eventList[event->getName()].push_back( event );
    }

    template <typename ..._args>
    void dispatchEvent( const std::string & eventName, _args...a )
    {
      auto it_eventList = _eventList.find( eventName );
      if ( it_eventList == _eventList.end() )
        return;
      for ( auto ie : it_eventList->second )
      {
        if ( Event<_args...> * event = dynamic_cast<Event<_args...>*>( ie ) )
          event->trigger( a... );
      }
    }

private:
    std::map<std::string, std::vector<IEvent*>> _eventList;
};

最后是应用程序:

void testCallback(int a) {
    std::cout << a << std::endl;
}

int main()
{
  EventDispatcher eventDisp;
  eventDisp.registerEvent( new Event<int>( "TestEvent", &testCallback ) );

  eventDisp.dispatchEvent( "TestEvent", 20 );

  return 0;
}

关于c++ - C++ 中带有模板化事件类型的中央事件调度器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44636240/

相关文章:

javascript - 如何存储/隐藏 JavaScript 事件并在以后重用?

jQuery 监听器不会 "listen"动态创建 DOM 元素上的事件

objective-c - NSTextField 没有诸如 'Editing did Change' 之类的事件?

c++ - 扫描数字数组并计算范围 (0 - 24)(25 - 49) 等之间的数字的算法

c++ - 编写自己的 STL 容器

android - 屏幕打开时更新 Android Widget

c# - WPF - 因值更改而触发的常见事件

C++,dll的多个实例,单例

c++ - BOOST图cc1plus : out of memory allocating

c++ - 拥有多态对象集合的首选 C++ 习惯用法是什么?