19 #include <unordered_map>
33 std::function<
void(
const std::any&)>&& func,
34 std::function<
void()>&& cleanup);
37 using Ptr = std::shared_ptr<Subscriber>;
41 std::function<
void(
const std::any&)>&& func,
42 std::function<
void()>&& cleanup);
44 void pub(
const std::any& a);
47 std::function<void(
const std::any&)>
m_func;
51 class Topic :
public std::enable_shared_from_this<Topic>
54 Topic(std::function<
void()>&& cleanup);
57 using Ptr = std::shared_ptr<Topic>;
61 template <
typename ARG,
typename Callable>
64 std::lock_guard<std::mutex> lock(
m_mutex);
68 auto capturedShared = shared_from_this();
70 [func{std::forward<Callable>(func)}](
const std::any& anyArg) {
73 std::invoke(func, std::any_cast<const ARG&>(anyArg));
75 catch (std::bad_any_cast&)
77 std::cerr <<
"Subscriber has wrong type: "
86 capturedShared->cleanupSubscriber(it);
92 void publish(
const std::any& any);
95 template <
typename CLEANUP>
98 return Ptr(
new Topic(std::forward<CLEANUP>(cleanup)));
103 std::list<std::weak_ptr<Subscriber>>
m_subs;
114 using Ptr = std::shared_ptr<TopicDirectory>;
116 template <
typename PATH>
119 std::lock_guard<std::mutex> lock(
m_mutex);
123 auto ptr = it->second.lock();
127 auto capturedShared = shared_from_this();
129 Topic::create([=]() { capturedShared->cleanupTopic(path); });