54cd728bd6c375d513d85a273c68a9d5b293816c
[oweals/minetest.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()
47         {
48         }
49         void put(MtEvent *e)
50         {
51                 std::map<std::string, Dest>::iterator i = m_dest.find(e->getType());
52                 if(i != m_dest.end()){
53                         std::list<FuncSpec> &funcs = i->second.funcs;
54                         for(std::list<FuncSpec>::iterator i = funcs.begin();
55                                         i != funcs.end(); ++i){
56                                 (*(i->f))(e, i->d);
57                         }
58                 }
59                 delete e;
60         }
61         void reg(const char *type, event_receive_func f, void *data)
62         {
63                 std::map<std::string, Dest>::iterator i = m_dest.find(type);
64                 if(i != m_dest.end()){
65                         i->second.funcs.push_back(FuncSpec(f, data));
66                 } else{
67                         std::list<FuncSpec> funcs;
68                         Dest dest;
69                         dest.funcs.push_back(FuncSpec(f, data));
70                         m_dest[type] = dest;
71                 }
72         }
73         void dereg(const char *type, event_receive_func f, void *data)
74         {
75                 if(type != NULL){
76                         std::map<std::string, Dest>::iterator i = m_dest.find(type);
77                         if(i != m_dest.end()){
78                                 std::list<FuncSpec> &funcs = i->second.funcs;
79                                 std::list<FuncSpec>::iterator j = funcs.begin();
80                                 while(j != funcs.end()){
81                                         bool remove = (j->f == f && (!data || j->d == data));
82                                         if(remove)
83                                                 funcs.erase(j++);
84                                         else
85                                                 ++j;
86                                 }
87                         }
88                 } else{
89                         for(std::map<std::string, Dest>::iterator
90                                         i = m_dest.begin(); i != m_dest.end(); ++i){
91                                 std::list<FuncSpec> &funcs = i->second.funcs;
92                                 std::list<FuncSpec>::iterator j = funcs.begin();
93                                 while(j != funcs.end()){
94                                         bool remove = (j->f == f && (!data || j->d == data));
95                                         if(remove)
96                                                 funcs.erase(j++);
97                                         else
98                                                 ++j;
99                                 }
100                         }
101                 }
102         }
103         void reg(MtEventReceiver *r, const char *type)
104         {
105                 reg(type, EventManager::receiverReceive, r);
106         }
107         void dereg(MtEventReceiver *r, const char *type)
108         {
109                 dereg(type, EventManager::receiverReceive, r);
110         }
111 };