version 0.4.1
listener.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 <memory>
11#include <vector>
12
15
16namespace Ikarus {
17
27{
28 using Token = std::shared_ptr<void>;
29
42 template <typename Broadcaster, typename F>
43 auto subscribe(Broadcaster& broadcaster, F&& f) {
44 using Signature = typename traits::FunctionTraits<F>::FreeSignature;
45 return subscribe<traits::MaybeDereferencedType<Broadcaster>, Signature>(utils::maybeDeref(broadcaster),
46 std::forward<F>(f));
47 }
48
59 template <typename Broadcaster, typename Signature, typename F>
61 auto subscribe(Broadcaster& broadcaster, F&& f) {
62 t.push_back(broadcaster.template station<Signature>().registerListener(std::forward<F>(f)));
63 return t.back();
64 }
65
69 void unSubscribeAll() { t.clear(); }
70
74 void unSubscribeLast() { t.pop_back(); }
75
76 void unSubscribe(Token&& ts) {
77 t.erase(std::ranges::find(t, ts)); // erase the shared ptr in t
78 assert(ts.unique() && "The given token has external references");
79 ts.reset();
80 }
81
82private:
83 std::vector<Token> t;
84};
85
86} // namespace Ikarus
Contains stl-like type traits.
Helper for dune-functions.
Definition: assemblermanipulatorbuildingblocks.hh:22
decltype(auto) maybeDeref(T &t)
if T is a pointer type, return the dereferenced value, otherwise return the value itself.
Definition: functionhelper.hh:118
Definition: broadcaster.hh:17
Definition: listener.hh:27
void unSubscribeAll()
Unsubscribe from all listeners. At the moment unsubscribing can't be done more granularly.
Definition: listener.hh:69
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
void unSubscribeLast()
Unsubscribe from the last subscribed listener.
Definition: listener.hh:74
void unSubscribe(Token &&ts)
Definition: listener.hh:76
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:61
std::shared_ptr< void > Token
Definition: listener.hh:28
Type trait for extracting information about functions.
Definition: traits.hh:371
Definition: utils/concepts.hh:631