Программы на C++. Форум разработчиков
Anonymous
Обработка событий с использованием шаблонов
Сообщение
Anonymous » 24 июн 2025, 19:47
в последнее время я играл с новым стандартом C ++ 11 и решил создать базовую систему обработки событий. Приведенный ниже код приведен небольшой пример моей текущей реализации. < /P>
Код: Выделить всё
#include
#include
#include
template
class EventBroadcaster
{
public:
typedef std::function Connection;
void connect(Connection&& connection)
{
connections.push_back(std::move(connection));
}
void signal(const Event& event)
{
for (const auto& connection : connections)
{
connection(event);
}
}
private:
std::vector connections;
};
struct MouseMotion
{
int x = 0;
int y = 0;
};
class Input : public EventBroadcaster
{
public:
void process()
{
MouseMotion mouseMotion;
mouseMotion.x = 10;
mouseMotion.y = 20;
signal(mouseMotion);
}
};
int main()
{
int x = 0;
int y = 0;
Input input;
input.connect([&](const MouseMotion& e){
x += e.x;
y += e.y;
});
input.process();
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/15028609/event-handling-using-templates[/url]
1750783673
Anonymous
в последнее время я играл с новым стандартом C ++ 11 и решил создать базовую систему обработки событий. Приведенный ниже код приведен небольшой пример моей текущей реализации. < /P> [code]#include #include #include template class EventBroadcaster { public: typedef std::function Connection; void connect(Connection&& connection) { connections.push_back(std::move(connection)); } void signal(const Event& event) { for (const auto& connection : connections) { connection(event); } } private: std::vector connections; }; struct MouseMotion { int x = 0; int y = 0; }; class Input : public EventBroadcaster { public: void process() { MouseMotion mouseMotion; mouseMotion.x = 10; mouseMotion.y = 20; signal(mouseMotion); } }; int main() { int x = 0; int y = 0; Input input; input.connect([&](const MouseMotion& e){ x += e.x; y += e.y; }); input.process(); std::cout Подробнее здесь: [url]https://stackoverflow.com/questions/15028609/event-handling-using-templates[/url]