WPOCT Software Developer's Kit (SDK)
SDK For using Wasatch Photonics OCT Spectrometers
SimpleTimer.h
Go to the documentation of this file.
1 #pragma once
2 
8 #include <boost/function.hpp>
9 #include <boost/system/error_code.hpp>
10 #ifdef _WIN32
11 #include <windows.h>
12 #else
13 // cannot get this to work on windows
14 #include <boost/asio.hpp>
15 #endif
16 
17 class CallbackInfo;
18 
20 {
21  virtual void timer_callback() = 0;
22 };
23 
24 typedef void(*TimerCallbackInfo)(CallbackInfo *);
25 
27 {
28 public:
29  typedef boost::function<void()> Handler;
30  typedef boost::function<void(CallbackInfo *info)> HandlerCBI;
31 
32 #ifdef _WIN32
33  SimpleTimer(const std::size_t& tid = 10, const std::size_t& interval = 500, Handler _handler = NULL)
34  : _tid(tid), _interval(interval), _tickCount(0)
35  {
36  _callbackInfo = NULL;
37  _caller = NULL;
38  _hTimer = NULL;
39  _isStopped = true;
40  _tid = 0;
41  _interval = 0;
42  _tickCount = 0;
43  };
44 #else
45  SimpleTimer(boost::asio::io_service& io_service,
46  const std::size_t& tid = 10,
47  const std::size_t& interval = 500,
48  Handler _handler = NULL)
49  : _strand(io_service),
50  _timer(io_service),
51  _tid(tid),
52  _interval(interval),
53  _tickCount(0)//,
54  // io_work(io_service_)
55  {
56  caller = NULL;
57  };
58 #endif
59  virtual ~SimpleTimer(void);
60  void Start();
61  void Stop();
62 
63  void SetInterval(long i);
64  void SetHandler(Handler handler);
65 
67  void SetHandler(HandlerCBI hcb, void * c);
68 
70  void SetCaller(TimerCallback *caller)
71  {
72  _caller = caller;
73  }
74 
75  void InitiateTimer();
76 
77  void TimerHandler();
78 
79 private:
80  void Print (const boost::system::error_code& error);
81  void HandleTimerEvent(const boost::system::error_code &error);
82 
83  Handler _handler;
84  HandlerCBI _handlerCallbackInfo;
85  CallbackInfo* _callbackInfo;
86 
87  TimerCallback* _caller;
88 
89  bool _isStopped;
90  std::size_t _tid;
91  std::size_t _interval;
92  std::size_t _tickCount;
93 
94 #ifdef _WIN32
95  HANDLE _hTimer;
96 #else
97  boost::asio::strand _strand;
98  // boost::asio::io_service::work& _io_work;
99  boost::asio::deadline_timer _timer;
100  // boost::posix_time::ptime _before;
101  // boost::posix_time::ptime _after;
102 #endif
103 };
Used for callbacks, in particular for the SimpleTimer class.
Definition: CallbackInfo.h:10
Definition: SimpleTimer.h:27
void SetCaller(TimerCallback *caller)
tell caller to call its callback method - seems simplest
Definition: SimpleTimer.h:70
Definition: SimpleTimer.h:20