version 0.4.4
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
genericlistener.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2021-2025 The Ikarus Developers ikarus@ibb.uni-stuttgart.de
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
10#pragma once
11#include <spdlog/spdlog.h>
12
15
16namespace Ikarus {
17
24template <typename BC>
26{
27 using Messages = BC::MessageType;
28 using State = BC::State;
29
30public:
40 template <typename F>
41 GenericListener(BC& bc, Messages message, F&& f)
42 : message_{message},
43 f_{std::forward<F>(f)} {
44 this->subscribe(bc, [&](Messages message, const BC::State& state) { this->updateImpl(message, state); });
45 }
46
47 void updateImpl(Messages message, const State& state) {
48 if (message_ == message)
49 f_(state);
50 }
51
52private:
53 Messages message_;
54 std::function<void(const State&)> f_;
55};
56
57template <typename BC, typename MT, typename F>
59
60} // namespace Ikarus
Enums for observer messages.
Implementation of the observer design pattern with broadcasters.
Definition: assemblermanipulatorbuildingblocks.hh:22
GenericListener(BC &, MT, F &&) -> GenericListener< BC >
GenericListener class for observing specific messages. This class template implements an listener for...
Definition: genericlistener.hh:26
GenericListener(BC &bc, Messages message, F &&f)
Constructor for GenericListener.
Definition: genericlistener.hh:41
void updateImpl(Messages message, const State &state)
Definition: genericlistener.hh:47
Implements a listener.
Definition: listener.hh:28
auto subscribe(Broadcaster &broadcaster, std::function< void(typename Broadcaster::MessageType, const typename Broadcaster::State &)> callback)
Function to subscribe to a broadcaster with a given function (either a lambda, std::function or funct...
Definition: listener.hh:41