zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Event.h
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2  * Copyright (C) 2011 Mitchell Keith Bloch (bazald).
3  *
4  * zenilib is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * zenilib is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with zenilib. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef ZENI_EVENT_H
19 #define ZENI_EVENT_H
20 
21 #include <Zeni/Hash_Map.h>
22 
23 namespace Zeni {
24 
25  class ZENI_DLL Event {
26  public:
27  class ZENI_DLL Handler {
28  public:
29  virtual ~Handler() {}
30 
31  virtual void operator()() = 0;
32 
33  virtual Handler * duplicate() const = 0;
34  };
35 
37 
38  ~Event() {
39  clear();
40  }
41 
42  void lend_Handler(Handler * const &handler) {
43  m_handlers[handler] = false;
44  }
45 
46  void give_Handler(Handler * const &handler) {
47  m_handlers[handler] = true;
48  }
49 
50  void fax_Handler(Handler * const &handler) {
51  give_Handler(handler->duplicate());
52  }
53 
54  void remove_Handler(Handler * const &handler) {
55  Handlers::iterator ht = m_handlers.find(handler);
56 
57  if(ht != m_handlers.end()) {
58  if(ht->second)
59  delete ht->first;
60 
61  m_handlers.erase(ht);
62  }
63  }
64 
65  void fire() {
66  const Handlers m_copy = m_handlers;
67  for(Handlers::const_iterator ht = m_copy.begin(), hend = m_copy.end(); ht != hend; ++ht)
68  (*ht->first)();
69  }
70 
71  void clear() {
72  for(Handlers::const_iterator ht = m_handlers.begin(), hend = m_handlers.end(); ht != hend; ++ht)
73  if(ht->second)
74  delete ht->first;
75 
76  m_handlers.clear();
77  }
78 
79  private:
80 #ifdef _WINDOWS
81 #pragma warning( push )
82 #pragma warning( disable : 4251 )
83 #endif
84  Handlers m_handlers;
85 #ifdef _WINDOWS
86 #pragma warning( pop )
87 #endif
88  };
89 
90 }
91 
92 #endif
void give_Handler(Handler *const &handler)
Definition: Event.h:46
void clear()
Definition: Event.h:71
~Event()
Definition: Event.h:38
virtual Handler * duplicate() const =0
void fax_Handler(Handler *const &handler)
Definition: Event.h:50
Unordered_Map< Handler *, bool > Handlers
Definition: Event.h:36
void fire()
Definition: Event.h:65
virtual ~Handler()
Definition: Event.h:29
void remove_Handler(Handler *const &handler)
Definition: Event.h:54
void lend_Handler(Handler *const &handler)
Definition: Event.h:42