rtmlib
event.h
Go to the documentation of this file.
1 /*
2  * rtmlib is a Real-Time Monitoring Library.
3  *
4  * Copyright (C) 2018-2020 AndrĂ© Pedro
5  *
6  * This file is part of rtmlib.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef _MONITOR_EVENT_H_
23 #define _MONITOR_EVENT_H_
24 
25 #include <time.h>
26 
27 #include "debug_compat.h"
28 #include "time_compat.h"
29 
38 template <typename T> class Event {
39 private:
41  T data;
43  timespan time;
44 
45 public:
46  typedef T data_t;
47 
53  Event();
54 
63  Event(const T &data, const timespan &time);
64 
70  const T &getData() const;
71 
77  const timespan &getTime() const;
78 
82  void set(T &, timespan &);
83 
87  void setTime(timespan &);
88 
92  void setData(T &);
93 
97  void debug() const;
98 
104  Event<T> &operator=(const Event<T> *event);
105 
112  bool operator<(const Event &event) const;
113 
120  bool operator<=(const Event &event) const;
121 
128  bool operator>(const Event &event) const;
129 
136  bool operator>=(const Event &event) const;
137 
144  bool operator==(const Event &event) const;
145 
152  bool operator!=(const Event &event) const;
153 
160  bool operator<(const timespan &time) const;
161 
168  bool operator<=(const timespan &time) const;
169 
176  bool operator>(const timespan &time) const;
177 
184  bool operator>=(const timespan &time) const;
185 
192  bool operator==(const timespan &time) const;
193 
200  bool operator!=(const timespan &time) const;
201 
210  template <typename D>
211  friend bool operator<(const timespan &time, const Event<D> &event);
212 
221  template <typename D>
222  friend bool operator<=(const timespan &time, const Event<D> &event);
223 
232  template <typename D>
233  friend bool operator>(const timespan &time, const Event<D> &event);
234 
243  template <typename D>
244  friend bool operator>=(const timespan &time, const Event<D> &event);
245 
254  template <typename D>
255  friend bool operator==(const timespan &time, const Event<D> &event);
256 
265  template <typename D>
266  friend bool operator!=(const timespan &time, const Event<D> &event);
267 };
268 
269 template <typename T> Event<T>::Event() : data(), time(0) {}
270 
271 template <typename T>
272 Event<T>::Event(const T &ddata, const timespan &ttime)
273  : data(ddata), time(ttime) {}
274 
275 template <typename T> const T &Event<T>::getData() const { return data; }
276 
277 template <typename T> const timespan &Event<T>::getTime() const { return time; }
278 
279 template <typename T> void Event<T>::set(T &d, timespan &t) {
280  time = t;
281  data = d;
282 }
283 
284 template <typename T> void Event<T>::setTime(timespan &t) { time = t; }
285 
286 template <typename T> void Event<T>::setData(T &d) { data = d; }
287 
288 template <typename T> void Event<T>::debug() const {
289  DEBUGV3_APPEND("%d,%lu; ", getData(), getTime());
290 }
291 
292 template <typename T> Event<T> &Event<T>::operator=(const Event *event) {
293  data = event->data;
294  time = event->time;
295  return *this;
296 }
297 
298 template <typename T> bool Event<T>::operator<(Event const &event) const {
299  return time < event.time;
300 }
301 
302 template <typename T> bool Event<T>::operator<=(Event const &event) const {
303  return time <= event.time;
304 }
305 
306 template <typename T> bool Event<T>::operator>(Event const &event) const {
307  return time > event.time;
308 }
309 
310 template <typename T> bool Event<T>::operator>=(Event const &event) const {
311  return time >= event.time;
312 }
313 
314 template <typename T> bool Event<T>::operator==(Event<T> const &event) const {
315  return time == event.time && data == event.data;
316 }
317 
318 template <typename T> bool Event<T>::operator!=(Event<T> const &event) const {
319  return time != event.time;
320 }
321 
322 template <typename T> bool Event<T>::operator<(const timespan &ttime) const {
323  return this->time < ttime;
324 }
325 
326 template <typename T> bool Event<T>::operator<=(const timespan &ttime) const {
327  return this->time <= ttime;
328 }
329 
330 template <typename T> bool Event<T>::operator>(const timespan &ttime) const {
331  return this->time > ttime;
332 }
333 
334 template <typename T> bool Event<T>::operator>=(const timespan &ttime) const {
335  return this->time >= ttime;
336 }
337 
338 template <typename T> bool Event<T>::operator==(const timespan &ttime) const {
339  return this->time == ttime;
340 }
341 
342 template <typename T> bool Event<T>::operator!=(const timespan &ttime) const {
343  return this->time != ttime;
344 }
345 
346 template <typename T>
347 bool operator<(const timespan &ttime, const Event<T> &event) {
348  return ttime < event.time;
349 }
350 
351 template <typename T>
352 bool operator<=(const timespan &ttime, const Event<T> &event) {
353  return ttime <= event.time;
354 }
355 
356 template <typename T>
357 bool operator>(const timespan &ttime, const Event<T> &event) {
358  return ttime > event.time;
359 }
360 
361 template <typename T>
362 bool operator>=(const timespan &ttime, const Event<T> &event) {
363  return ttime >= event.time;
364 }
365 
366 template <typename T>
367 bool operator==(const timespan &ttime, const Event<T> &event) {
368  return ttime == event.time;
369 }
370 
371 template <typename T>
372 bool operator!=(const timespan &ttime, const Event<T> &event) {
373  return ttime != event.time;
374 }
375 #endif //_MONITOR_EVENT_H_
void setData(T &)
Definition: event.h:286
void set(T &, timespan &)
Definition: event.h:279
bool operator>(const Event &event) const
Definition: event.h:306
bool operator<=(const Event &event) const
Definition: event.h:302
void debug() const
Definition: event.h:288
bool operator==(const timespan &ttime, const Event< T > &event)
Definition: event.h:367
bool operator<=(const timespan &ttime, const Event< T > &event)
Definition: event.h:352
Event()
Definition: event.h:269
T data_t
Definition: event.h:46
bool operator>(const timespan &ttime, const Event< T > &event)
Definition: event.h:357
void setTime(timespan &)
Definition: event.h:284
bool operator!=(const Event &event) const
Definition: event.h:318
bool operator!=(const timespan &ttime, const Event< T > &event)
Definition: event.h:372
bool operator>=(const timespan &ttime, const Event< T > &event)
Definition: event.h:362
const timespan & getTime() const
Definition: event.h:277
T data
Definition: event.h:41
Event< T > & operator=(const Event< T > *event)
Definition: event.h:292
bool operator<(const timespan &ttime, const Event< T > &event)
Definition: event.h:347
Definition: event.h:38
#define DEBUGV3_APPEND(...)
Definition: debug_compat.h:86
timespan time
Definition: event.h:43
const T & getData() const
Definition: event.h:275
bool operator>=(const Event &event) const
Definition: event.h:310
bool operator==(const Event &event) const
Definition: event.h:314
bool operator<(const Event &event) const
Definition: event.h:298