Files
firmware/src/Observer.h

97 lines
2.6 KiB
C
Raw Normal View History

2020-02-06 07:39:21 -08:00
#pragma once
#include <Arduino.h>
#include <list>
template <class T> class Observable;
2020-02-06 07:39:21 -08:00
/**
* An observer which can be mixed in as a baseclass. Implement onNotify as a method in your class.
*/
2026-01-03 21:19:24 +01:00
template <class T> class Observer {
std::list<Observable<T> *> observables;
2020-02-06 07:39:21 -08:00
2026-01-03 21:19:24 +01:00
public:
virtual ~Observer();
2020-02-06 07:39:21 -08:00
2026-01-03 21:19:24 +01:00
/// Stop watching the observable
void unobserve(Observable<T> *o);
2026-01-03 21:19:24 +01:00
/// Start watching a specified observable
void observe(Observable<T> *o);
2020-02-06 07:39:21 -08:00
2026-01-03 21:19:24 +01:00
private:
friend class Observable<T>;
2026-01-03 21:19:24 +01:00
protected:
/**
* returns 0 if other observers should continue to be called
* returns !0 if the observe calls should be aborted and this result code returned for notifyObservers
**/
virtual int onNotify(T arg) = 0;
2020-02-06 07:39:21 -08:00
};
/**
* An observer that calls an arbitrary method
*/
2026-01-03 21:19:24 +01:00
template <class Callback, class T> class CallbackObserver : public Observer<T> {
typedef int (Callback::*ObserverCallback)(T arg);
2026-01-03 21:19:24 +01:00
Callback *objPtr;
ObserverCallback method;
2026-01-03 21:19:24 +01:00
public:
CallbackObserver(Callback *_objPtr, ObserverCallback _method) : objPtr(_objPtr), method(_method) {}
2026-01-03 21:19:24 +01:00
protected:
virtual int onNotify(T arg) override { return (objPtr->*method)(arg); }
};
/**
2026-01-03 21:19:24 +01:00
* An observable class that will notify observers anytime notifyObservers is called. Argument type T can be any type,
* but for performance reasons a pointer or word sized object is recommended.
*/
2026-01-03 21:19:24 +01:00
template <class T> class Observable {
std::list<Observer<T> *> observers;
public:
/**
* Tell all observers about a change, observers can process arg as they wish
*
* returns !0 if an observer chose to abort processing by returning this code
*/
int notifyObservers(T arg) {
for (typename std::list<Observer<T> *>::const_iterator iterator = observers.begin(); iterator != observers.end(); ++iterator) {
int result = (*iterator)->onNotify(arg);
if (result != 0)
return result;
2020-02-06 07:39:21 -08:00
}
2026-01-03 21:19:24 +01:00
return 0;
}
2026-01-03 21:19:24 +01:00
private:
friend class Observer<T>;
2020-02-06 07:39:21 -08:00
2026-01-03 21:19:24 +01:00
// Not called directly, instead call observer.observe
void addObserver(Observer<T> *o) { observers.push_back(o); }
void removeObserver(Observer<T> *o) { observers.remove(o); }
2020-02-06 07:39:21 -08:00
};
2026-01-03 21:19:24 +01:00
template <class T> Observer<T>::~Observer() {
for (typename std::list<Observable<T> *>::const_iterator iterator = observables.begin(); iterator != observables.end(); ++iterator) {
(*iterator)->removeObserver(this);
}
observables.clear();
}
2026-01-03 21:19:24 +01:00
template <class T> void Observer<T>::unobserve(Observable<T> *o) {
o->removeObserver(this);
observables.remove(o);
}
2026-01-03 21:19:24 +01:00
template <class T> void Observer<T>::observe(Observable<T> *o) {
observables.push_back(o);
o->addObserver(this);
}