]> git.lizzy.rs Git - dragonfireclient.git/blob - src/event_manager.h
Fix OSX builds caused by __WORDSIZE again (#6307)
[dragonfireclient.git] / src / event_manager.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 #include "event.h"
23 #include <list>
24 #include <map>
25
26 class EventManager: public MtEventManager
27 {
28         static void receiverReceive(MtEvent *e, void *data)
29         {
30                 MtEventReceiver *r = (MtEventReceiver*)data;
31                 r->onEvent(e);
32         }
33         struct FuncSpec{
34                 event_receive_func f;
35                 void *d;
36                 FuncSpec(event_receive_func f, void *d):
37                         f(f), d(d)
38                 {}
39         };
40         struct Dest{
41                 std::list<FuncSpec> funcs;
42         };
43         std::map<std::string, Dest> m_dest;
44
45 public:
46         ~EventManager() = default;
47
48         void put(MtEvent *e)
49         {
50                 std::map<std::string, Dest>::iterator i = m_dest.find(e->getType());
51                 if(i != m_dest.end()){
52                         std::list<FuncSpec> &funcs = i->second.funcs;
53                         for (FuncSpec &func : funcs) {
54                                 (*(func.f))(e, func.d);
55                         }
56                 }
57                 delete e;
58         }
59         void reg(const char *type, event_receive_func f, void *data)
60         {
61                 std::map<std::string, Dest>::iterator i = m_dest.find(type);
62                 if(i != m_dest.end()){
63                         i->second.funcs.emplace_back(f, data);
64                 } else{
65                         std::list<FuncSpec> funcs;
66                         Dest dest;
67                         dest.funcs.emplace_back(f, data);
68                         m_dest[type] = dest;
69                 }
70         }
71         void dereg(const char *type, event_receive_func f, void *data)
72         {
73                 if(type != NULL){
74                         std::map<std::string, Dest>::iterator i = m_dest.find(type);
75                         if(i != m_dest.end()){
76                                 std::list<FuncSpec> &funcs = i->second.funcs;
77                                 std::list<FuncSpec>::iterator j = funcs.begin();
78                                 while(j != funcs.end()){
79                                         bool remove = (j->f == f && (!data || j->d == data));
80                                         if(remove)
81                                                 funcs.erase(j++);
82                                         else
83                                                 ++j;
84                                 }
85                         }
86                 } else{
87                         for (auto &dest : m_dest) {
88                                 std::list<FuncSpec> &funcs = dest.second.funcs;
89                                 std::list<FuncSpec>::iterator j = funcs.begin();
90                                 while(j != funcs.end()){
91                                         bool remove = (j->f == f && (!data || j->d == data));
92                                         if(remove)
93                                                 funcs.erase(j++);
94                                         else
95                                                 ++j;
96                                 }
97                         }
98                 }
99         }
100         void reg(MtEventReceiver *r, const char *type)
101         {
102                 reg(type, EventManager::receiverReceive, r);
103         }
104         void dereg(MtEventReceiver *r, const char *type)
105         {
106                 dereg(type, EventManager::receiverReceive, r);
107         }
108 };