retdec
|
Implementation of a generic typed observer using shared pointers (subject part). More...
#include <subject.h>
Public Types | |
using | ConcreteObserver = Observer< SubjectType, ArgType > |
A concrete observer. More... | |
using | ObserverPtr = WkPtr< ConcreteObserver > |
A pointer to an observer. More... | |
Public Member Functions | |
Subject () | |
Creates a new subject. More... | |
virtual | ~Subject ()=default |
Destructs the subject. More... | |
virtual ShPtr< SubjectType > | getSelf ()=0 |
Returns a shared pointer of self. More... | |
void | addObserver (ObserverPtr observer) |
Adds a new observer to the list of observers. More... | |
void | removeObserver (ObserverPtr observer) |
Removes the selected observer from the list of observers. More... | |
void | removeObservers () |
Removes all observers. More... | |
void | notifyObservers (ShPtr< ArgType > arg=nullptr) |
Notifies all observers by calling Observer::update() on them. More... | |
Protected Types | |
using | ObserverContainer = std::vector< ObserverPtr > |
A container to store observers. More... | |
using | observer_iterator = typename ObserverContainer::const_iterator |
Protected Member Functions | |
observer_iterator | observer_begin () const |
Returns a constant iterator to the first observer. More... | |
observer_iterator | observer_end () const |
Returns a constant iterator past the last observer. More... | |
Private Member Functions | |
void | notifyObserverOrRemoveItIfNotExists (ObserverPtr observer, ShPtr< ArgType > arg) |
Notifies the given observer (if it exists) or removes it (if it does not exist). More... | |
bool | observerExists (ObserverPtr observer) |
Checks if the given observer still exists. More... | |
void | notifyObserver (ObserverPtr observer, ShPtr< ArgType > arg) |
Notifies the given observer, provided it still exists. More... | |
void | removeObserverAndNonExistingObservers (ObserverPtr observer) |
Removes the given observer and all the non-existing observers. More... | |
Private Attributes | |
ObserverContainer | observers |
Container to store observers. More... | |
Implementation of a generic typed observer using shared pointers (subject part).
SubjectType | Type of a subject (usually the class that inherits from this class). |
ArgType | Type of an optional argument. |
Implements the Observer design pattern.
Usage:
using retdec::llvmir2hll::Subject< SubjectType, ArgType >::ConcreteObserver = Observer<SubjectType, ArgType> |
A concrete observer.
|
protected |
|
protected |
A container to store observers.
using retdec::llvmir2hll::Subject< SubjectType, ArgType >::ObserverPtr = WkPtr<ConcreteObserver> |
A pointer to an observer.
|
inline |
Creates a new subject.
|
virtualdefault |
Destructs the subject.
|
inline |
Adds a new observer to the list of observers.
[in] | observer | Observer to be added. |
|
pure virtual |
Returns a shared pointer of self.
Usually, if the class inherits from SharableFromThis<>, then this function can be implemented as
Notice, however, that then notifyObservers() or removeObserver() cannot be called from a constructor or destructor of the class which inherits from Subject because it is not safe to call shared_from_this() within there.
This function is used in notifyObservers() to get the pointer of self. Since shared pointers are used, the situation is a bit more difficult; indeed, one cannot return just ShPtr<SubjectType>(this)
.
Implemented in retdec::llvmir2hll::Value.
|
inlineprivate |
Notifies the given observer, provided it still exists.
|
inlineprivate |
Notifies the given observer (if it exists) or removes it (if it does not exist).
|
inline |
Notifies all observers by calling Observer::update() on them.
[in] | arg | Optional argument to Observer::update() calls. |
Observers are notified in the exact order they have been added by addObserver() calls. The function getSelf() is used to get a pointer to the subject.
A call to this function may result into the removal of observers that either do not exist or are removed as a consequence of another removal.
|
inlineprotected |
Returns a constant iterator to the first observer.
|
inlineprotected |
Returns a constant iterator past the last observer.
|
inlineprivate |
Checks if the given observer still exists.
|
inline |
Removes the selected observer from the list of observers.
[in] | observer | Observer to be removed. |
|
inlineprivate |
Removes the given observer and all the non-existing observers.
|
inline |
Removes all observers.
|
private |
Container to store observers.