ros_parser.hpp
Go to the documentation of this file.
1 /***** MIT License ****
2  *
3  * Copyright (c) 2016-2022 Davide Faconti
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 #pragma once
24 
25 #include <unordered_set>
26 
29 
30 namespace RosMsgParser
31 {
32 
34 {
35  std::shared_ptr<MessageSchema> schema;
36 
39  std::vector<std::pair<FieldsVector, Variant>> value;
40 
42  std::vector<std::pair<FieldsVector, std::string>> name;
43 
46  std::vector<std::pair<FieldsVector, Span<const uint8_t>>> blob;
47 
48  std::vector<std::vector<uint8_t>> blob_storage;
49 };
50 
51 class Parser
52 {
53 public:
63  Parser(const std::string& topic_name, const ROSType& msg_type,
64  const std::string& definition);
65 
66  enum MaxArrayPolicy : bool
67  {
70  };
71 
74  void setMaxArrayPolicy(MaxArrayPolicy discard_entire_array, size_t max_array_size)
75  {
76  _discard_large_array = discard_entire_array;
77  _max_array_size = max_array_size;
78  if (_max_array_size > 10000)
79  {
80  throw std::runtime_error("max_array_size limited to 10000 at most");
81  }
82  }
83 
85  {
86  return _discard_large_array;
87  }
88 
89  size_t maxArraySize() const
90  {
91  return _max_array_size;
92  }
93 
95  {
98  };
99 
100  // If set to STORE_BLOB_AS_COPY, a copy of the original vector will be stored in the
101  // FlatMessage. This may have a large impact on performance. if STORE_BLOB_AS_REFERENCE
102  // is used instead, it is dramatically faster, but you must be careful with dangling
103  // pointers.
105  {
106  _blob_policy = policy;
107  }
108 
110  {
111  return _blob_policy;
112  }
113 
117  const std::shared_ptr<MessageSchema>& getSchema() const;
118 
119  ROSMessage::Ptr getMessageByType(const ROSType& type) const;
120 
142  bool deserialize(Span<const uint8_t> buffer, FlatMessage* flat_output,
143  Deserializer* deserializer) const;
144 
145  typedef std::function<void(const ROSType&, Span<uint8_t>&)> VisitingCallback;
146 
162  void applyVisitorToBuffer(const ROSType& msg_type, Span<uint8_t>& buffer,
163  VisitingCallback callback) const;
164 
166  void setWarningsStream(std::ostream* output)
167  {
169  }
170 
171 private:
172  std::shared_ptr<MessageSchema> _schema;
173 
174  std::ostream* _global_warnings;
175 
176  std::string _topic_name;
177 
178  std::vector<int> _alias_array_pos;
179  std::vector<std::string> _formatted_string;
180  std::vector<int8_t> _substituted;
184  std::shared_ptr<ROSField> _dummy_root_field;
185 
186  std::unique_ptr<Deserializer> _deserializer;
187 };
188 
189 typedef std::vector<std::pair<std::string, double>> RenamedValues;
190 
191 template <class DeserializerT>
193 {
194 public:
196  {
197  _deserializer = std::make_unique<DeserializerT>();
198  }
199 
200  void registerParser(const std::string& topic_name, const ROSType& msg_type,
201  const std::string& definition)
202  {
203  if (_pack.count(topic_name) == 0)
204  {
205  Parser parser(topic_name, msg_type, definition);
206  CachedPack pack = { std::move(parser), {} };
207  _pack.insert({ topic_name, std::move(pack) });
208  }
209  }
210 
211  const Parser* getParser(const std::string& topic_name) const
212  {
213  auto it = _pack.find(topic_name);
214  if (it != _pack.end())
215  {
216  return &it->second.parser;
217  }
218  return nullptr;
219  }
220 
221  const FlatMessage* deserialize(const std::string& topic_name,
222  Span<const uint8_t> buffer)
223  {
224  auto it = _pack.find(topic_name);
225  if (it != _pack.end())
226  {
227  CachedPack& pack = it->second;
228  Parser& parser = pack.parser;
229 
230  parser.deserialize(buffer, &pack.msg, _deserializer.get());
231  return &pack.msg;
232  }
233  return nullptr;
234  }
235 
236 private:
237  struct CachedPack
238  {
241  };
242  std::unordered_map<std::string, CachedPack> _pack;
243  std::vector<uint8_t> _buffer;
244  std::unique_ptr<Deserializer> _deserializer;
245 };
246 
247 } // namespace RosMsgParser
RosMsgParser::Parser::BlobPolicy
BlobPolicy
Definition: ros_parser.hpp:94
RosMsgParser::RenamedValues
std::vector< std::pair< std::string, double > > RenamedValues
Definition: ros_parser.hpp:189
RosMsgParser::ParsersCollection::registerParser
void registerParser(const std::string &topic_name, const ROSType &msg_type, const std::string &definition)
Definition: ros_parser.hpp:200
RosMsgParser::ROSMessage::Ptr
std::shared_ptr< ROSMessage > Ptr
Definition: ros_message.hpp:37
RosMsgParser::Parser::_blob_policy
BlobPolicy _blob_policy
Definition: ros_parser.hpp:183
RosMsgParser::ParsersCollection::CachedPack::parser
Parser parser
Definition: ros_parser.hpp:239
RosMsgParser::Parser::setWarningsStream
void setWarningsStream(std::ostream *output)
Change where the warning messages are displayed.
Definition: ros_parser.hpp:166
RosMsgParser::ROSType
ROSType.
Definition: ros_type.hpp:39
RosMsgParser::Parser::_global_warnings
std::ostream * _global_warnings
Definition: ros_parser.hpp:174
RosMsgParser::FlatMessage::blob_storage
std::vector< std::vector< uint8_t > > blob_storage
Definition: ros_parser.hpp:48
RosMsgParser::Parser
Definition: ros_parser.hpp:51
RosMsgParser::Parser::setBlobPolicy
void setBlobPolicy(BlobPolicy policy)
Definition: ros_parser.hpp:104
RosMsgParser::FlatMessage::schema
std::shared_ptr< MessageSchema > schema
Definition: ros_parser.hpp:35
definition
const char * definition()
RosMsgParser::Parser::_discard_large_array
MaxArrayPolicy _discard_large_array
Definition: ros_parser.hpp:181
RosMsgParser::Parser::setMaxArrayPolicy
void setMaxArrayPolicy(MaxArrayPolicy discard_entire_array, size_t max_array_size)
Definition: ros_parser.hpp:74
RosMsgParser::Parser::_formatted_string
std::vector< std::string > _formatted_string
Definition: ros_parser.hpp:179
RosMsgParser::ParsersCollection::CachedPack
Definition: ros_parser.hpp:237
RosMsgParser::Parser::_alias_array_pos
std::vector< int > _alias_array_pos
Definition: ros_parser.hpp:178
RosMsgParser::ParsersCollection::_deserializer
std::unique_ptr< Deserializer > _deserializer
Definition: ros_parser.hpp:244
RosMsgParser::ParsersCollection::_buffer
std::vector< uint8_t > _buffer
Definition: ros_parser.hpp:243
RosMsgParser::Parser::applyVisitorToBuffer
void applyVisitorToBuffer(const ROSType &msg_type, Span< uint8_t > &buffer, VisitingCallback callback) const
applyVisitorToBuffer is used to pass a callback that is invoked every time a chunk of memory storing ...
stringtree_leaf.hpp
RosMsgParser::Parser::_substituted
std::vector< int8_t > _substituted
Definition: ros_parser.hpp:180
RosMsgParser::Parser::_dummy_root_field
std::shared_ptr< ROSField > _dummy_root_field
Definition: ros_parser.hpp:184
RosMsgParser::Parser::VisitingCallback
std::function< void(const ROSType &, Span< uint8_t > &)> VisitingCallback
Definition: ros_parser.hpp:145
RosMsgParser::ParsersCollection::deserialize
const FlatMessage * deserialize(const std::string &topic_name, Span< const uint8_t > buffer)
Definition: ros_parser.hpp:221
RosMsgParser::ParsersCollection::CachedPack::msg
FlatMessage msg
Definition: ros_parser.hpp:240
RosMsgParser
Definition: builtin_types.hpp:34
RosMsgParser::Parser::maxArrayPolicy
MaxArrayPolicy maxArrayPolicy() const
Definition: ros_parser.hpp:84
RosMsgParser::Parser::KEEP_LARGE_ARRAYS
@ KEEP_LARGE_ARRAYS
Definition: ros_parser.hpp:69
output
static const char * output
Definition: luac.c:38
RosMsgParser::Parser::Parser
Parser(const std::string &topic_name, const ROSType &msg_type, const std::string &definition)
Definition: rosx_introspection/src/ros_parser.cpp:36
RosMsgParser::Parser::_topic_name
std::string _topic_name
Definition: ros_parser.hpp:176
backward::details::move
const T & move(const T &v)
Definition: backward.hpp:394
RosMsgParser::Parser::MaxArrayPolicy
MaxArrayPolicy
Definition: ros_parser.hpp:66
RosMsgParser::Parser::_max_array_size
size_t _max_array_size
Definition: ros_parser.hpp:182
nlohmann::detail::void
j template void())
Definition: json.hpp:4061
RosMsgParser::Parser::maxArraySize
size_t maxArraySize() const
Definition: ros_parser.hpp:89
udp_client.parser
parser
Definition: udp_client.py:9
RosMsgParser::FlatMessage::blob
std::vector< std::pair< FieldsVector, Span< const uint8_t > > > blob
Definition: ros_parser.hpp:46
RosMsgParser::Parser::STORE_BLOB_AS_COPY
@ STORE_BLOB_AS_COPY
Definition: ros_parser.hpp:96
RosMsgParser::Deserializer
Definition: deserializer.hpp:20
RosMsgParser::Parser::deserialize
bool deserialize(Span< const uint8_t > buffer, FlatMessage *flat_output, Deserializer *deserializer) const
deserializeIntoFlatContainer takes a raw buffer of memory and extract information from it....
Definition: rosx_introspection/src/ros_parser.cpp:76
RosMsgParser::FlatMessage::name
std::vector< std::pair< FieldsVector, std::string > > name
List of all those parsed fields that can be represented by a "string".
Definition: ros_parser.hpp:42
RosMsgParser::FlatMessage::value
std::vector< std::pair< FieldsVector, Variant > > value
Definition: ros_parser.hpp:39
RosMsgParser::Parser::STORE_BLOB_AS_REFERENCE
@ STORE_BLOB_AS_REFERENCE
Definition: ros_parser.hpp:97
RosMsgParser::Parser::_schema
std::shared_ptr< MessageSchema > _schema
Definition: ros_parser.hpp:172
nonstd::span_lite::span
Definition: span.hpp:581
RosMsgParser::ParsersCollection::_pack
std::unordered_map< std::string, CachedPack > _pack
Definition: ros_parser.hpp:242
RosMsgParser::ParsersCollection::ParsersCollection
ParsersCollection()
Definition: ros_parser.hpp:195
RosMsgParser::ParsersCollection
Definition: ros_parser.hpp:192
RosMsgParser::Parser::DISCARD_LARGE_ARRAYS
@ DISCARD_LARGE_ARRAYS
Definition: ros_parser.hpp:68
RosMsgParser::Parser::getMessageByType
ROSMessage::Ptr getMessageByType(const ROSType &type) const
Definition: rosx_introspection/src/ros_parser.cpp:54
deserializer.hpp
RosMsgParser::Parser::blobPolicy
BlobPolicy blobPolicy() const
Definition: ros_parser.hpp:109
msg_type
def msg_type(f)
RosMsgParser::ParsersCollection::getParser
const Parser * getParser(const std::string &topic_name) const
Definition: ros_parser.hpp:211
RosMsgParser::Parser::getSchema
const std::shared_ptr< MessageSchema > & getSchema() const
getSchema provides some metadata amout a registered ROSMessage.
Definition: rosx_introspection/src/ros_parser.cpp:49
RosMsgParser::FlatMessage
Definition: ros_parser.hpp:33
RosMsgParser::Parser::_deserializer
std::unique_ptr< Deserializer > _deserializer
Definition: ros_parser.hpp:186


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Aug 11 2024 02:24:25