#pragma once #include #include #include namespace esphome { namespace ratgdo { template class OnceCallbacks; template class OnceCallbacks { public: template void operator()(Callback&& callback) { this->callbacks_.push_back(std::forward(callback)); } void trigger(Ts... args) { for (auto& cb : this->callbacks_) { cb(args...); } this->callbacks_.clear(); } protected: std::vector> callbacks_; }; template class ExpiringCallbacks; template class ExpiringCallbacks { public: template void operator()(uint32_t expiration, Callback&& callback) { this->callbacks_.push_back(std::make_pair(expiration, std::forward(callback))); } void trigger(uint32_t now, Ts... args) { for (auto& cb : this->callbacks_) { if (cb.first >= now) { cb.second(args...); } } this->callbacks_.clear(); } bool is_expired(uint32_t now) const { bool expired = true; for (auto& cb : this->callbacks_) { if (cb.first >= now) { expired = false; } } return expired; } void clear() { this->callbacks_.clear(); } protected: std::vector>> callbacks_; }; } // namespace ratgdo } // namespace esphome