6 typedef std::shared_ptr<timer_manager> timer_mgr_ptr;
7 typedef std::weak_ptr<timer_manager> timer_mgr_wptr;
11 typedef std::chrono::system_clock timer_clock_type;
12 typedef std::function<void(
bool)> timer_callback_type;
17 struct timer_target :
public std::enable_shared_from_this<timer_target>
21 enum struct State : uint32_t
27 timer_clock_type::time_point tp;
28 timer_callback_type cb;
29 State st = State::Invalid;
35 timer_target(
const timer_clock_type::time_point & tp_,
const timer_callback_type & cb_)
40 timer_target(
const timer_clock_type::time_point & tp_, timer_callback_type && cb_)
42 , cb(std::forward<timer_callback_type>(cb_))
53 typedef std::shared_ptr<timer_target> timer_target_ptr;
54 typedef std::weak_ptr<timer_target> timer_target_wptr;
64 timer_mgr_wptr _manager;
65 detail::timer_target_wptr _target;
67 #ifndef DOXYGEN_SKIP_PROPERTY
73 #endif //DOXYGEN_SKIP_PROPERTY
84 struct timer_manager :
public std::enable_shared_from_this<timer_manager>
86 #ifndef DOXYGEN_SKIP_PROPERTY
88 typedef detail::timer_target_ptr timer_target_ptr;
89 typedef detail::timer_clock_type clock_type;
90 typedef clock_type::duration duration_type;
91 typedef clock_type::time_point time_point_type;
93 typedef std::vector<timer_target_ptr> timer_vector_type;
94 typedef std::multimap<clock_type::time_point, timer_target_ptr> timer_map_type;
100 template<
class _Rep,
class _Period,
class _Cb>
101 timer_target_ptr add(
const std::chrono::duration<_Rep, _Period> & dt_, _Cb && cb_)
103 return add_(std::chrono::duration_cast<duration_type>(dt_), std::forward<_Cb>(cb_));
105 template<
class _Clock,
class _Duration =
typename _Clock::duration,
class _Cb>
106 timer_target_ptr add(
const std::chrono::time_point<_Clock, _Duration> & tp_, _Cb && cb_)
108 return add_(std::chrono::time_point_cast<duration_type>(tp_), std::forward<_Cb>(cb_));
110 template<
class _Rep,
class _Period,
class _Cb>
111 timer_handler add_handler(
const std::chrono::duration<_Rep, _Period> & dt_, _Cb && cb_)
113 return{
this, add(dt_, std::forward<_Cb>(cb_)) };
115 template<
class _Clock,
class _Duration =
typename _Clock::duration,
class _Cb>
116 timer_handler add_handler(
const std::chrono::time_point<_Clock, _Duration> & tp_, _Cb && cb_)
118 return{
this, add(tp_, std::forward<_Cb>(cb_)) };
121 bool stop(
const timer_target_ptr & sptr);
123 inline bool empty()
const
125 return _runing_timers.empty() && _added_timers.empty();
130 #ifndef DOXYGEN_SKIP_PROPERTY
132 timer_target_ptr add_(
const duration_type & dt_, _Cb && cb_)
134 return add_(std::make_shared<timer_target>(clock_type::now() + dt_, std::forward<_Cb>(cb_)));
137 timer_target_ptr add_(
const time_point_type & tp_, _Cb && cb_)
139 return add_(std::make_shared<timer_target>(tp_, std::forward<_Cb>(cb_)));
143 timer_vector_type _added_timers;
144 timer_map_type _runing_timers;
146 timer_target_ptr add_(
const timer_target_ptr & sptr);
147 static void call_target_(
const timer_target_ptr & sptr,
bool canceld);
152 inline timer_handler::timer_handler(
timer_manager * manager_,
const detail::timer_target_ptr & target_)
153 : _manager(manager_->shared_from_this())
157 inline timer_handler::timer_handler(timer_handler && right_) noexcept
158 : _manager(std::move(right_._manager))
159 , _target(std::move(right_._target))
163 inline timer_handler & timer_handler::operator = (timer_handler && right_) noexcept
167 _manager = std::move(right_._manager);
168 _target = std::move(right_._target);
173 inline void timer_handler::reset()
179 inline bool timer_handler::stop()
183 if (!_target.expired())
185 auto sptr = _manager.lock();
187 result = sptr->stop(_target.lock());
195 inline bool timer_handler::expired()
const
197 return _target.expired();