From 7c5ee6941826996e5b87f28225f1c6a43c4d999d Mon Sep 17 00:00:00 2001 From: Marius Muja Date: Thu, 18 Jan 2024 18:13:18 -0800 Subject: [PATCH] Revert observable refactorings --- components/ratgdo/observable.h | 62 ++++++++-------------------------- 1 file changed, 15 insertions(+), 47 deletions(-) diff --git a/components/ratgdo/observable.h b/components/ratgdo/observable.h index 96e0332..5947a0d 100644 --- a/components/ratgdo/observable.h +++ b/components/ratgdo/observable.h @@ -7,46 +7,19 @@ namespace esphome { namespace ratgdo { template - class distinct_observable; - - - - template - class observable_base { + class observable { public: - template - void subscribe(Observer&& observer) + observable(const T& value) + : value_(value) { - this->observers_.push_back(std::forward(observer)); } - void notify(T value) const - { - for (const auto& observer : this->observers_) { - observer(value); - } - } - - distinct_observable distinct() - { - return std::make_shared(this); - } - - private: - std::vector> observers_; - }; - - template - class observable : public observable_base { - public: - observable(const T& value) : value_(value) {} - template observable& operator=(U value) { if (value != this->value_) { this->value_ = value; - this->notify(value); + this->notify(); } return *this; } @@ -54,27 +27,22 @@ namespace ratgdo { T const* operator&() const { return &this->value_; } T const& operator*() const { return this->value_; } - private: - T value_; - }; + template + void subscribe(Observer&& observer) + { + this->observers_.push_back(std::forward(observer)); + } - - - template - class distinct_observable : public observable { - public: - distinct_observable(std::shared_ptr> inner) : inner_(inner) { - inner.subscribe([=] (T value) { - if (value != this->value_) { - this->value_ = value; - this->notify(value); - } - }); + void notify() const + { + for (const auto& observer : this->observers_) { + observer(this->value_); + } } private: - std::shared_ptr> inner_; T value_; + std::vector> observers_; }; } // namespace ratgdo