Mantid
Loading...
Searching...
No Matches
ObserverPattern.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2014 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#pragma once
8#include <functional>
9#include <set>
10#include <string>
11
17class Observable;
18
19class Observer {
20public:
21 virtual ~Observer(){};
22 virtual void update() = 0;
23};
24
31 std::set<Observer *> m_observers;
32
33public:
37 void attach(Observer *listener) { m_observers.insert(listener); };
41 void detach(Observer *listener) { m_observers.erase(listener); };
45 void notify() {
46 for (auto &listener : m_observers) {
47 listener->update();
48 }
49 };
50};
51
56class VoidObserver : public Observer {
57public:
58 VoidObserver() : m_slot(nullptr){};
65 void setSlot(const std::function<void()> &func) { m_slot = func; };
69 void update() override { m_slot(); };
70
71private:
72 std::function<void()> m_slot;
73};
Simple observable class.
void notify()
Update all of the observers that a change has been made.
std::set< Observer * > m_observers
void detach(Observer *listener)
void attach(Observer *listener)
virtual void update()=0
virtual ~Observer()
Simple observer class (for void functions/slots).
std::function< void()> m_slot
void update() override
Calls the function/slot.
void setSlot(const std::function< void()> &func)
Sets the function/slot for the oberver.