26 [[nodiscard]]
bool anyObservers()
const {
return !observers.empty(); }
40 mutable std::vector<Observer<T>*> observers;
41 mutable NotifyState notifyState = IDLE;
46 assert(notifyState == IDLE);
47 for (
auto copy = observers;
auto& o : copy) {
48 o->subjectDeleted(*
static_cast<const T*
>(
this));
50 assert(observers.empty());
55 assert(notifyState == IDLE);
56 observers.push_back(&observer);
62 if (notifyState == IDLE) {
72 assert(notifyState == IDLE);
73 notifyState = IN_PROGRESS;
75 for (
auto& o : observers) {
77 o->update(*
static_cast<const T*
>(
this));
79 assert(
false &&
"Observer::update() shouldn't throw");
83 if (notifyState == DETACH) {
84 std::erase(observers,
nullptr);
Generic Gang-of-Four Observer class, templatized edition.
Generic Gang-of-Four Subject class of the Observer pattern, templatized edition.
void detach(Observer< T > &observer)
Subject(Subject &&)=delete
Subject & operator=(Subject &&)=delete
Subject(const Subject &)=delete
void attach(Observer< T > &observer)
bool anyObservers() const
Subject & operator=(const Subject &)=delete
This file implemented 3 utility functions:
void move_pop_back(VECTOR &v, typename VECTOR::iterator it)
Erase the pointed to element from the given vector.
auto rfind_unguarded(RANGE &range, const VAL &val, Proj proj={})
Similar to the find(_if)_unguarded functions above, but searches from the back to front.