Luanti 5.11.0-dev
 
Loading...
Searching...
No Matches
event_manager.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include "mtevent.h"
8#include <list>
9#include <map>
10
12{
13 static void receiverReceive(MtEvent *e, void *data)
14 {
16 r->onEvent(e);
17 }
18 struct FuncSpec
19 {
21 void *d;
22 FuncSpec(event_receive_func f, void *d) : f(f), d(d) {}
23 };
24
25 struct Dest
26 {
27 std::list<FuncSpec> funcs{};
28 };
29 std::map<MtEvent::Type, Dest> m_dest{};
30
31public:
32 ~EventManager() override = default;
33
34 void put(MtEvent *e) override
35 {
36 std::map<MtEvent::Type, Dest>::iterator i = m_dest.find(e->getType());
37 if (i != m_dest.end()) {
38 std::list<FuncSpec> &funcs = i->second.funcs;
39 for (FuncSpec &func : funcs) {
40 (*(func.f))(e, func.d);
41 }
42 }
43 delete e;
44 }
45 void reg(MtEvent::Type type, event_receive_func f, void *data) override
46 {
47 std::map<MtEvent::Type, Dest>::iterator i = m_dest.find(type);
48 if (i != m_dest.end()) {
49 i->second.funcs.emplace_back(f, data);
50 } else {
51 Dest dest;
52 dest.funcs.emplace_back(f, data);
53 m_dest[type] = dest;
54 }
55 }
56 void dereg(MtEvent::Type type, event_receive_func f, void *data) override
57 {
58 auto i = m_dest.find(type);
59 if (i != m_dest.end()) {
60 std::list<FuncSpec> &funcs = i->second.funcs;
61 for (auto j = funcs.begin(); j != funcs.end(); ) {
62 bool remove = (j->f == f && (!data || j->d == data));
63 if (remove)
64 j = funcs.erase(j);
65 else
66 ++j;
67 }
68 }
69 }
70};
static void remove(TestMap &map, size_t offset, size_t count)
Definition benchmark_mapmodify.cpp:32
Definition event_manager.h:12
~EventManager() override=default
static void receiverReceive(MtEvent *e, void *data)
Definition event_manager.h:13
void reg(MtEvent::Type type, event_receive_func f, void *data) override
Definition event_manager.h:45
std::map< MtEvent::Type, Dest > m_dest
Definition event_manager.h:29
void put(MtEvent *e) override
Definition event_manager.h:34
void dereg(MtEvent::Type type, event_receive_func f, void *data) override
Definition event_manager.h:56
Definition mtevent.h:49
Definition mtevent.h:40
virtual void onEvent(MtEvent *e)=0
Definition mtevent.h:10
Type
Definition mtevent.h:13
virtual Type getType() const =0
void(* event_receive_func)(MtEvent *e, void *data)
Definition mtevent.h:46
Definition event_manager.h:26
std::list< FuncSpec > funcs
Definition event_manager.h:27
Definition event_manager.h:19
event_receive_func f
Definition event_manager.h:20
FuncSpec(event_receive_func f, void *d)
Definition event_manager.h:22
void * d
Definition event_manager.h:21