22 [[nodiscard]]
bool anyObservers()
const {
return !observers.empty(); }
36 mutable std::vector<Observer<T>*> observers;
37 mutable NotifyState notifyState = IDLE;
42 assert(notifyState == IDLE);
43 auto copy = observers;
44 for (
auto& o :
copy) {
45 o->subjectDeleted(*
static_cast<const T*
>(
this));
47 assert(observers.empty());
52 assert(notifyState == IDLE);
53 observers.push_back(&observer);
59 if (notifyState == IDLE) {
69 assert(notifyState == IDLE);
70 notifyState = IN_PROGRESS;
72 for (
auto& o : observers) {
74 o->update(*
static_cast<const T*
>(
this));
76 assert(
false &&
"Observer::update() shouldn't throw");
80 if (notifyState == DETACH) {
81 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)
void attach(Observer< T > &observer)
bool anyObservers() const
This file implemented 3 utility functions:
auto remove(ForwardRange &&range, const T &value)
auto copy(InputRange &&range, OutputIter out)
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.