Inviwo  0.9.10.1
Inviwo documentation
inviwo::Observable< T > Class Template Reference

#include <observer.h>

+ Inheritance diagram for inviwo::Observable< T >:

Public Member Functions

 Observable (const Observable< T > &other)
 
 Observable (Observable< T > &&other)
 
Observable< T > & operator= (const Observable< T > &other)
 
Observable< T > & operator= (Observable< T > &&other)
 
void addObserver (T *observer)
 
void removeObserver (T *observer)
 
virtual void startBlockingNotifications () override final
 
virtual void stopBlockingNotifications () override final
 

Protected Member Functions

template<typename C >
void forEachObserver (C callback)
 
- Protected Member Functions inherited from inviwo::ObservableInterface
void addObservationHelper (Observer *observer)
 
void removeObservationHelper (Observer *observer)
 

Detailed Description

template<typename T>
class inviwo::Observable< T >

Class to support observer pattern.

Example of how to apply it to a simple button.

class ButtonObservable;
class IVW_XXX_API ButtonObserver: public Observer {
public:
friend ButtonObservable;
ButtonObserver() = default
// Override to be notified when the observed button is pressed.
virtual void onButtonPressed(){};
};
class IVW_XXX_API ButtonObservable: public Observable<ButtonObserver> {
protected:
ButtonObservable() = default;
void notifyObserversAboutButtonPressed() {
forEachObserver([](ButtonObserver* o) { o->onButtonPressed(); });
}
};

Usage:

class Button : public ButtonObservable {
...
void handleButtonPress() {
...
notifyObserversAboutButtonPressed();
...
}
...
};
class MyClass : public ButtonObserver {
public:
MyClass(Button* button) {
button->addObserver(this);
}
...
private:
virtual void onButtonPressed() override {
// Do stuff on button press
};
...
};
See also
Observer
VoidObserver

Constructor & Destructor Documentation

◆ Observable() [1/2]

template<typename T>
inviwo::Observable< T >::Observable ( const Observable< T > &  other)

This operation does nothing. We will not touch the observers of other.

◆ Observable() [2/2]

template<typename T>
inviwo::Observable< T >::Observable ( Observable< T > &&  other)

This operation will remove all observers from other and add them to this.

Member Function Documentation

◆ operator=() [1/2]

template<typename T>
Observable< T > & inviwo::Observable< T >::operator= ( const Observable< T > &  other)

This operation does nothing. We will not touch the observers of other.

◆ operator=() [2/2]

template<typename T>
Observable< T > & inviwo::Observable< T >::operator= ( Observable< T > &&  other)

This operation will remove all observers of this, and make all observers of other observe this instead.


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