MRPT  1.9.9
nodelets.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://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 
25 
27  std::function<void(const std::any&)>&& func,
28  std::function<void()>&& cleanup)
29 {
30  return Ptr(new Subscriber(std::move(func), std::move(cleanup)));
31 }
32 
33 void Subscriber::pub(const std::any& a) { m_func(a); }
34 
35 // ------- Topic --------------
36 Topic::Topic(std::function<void()>&& cleanup) : m_cleanup(std::move(cleanup)) {}
38 
39 void Topic::publish(const std::any& any)
40 {
41  std::lock_guard<std::mutex> lock(m_mutex);
42  for (auto& sub : m_subs)
43  {
44  sub.lock()->pub(any);
45  }
46 }
47 
48 void Topic::cleanupSubscriber(std::list<std::weak_ptr<Subscriber>>::iterator it)
49 {
50  std::lock_guard<std::mutex> lock(m_mutex);
51  m_subs.erase(it);
52 }
53 
54 // -------------- TopicDirectory ---------------------
56 
58 {
59  std::lock_guard<std::mutex> lock(m_mutex);
60  m_mapService.erase(m_mapService.find(key));
61 }
62 
Scalar * iterator
Definition: eigen_plugins.h:26
std::function< void(const std::any &)> m_func
Definition: nodelets.h:47
void cleanupTopic(const std::string &key)
Definition: nodelets.cpp:57
void publish(const std::any &any)
Definition: nodelets.cpp:39
std::mutex m_mutex
Definition: nodelets.h:102
void pub(const std::any &a)
Definition: nodelets.cpp:33
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:26
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
GLsizei const GLchar ** string
Definition: glext.h:4101
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:48
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
GLubyte GLubyte GLubyte a
Definition: glext.h:6279
Topic(std::function< void()> &&cleanup)
Definition: nodelets.cpp:36



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020