RTXI  3.0.0
The Real-Time eXperiment Interface Reference Manual
event.hpp
Go to the documentation of this file.
1 /*
2  The Real-Time eXperiment Interface (RTXI)
3  Copyright (C) 2011 Georgia Institute of Technology, University of Utah,
4  Will Cornell Medical College
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19 */
20 
21 #ifndef EVENT_H
22 #define EVENT_H
23 
24 #include <any>
25 #include <atomic>
26 #include <condition_variable>
27 #include <list>
28 #include <mutex>
29 #include <queue>
30 #include <shared_mutex>
31 #include <string>
32 #include <thread>
33 #include <vector>
34 
35 class eventLogger;
36 
48 namespace Event
49 {
50 
54 enum Type
55 {
93  NOOP
94 };
95 
103 std::string type_to_string(Type event_type);
104 
105 // TODO: create a standardize way of generating events and their params
106 
120 class Object
121 {
122 public:
123  explicit Object(Event::Type et);
124  Object(const Object& obj); // copy constructor
125  Object& operator=(const Object& obj) = delete; // copy assignment operator
126  Object(Object&&) = delete; // move constructor
127  Object& operator=(Object&&) = delete; // move assignment operator
128  ~Object() = default;
129 
135  std::string getName();
136 
143  Event::Type getType() const;
144 
151  std::any getParam(const std::string& param_name) const;
152 
159  void setParam(const std::string& param_name, const std::any& param_value);
160 
173  void wait();
174 
184  void done();
185 
193  bool isdone() const;
194 
201  bool paramExists(const std::string& param_name);
202 
203 private:
204  struct param
205  {
206  std::string name;
207  std::any value;
208  };
209 
210  std::vector<param> params;
211  std::mutex processing_done_mut;
212  std::condition_variable processing_done_cond;
213  Type event_type;
214  bool processed = false;
215 }; // class Object
216 
227 class Handler
228 {
229 public:
230  Handler() = default;
231  Handler(const Handler&) = default;
232  Handler(Handler&&) = delete;
233  Handler& operator=(const Handler&) = default;
234  Handler& operator=(Handler&&) = delete;
235  virtual ~Handler() = default;
245  virtual void receiveEvent(Object* event) = 0;
246 }; // class Handler
247 
248 /*
249  * Managaes the collection of all objects waiting to
250  * receive signals from events.
251  *
252  * All event handlers must register themselves with the manager in order to
253  * receive future signals.
254  */
255 class Manager
256 {
257 public:
258  Manager();
259  Manager(const Manager& manager) = delete; // copy constructor
260  Manager& operator=(const Manager& manager) =
261  delete; // copy assignment operator
262  Manager(Manager&&) = delete; // move constructor
263  Manager& operator=(Manager&&) = delete; // move assignment operator
264  ~Manager();
265 
282  void postEvent(Object* event);
283 
295  void postEvent(std::vector<Object>& events);
296 
302  void registerHandler(Handler* handler);
303 
309  void unregisterHandler(Handler* handler);
310 
317  bool isRegistered(Handler* handler);
318 
333  eventLogger* getLogger() { return this->logger.get(); }
334 
335 private:
336  std::list<Handler*> handlerList;
337  std::queue<Event::Object*> event_q;
338  std::mutex event_mut; // Mutex for posting events
339  std::condition_variable available_event_cond;
340  std::atomic<bool> running = true;
341  std::thread event_thread;
342  std::vector<std::thread> thread_pool;
343 
344  // Shared mutex allows for multiple reader single writer scenarios.
345  std::shared_mutex handlerlist_mut; // Mutex for modifying event handler queue
346 
347  std::unique_ptr<eventLogger> logger;
348 }; // class Manager
349 
350 } // namespace Event
351 
352 #endif // EVENT_H
virtual ~Handler()=default
virtual void receiveEvent(Object *event)=0
Handler(Handler &&)=delete
Handler & operator=(const Handler &)=default
Handler & operator=(Handler &&)=delete
Handler()=default
Handler(const Handler &)=default
bool isRegistered(Handler *handler)
Definition: event.cpp:356
void unregisterHandler(Handler *handler)
Definition: event.cpp:346
void registerHandler(Handler *handler)
Definition: event.cpp:336
Manager(Manager &&)=delete
Manager & operator=(Manager &&)=delete
Manager & operator=(const Manager &manager)=delete
Manager(const Manager &manager)=delete
eventLogger * getLogger()
Definition: event.hpp:333
void postEvent(Object *event)
Definition: event.cpp:299
void setParam(const std::string &param_name, const std::any &param_value)
Definition: event.cpp:191
Object(Event::Type et)
Definition: event.cpp:154
~Object()=default
std::string getName()
Definition: event.cpp:165
void done()
Definition: event.cpp:215
bool paramExists(const std::string &param_name)
Definition: event.cpp:180
Object(Object &&)=delete
Event::Type getType() const
Definition: event.cpp:228
std::any getParam(const std::string &param_name) const
Definition: event.cpp:170
Object & operator=(Object &&)=delete
bool isdone() const
Definition: event.cpp:223
void wait()
Definition: event.cpp:207
Object & operator=(const Object &obj)=delete
Definition: event.hpp:49
std::string type_to_string(Type event_type)
Definition: event.cpp:30
Type
Definition: event.hpp:55
@ RT_WIDGET_PARAMETER_CHANGE_EVENT
Definition: event.hpp:68
@ RT_THREAD_PAUSE_EVENT
Definition: event.hpp:62
@ RT_GET_PERIOD_EVENT
Definition: event.hpp:59
@ THRESHOLD_CROSSING_EVENT
Definition: event.hpp:86
@ PLUGIN_INSERT_EVENT
Definition: event.hpp:77
@ STOP_RECORDING_EVENT
Definition: event.hpp:84
@ START_RECORDING_EVENT
Definition: event.hpp:83
@ RT_PERIOD_EVENT
Definition: event.hpp:56
@ RT_DEVICE_UNPAUSE_EVENT
Definition: event.hpp:67
@ SETTINGS_OBJECT_REMOVE_EVENT
Definition: event.hpp:81
@ DAQ_DEVICE_QUERY_EVENT
Definition: event.hpp:79
@ RT_PREPERIOD_EVENT
Definition: event.hpp:57
@ START_GENICAM_RECORDING_EVENT
Definition: event.hpp:87
@ RT_THREAD_UNPAUSE_EVENT
Definition: event.hpp:63
@ RT_WIDGET_STATE_CHANGE_EVENT
Definition: event.hpp:69
@ IO_LINK_INSERT_EVENT
Definition: event.hpp:71
@ ASYNC_DATA_EVENT
Definition: event.hpp:85
@ PLUGIN_REMOVE_EVENT
Definition: event.hpp:78
@ SETTINGS_OBJECT_INSERT_EVENT
Definition: event.hpp:80
@ GENICAM_SNAPSHOT_EVENT
Definition: event.hpp:90
@ IO_LINK_REMOVE_EVENT
Definition: event.hpp:72
@ RT_THREAD_INSERT_EVENT
Definition: event.hpp:60
@ NOOP
Definition: event.hpp:93
@ RT_DEVICE_PAUSE_EVENT
Definition: event.hpp:66
@ OPEN_FILE_EVENT
Definition: event.hpp:82
@ RT_DEVICE_INSERT_EVENT
Definition: event.hpp:64
@ RT_THREAD_REMOVE_EVENT
Definition: event.hpp:61
@ IO_BLOCK_QUERY_EVENT
Definition: event.hpp:73
@ IO_ALL_CONNECTIONS_QUERY_EVENT
Definition: event.hpp:76
@ IO_CONNECTION_QUERY_EVENT
Definition: event.hpp:75
@ RT_SHUTDOWN_EVENT
Definition: event.hpp:70
@ PAUSE_GENICAM_RECORDING_EVENT
Definition: event.hpp:88
@ GENERIC_WIDGET_EVENT
Definition: event.hpp:91
@ IO_BLOCK_OUTPUTS_QUERY_EVENT
Definition: event.hpp:74
@ RT_POSTPERIOD_EVENT
Definition: event.hpp:58
@ RT_DEVICE_REMOVE_EVENT
Definition: event.hpp:65
@ STOP_GENICAM_RECORDING_EVENT
Definition: event.hpp:89
@ MANAGER_SHUTDOWN_EVENT
Definition: event.hpp:92