MRPT  1.9.9
nodelets.h
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 #pragma once
10 
11 #include <memory> // shared_ptr
12 #include <unordered_map>
13 #include <mutex>
14 #include <list>
15 #include <any>
16 #include <iostream>
17 #include <functional>
20 
21 namespace mrpt
22 {
23 namespace comms
24 {
25 /** Nodelet-like Pub/Sub communications (in #include <mrpt/comms/nodelets.h>)
26  * \ingroup mrpt_comms_grp
27  * @{ */
28 
30 {
31  private:
32  Subscriber(
33  std::function<void(const std::any&)>&& func,
34  std::function<void()>&& cleanup);
35 
36  public:
38  ~Subscriber();
39 
40  static Ptr create(
41  std::function<void(const std::any&)>&& func,
42  std::function<void()>&& cleanup);
43 
44  void pub(const std::any& a);
45 
46  private:
47  std::function<void(const std::any&)> m_func;
48  std::function<void()> m_cleanup;
49 };
50 
51 class Topic : public std::enable_shared_from_this<Topic>
52 {
53  private:
54  Topic(std::function<void()>&& cleanup);
55 
56  public:
58 
59  ~Topic();
60 
61  template <typename ARG, typename Callable>
63  {
64  std::lock_guard<std::mutex> lock(m_mutex);
65  m_subs.emplace_back();
66  auto it = m_subs.end();
67  it--;
68  auto capturedShared = shared_from_this();
69  auto newNode = Subscriber::create(
70  [func{std::forward<Callable>(func)}](const std::any& anyArg) {
71  try
72  {
73  std::invoke(func, std::any_cast<const ARG&>(anyArg));
74  }
75  catch (std::bad_any_cast&)
76  {
77  std::cerr << "Subscriber has wrong type: "
79  << std::endl;
80  }
81  },
82  // cleanup function
83  [=] {
84  // list operations don't invalidate iterators
85  // This iterator capture will be valid
86  capturedShared->cleanupSubscriber(it);
87  });
88  *it = newNode;
89  return newNode;
90  }
91 
92  void publish(const std::any& any);
93  void cleanupSubscriber(std::list<std::weak_ptr<Subscriber>>::iterator it);
94 
95  template <typename CLEANUP>
96  static Ptr create(CLEANUP&& cleanup)
97  {
98  return Ptr(new Topic(std::forward<CLEANUP>(cleanup)));
99  }
100 
101  private:
102  std::mutex m_mutex;
103  std::list<std::weak_ptr<Subscriber>> m_subs;
104  std::function<void()> m_cleanup;
105 };
106 
107 /** The central directory of existing topics for pub/sub */
108 class TopicDirectory : public std::enable_shared_from_this<TopicDirectory>
109 {
110  private:
111  TopicDirectory();
112 
113  public:
115 
116  template <typename PATH>
117  Topic::Ptr getTopic(PATH&& path)
118  {
119  std::lock_guard<std::mutex> lock(m_mutex);
120  auto it = m_mapService.find(path);
121  if (it != m_mapService.end())
122  {
123  auto ptr = it->second.lock();
124  if (ptr) return ptr;
125  }
126 
127  auto capturedShared = shared_from_this();
128  auto newNode =
129  Topic::create([=]() { capturedShared->cleanupTopic(path); });
130  m_mapService.insert({path, newNode});
131  return newNode;
132  }
133 
134  void cleanupTopic(const std::string& key);
135  static Ptr create();
136 
137  private:
138  std::mutex m_mutex;
139  std::unordered_map<std::string, std::weak_ptr<Topic>> m_mapService;
140 };
141 
142 /** @} */ // end grouping
143 
144 } // namespace comms
145 } // namespace mrpt
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
static Ptr create(CLEANUP &&cleanup)
Definition: nodelets.h:96
std::mutex m_mutex
Definition: nodelets.h:102
void pub(const std::any &a)
Definition: nodelets.cpp:33
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
Nodelet-like Pub/Sub communications (in #include <mrpt/comms/nodelets.h>)
Definition: nodelets.h:29
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
Topic::Ptr getTopic(PATH &&path)
Definition: nodelets.h:117
std::unordered_map< std::string, std::weak_ptr< Topic > > m_mapService
Definition: nodelets.h:139
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Subscriber::Ptr createSubscriber(Callable &&func)
Definition: nodelets.h:62
void cleanupSubscriber(std::list< std::weak_ptr< Subscriber >>::iterator it)
Definition: nodelets.cpp:48
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
std::shared_ptr< Topic > Ptr
Definition: nodelets.h:57
static constexpr auto get()
Definition: TTypeName.h:67



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