WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
Notifier.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2008-2017. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <atomic>
11 #include <functional>
12 #include <utility>
13 
14 #include "ErrorBase.h"
15 #include "HAL/Notifier.h"
16 #include "HAL/cpp/priority_mutex.h"
17 
18 namespace frc {
19 
20 typedef std::function<void()> TimerEventHandler;
21 
22 class Notifier : public ErrorBase {
23  public:
24  explicit Notifier(TimerEventHandler handler);
25 
26  template <typename Callable, typename Arg, typename... Args>
27  Notifier(Callable&& f, Arg&& arg, Args&&... args)
28  : Notifier(std::bind(std::forward<Callable>(f), std::forward<Arg>(arg),
29  std::forward<Args>(args)...)) {}
30 
31  virtual ~Notifier();
32 
33  Notifier(const Notifier&) = delete;
34  Notifier& operator=(const Notifier&) = delete;
35 
36  void StartSingle(double delay);
37  void StartPeriodic(double period);
38  void Stop();
39 
40  private:
41  // update the HAL alarm
42  void UpdateAlarm();
43  // HAL callback
44  static void Notify(uint64_t currentTimeInt, HAL_NotifierHandle handle);
45 
46  // used to constrain execution between destructors and callback
47  static priority_mutex m_destructorMutex;
48  // held while updating process information
49  priority_mutex m_processMutex;
50  // HAL handle, atomic for proper destruction
51  std::atomic<HAL_NotifierHandle> m_notifier{0};
52  // address of the handler
53  TimerEventHandler m_handler;
54  // the absolute expiration time
55  double m_expirationTime = 0;
56  // the relative time (either periodic or single)
57  double m_period = 0;
58  // true if this is a periodic event
59  bool m_periodic = false;
60 };
61 
62 } // namespace frc
void Stop()
Stop timer events from occuring.
Definition: Notifier.cpp:132
Definition: Notifier.h:22
void StartPeriodic(double period)
Register for periodic event notification.
Definition: Notifier.cpp:115
Definition: priority_mutex.h:53
void StartSingle(double delay)
Register for single event notification.
Definition: Notifier.cpp:97
Base class for most objects.
Definition: ErrorBase.h:72
Notifier(TimerEventHandler handler)
Create a Notifier for timer event notification.
Definition: Notifier.cpp:25
virtual ~Notifier()
Free the resources for a timer event.
Definition: Notifier.cpp:37