Gazebo Transport

API Reference

13.4.1
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::transport
30 {
31  // Inline bracket to help doxygen filtering.
32  inline namespace GZ_TRANSPORT_VERSION_NAMESPACE {
33  //
37  template<typename T> class HandlerStorage
38  {
45 
47  using TopicServiceCalls_M =
49 
51  public: HandlerStorage() = default;
52 
54  public: virtual ~HandlerStorage() = default;
55 
64  public: bool Handlers(const std::string &_topic,
66  std::map<std::string, std::shared_ptr<T> >> &_handlers) const
67  {
68  if (this->data.find(_topic) == this->data.end())
69  return false;
70 
71  _handlers = this->data.at(_topic);
72  return true;
73  }
74 
82  public: bool FirstHandler(const std::string &_topic,
83  const std::string &_reqTypeName,
84  const std::string &_repTypeName,
85  std::shared_ptr<T> &_handler) const
86  {
87  if (this->data.find(_topic) == this->data.end())
88  return false;
89 
90  const auto &m = this->data.at(_topic);
91  for (const auto &node : m)
92  {
93  for (const auto &handler : node.second)
94  {
95  if (_reqTypeName == handler.second->ReqTypeName() &&
96  _repTypeName == handler.second->RepTypeName())
97  {
98  _handler = handler.second;
99  return true;
100  }
101  }
102  }
103  return false;
104  }
105 
112  public: bool FirstHandler(const std::string &_topic,
113  const std::string &_msgTypeName,
114  std::shared_ptr<T> &_handler) const
115  {
116  if (this->data.find(_topic) == this->data.end())
117  return false;
118 
119  const auto &m = this->data.at(_topic);
120  for (const auto &node : m)
121  {
122  for (const auto &handler : node.second)
123  {
124  if (_msgTypeName == handler.second->TypeName() ||
125  handler.second->TypeName() == kGenericMessageType)
126  {
127  _handler = handler.second;
128  return true;
129  }
130  }
131  }
132  return false;
133  }
134 
141  public: bool Handler(const std::string &_topic,
142  const std::string &_nUuid,
143  const std::string &_hUuid,
144  std::shared_ptr<T> &_handler) const
145  {
146  if (this->data.find(_topic) == this->data.end())
147  return false;
148 
149  auto const &m = this->data.at(_topic);
150  if (m.find(_nUuid) == m.end())
151  return false;
152 
153  if (m.at(_nUuid).find(_hUuid) == m.at(_nUuid).end())
154  return false;
155 
156  _handler = m.at(_nUuid).at(_hUuid);
157  return true;
158  }
159 
163  {
164  return this->data;
165  }
166 
172  public: void AddHandler(const std::string &_topic,
173  const std::string &_nUuid,
174  const std::shared_ptr<T> &_handler)
175  {
176  // Create the topic entry.
177  if (this->data.find(_topic) == this->data.end())
178  this->data[_topic] = UUIDHandler_Collection_M();
179 
180  // Create the Node UUID entry.
181  if (this->data[_topic].find(_nUuid) == this->data[_topic].end())
182  this->data[_topic][_nUuid] = UUIDHandler_M();
183 
184  // Add/Replace the Req handler.
185  this->data[_topic][_nUuid].insert(
186  std::make_pair(_handler->HandlerUuid(), _handler));
187  }
188 
193  public: bool HasHandlersForTopic(const std::string &_topic) const
194  {
195  if (this->data.find(_topic) == this->data.end())
196  return false;
197 
198  return !this->data.at(_topic).empty();
199  }
200 
205  public: bool HasHandlersForNode(const std::string &_topic,
206  const std::string &_nUuid) const
207  {
208  if (this->data.find(_topic) == this->data.end())
209  return false;
210 
211  return this->data.at(_topic).find(_nUuid) !=
212  this->data.at(_topic).end();
213  }
214 
221  public: bool RemoveHandler(const std::string &_topic,
222  const std::string &_nUuid,
223  const std::string &_reqUuid)
224  {
225  size_t counter = 0;
226  if (this->data.find(_topic) != this->data.end())
227  {
228  if (this->data[_topic].find(_nUuid) != this->data[_topic].end())
229  {
230  counter = this->data[_topic][_nUuid].erase(_reqUuid);
231  if (this->data[_topic][_nUuid].empty())
232  this->data[_topic].erase(_nUuid);
233  if (this->data[_topic].empty())
234  this->data.erase(_topic);
235  }
236  }
237 
238  return counter > 0;
239  }
240 
245  public: bool RemoveHandlersForNode(const std::string &_topic,
246  const std::string &_nUuid)
247  {
248  size_t counter = 0;
249  if (this->data.find(_topic) != this->data.end())
250  {
251  counter = this->data[_topic].erase(_nUuid);
252  if (this->data[_topic].empty())
253  this->data.erase(_topic);
254  }
255 
256  return counter > 0;
257  }
258 
262  private: TopicServiceCalls_M data;
263  };
264  }
265 }
266 
267 #endif