Hello!
I have almost skipped the "A solution" section. The code is impressive, I don't have time to understand it now, but it looks like a great template work :D
However, while reading the "Usage" section, I tend to believe you could have used std::variant from C++17. Here is an example:
#include <variant>
#include <iostream>
// Our eventsclassMouseEvent{public:voiddo_mouse_event_stuff()const{std::cout<<"Doing MouseEvent stuff\n";};};classKeyboardEvent{public:voiddo_keyboard_event_stuff()const{std::cout<<"Doing KeyboardEvent stuff\n";};};classJoystickEvent{public:voiddo_joystick_event_stuff()const{std::cout<<"Doing JoystickEvent stuff\n";};};// Another event, not recognized by the engineclassNotAnEngineEvent{};// Create a type to represent the events the engine can handleusingEvent=std::variant<MouseEvent,KeyboardEvent,JoystickEvent>;// To use with std::visit (see below)structEventHandler{voidoperator()(constMouseEvent&e){e.do_mouse_event_stuff();}voidoperator()(constKeyboardEvent&e){e.do_keyboard_event_stuff();}voidoperator()(constJoystickEvent&e){e.do_joystick_event_stuff();}};intmain(){// Create eventsEventm=MouseEvent{};Eventk=KeyboardEvent{};Eventj=JoystickEvent{};// Test the type of the event manuallyif(std::holds_alternative<MouseEvent>(m)){autoe=std::get<MouseEvent>(m);e.do_mouse_event_stuff();}if(autop=std::get_if<KeyboardEvent>(&k)){p->do_keyboard_event_stuff();}// With std::visitEventHandlerhandler;std::visit(handler,m);std::visit(handler,k);std::visit(handler,j);// Event bad = NotAnEngineEvent{}; // don't compile}
It is absolutely different from your solution: I don't create an ID that identifies a class. But I can determine the actual type of a generic event so that I can use its specific member functions.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hello!
I have almost skipped the "A solution" section. The code is impressive, I don't have time to understand it now, but it looks like a great template work :D
However, while reading the "Usage" section, I tend to believe you could have used
std::variant
from C++17. Here is an example:It is absolutely different from your solution: I don't create an ID that identifies a class. But I can determine the actual type of a generic event so that I can use its specific member functions.