LCOV - code coverage report
Current view: top level - src - event_manager.h (source / functions) Hit Total Coverage
Test: report Lines: 25 57 43.9 %
Date: 2015-07-11 18:23:49 Functions: 10 14 71.4 %

          Line data    Source code
       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             : #ifndef EVENT_MANAGER_HEADER
      21             : #define EVENT_MANAGER_HEADER
      22             : 
      23             : #include "event.h"
      24             : #include <list>
      25             : #include <map>
      26             : 
      27           1 : class EventManager: public MtEventManager
      28             : {
      29           0 :         static void receiverReceive(MtEvent *e, void *data)
      30             :         {
      31           0 :                 MtEventReceiver *r = (MtEventReceiver*)data;
      32           0 :                 r->onEvent(e);
      33           0 :         }
      34             :         struct FuncSpec{
      35             :                 event_receive_func f;
      36             :                 void *d;
      37           8 :                 FuncSpec(event_receive_func f, void *d):
      38           8 :                         f(f), d(d)
      39           8 :                 {}
      40             :         };
      41          72 :         struct Dest{
      42             :                 std::list<FuncSpec> funcs;
      43             :         };
      44             :         std::map<std::string, Dest> m_dest;
      45             : 
      46             : public:
      47           2 :         ~EventManager()
      48           1 :         {
      49           2 :         }
      50          38 :         void put(MtEvent *e)
      51             :         {
      52          38 :                 std::map<std::string, Dest>::iterator i = m_dest.find(e->getType());
      53          38 :                 if(i != m_dest.end()){
      54          38 :                         std::list<FuncSpec> &funcs = i->second.funcs;
      55         228 :                         for(std::list<FuncSpec>::iterator i = funcs.begin();
      56         152 :                                         i != funcs.end(); i++){
      57          38 :                                 (*(i->f))(e, i->d);
      58             :                         }
      59             :                 }
      60          38 :                 delete e;
      61          38 :         }
      62           8 :         void reg(const char *type, event_receive_func f, void *data)
      63             :         {
      64           8 :                 std::map<std::string, Dest>::iterator i = m_dest.find(type);
      65           8 :                 if(i != m_dest.end()){
      66           0 :                         i->second.funcs.push_back(FuncSpec(f, data));
      67             :                 } else{
      68          16 :                         std::list<FuncSpec> funcs;
      69          16 :                         Dest dest;
      70           8 :                         dest.funcs.push_back(FuncSpec(f, data));
      71           8 :                         m_dest[type] = dest;
      72             :                 }
      73           8 :         }
      74           0 :         void dereg(const char *type, event_receive_func f, void *data)
      75             :         {
      76           0 :                 if(type != NULL){
      77           0 :                         std::map<std::string, Dest>::iterator i = m_dest.find(type);
      78           0 :                         if(i != m_dest.end()){
      79           0 :                                 std::list<FuncSpec> &funcs = i->second.funcs;
      80           0 :                                 std::list<FuncSpec>::iterator j = funcs.begin();
      81           0 :                                 while(j != funcs.end()){
      82           0 :                                         bool remove = (j->f == f && (!data || j->d == data));
      83           0 :                                         if(remove)
      84           0 :                                                 funcs.erase(j++);
      85             :                                         else
      86           0 :                                                 j++;
      87             :                                 }
      88             :                         }
      89             :                 } else{
      90           0 :                         for(std::map<std::string, Dest>::iterator
      91           0 :                                         i = m_dest.begin(); i != m_dest.end(); i++){
      92           0 :                                 std::list<FuncSpec> &funcs = i->second.funcs;
      93           0 :                                 std::list<FuncSpec>::iterator j = funcs.begin();
      94           0 :                                 while(j != funcs.end()){
      95           0 :                                         bool remove = (j->f == f && (!data || j->d == data));
      96           0 :                                         if(remove)
      97           0 :                                                 funcs.erase(j++);
      98             :                                         else
      99           0 :                                                 j++;
     100             :                                 }
     101             :                         }
     102             :                 }
     103           0 :         }
     104           0 :         void reg(MtEventReceiver *r, const char *type)
     105             :         {
     106           0 :                 reg(type, EventManager::receiverReceive, r);
     107           0 :         }
     108           0 :         void dereg(MtEventReceiver *r, const char *type)
     109             :         {
     110           0 :                 dereg(type, EventManager::receiverReceive, r);
     111           0 :         }
     112             : };
     113             : 
     114             : #endif
     115             : 

Generated by: LCOV version 1.11