#pragma once #include #include #include #include "esphome/core/log.h" namespace esphome { namespace ratgdo { template class observable { public: observable(const T& value) : value_(value) { } template observable& operator=(U value) { if (value != this->value_) { this->value_ = value; this->notify(); } return *this; } T const* operator&() const { return &this->value_; } T const& operator*() const { return this->value_; } template void subscribe(Observer&& observer) { this->observers_.push_back(std::forward(observer)); ESP_LOGD("XXX","The observers vector is %d in length",this->observers_.size()); } void notify() const { ESP_LOGD("YYY","NOTIFY %d subscribers",this->observers_.size()); for (const auto& observer : this->observers_) { observer(this->value_); } } private: T value_; std::vector> observers_; }; } // namespace ratgdo } // namespace esphome