version 0.4.1
genericlistener.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2021-2025 The Ikarus Developers mueller@ibb.uni-stuttgart.de
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
9#pragma once
10#include <spdlog/spdlog.h>
11
14
15namespace Ikarus {
16
23template <typename M>
25{
26 using Messages = M;
27
28public:
38 template <typename F>
39 GenericListener(Messages message, F&& f)
40 : message_{message},
41 f_{f} {}
42
43 template <typename BC>
45 this->subscribe(bc, [&](M message) { this->updateImpl(message); });
46 return *this;
47 }
48
49 void updateImpl(Messages message) {
50 if (message_ == message) {
51 f_(step_);
52 ++step_;
53 }
54 }
55
56private:
57 Messages message_;
58 std::function<void(int)> f_;
59 int step_{0};
60};
61} // namespace Ikarus
Enums for observer messages.
Implementation of the observer design pattern with broadcasters.
Definition: assemblermanipulatorbuildingblocks.hh:22
GenericListener class for observing specific messages. This class template implements an listener for...
Definition: genericlistener.hh:25
void updateImpl(Messages message)
Definition: genericlistener.hh:49
GenericListener & subscribeTo(BC &bc)
Definition: genericlistener.hh:44
GenericListener(Messages message, F &&f)
Constructor for GenericListener.
Definition: genericlistener.hh:39
Definition: listener.hh:27
auto subscribe(Broadcaster &broadcaster, F &&f)
Function to subscribe to a broadcaster with a given function (either a lambda, std::function or funct...
Definition: listener.hh:43