VSM C++ SDK
Vehicle Specific Modules SDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
timer_processor.h
Go to the documentation of this file.
1 // Copyright (c) 2018, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
4 
11 #ifndef _UGCS_VSM_TIMER_PROCESSOR_H_
12 #define _UGCS_VSM_TIMER_PROCESSOR_H_
13 
15 #include <ugcs/vsm/singleton.h>
16 #include <thread>
17 #include <map>
18 
19 namespace ugcs {
20 namespace vsm {
21 
25 
26 public:
28  template <typename... Args>
29  static Ptr
30  Get_instance(Args &&... args)
31  {
32  return singleton.Get_instance(std::forward<Args>(args)...);
33  }
34 
36 
43 
45  class Timer: public std::enable_shared_from_this<Timer> {
47 
48  public:
50  Timer(const Timer_processor::Ptr &processor,
51  std::chrono::milliseconds interval);
52 
54  void
55  Cancel();
56 
60  bool
61  Is_running() const;
62 
64  std::chrono::steady_clock::time_point
65  Get_fire_time() const
66  {
67  return fire_time;
68  }
69 
70  private:
71  friend class Timer_processor;
72 
74  Timer_processor::Ptr processor;
76  bool is_running = true;
78  std::chrono::milliseconds interval;
80  std::chrono::steady_clock::time_point fire_time;
82  Request::Ptr request;
84  mutable std::mutex mutex;
86  std::list<Ptr> attached_timers;
87 
89  void
90  Set_request(Request::Ptr &request);
91 
93  void
94  Fire();
95 
101  void
102  Destroy(bool cancel = false);
103 
105  void
106  Attach(Timer::Ptr &timer);
107 
109  void
110  Attach(std::list<Ptr> &&timers);
111 
113  void
114  Detach(Timer::Ptr &timer);
115 
117  Ptr
118  Get_attached();
119  };
120 
133  Timer::Ptr
134  Create_timer(std::chrono::milliseconds interval, const Handler &handler,
135  Request_container::Ptr container);
136 
138  void
139  Cancel_timer(Timer::Ptr timer);
140 
141 private:
143  typedef decltype(std::chrono::milliseconds().count()) Tick_type;
144 
146  std::thread thread;
148  std::map<Tick_type, Timer::Ptr> tree;
150  std::mutex tree_lock;
152  static Singleton<Timer_processor> singleton;
153 
155  virtual void
156  On_enable() override;
157 
159  virtual void
160  On_disable() override;
161 
163  virtual void
164  On_wait_and_process() override;
165 
167  void
168  Timer_process_handler(Timer::Ptr timer);
169 
171  void
172  Timer_handler(Timer::Ptr timer, Handler handler,
173  Request_container::Ptr container);
174 
176  void
177  Insert_timer(Timer::Ptr &timer);
178 
180  static Tick_type
181  Get_ticks(const std::chrono::steady_clock::time_point &time);
182 
184  void
185  Create_request(Timer::Ptr &timer, const Handler &handler,
186  Request_container::Ptr &container);
187 };
188 
189 } /* namespace vsm */
190 } /* namespace ugcs */
191 
192 #endif /* _UGCS_VSM_TIMER_PROCESSOR_H_ */
Singleton class definition.
void Cancel_timer(Timer::Ptr timer)
Cancel the specified timer in case it is running.
Timer processor manages all timers in the VSM.
Definition: timer_processor.h:23
std::shared_ptr< Timer_processor > Ptr
Pointer type.
Definition: timer_processor.h:24
std::chrono::steady_clock::time_point Get_fire_time() const
Get time of next timer firing.
Definition: timer_processor.h:65
Timer::Ptr Create_timer(std::chrono::milliseconds interval, const Handler &handler, Request_container::Ptr container)
Create and schedule the timer instance.
std::shared_ptr< Request > Ptr
Pointer type.
Definition: request_container.h:38
Request execution context.
Request execution context.
Definition: request_context.h:24
Callback_proxy< bool > Handler
Timer handler.
Definition: timer_processor.h:42
Helper class for proxying callback invocation.
Definition: callback.h:699
static Ptr Get_instance(Args &&...args)
Get global or create new processor instance.
Definition: timer_processor.h:30
Represents timer instance.
Definition: timer_processor.h:45
std::shared_ptr< Request_container > Ptr
Pointer type.
Definition: request_container.h:31
Generic container for queued requests.
Definition: request_container.h:30
#define DEFINE_COMMON_CLASS(__class_name,...)
Use this macro to define some common attributes for a class.
Definition: utils.h:25
std::shared_ptr< Timer > Ptr
Pointer type.
Definition: timer_processor.h:46
void Cancel()
Cancel running timer.
bool Is_running() const
Check if the timer still is running - i.e.
Helper class for implementing singletons.
Definition: singleton.h:69