Robot Raconteur Core C++ Library
Timer.h
Go to the documentation of this file.
1 
24 #include <boost/date_time.hpp>
26 
27 #ifndef ROBOTRACONTEUR_EMSCRIPTEN
28 #include <boost/asio/deadline_timer.hpp>
29 #endif
30 
31 #include <boost/system/error_code.hpp>
32 
33 #pragma once
34 
35 namespace RobotRaconteur
36 {
37 
45 struct ROBOTRACONTEUR_CORE_API TimerEvent
46 {
48  bool stopped;
50  boost::posix_time::ptime last_expected;
52  boost::posix_time::ptime last_real;
54  boost::posix_time::ptime current_expected;
56  boost::posix_time::ptime current_real;
57 
58  TimerEvent();
59 };
60 
70 class ROBOTRACONTEUR_CORE_API Timer : private boost::noncopyable
71 {
72  public:
79  virtual void Start() = 0;
80 
85  virtual void Stop() = 0;
86 
91  virtual void TryStop() = 0;
92 
98  virtual boost::posix_time::time_duration GetPeriod() = 0;
99 
105  virtual void SetPeriod(const boost::posix_time::time_duration& period) = 0;
106 
113  virtual bool IsRunning() = 0;
114 
119  virtual void Clear() = 0;
120 
121  virtual ~Timer() {}
122 };
123 
124 class ROBOTRACONTEUR_CORE_API RobotRaconteurNode;
125 
133 class ROBOTRACONTEUR_CORE_API Rate : private boost::noncopyable
134 {
135 
136  public:
141  virtual void Sleep() = 0;
142 
143  virtual ~Rate(){};
144 };
145 
146 class ROBOTRACONTEUR_CORE_API WallRate : public Rate
147 {
148  protected:
149  RR_WEAK_PTR<RobotRaconteurNode> node;
150  boost::posix_time::time_duration period;
151  boost::posix_time::ptime start_time;
152  boost::posix_time::ptime last_time;
153 
154 #if !defined(ROBOTRACONTEUR_EMSCRIPTEN) && !defined(ROBOTRACONTEUR_WINDOWS)
155  boost::asio::deadline_timer timer;
156 #endif
157 
158 #ifdef ROBOTRACONTEUR_WINDOWS
159  boost::shared_ptr<void> timer_handle;
160 #endif
161 #ifdef ROBOTRACONTEUR_LINUX
162  timespec ts;
163 #endif
164 
165  public:
166  WallRate(double frequency, const RR_SHARED_PTR<RobotRaconteurNode>& node = RR_SHARED_PTR<RobotRaconteurNode>());
167 
168  RR_OVIRTUAL void Sleep() RR_OVERRIDE;
169 
170  RR_OVIRTUAL ~WallRate() RR_OVERRIDE {}
171 };
172 
173 class ROBOTRACONTEUR_CORE_API WallTimer : public Timer, public RR_ENABLE_SHARED_FROM_THIS<WallTimer>
174 {
175  protected:
176  boost::posix_time::time_duration period;
177  boost::posix_time::ptime start_time;
178  boost::posix_time::ptime actual_last_time;
179  boost::posix_time::ptime last_time;
180  bool oneshot;
181 
182  bool running;
183  boost::mutex running_lock;
184 
185  boost::function<void(const TimerEvent&)> handler;
186 
187 #ifndef ROBOTRACONTEUR_EMSCRIPTEN
188  RR_SHARED_PTR<boost::asio::deadline_timer> timer;
189 #else
190  boost::initialized<long> em_timer;
191  static std::map<void*, RR_SHARED_PTR<WallTimer> > em_timers;
192 #endif
193 
194  RR_WEAK_PTR<RobotRaconteurNode> node;
195 
196 #ifndef ROBOTRACONTEUR_EMSCRIPTEN
197  void timer_handler(const boost::system::error_code& ec);
198 #else
199  friend void timer_handler(void* userData);
200  void timer_handler1();
201  static void node_shutdown(RobotRaconteurNode* node);
202 #endif
203 
204  public:
205  WallTimer(const boost::posix_time::time_duration& period, boost::function<void(const TimerEvent&)> handler,
206  bool oneshot, const RR_SHARED_PTR<RobotRaconteurNode>& node = RR_SHARED_PTR<RobotRaconteurNode>());
207 
208  RR_OVIRTUAL void Start() RR_OVERRIDE;
209 
210  RR_OVIRTUAL void Stop() RR_OVERRIDE;
211 
212  RR_OVIRTUAL void TryStop() RR_OVERRIDE;
213 
214  RR_OVIRTUAL boost::posix_time::time_duration GetPeriod() RR_OVERRIDE;
215 
216  RR_OVIRTUAL void SetPeriod(const boost::posix_time::time_duration& period) RR_OVERRIDE;
217 
218  RR_OVIRTUAL bool IsRunning() RR_OVERRIDE;
219 
220  RR_OVIRTUAL void Clear() RR_OVERRIDE;
221 
222  RR_OVIRTUAL ~WallTimer() RR_OVERRIDE {}
223 };
224 
225 #ifndef ROBOTRACONTEUR_NO_CXX11_TEMPLATE_ALIASES
227 using TimerPtr = RR_SHARED_PTR<Timer>;
229 using RatePtr = RR_SHARED_PTR<Rate>;
230 #endif
231 
240 void HighResolutionSleep(const boost::posix_time::time_duration& duration);
241 
242 } // namespace RobotRaconteur
void HighResolutionSleep(const boost::posix_time::time_duration &duration)
Sleep using high resolution timer provided by the OS.
boost::shared_ptr< Rate > RatePtr
Convenience alias for Rate shared_ptr.
Definition: Timer.h:229
boost::shared_ptr< Timer > TimerPtr
Convenience alias for Timer shared_ptr.
Definition: Timer.h:227
Rate to stabilize a loop.
Definition: Timer.h:134
virtual void Sleep()=0
Sleep the calling thread until the current loop period expires.
A timer to invoke a callback.
Definition: Timer.h:71
virtual void Clear()=0
Clear the timer.
virtual void Start()=0
Start the timer.
virtual bool IsRunning()=0
Check if timer is running.
virtual void Stop()=0
Stop the timer.
virtual void TryStop()=0
Stop the timer without throwing an exception if the timer is not running.
virtual boost::posix_time::time_duration GetPeriod()=0
Get the period of the timer.
virtual void SetPeriod(const boost::posix_time::time_duration &period)=0
Set the period of the timer.
Timer event structure.
Definition: Timer.h:46
boost::posix_time::ptime current_expected
The current expected invocation time.
Definition: Timer.h:54
boost::posix_time::ptime current_real
The current invocation time.
Definition: Timer.h:56
boost::posix_time::ptime last_expected
The last expected callback invocation time.
Definition: Timer.h:50
bool stopped
true if timer has been stopped
Definition: Timer.h:48
boost::posix_time::ptime last_real
The real last callback invocation time.
Definition: Timer.h:52