17#ifndef TOOLBOX_IO_TIMER_HPP
18#define TOOLBOX_IO_TIMER_HPP
25#include <boost/intrusive_ptr.hpp>
57 Timer(std::nullptr_t =
nullptr) noexcept {}
68 bool empty() const noexcept {
return !impl_; }
69 explicit operator bool() const noexcept {
return impl_ !=
nullptr; }
70 long id() const noexcept {
return impl_ ? impl_->id : 0; }
71 bool pending() const noexcept {
return impl_ !=
nullptr &&
bool{impl_->slot}; }
76 template <
typename RepT,
typename PeriodT>
77 void set_interval(std::chrono::duration<RepT, PeriodT> interval)
noexcept
80 impl_->interval = duration_cast<Duration>(interval);
82 void reset(std::nullptr_t =
nullptr) noexcept { impl_.reset(); }
83 void swap(
Timer& rhs)
noexcept { impl_.swap(rhs.impl_); }
84 void cancel() noexcept;
86 std::partial_ordering operator<=>(const
Timer& rhs) const noexcept
89 return std::partial_ordering::unordered;
91 return id() <=> rhs.id();
95 void set_expiry(
MonoTime expiry)
noexcept { impl_->expiry = expiry; }
96 TimerSlot& slot() noexcept {
return impl_->slot; }
98 boost::intrusive_ptr<Timer::Impl> impl_;
102 using SlabPtr = std::unique_ptr<Timer::Impl[]>;
117 void deallocate(
Timer::Impl* impl) noexcept
125 std::vector<SlabPtr> slabs_;
129 std::vector<Timer> heap_;
137 using SlabPtr = std::unique_ptr<Timer::Impl[]>;
156 std::
size_t size() const noexcept {
return heap_.size() - cancelled_; }
166 return insert(expiry, Duration::zero(), slot);
174 void cancel() noexcept;
177 Timer pop() noexcept;
183 std::vector<Timer> heap_;
196 if (impl_ && impl_->slot) {