messageparser_base.h
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5  */
6 
7 #pragma once
8 
9 #include <QtPlugin>
10 #include <QApplication>
11 #include <array>
12 #include <unordered_map>
13 #include <unordered_set>
14 #include <functional>
15 #include <map>
16 #include <set>
17 #include "PlotJuggler/plotdata.h"
18 #include "PlotJuggler/pj_plugin.h"
19 
20 namespace PJ
21 {
22 /*
23  * A messgaeParser is a clas that is able to convert a message received by
24  * a DataStreamer plugin into data in PlotDataMapRef.
25  *
26  * - Each data Source has its own instance of MessageParser
27  * - MessageParser objects are created by MessageParserCreator.
28  * - The actual plugin created here is the MessageParserCreator.
29  * - Each DataStreamer plugin has its own set of MessageParserCreator
30  *
31  * */
32 
34 {
35 public:
36  explicit MessageRef(const uint8_t* first_ptr, size_t size)
37  : _ptr(first_ptr), _size(size)
38  {
39  }
40 
41  explicit MessageRef(const std::byte* first_ptr, size_t size)
42  : _ptr(reinterpret_cast<const uint8_t*>(first_ptr)), _size(size)
43  {
44  }
45 
46  explicit MessageRef(const int8_t* first_ptr, size_t size)
47  : _ptr(reinterpret_cast<const uint8_t*>(first_ptr)), _size(size)
48  {
49  }
50 
51  template <typename T>
52  explicit MessageRef(const std::vector<T>& vect)
53  : MessageRef(vect.data(), vect.size())
54  {
55  }
56 
57  const uint8_t* data() const
58  {
59  return _ptr;
60  }
61 
62  uint8_t* data() // this is bad and will be removed
63  {
64  return const_cast<uint8_t*>(_ptr);
65  }
66 
67  size_t size() const
68  {
69  return _size;
70  }
71 
72 private:
73  const uint8_t* _ptr = nullptr;
74  size_t _size = 0;
75 };
76 
82 {
83 public:
84  MessageParser(const std::string& topic_name, PlotDataMapRef& plot_data)
85  : _plot_data(plot_data), _topic_name(topic_name)
86  {
87  }
88  virtual ~MessageParser() = default;
89 
90  virtual bool parseMessage(const MessageRef serialized_msg, double& timestamp) = 0;
91 
92  // Decide what to do if an array is particularly large (size > max_size):
93  //
94  // if clamp == true, then keep the first max_size elements,
95  // otherwise, skip the entire array.
96  virtual void setLargeArraysPolicy(bool clamp, unsigned max_size)
97  {
98  _clamp_large_arrays = clamp;
99  _max_array_size = max_size;
100  }
101 
102  unsigned maxArraySize() const
103  {
104  return _max_array_size;
105  }
106 
107  bool clampLargeArray() const
108  {
109  return _clamp_large_arrays;
110  }
111 
112 protected:
114  std::string _topic_name;
115 
116  PlotData& getSeries(const std::string& key)
117  {
118  return _plot_data.getOrCreateNumeric(key);
119  }
120 
121  StringSeries& getStringSeries(const std::string& key)
122  {
123  return _plot_data.getOrCreateStringSeries(key);
124  }
125 private:
126  bool _clamp_large_arrays = false;
127  unsigned _max_array_size = 10000;
128 };
129 
130 using MessageParserPtr = std::shared_ptr<MessageParser>;
131 
132 //------------- This is the actual plugin interface --------------
134 {
135 public:
136  using Ptr = std::shared_ptr<ParserFactoryPlugin>;
137 
138  // provide an identifier of the provided encoding.
139  // example "ros1", "ros2", "json", "protobuf", etc.
140  virtual const char* encoding() const = 0;
141 
142  // create an instance of MessageParser, already configured to
143  // decode a specific schema.
144  virtual MessageParserPtr createParser(const std::string& topic_name,
145  const std::string& type_name,
146  const std::string& schema,
147  PlotDataMapRef& data) = 0;
148 };
149 
150 using ParserFactories = std::map<QString, std::shared_ptr<ParserFactoryPlugin>>;
151 
152 
153 } // namespace PJ
154 
155 QT_BEGIN_NAMESPACE
156 #define ParserFactoryPlugin_iid "facontidavide.PlotJuggler3.ParserFactoryPlugin"
158 QT_END_NAMESPACE
PlotData & getOrCreateNumeric(const std::string &name, PlotGroup::Ptr group={})
Definition: plotdata.cpp:75
MessageParser(const std::string &topic_name, PlotDataMapRef &plot_data)
virtual void setLargeArraysPolicy(bool clamp, unsigned max_size)
std::map< QString, std::shared_ptr< ParserFactoryPlugin > > ParserFactories
unsigned maxArraySize() const
size_t size() const
PlotDataMapRef & _plot_data
std::shared_ptr< ParserFactoryPlugin > Ptr
std::shared_ptr< MessageParser > MessageParserPtr
StringSeries & getOrCreateStringSeries(const std::string &name, PlotGroup::Ptr group={})
Definition: plotdata.cpp:81
bool clampLargeArray() const
std::string type_name(lua_State *L, type t)
Definition: sol.hpp:8079
MessageRef(const std::vector< T > &vect)
MessageRef(const std::byte *first_ptr, size_t size)
const uint8_t * _ptr
#define ParserFactoryPlugin_iid
const uint8_t * data() const
The MessageParser is the base class used to parse a message with a specific encoding+schema.
PlotData & getSeries(const std::string &key)
MessageRef(const int8_t *first_ptr, size_t size)
StringSeries & getStringSeries(const std::string &key)
Definition: format.h:895
MessageRef(const uint8_t *first_ptr, size_t size)
The PlotJugglerPlugin is the base class of all the plugins.
Definition: pj_plugin.h:22


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