AnCH Framework  0.1
Another C++ Hack Framework
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
eventHandler.hpp
1 /*
2  ANCH Framework: ANother C++ Hack is a C++ framework based on C++11 standard
3  Copyright (C) 2012 Vincent Lachenal
4 
5  This file is part of ANCH Framework.
6 
7  ANCH Framework is free software: you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  ANCH Framework is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with ANCH Framework. If not, see <http://www.gnu.org/licenses/>.
19 */
20 #ifndef _ANCH_EVENTS_EVENT_HANDLER_H_
21 #define _ANCH_EVENTS_EVENT_HANDLER_H_
22 
23 #include "events/eventBus.hpp"
24 
25 
26 namespace anch {
27  namespace events {
28 
38  template<typename Event, typename Derived>
39  class EventHandler: public Observer<Event> {
40  private:
42  std::function<void(Derived&,const Event&) noexcept> _callbackFunction;
43 
46 
47  public:
56  EventHandler(const std::function<void(Derived&,const Event&) noexcept>& callbackFunction,
57  bool useEventBus = false) {
58  _callbackFunction = callbackFunction;
59  if(useEventBus) {
61  eventBus.addObserver(*this);
62  _eventBus = &eventBus;
63  } else {
64  _eventBus = NULL;
65  }
66  }
67 
76  EventHandler(const std::function<void(Derived&,const Event&) noexcept>& callbackFunction,
78  _callbackFunction = callbackFunction;
79  eventBus.addObserver(*this);
80  _eventBus = &eventBus;
81  }
82 
87  virtual ~EventHandler() {
88  if(_eventBus != NULL) {
89  _eventBus->removeObserver(*this);
90  }
91  }
92 
93  public:
99  virtual void notify(const Event& event) noexcept {
100  _callbackFunction(*static_cast<Derived*>(this), event);
101  }
102 
103  };
104 
105  }
106 }
107 
108 #endif //_ANCH_EVENTS_EVENT_HANDLER_H_
An observer interface of the observers/observable design pattern.
Definition: observer.hpp:39
void removeObserver(anch::events::Observer< Event > &observer) noexcept
Definition: eventBus.hpp:118
EventHandler(const std::function< void(Derived &, const Event &) noexcept > &callbackFunction, anch::events::EventBus< Event > &eventBus)
Definition: eventHandler.hpp:76
virtual void notify(const Event &event) noexcept
Definition: eventHandler.hpp:99
A class which manage global events firing and events QoS.
Definition: eventBus.hpp:46
EventHandler(const std::function< void(Derived &, const Event &) noexcept > &callbackFunction, bool useEventBus=false)
Definition: eventHandler.hpp:56
AnCH framework base namespace.
Definition: base64.hpp:28
virtual ~EventHandler()
Definition: eventHandler.hpp:87
An event handler.
Definition: eventHandler.hpp:39
static EventBus< Event > & getInstance()
Definition: singleton.hpp:42
bool addObserver(anch::events::Observer< Event > &observer) noexcept
Definition: eventBus.hpp:98