MRPT  2.0.4
nodelets.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "comms-precomp.h" // Precompiled headers
11 
12 #include <mrpt/comms/nodelets.h>
13 
14 using namespace mrpt::comms;
15 
16 // ------- Subscriber --------------
18  std::function<void(const std::any&)>&& func,
19  std::function<void()>&& cleanup)
20  : m_func(std::move(func)), m_cleanup(std::move(cleanup))
21 {
22 }
23 
26  std::function<void(const std::any&)>&& func,
27  std::function<void()>&& cleanup)
28 {
29  return Ptr(new Subscriber(std::move(func), std::move(cleanup)));
30 }
31 
32 void Subscriber::pub(const std::any& a) { m_func(a); }
33 // ------- Topic --------------
34 Topic::Topic(std::function<void()>&& cleanup) : m_cleanup(std::move(cleanup)) {}
36 void Topic::publish(const std::any& any)
37 {
38  std::lock_guard<std::mutex> lock(m_mutex);
39  for (auto& sub : m_subs)
40  {
41  sub.lock()->pub(any);
42  }
43 }
44 
45 void Topic::cleanupSubscriber(std::list<std::weak_ptr<Subscriber>>::iterator it)
46 {
47  std::lock_guard<std::mutex> lock(m_mutex);
48  m_subs.erase(it);
49 }
50 
51 // -------------- TopicDirectory ---------------------
53 
54 void TopicDirectory::cleanupTopic(const std::string& key)
55 {
56  std::lock_guard<std::mutex> lock(m_mutex);
57  m_mapService.erase(m_mapService.find(key));
58 }
59 
std::function< void(const std::any &)> m_func
Definition: nodelets.h:47
void cleanupTopic(const std::string &key)
Definition: nodelets.cpp:54
void publish(const std::any &any)
Definition: nodelets.cpp:36
std::mutex m_mutex
Definition: nodelets.h:102
void pub(const std::any &a)
Definition: nodelets.cpp:32
STL namespace.
std::shared_ptr< TopicDirectory > Ptr
Definition: nodelets.h:114
static Ptr create(std::function< void(const std::any &)> &&func, std::function< void()> &&cleanup)
Definition: nodelets.cpp:25
Subscriber(std::function< void(const std::any &)> &&func, std::function< void()> &&cleanup)
Definition: nodelets.cpp:17
std::function< void()> m_cleanup
Definition: nodelets.h:104
std::function< void()> m_cleanup
Definition: nodelets.h:48
std::unordered_map< std::string, std::weak_ptr< Topic > > m_mapService
Definition: nodelets.h:139
std::shared_ptr< Subscriber > Ptr
Definition: nodelets.h:37
void cleanupSubscriber(std::list< std::weak_ptr< Subscriber >>::iterator it)
Definition: nodelets.cpp:45
Serial and networking devices and utilities.
std::list< std::weak_ptr< Subscriber > > m_subs
Definition: nodelets.h:103
The central directory of existing topics for pub/sub.
Definition: nodelets.h:108
Topic(std::function< void()> &&cleanup)
Definition: nodelets.cpp:34



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020