27 [[nodiscard]]
bool anyObservers()
const {
return !observers.empty(); }
41 mutable std::vector<Observer<T>*> observers;
42 mutable NotifyState notifyState = IDLE;
47 assert(notifyState == IDLE);
48 for (
auto copy = observers;
auto& o : copy) {
49 o->subjectDeleted(*
static_cast<const T*
>(
this));
51 assert(observers.empty());
56 assert(notifyState == IDLE);
57 observers.push_back(&observer);
63 if (notifyState == IDLE) {
73 assert(notifyState == IDLE);
74 notifyState = IN_PROGRESS;
76 for (
auto& o : observers) {
78 o->update(*
static_cast<const T*
>(
this));
80 assert(
false &&
"Observer::update() shouldn't throw");
84 if (notifyState == DETACH) {
85 observers.erase(
ranges::remove(observers,
nullptr), observers.end());
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:
auto remove(ForwardRange &&range, const T &value)
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.