libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
IEvents.h
Go to the documentation of this file.
1 /* Copyright 2013-2019 Homegear GmbH
2  *
3  * libhomegear-base is free software: you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public License as
5  * published by the Free Software Foundation, either version 3 of the
6  * License, or (at your option) any later version.
7  *
8  * libhomegear-base is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with libhomegear-base. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  * In addition, as a special exception, the copyright holders give
18  * permission to link the code of portions of this program with the
19  * OpenSSL library under certain conditions as described in each
20  * individual source file, and distribute linked combinations
21  * including the two.
22  * You must obey the GNU Lesser General Public License in all respects
23  * for all of the code used other than OpenSSL. If you modify
24  * file(s) with this exception, you may extend this exception to your
25  * version of the file(s), but you are not obligated to do so. If you
26  * do not wish to do so, delete this exception statement from your
27  * version. If you delete this exception statement from all source
28  * files in the program, then also delete it here.
29 */
30 
31 #ifndef IEVENTS_H_
32 #define IEVENTS_H_
33 
34 #include "Output/Output.h"
35 
36 #include <atomic>
37 #include <forward_list>
38 #include <mutex>
39 #include <unordered_map>
40 
41 namespace BaseLib {
42 
43 class IEventSinkBase;
44 
45 class EventHandler {
46  public:
47  EventHandler(int32_t id);
48  EventHandler(int32_t id, IEventSinkBase *handler);
49  virtual ~EventHandler();
50 
51  int32_t id();
52  int32_t useCount();
54  void lock();
55  void unlock();
56  void invalidate();
57  private:
58  int32_t _id;
59  std::atomic<int32_t> _useCount;
60  IEventSinkBase *_handler = nullptr;
61 };
62 
63 typedef std::shared_ptr<EventHandler> PEventHandler;
64 typedef std::unordered_map<IEventSinkBase *, PEventHandler> EventHandlers;
65 
67  public:
69  virtual ~IEventSinkBase() {};
70 };
71 
72 class IEvents {
73  public:
74  IEvents();
75  virtual ~IEvents();
76 
77  virtual void setEventHandler(IEventSinkBase *eventHandler);
78  virtual void resetEventHandler();
79  virtual IEventSinkBase *getEventHandler();
80  protected:
81  IEventSinkBase *_eventHandler = nullptr;
82  private:
83  IEvents(const IEvents &);
84  IEvents &operator=(const IEvents &);
85 };
86 
87 class IEventsEx {
88  public:
89  IEventsEx();
90  virtual ~IEventsEx();
91 
92  virtual PEventHandler addEventHandler(IEventSinkBase *eventHandler);
93  virtual std::vector<PEventHandler> addEventHandlers(EventHandlers eventHandlers);
94  virtual void removeEventHandler(PEventHandler eventHandler);
95  virtual EventHandlers getEventHandlers();
96  protected:
97  int32_t _currentId = 0;
98  std::mutex _eventHandlerMutex;
99  EventHandlers _eventHandlers;
100  private:
101  IEventsEx(const IEventsEx &);
102  IEventsEx &operator=(const IEventsEx &);
103 };
104 }
105 #endif
IEventSinkBase * handler()
Definition: IEvents.cpp:58
EventHandlers _eventHandlers
Definition: IEvents.h:99
Definition: IEvents.h:72
std::shared_ptr< EventHandler > PEventHandler
Definition: IEvents.h:63
Definition: BaseLib.cpp:34
IEventSinkBase()
Definition: IEvents.h:68
std::mutex _eventHandlerMutex
Definition: IEvents.h:98
int32_t id()
Definition: IEvents.cpp:50
void unlock()
Definition: IEvents.cpp:66
void invalidate()
Definition: IEvents.cpp:70
void lock()
Definition: IEvents.cpp:62
Definition: IEvents.h:87
virtual ~IEventSinkBase()
Definition: IEvents.h:69
int32_t useCount()
Definition: IEvents.cpp:54
std::unordered_map< IEventSinkBase *, PEventHandler > EventHandlers
Definition: IEvents.h:64
virtual ~EventHandler()
Definition: IEvents.cpp:47
Definition: IEvents.h:66
EventHandler(int32_t id)
Definition: IEvents.cpp:36
Definition: IEvents.h:45