#pragma once #include #include #include 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)); } void notify() const { for (const auto& observer : this->observers_) { observer(this->value_); } } private: T value_; std::vector> observers_; }; } // namespace ratgdo } // namespace esphome