Subject
Namespace: UTILSLIB · Library: Utilities Library
#include <utils/observerpattern.h>
class UTILSLIB::Subject
DECLARE BASE CLASS SUBJECT.
The Subject class provides the base class of every subject of the observer design pattern.
src/examples/ex_utils/main.cpp
// A subject notifies every attached observer; observers pull the state they need
class Counter : public Subject
{
public:
void increment()
{
++m_value;
notify();
}
int value() const
{
return m_value;
}
private:
int m_value = 0;
};
class LastSeen : public IObserver
{
public:
void update(Subject* pSubject) override
{
m_seen = static_cast<Counter*>(pSubject)->value();
}
int seen() const
{
return m_seen;
}
private:
int m_seen = -1;
};
Inheritance
Public Methods
~Subject()
Destroys the Subject.
attach(pObserver)
Attaches an observer to the subject.
Parameters:
- pObserver : *IObserver ** pointer to the observer which should be attached to the subject.
detach(pObserver)
Detaches an observer of the subject.
Parameters:
- pObserver : *IObserver ** pointer to the observer which should be detached of the subject.
notify()
Notifies all attached servers by calling there update method.
Is used when subject has updates to provide. This method is enabled when nothifiedEnabled is true.
observers()
Returns attached observers.
Returns:
- t_Observers & — attached observers.
observerNumDebug()
Returns attached observers.
Returns:
- int — attached observers. Returns number of attached observers.
Example
- Full source:
src/examples/ex_utils/main.cpp - Build target:
ex_utils(runs under CTest)
Used in examples
ex_utils
Utilities library: buffers, observers, logging, file formats, montages, layouts and reports.
src/examples/ex_utils/main.cpp, line 79
// A subject notifies every attached observer; observers pull the state they need
class Counter : public Subject
{
public:
void increment()
{
++m_value;
notify();
}
int value() const
{
return m_value;
}
Authors of this file
- Christoph Dinh <christoph.dinh@mne-cpp.org>
- Lorenz Esch <lorenz.esch@tu-ilmenau.de>
- Juan GPC <jgarciaprieto@mgh.harvard.edu>
- Gabriel Motta <gabrielbenmotta@gmail.com>