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