diff --git a/src/ivos/InterViews/observe.h b/src/ivos/InterViews/observe.h index 8a733226df8..48490e717f9 100755 --- a/src/ivos/InterViews/observe.h +++ b/src/ivos/InterViews/observe.h @@ -32,30 +32,30 @@ #include #include +#include class Observer; -class ObserverList; class Observable { public: - Observable(); + Observable() = default; virtual ~Observable(); virtual void attach(Observer*); virtual void detach(Observer*); virtual void notify(); private: - ObserverList* observers_; + std::vector observers_; }; class Observer { protected: - Observer(); + Observer() = default; public: - virtual ~Observer(); + virtual ~Observer() = default; - virtual void update(Observable*); - virtual void disconnect(Observable*); + virtual void update(Observable*) {}; + virtual void disconnect(Observable*) {}; }; #include diff --git a/src/ivos/observe.cpp b/src/ivos/observe.cpp index c6b09e53bbd..e5e991233fa 100755 --- a/src/ivos/observe.cpp +++ b/src/ivos/observe.cpp @@ -30,58 +30,28 @@ */ #include -#include - -declarePtrList(ObserverList,Observer) -implementPtrList(ObserverList,Observer) - -Observable::Observable() { - observers_ = nil; -} +#include "utils/enumerate.h" Observable::~Observable() { - ObserverList* list = observers_; - if (list != nil) { - // in case a disconnect removes items from the ObserverList - for (long i = list->count() - 1; i >= 0; --i) { - list->item(i)->disconnect(this); - if (i > list->count()) { i = list->count(); } + // in case a disconnect removes items from the observers + for (long long i = static_cast(observers_.size()) - 1; i >= 0; --i) { + observers_[i]->disconnect(this); + if (i > observers_.size()) { + i = observers_.size(); + } } - delete list; - } } void Observable::attach(Observer* o) { - ObserverList* list = observers_; - if (list == nil) { - list = new ObserverList(5); - observers_ = list; - } - list->append(o); + observers_.push_back(o); } void Observable::detach(Observer* o) { - ObserverList* list = observers_; - if (list != nil) { - for (ListUpdater(ObserverList) i(*list); i.more(); i.next()) { - if (i.cur() == o) { - i.remove_cur(); - break; - } - } - } + erase_first(observers_, o); } void Observable::notify() { - ObserverList* list = observers_; - if (list != nil) { - for (ListItr(ObserverList) i(*list); i.more(); i.next()) { - i.cur()->update(this); - } + for (auto& obs: observers_) { + obs->update(this); } } - -Observer::Observer() { } -Observer::~Observer() { } -void Observer::update(Observable*) { } -void Observer::disconnect(Observable*) { }