19 #include <unordered_map> 33 std::function<
void(
const std::any&)>&& func,
34 std::function<
void()>&& cleanup);
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);
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;
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); });
std::function< void(const std::any &)> m_func
void cleanupTopic(const std::string &key)
void publish(const std::any &any)
static Ptr create(CLEANUP &&cleanup)
void pub(const std::any &a)
std::shared_ptr< TopicDirectory > Ptr
static Ptr create(std::function< void(const std::any &)> &&func, std::function< void()> &&cleanup)
Subscriber(std::function< void(const std::any &)> &&func, std::function< void()> &&cleanup)
Nodelet-like Pub/Sub communications (in #include <mrpt/comms/nodelets.h>)
std::function< void()> m_cleanup
std::function< void()> m_cleanup
Topic::Ptr getTopic(PATH &&path)
std::unordered_map< std::string, std::weak_ptr< Topic > > m_mapService
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Subscriber::Ptr createSubscriber(Callable &&func)
void cleanupSubscriber(std::list< std::weak_ptr< Subscriber >>::iterator it)
std::list< std::weak_ptr< Subscriber > > m_subs
The central directory of existing topics for pub/sub.
Topic(std::function< void()> &&cleanup)
std::shared_ptr< Topic > Ptr