Gazebo Transport

API Reference

13.4.0
HandlerStorage.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 
18 #ifndef GZ_TRANSPORT_HANDLERSTORAGE_HH_
19 #define GZ_TRANSPORT_HANDLERSTORAGE_HH_
20 
21 #include <map>
22 #include <memory>
23 #include <string>
24 #include <utility>
25 
26 #include "gz/transport/config.hh"
28 
29 namespace gz
30 {
31  namespace transport
32  {
33  // Inline bracket to help doxygen filtering.
34  inline namespace GZ_TRANSPORT_VERSION_NAMESPACE {
35  //
39  template<typename T> class HandlerStorage
40  {
47 
49  using TopicServiceCalls_M =
51 
53  public: HandlerStorage() = default;
54 
56  public: virtual ~HandlerStorage() = default;
57 
66  public: bool Handlers(const std::string &_topic,
68  std::map<std::string, std::shared_ptr<T> >> &_handlers) const
69  {
70  if (this->data.find(_topic) == this->data.end())
71  return false;
72 
73  _handlers = this->data.at(_topic);
74  return true;
75  }
76 
84  public: bool FirstHandler(const std::string &_topic,
85  const std::string &_reqTypeName,
86  const std::string &_repTypeName,
87  std::shared_ptr<T> &_handler) const
88  {
89  if (this->data.find(_topic) == this->data.end())
90  return false;
91 
92  const auto &m = this->data.at(_topic);
93  for (const auto &node : m)
94  {
95  for (const auto &handler : node.second)
96  {
97  if (_reqTypeName == handler.second->ReqTypeName() &&
98  _repTypeName == handler.second->RepTypeName())
99  {
100  _handler = handler.second;
101  return true;
102  }
103  }
104  }
105  return false;
106  }
107 
114  public: bool FirstHandler(const std::string &_topic,
115  const std::string &_msgTypeName,
116  std::shared_ptr<T> &_handler) const
117  {
118  if (this->data.find(_topic) == this->data.end())
119  return false;
120 
121  const auto &m = this->data.at(_topic);
122  for (const auto &node : m)
123  {
124  for (const auto &handler : node.second)
125  {
126  if (_msgTypeName == handler.second->TypeName() ||
127  handler.second->TypeName() == kGenericMessageType)
128  {
129  _handler = handler.second;
130  return true;
131  }
132  }
133  }
134  return false;
135  }
136 
143  public: bool Handler(const std::string &_topic,
144  const std::string &_nUuid,
145  const std::string &_hUuid,
146  std::shared_ptr<T> &_handler) const
147  {
148  if (this->data.find(_topic) == this->data.end())
149  return false;
150 
151  auto const &m = this->data.at(_topic);
152  if (m.find(_nUuid) == m.end())
153  return false;
154 
155  if (m.at(_nUuid).find(_hUuid) == m.at(_nUuid).end())
156  return false;
157 
158  _handler = m.at(_nUuid).at(_hUuid);
159  return true;
160  }
161 
165  {
166  return this->data;
167  }
168 
174  public: void AddHandler(const std::string &_topic,
175  const std::string &_nUuid,
176  const std::shared_ptr<T> &_handler)
177  {
178  // Create the topic entry.
179  if (this->data.find(_topic) == this->data.end())
180  this->data[_topic] = UUIDHandler_Collection_M();
181 
182  // Create the Node UUID entry.
183  if (this->data[_topic].find(_nUuid) == this->data[_topic].end())
184  this->data[_topic][_nUuid] = UUIDHandler_M();
185 
186  // Add/Replace the Req handler.
187  this->data[_topic][_nUuid].insert(
188  std::make_pair(_handler->HandlerUuid(), _handler));
189  }
190 
195  public: bool HasHandlersForTopic(const std::string &_topic) const
196  {
197  if (this->data.find(_topic) == this->data.end())
198  return false;
199 
200  return !this->data.at(_topic).empty();
201  }
202 
207  public: bool HasHandlersForNode(const std::string &_topic,
208  const std::string &_nUuid) const
209  {
210  if (this->data.find(_topic) == this->data.end())
211  return false;
212 
213  return this->data.at(_topic).find(_nUuid) !=
214  this->data.at(_topic).end();
215  }
216 
223  public: bool RemoveHandler(const std::string &_topic,
224  const std::string &_nUuid,
225  const std::string &_reqUuid)
226  {
227  size_t counter = 0;
228  if (this->data.find(_topic) != this->data.end())
229  {
230  if (this->data[_topic].find(_nUuid) != this->data[_topic].end())
231  {
232  counter = this->data[_topic][_nUuid].erase(_reqUuid);
233  if (this->data[_topic][_nUuid].empty())
234  this->data[_topic].erase(_nUuid);
235  if (this->data[_topic].empty())
236  this->data.erase(_topic);
237  }
238  }
239 
240  return counter > 0;
241  }
242 
247  public: bool RemoveHandlersForNode(const std::string &_topic,
248  const std::string &_nUuid)
249  {
250  size_t counter = 0;
251  if (this->data.find(_topic) != this->data.end())
252  {
253  counter = this->data[_topic].erase(_nUuid);
254  if (this->data[_topic].empty())
255  this->data.erase(_topic);
256  }
257 
258  return counter > 0;
259  }
260 
264  private: TopicServiceCalls_M data;
265  };
266  }
267  }
268 }
269 
270 #endif