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 struct FlatMessage {
33 
34  std::shared_ptr<MessageSchema> schema;
35 
38  std::vector< std::pair<FieldsVector, Variant> > value;
39 
41  std::vector< std::pair<FieldsVector, std::string> > name;
42 
45  std::vector< std::pair<FieldsVector, Span<const uint8_t>>> blob;
46 
47  std::vector<std::vector<uint8_t>> blob_storage;
48 };
49 
50 
51 class Parser{
52 
53 public:
63  Parser(const std::string& topic_name,
64  const ROSType& msg_type,
65  const std::string& definition);
66 
67  enum MaxArrayPolicy: bool {
68  DISCARD_LARGE_ARRAYS = true,
69  KEEP_LARGE_ARRAYS = false
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 
94  enum BlobPolicy {
96  STORE_BLOB_AS_REFERENCE};
97 
98  // If set to STORE_BLOB_AS_COPY, a copy of the original vector will be stored in the FlatMessage.
99  // This may have a large impact on performance.
100  // if STORE_BLOB_AS_REFERENCE is used instead, it is dramatically faster, but you must be careful with
101  // dangling pointers.
102  void setBlobPolicy( BlobPolicy policy )
103  {
104  _blob_policy = policy;
105  }
106 
108  {
109  return _blob_policy;
110  }
111 
115  const std::shared_ptr<MessageSchema>& getSchema() const;
116 
117  ROSMessage::Ptr getMessageByType(const ROSType& type) const;
118 
141  FlatMessage* flat_output,
142  Deserializer *deserializer) const;
143 
144  typedef std::function<void(const ROSType&, Span<uint8_t>&)> VisitingCallback;
145 
158  void applyVisitorToBuffer(const ROSType &msg_type,
159  Span<uint8_t> &buffer,
160  VisitingCallback callback) const;
161 
163  void setWarningsStream(std::ostream* output) { _global_warnings = output; }
164 
165 private:
166 
167  std::shared_ptr<MessageSchema> _schema;
168 
169  std::ostream* _global_warnings;
170 
171  std::string _topic_name;
172 
173  std::vector<int> _alias_array_pos;
174  std::vector<std::string> _formatted_string;
175  std::vector<int8_t> _substituted;
179  std::shared_ptr<ROSField> _dummy_root_field;
180 
181  std::unique_ptr<Deserializer> _deserializer;
182 };
183 
184 typedef std::vector<std::pair<std::string, double> > RenamedValues;
185 
186 void CreateRenamedValues(const FlatMessage& flat_msg, RenamedValues& renamed);
187 
188 
189 template <class DeserializerT>
191 
192 public:
193 
195  {
196  _deserializer = std::make_unique<DeserializerT>();
197  }
198 
199  void registerParser(const std::string& topic_name,
200  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,
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{
240  };
241  std::unordered_map<std::string, CachedPack> _pack;
242  std::vector<uint8_t> _buffer;
243  std::unique_ptr<Deserializer> _deserializer;
244 };
245 
246 
247 
248 }
249 
std::string _topic_name
Definition: ros_parser.hpp:171
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:41
size_t maxArraySize() const
Definition: ros_parser.hpp:89
std::vector< std::pair< FieldsVector, Span< const uint8_t > > > blob
Definition: ros_parser.hpp:45
std::shared_ptr< MessageSchema > _schema
Definition: ros_parser.hpp:167
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...
std::vector< uint8_t > _buffer
Definition: ros_parser.hpp:242
std::shared_ptr< ROSMessage > Ptr
Definition: ros_message.hpp:37
BlobPolicy blobPolicy() const
Definition: ros_parser.hpp:107
std::unique_ptr< Deserializer > _deserializer
Definition: ros_parser.hpp:243
type
Definition: core.h:1059
Definition: core.h:760
const FlatMessage * deserialize(const std::string &topic_name, Span< const uint8_t > buffer)
Definition: ros_parser.hpp:221
std::unique_ptr< Deserializer > _deserializer
Definition: ros_parser.hpp:181
void setWarningsStream(std::ostream *output)
Change where the warning messages are displayed.
Definition: ros_parser.hpp:163
void registerParser(const std::string &topic_name, const ROSType &msg_type, const std::string &definition)
Definition: ros_parser.hpp:199
std::vector< std::pair< std::string, double > > RenamedValues
Definition: ros_parser.hpp:184
void setBlobPolicy(BlobPolicy policy)
Definition: ros_parser.hpp:102
BlobPolicy _blob_policy
Definition: ros_parser.hpp:178
MaxArrayPolicy _discard_large_array
Definition: ros_parser.hpp:176
std::ostream * _global_warnings
Definition: ros_parser.hpp:169
const T & move(const T &v)
Definition: backward.hpp:394
std::vector< std::string > _formatted_string
Definition: ros_parser.hpp:174
std::shared_ptr< MessageSchema > schema
Definition: ros_parser.hpp:34
std::unordered_map< std::string, CachedPack > _pack
Definition: ros_parser.hpp:241
def msg_type(f)
void setMaxArrayPolicy(MaxArrayPolicy discard_entire_array, size_t max_array_size)
Definition: ros_parser.hpp:74
std::vector< std::vector< uint8_t > > blob_storage
Definition: ros_parser.hpp:47
static unordered_set< string > deserialize(const QDomElement &elt)
std::vector< int > _alias_array_pos
Definition: ros_parser.hpp:173
std::function< void(const ROSType &, Span< uint8_t > &)> VisitingCallback
Definition: ros_parser.hpp:144
static const char * output
Definition: luac.c:38
const char * definition()
std::shared_ptr< ROSField > _dummy_root_field
Definition: ros_parser.hpp:179
std::vector< int8_t > _substituted
Definition: ros_parser.hpp:175
std::vector< std::pair< FieldsVector, Variant > > value
Definition: ros_parser.hpp:38
void CreateRenamedValues(const FlatMessage &flat_msg, RenamedValues &renamed)
const Parser * getParser(const std::string &topic_name) const
Definition: ros_parser.hpp:211
MaxArrayPolicy maxArrayPolicy() const
Definition: ros_parser.hpp:84


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Jun 19 2023 03:01:39