#pragma once #include #include #include "misc.h" namespace asyncpp { namespace timer { struct delay; struct timer; struct registration { public: struct inner { timer& owner; time_point timeout; /** * @brief Constructor. */ inline inner(timer& p_owner, time_point p_timeout); }; using inner_ptr_w = std::weak_ptr; public: delay& owner; inner_ptr_w ptr; /** * @brief Constructor. */ inline registration(delay& p_owner); }; struct timer { private: using inner_ptr_s = std::shared_ptr; struct inner_less_compare { constexpr bool operator()(const inner_ptr_s& lhs, const inner_ptr_s& rhs) const; }; using inner_set = std::set; private: inner_set _registrations; public: /** * @brief Get the number of resources assigned to this timer. */ inline size_t resource_count() const; /** * @brief Set the thread local current timer. */ inline void make_current(); /** * @brief Set the thread local current timer. */ inline void clear_current(); public: /** * @brief Get the thread local timer instance. */ static inline timer* current(); /** * @brief Register a new resource within this timer. */ static inline void register_resource(registration& p_value); /** * @brief Register a new resource within this timer. */ static inline void unregister_resource(registration& p_value); private: /** * @brief Add a new resource to the timer. */ inline inner_ptr_s make_inner(registration& p_value); private: struct storage { timer* current { nullptr }; }; /** * @brief Get the thread local storage. */ static inline storage& local_storage(); }; } }