esphome-ratgdo/components/ratgdo/callbacks.h

33 lines
703 B
C++

#pragma once
#include <functional>
#include <utility>
#include <vector>
namespace esphome {
namespace ratgdo {
template <typename... X>
class OnceCallbacks;
template <typename... Ts>
class OnceCallbacks<void(Ts...)> {
public:
template <typename Callback>
void operator()(Callback&& callback) { this->callbacks_.push_back(std::forward<Callback>(callback)); }
void trigger(Ts... args)
{
for (auto& cb : this->callbacks_)
cb(args...);
this->callbacks_.clear();
}
protected:
std::vector<std::function<void(Ts...)>> callbacks_;
};
} // namespace ratgdo
} // namespace esphome