Gazebo Transport

API Reference

12.2.1
gz/transport/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 
167  public: void AddHandler(const std::string &_topic,
168  const std::string &_nUuid,
169  const std::shared_ptr<T> &_handler)
170  {
171  // Create the topic entry.
172  if (this->data.find(_topic) == this->data.end())
173  this->data[_topic] = UUIDHandler_Collection_M();
174 
175  // Create the Node UUID entry.
176  if (this->data[_topic].find(_nUuid) == this->data[_topic].end())
177  this->data[_topic][_nUuid] = UUIDHandler_M();
178 
179  // Add/Replace the Req handler.
180  this->data[_topic][_nUuid].insert(
181  std::make_pair(_handler->HandlerUuid(), _handler));
182  }
183 
188  public: bool HasHandlersForTopic(const std::string &_topic) const
189  {
190  if (this->data.find(_topic) == this->data.end())
191  return false;
192 
193  return !this->data.at(_topic).empty();
194  }
195 
200  public: bool HasHandlersForNode(const std::string &_topic,
201  const std::string &_nUuid) const
202  {
203  if (this->data.find(_topic) == this->data.end())
204  return false;
205 
206  return this->data.at(_topic).find(_nUuid) !=
207  this->data.at(_topic).end();
208  }
209 
216  public: bool RemoveHandler(const std::string &_topic,
217  const std::string &_nUuid,
218  const std::string &_reqUuid)
219  {
220  size_t counter = 0;
221  if (this->data.find(_topic) != this->data.end())
222  {
223  if (this->data[_topic].find(_nUuid) != this->data[_topic].end())
224  {
225  counter = this->data[_topic][_nUuid].erase(_reqUuid);
226  if (this->data[_topic][_nUuid].empty())
227  this->data[_topic].erase(_nUuid);
228  if (this->data[_topic].empty())
229  this->data.erase(_topic);
230  }
231  }
232 
233  return counter > 0;
234  }
235 
240  public: bool RemoveHandlersForNode(const std::string &_topic,
241  const std::string &_nUuid)
242  {
243  size_t counter = 0;
244  if (this->data.find(_topic) != this->data.end())
245  {
246  counter = this->data[_topic].erase(_nUuid);
247  if (this->data[_topic].empty())
248  this->data.erase(_topic);
249  }
250 
251  return counter > 0;
252  }
253 
257  private: TopicServiceCalls_M data;
258  };
259  }
260  }
261 }
262 
263 #endif