Last active
January 2, 2023 21:24
-
-
Save faceslog/f5008eb215488eb8ef2ce86402261164 to your computer and use it in GitHub Desktop.
DummyEventHandlerCpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <string> | |
#include <unordered_map> | |
#include <functional> | |
// Define a type for the event handler function | |
using EventHandlerFunc = std::function<void(void)>; | |
class EventHandler | |
{ | |
public: | |
// Get a reference to the singleton instance | |
static EventHandler& getInstance() | |
{ | |
static EventHandler instance; | |
return instance; | |
} | |
static void add(const std::string& eventName, EventHandlerFunc handler) | |
{ | |
EventHandler::getInstance().registerEvent(eventName, handler); | |
} | |
static void trigger(const std::string& eventName) | |
{ | |
EventHandler::getInstance().dispatchEvent(eventName); | |
} | |
private: | |
// Make the constructor, copy constructor, assignment, move operator private | |
EventHandler() {} | |
EventHandler(const EventHandler&) = delete; | |
EventHandler& operator=(EventHandler&& other) = delete; | |
// Register an event handler for a given event | |
void registerEvent(const std::string& eventName, EventHandlerFunc handler) | |
{ | |
m_eventHandlers[eventName] = handler; | |
} | |
// Dispatch an event by calling the registered event handler | |
void dispatchEvent(const std::string& eventName) | |
{ | |
auto it = m_eventHandlers.find(eventName); | |
if (it != m_eventHandlers.end()) | |
{ | |
it->second(); | |
} | |
} | |
// Create a map of event names to event handlers | |
std::unordered_map<std::string, EventHandlerFunc> m_eventHandlers; | |
}; | |
// Register an event handler for a given event. | |
#define REGISTER_EVENT(eventName, handlerFunc) \ | |
EventHandler::add(eventName, handlerFunc); | |
// Create a static function youreventname_Func() to write the event like a function | |
// The double pound sign (##) is used to concatenate two tokens | |
// This ensures that the event handler is registered only once, even if the macro is called multiple times. | |
#define REGISTER_EVENT_FUNC(eventName, handlerFunc) \ | |
static int eventName##_Func = []() { \ | |
EventHandler::add(#eventName, handlerFunc); \ | |
return 0; \ | |
}() | |
// Define the DISPATCH_EVENT macro | |
#define DISPATCH_EVENT(eventName) \ | |
EventHandler::trigger(eventName) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "EventHandler.h" | |
#include <iostream> | |
REGISTER_EVENT_FUNC(kiss, []() { | |
std::cout << "Blow a kiss !" << std::endl; | |
}); | |
void hello() { | |
std::cout << "Hello World!" << std::endl; | |
} | |
int main() | |
{ | |
REGISTER_EVENT("hello", hello); | |
REGISTER_EVENT("goodbye", []() { | |
std::cout << "Good Bye!" << std::endl; | |
}); | |
std::string input; | |
while (true) { | |
std::cout << "Enter a string: "; | |
std::getline(std::cin, input); | |
if (input == "") { | |
break; | |
} | |
DISPATCH_EVENT(input); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment