Libmacro  0.2
Libmacro is an extensible macro and hotkey library.
alarm.h
Go to the documentation of this file.
1 /* Libmacro - A multi-platform, extendable macro and hotkey C library
2  Copyright (C) 2013 Jonathan D. Pelletier
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library 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 GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
23 #ifndef __cplusplus
24  #pragma message "C++ support is required for extras module"
25  #include "mcr/err.h"
26 #endif
27 
28 #ifndef MCR_EXTRAS_TRIGGERS_ALARM_H_
29 #define MCR_EXTRAS_TRIGGERS_ALARM_H_
30 
32 
33 namespace mcr
34 {
36 class MCR_API Alarm : public ITriggerMember
37 {
38 public:
39  tm time;
40 
41  Alarm()
42  : time()
43  {
44  }
45  Alarm(const Alarm &) = default;
46  Alarm &operator =(const Alarm &) = default;
47 
52  static inline Alarm *data(mcr_Signal *sigPt)
53  {
54  return (Alarm *)(sigPt ? sigPt->instance.data_member.data : NULL);
55  }
56 
61  inline int &sec()
62  {
63  return time.tm_sec;
64  }
69  inline int sec() const
70  {
71  return time.tm_sec;
72  }
77  inline int &min()
78  {
79  return time.tm_min;
80  }
85  inline int min() const
86  {
87  return time.tm_min;
88  }
93  inline int &hour()
94  {
95  return time.tm_hour;
96  }
101  inline int hour() const
102  {
103  return time.tm_hour;
104  }
109  inline int &mday()
110  {
111  return time.tm_mday;
112  }
117  inline int mday() const
118  {
119  return time.tm_mday;
120  }
125  inline int &mon()
126  {
127  return time.tm_mon;
128  }
133  inline int mon() const
134  {
135  return time.tm_mon;
136  }
138  inline int &year()
139  {
140  return time.tm_year;
141  }
143  inline int year() const
144  {
145  return time.tm_year;
146  }
151  inline int &wday()
152  {
153  return time.tm_wday;
154  }
159  inline int wday() const
160  {
161  return time.tm_wday;
162  }
167  inline int &yday()
168  {
169  return time.tm_yday;
170  }
175  inline int yday() const
176  {
177  return time.tm_yday;
178  }
183  inline int &isdst()
184  {
185  return time.tm_isdst;
186  }
191  inline int isdst() const
192  {
193  return time.tm_isdst;
194  }
195 
197  virtual int compare(const ISignalMember &rhs) const override
198  {
199  return compare(dynamic_cast<const Alarm &>(rhs));
200  }
202  inline int compare(const Alarm &rhs) const
203  {
204  tm lMem = time, rMem = rhs.time;
205  time_t lT = ::mktime(&lMem), rT = ::mktime(&rMem);
206  return lT < rT ? -1 : rT > lT;
207  }
211  virtual void copy(const ISignalMember *copytron) override;
213  virtual const char *name() const override
214  {
215  return "Alarm";
216  }
218  virtual inline void send() override
219  {
220  auto until_time = std::chrono::system_clock::from_time_t(::mktime(&time));
221  std::this_thread::sleep_until(until_time);
222  }
223 
224  void zero()
225  {
226  std::memset(&time, 0, sizeof(time));
227  }
228  void now()
229  {
230  time_t t = std::chrono::system_clock::to_time_t(
231  std::chrono::system_clock::now());
232  time = *::localtime(&t);
233  }
234 };
235 
237 class MCR_API AlarmRef : public SignalManager
238 {
239 public:
240  AlarmRef(Libmacro *context = NULL, mcr_Signal *sigPt = NULL);
241 
242  inline const Alarm *data() const
243  {
244  return SignalManager::data<Alarm>();
245  }
246  inline Alarm *data()
247  {
248  return SignalManager::data<Alarm>();
249  }
250 
251  inline tm time() const
252  {
253  if (data())
254  return data()->time;
255  return tm();
256  }
257  inline void setTime(tm &val)
258  {
259  mkdata();
260  data()->time = val;
261  }
262 
263  inline int sec() const
264  {
265  if (data())
266  return data()->sec();
267  return 0;
268  }
269  inline void setSec(int val)
270  {
271  mkdata();
272  data()->sec() = val;
273  }
274 
275  inline int min() const
276  {
277  if (data())
278  return data()->min();
279  return 0;
280  }
281  inline void setMin(int val)
282  {
283  mkdata();
284  data()->min() = val;
285  }
286 
287  inline int hour() const
288  {
289  if (data())
290  return data()->hour();
291  return 0;
292  }
293  inline void setHour(int val)
294  {
295  mkdata();
296  data()->hour() = val;
297  }
298 
299  inline int mday() const
300  {
301  if (data())
302  return data()->mday();
303  return 0;
304  }
305  inline void setMday(int val)
306  {
307  mkdata();
308  data()->mday() = val;
309  }
310 
311  inline int mon() const
312  {
313  if (data())
314  return data()->mon();
315  return 0;
316  }
317  inline void setMon(int val)
318  {
319  mkdata();
320  data()->mon() = val;
321  }
322 
323  inline int year() const
324  {
325  if (data())
326  return data()->year();
327  return 0;
328  }
329  inline void setYear(int val)
330  {
331  mkdata();
332  data()->year() = val;
333  }
334 
335  inline int wday() const
336  {
337  if (data())
338  return data()->wday();
339  return 0;
340  }
341  inline void setWday(int val)
342  {
343  mkdata();
344  data()->wday() = val;
345  }
346 
347  inline int yday() const
348  {
349  if (data())
350  return data()->yday();
351  return 0;
352  }
353  inline void setYday(int val)
354  {
355  mkdata();
356  data()->yday() = val;
357  }
358 
359  inline int isdst() const
360  {
361  if (data())
362  return data()->isdst();
363  return 0;
364  }
365  inline void setIsdst(int val)
366  {
367  mkdata();
368  data()->isdst() = val;
369  }
370 };
371 }
372 
373 #endif
int & year()
Definition: alarm.h:138
int yday() const
Definition: alarm.h:175
int wday() const
Definition: alarm.h:159
int compare(const Alarm &rhs) const
Definition: alarm.h:202
virtual const char * name() const override
Definition: alarm.h:213
int year() const
Definition: alarm.h:143
int & yday()
Definition: alarm.h:167
static Alarm * data(mcr_Signal *sigPt)
Definition: alarm.h:52
virtual int compare(const ISignalMember &rhs) const override
Definition: alarm.h:197
int mday() const
Definition: alarm.h:117
int & mday()
Definition: alarm.h:109
int & min()
Definition: alarm.h:77
int & isdst()
Definition: alarm.h:183
int & mon()
Definition: alarm.h:125
Raise a compiler error. Usage: #include "mcr/err.h"
Libmacro, by Jonathan Pelletier, New Paradigm Software. Alpha version.
Definition: classes.h:31
int hour() const
Definition: alarm.h:101
int mon() const
Definition: alarm.h:133
struct mcr_DataMember data_member
Definition: instance.h:41
virtual void send() override
Definition: alarm.h:218
int & hour()
Definition: alarm.h:93
int & sec()
Definition: alarm.h:61
ITriggerMember - Data type for trigger instances C++.
int min() const
Definition: alarm.h:85
int sec() const
Definition: alarm.h:69
int & wday()
Definition: alarm.h:151
int isdst() const
Definition: alarm.h:191