retdec
Public Types | Public Member Functions | Protected Types | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
retdec::llvmir2hll::Subject< SubjectType, ArgType > Class Template Referenceabstract

Implementation of a generic typed observer using shared pointers (subject part). More...

#include <subject.h>

Collaboration diagram for retdec::llvmir2hll::Subject< SubjectType, ArgType >:
Collaboration graph
[legend]

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...
 

Detailed Description

template<typename SubjectType, typename ArgType = SubjectType>
class retdec::llvmir2hll::Subject< SubjectType, ArgType >

Implementation of a generic typed observer using shared pointers (subject part).

Template Parameters
SubjectTypeType of a subject (usually the class that inherits from this class).
ArgTypeType of an optional argument.

Implements the Observer design pattern.

Usage:

class Planet: public Subject<Planet> {
private:
std::string name;
public:
Planet(std::string name): Subject<Planet>(this), name(name) {}
~Planet() {}
std::string getName() { return name; }
};
Subject()
Creates a new subject.
Definition: subject.h:57
std::string getName(const Rule *rule)
Definition: utils.cpp:43
See also
Observer

Member Typedef Documentation

◆ ConcreteObserver

template<typename SubjectType , typename ArgType = SubjectType>
using retdec::llvmir2hll::Subject< SubjectType, ArgType >::ConcreteObserver = Observer<SubjectType, ArgType>

A concrete observer.

◆ observer_iterator

template<typename SubjectType , typename ArgType = SubjectType>
using retdec::llvmir2hll::Subject< SubjectType, ArgType >::observer_iterator = typename ObserverContainer::const_iterator
protected

◆ ObserverContainer

template<typename SubjectType , typename ArgType = SubjectType>
using retdec::llvmir2hll::Subject< SubjectType, ArgType >::ObserverContainer = std::vector<ObserverPtr>
protected

A container to store observers.

◆ ObserverPtr

template<typename SubjectType , typename ArgType = SubjectType>
using retdec::llvmir2hll::Subject< SubjectType, ArgType >::ObserverPtr = WkPtr<ConcreteObserver>

A pointer to an observer.

Constructor & Destructor Documentation

◆ Subject()

template<typename SubjectType , typename ArgType = SubjectType>
retdec::llvmir2hll::Subject< SubjectType, ArgType >::Subject ( )
inline

Creates a new subject.

◆ ~Subject()

template<typename SubjectType , typename ArgType = SubjectType>
virtual retdec::llvmir2hll::Subject< SubjectType, ArgType >::~Subject ( )
virtualdefault

Destructs the subject.

Member Function Documentation

◆ addObserver()

template<typename SubjectType , typename ArgType = SubjectType>
void retdec::llvmir2hll::Subject< SubjectType, ArgType >::addObserver ( ObserverPtr  observer)
inline

Adds a new observer to the list of observers.

Parameters
[in]observerObserver to be added.

◆ getSelf()

template<typename SubjectType , typename ArgType = SubjectType>
virtual ShPtr<SubjectType> retdec::llvmir2hll::Subject< SubjectType, ArgType >::getSelf ( )
pure virtual

Returns a shared pointer of self.

Usually, if the class inherits from SharableFromThis<>, then this function can be implemented as

return shared_from_this();

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).

See also
notifyObservers(), removeObserver()

Implemented in retdec::llvmir2hll::Value.

◆ notifyObserver()

template<typename SubjectType , typename ArgType = SubjectType>
void retdec::llvmir2hll::Subject< SubjectType, ArgType >::notifyObserver ( ObserverPtr  observer,
ShPtr< ArgType >  arg 
)
inlineprivate

Notifies the given observer, provided it still exists.

◆ notifyObserverOrRemoveItIfNotExists()

template<typename SubjectType , typename ArgType = SubjectType>
void retdec::llvmir2hll::Subject< SubjectType, ArgType >::notifyObserverOrRemoveItIfNotExists ( ObserverPtr  observer,
ShPtr< ArgType >  arg 
)
inlineprivate

Notifies the given observer (if it exists) or removes it (if it does not exist).

◆ notifyObservers()

template<typename SubjectType , typename ArgType = SubjectType>
void retdec::llvmir2hll::Subject< SubjectType, ArgType >::notifyObservers ( ShPtr< ArgType >  arg = nullptr)
inline

Notifies all observers by calling Observer::update() on them.

Parameters
[in]argOptional 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.

See also
addObserver(), Observer::update(), getSelf()

◆ observer_begin()

template<typename SubjectType , typename ArgType = SubjectType>
observer_iterator retdec::llvmir2hll::Subject< SubjectType, ArgType >::observer_begin ( ) const
inlineprotected

Returns a constant iterator to the first observer.

◆ observer_end()

template<typename SubjectType , typename ArgType = SubjectType>
observer_iterator retdec::llvmir2hll::Subject< SubjectType, ArgType >::observer_end ( ) const
inlineprotected

Returns a constant iterator past the last observer.

◆ observerExists()

template<typename SubjectType , typename ArgType = SubjectType>
bool retdec::llvmir2hll::Subject< SubjectType, ArgType >::observerExists ( ObserverPtr  observer)
inlineprivate

Checks if the given observer still exists.

◆ removeObserver()

template<typename SubjectType , typename ArgType = SubjectType>
void retdec::llvmir2hll::Subject< SubjectType, ArgType >::removeObserver ( ObserverPtr  observer)
inline

Removes the selected observer from the list of observers.

Parameters
[in]observerObserver to be removed.

◆ removeObserverAndNonExistingObservers()

template<typename SubjectType , typename ArgType = SubjectType>
void retdec::llvmir2hll::Subject< SubjectType, ArgType >::removeObserverAndNonExistingObservers ( ObserverPtr  observer)
inlineprivate

Removes the given observer and all the non-existing observers.

◆ removeObservers()

template<typename SubjectType , typename ArgType = SubjectType>
void retdec::llvmir2hll::Subject< SubjectType, ArgType >::removeObservers ( )
inline

Removes all observers.

Member Data Documentation

◆ observers

template<typename SubjectType , typename ArgType = SubjectType>
ObserverContainer retdec::llvmir2hll::Subject< SubjectType, ArgType >::observers
private

Container to store observers.


The documentation for this class was generated from the following file: