我试图在 C++ (C++17) 中模仿 JavaScript 风格的
Event/EventTarget
操作。
我有一个 Event
基类可以根据需要工作,还有一个 KeyboardEvent
类继承自 Event
。
我的 EventTarget.addEventListener
函数将 EventObjects
存储在一个可以用 dispatchEvent
调用的向量中。
我可以使用
addEventListener
使用 Event
参数存储回调函数,例如onGenericEvent(Event event)
。
但是,我无法使用 addEventListener
添加带有 KeyboardEvent
参数的回调函数,例如onKeyDown(KeyboardEvent event)
.
我期待
std::function<void(Event)>
接受KeyboardEvent
,因为它继承自Event
,但我收到一个编译错误:
错误:无法将“void (*)(KeyboardEvent)”转换为“std::function
”
我该如何进行?
#include <iostream>
#include <functional>
#include <string>
#include <vector>
class Event {
public:
Event(std::string _type, void* _pointer) {
type = _type;
pointer = _pointer;
}
std::string type;
void* pointer;
};
struct EventObject {
std::string type;
std::function<void(Event)> listener;
void* pointer;
};
class EventTarget {
public:
static void addEventListener(std::string type, std::function<void(Event)> listener, void* pointer){
EventObject eventObj;
eventObj.type = type;
eventObj.listener = listener;
eventObj.pointer = pointer;
events.push_back(eventObj);
}
static void dispatchEvent(Event event){
for (unsigned int n = 0; n < events.size(); ++n) {
if (event.type == events[n].type && event.pointer == events[n].pointer) {
events[n].listener(event);
}
}
}
static std::vector<EventObject> events;
};
std::vector<EventObject> EventTarget::events;
class KeyboardEvent : public Event {
public:
KeyboardEvent(std::string _type, void* _pointer) : Event(_type, _pointer) {
key = "random key";
}
std::string key;
};
class App
{
public:
static void onGenericEvent(Event event) {
printf("event.type = %s \n", event.type.c_str());
}
static void onKeyDown(KeyboardEvent event) {
printf("event.type = %s \n", event.type.c_str()); // print Event class property
printf("event.key = %s \n", event.key.c_str()); // print KeyboardEvent class property
}
};
int main() {
App* app = new App();
EventTarget::addEventListener("generic", &App::onGenericEvent, app);
Event event("generic", app);
EventTarget::dispatchEvent(event); // simulate generic event
EventTarget::addEventListener("keydown", &App::onKeyDown, app); //ERROR: cannot convert 'void (*)(KeyboardEvent)' to std::function<void(Event)>'
KeyboardEvent keyEvent("keydown", app);
EventTarget::dispatchEvent(keyEvent); // simulate keydown event
}
注意:首先也是最重要的是,如果您按这样的值传递对象,您将对
Event
对象进行切片。您需要传递引用或指针才能使多态性起作用!
void(KeyboardEvent&)
比void(Event&)
更具体,所以
std::function<void(Event&)>
不能包裹void(KeyboardEvent&)
。
考虑如果可以,那么这将是可能的:
void handler(KeybaordEvent& evt)
{
std::cout << evt.key;
}
int main()
{
std::function<void(Event&)> callback(&handler);
MouseEvent mouseEvent; // Imagine MouseEvent also derives from Event
callback(mouseEvent);
}
因为
MouseEvent
是一个 Event
一个 std::function<void(Event&)>
可以接受一个 MouseEvent
参数。如果它被允许包装一个 void(KeyboardEvent&)
,这将导致处理程序被传递错误的类型。出于这个原因,你想要的是不允许的。
记住,类型检查是在编译时完成的。仅仅因为你(尝试)永远不会通过在运行时检查
EventObject
的type
成员来传递错误的类型并不意味着你可以在编译时有不匹配的类型。
有两种处理方法:
Event
并进行运行时检查以确保他们获得了他们期望的事件类型(即使用 dynamic_cast
)。作为第二个选项的示例,您可以这样:
template <typename EventType>
struct EventObject {
std::string type;
std::function<void(EventType)> listener;
void* pointer;
};
class EventTarget {
public:
template <typename EventType>
static void addEventListener(std::string type, std::function<void(EventType)> listener, void* pointer){
EventObject<EventType> eventObj;
eventObj.type = type;
eventObj.listener = listener;
eventObj.pointer = pointer;
events<EventType>.push_back(eventObj);
}
template <typename EventType>
static void dispatchEvent(EventType event){
for (auto& eventObj : events<EventType>) {
eventObj.listener(event);
}
}
template <typename EventType>
static std::vector<EventObject<EventType>> events;
};
template <typename EventType>
std::vector<EventObject<EventType>> EventTarget::events;
class KeyboardEvent : public Event {
public:
KeyboardEvent(std::string _type, void* _pointer) : Event(_type, _pointer) {
key = "random key";
}
std::string key;
};
class App
{
public:
static void onGenericEvent(Event event) {
printf("event.type = %s \n", event.type.c_str());
}
static void onKeyDown(KeyboardEvent event) {
printf("event.type = %s \n", event.type.c_str()); // print Event class property
printf("event.key = %s \n", event.key.c_str()); // print KeyboardEvent class property
}
};
int main() {
App* app = new App();
EventTarget::addEventListener<Event>("generic", &App::onGenericEvent, app);
Event event("generic", app);
EventTarget::dispatchEvent(event); // simulate generic event
EventTarget::addEventListener<KeyboardEvent>("keydown", &App::onKeyDown, app); //ERROR: cannot convert 'void (*)(KeyboardEvent)' to std::function<void(Event)>'
KeyboardEvent keyEvent("keydown", app);
EventTarget::dispatchEvent(keyEvent); // simulate keydown event
}
这会为每种类型的事件创建单独的
events
向量。现在编译器可以在编译时检查正确类型的事件是否总是传递给每个处理程序。