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 <map>
12 #include "PlotJuggler/plotdata.h"
13 #include "PlotJuggler/pj_plugin.h"
14 
15 namespace PJ
16 {
17 /*
18  * A messageParser is a class that is able to convert a message received by
19  * a DataStreamer plugin into data in PlotDataMapRef.
20  *
21  * - Each data Source has its own instance of MessageParser
22  * - MessageParser objects are created by MessageParserCreator.
23  * - The actual plugin created here is the MessageParserCreator.
24  * - Each DataStreamer plugin has its own set of MessageParserCreator
25  *
26  * */
27 
29 {
30 public:
31  explicit MessageRef(const uint8_t* first_ptr, size_t size)
32  : _ptr(first_ptr), _size(size)
33  {
34  }
35 
36  explicit MessageRef(const std::byte* first_ptr, size_t size)
37  : _ptr(reinterpret_cast<const uint8_t*>(first_ptr)), _size(size)
38  {
39  }
40 
41  explicit MessageRef(const int8_t* first_ptr, size_t size)
42  : _ptr(reinterpret_cast<const uint8_t*>(first_ptr)), _size(size)
43  {
44  }
45 
46  template <typename T>
47  explicit MessageRef(const std::vector<T>& vect) : MessageRef(vect.data(), vect.size())
48  {
49  }
50 
51  const uint8_t* data() const
52  {
53  return _ptr;
54  }
55 
56  uint8_t* data() // this is bad and will be removed
57  {
58  return const_cast<uint8_t*>(_ptr);
59  }
60 
61  size_t size() const
62  {
63  return _size;
64  }
65 
66 private:
67  const uint8_t* _ptr = nullptr;
68  size_t _size = 0;
69 };
70 
76 {
77 public:
78  MessageParser(const std::string& topic_name, PlotDataMapRef& plot_data)
79  : _plot_data(plot_data), _topic_name(topic_name)
80  {
81  }
82  virtual ~MessageParser() = default;
83 
84  virtual bool parseMessage(const MessageRef serialized_msg, double& timestamp) = 0;
85 
86  // Decide what to do if an array is particularly large (size > max_size):
87  //
88  // if clamp == true, then keep the first max_size elements,
89  // otherwise, skip the entire array.
90  virtual void setLargeArraysPolicy(bool clamp, unsigned max_size)
91  {
92  _clamp_large_arrays = clamp;
93  _max_array_size = max_size;
94  }
95 
96  unsigned maxArraySize() const
97  {
98  return _max_array_size;
99  }
100 
101  bool clampLargeArray() const
102  {
103  return _clamp_large_arrays;
104  }
105 
106  virtual bool useEmbeddedTimestamp() const
107  {
109  }
110 
111  virtual void enableEmbeddedTimestamp(bool enable)
112  {
114  }
115 
116 protected:
118  std::string _topic_name;
119 
120  PlotData& getSeries(const std::string& key)
121  {
122  return _plot_data.getOrCreateNumeric(key);
123  }
124 
125  StringSeries& getStringSeries(const std::string& key)
126  {
128  }
129 
130 private:
131  bool _clamp_large_arrays = false;
132  unsigned _max_array_size = 10000;
134 };
135 
136 using MessageParserPtr = std::shared_ptr<MessageParser>;
137 
138 //------------- This is the actual plugin interface --------------
140 {
141 public:
142  using Ptr = std::shared_ptr<ParserFactoryPlugin>;
143 
144  // provide an identifier of the provided encoding.
145  // example "ros1", "ros2", "json", "protobuf", etc.
146  // If more than one, separate the name using semicolon
147  virtual const char* encoding() const = 0;
148 
149  // create an instance of MessageParser, already configured to
150  // decode a specific schema.
151  virtual MessageParserPtr createParser(const std::string& topic_name,
152  const std::string& type_name,
153  const std::string& schema,
154  PlotDataMapRef& data) = 0;
155 };
156 
157 using ParserFactories = std::map<QString, std::shared_ptr<ParserFactoryPlugin>>;
158 
159 } // namespace PJ
160 
161 QT_BEGIN_NAMESPACE
162 #define ParserFactoryPlugin_iid "facontidavide.PlotJuggler3.ParserFactoryPlugin"
164 QT_END_NAMESPACE
PJ::ParserFactoryPlugin::createParser
virtual MessageParserPtr createParser(const std::string &topic_name, const std::string &type_name, const std::string &schema, PlotDataMapRef &data)=0
PJ::TimeseriesBase
Definition: timeseries.h:16
PJ::MessageParserPtr
std::shared_ptr< MessageParser > MessageParserPtr
Definition: messageparser_base.h:136
PJ::MessageParser::_max_array_size
unsigned _max_array_size
Definition: messageparser_base.h:132
PJ::MessageParser::_topic_name
std::string _topic_name
Definition: messageparser_base.h:118
PJ::MessageRef::data
uint8_t * data()
Definition: messageparser_base.h:56
PJ::MessageRef::data
const uint8_t * data() const
Definition: messageparser_base.h:51
PJ::MessageParser::MessageParser
MessageParser(const std::string &topic_name, PlotDataMapRef &plot_data)
Definition: messageparser_base.h:78
PJ::MessageRef::MessageRef
MessageRef(const std::byte *first_ptr, size_t size)
Definition: messageparser_base.h:36
sol::type_name
std::string type_name(lua_State *L, type t)
Definition: sol.hpp:8079
PJ::MessageParser::useEmbeddedTimestamp
virtual bool useEmbeddedTimestamp() const
Definition: messageparser_base.h:106
PJ::MessageParser::maxArraySize
unsigned maxArraySize() const
Definition: messageparser_base.h:96
PJ::MessageRef::MessageRef
MessageRef(const std::vector< T > &vect)
Definition: messageparser_base.h:47
PJ::MessageRef::_ptr
const uint8_t * _ptr
Definition: messageparser_base.h:67
PJ::PlotJugglerPlugin
The PlotJugglerPlugin is the base class of all the plugins.
Definition: pj_plugin.h:22
PJ::MessageParser::_plot_data
PlotDataMapRef & _plot_data
Definition: messageparser_base.h:117
PJ::ParserFactoryPlugin::encoding
virtual const char * encoding() const =0
PJ::MessageParser::enableEmbeddedTimestamp
virtual void enableEmbeddedTimestamp(bool enable)
Definition: messageparser_base.h:111
PJ::MessageRef::MessageRef
MessageRef(const int8_t *first_ptr, size_t size)
Definition: messageparser_base.h:41
PJ::MessageParser
The MessageParser is the base class used to parse a message with a specific encoding+schema.
Definition: messageparser_base.h:75
PJ::MessageRef::MessageRef
MessageRef(const uint8_t *first_ptr, size_t size)
Definition: messageparser_base.h:31
PJ::MessageParser::getSeries
PlotData & getSeries(const std::string &key)
Definition: messageparser_base.h:120
sol::meta::enable
std::enable_if_t< all< Args... >::value, enable_t > enable
Definition: sol.hpp:2244
PJ::ParserFactoryPlugin
Definition: messageparser_base.h:139
PJ::MessageParser::getStringSeries
StringSeries & getStringSeries(const std::string &key)
Definition: messageparser_base.h:125
PJ::MessageParser::parseMessage
virtual bool parseMessage(const MessageRef serialized_msg, double &timestamp)=0
PJ::PlotDataMapRef::getOrCreateNumeric
PlotData & getOrCreateNumeric(const std::string &name, PlotGroup::Ptr group={})
Definition: plotdata.cpp:75
PJ::ParserFactoryPlugin::Ptr
std::shared_ptr< ParserFactoryPlugin > Ptr
Definition: messageparser_base.h:142
PJ::MessageParser::_use_embedded_timestamp
bool _use_embedded_timestamp
Definition: messageparser_base.h:133
plotdata.h
pj_plugin.h
ParserFactoryPlugin_iid
#define ParserFactoryPlugin_iid
Definition: messageparser_base.h:162
PJ::MessageParser::_clamp_large_arrays
bool _clamp_large_arrays
Definition: messageparser_base.h:131
PJ::ParserFactories
std::map< QString, std::shared_ptr< ParserFactoryPlugin > > ParserFactories
Definition: messageparser_base.h:157
PJ::MessageParser::setLargeArraysPolicy
virtual void setLargeArraysPolicy(bool clamp, unsigned max_size)
Definition: messageparser_base.h:90
PJ::MessageRef
Definition: messageparser_base.h:28
PJ
Definition: dataloader_base.h:16
mqtt_test.data
dictionary data
Definition: mqtt_test.py:22
PJ::MessageRef::size
size_t size() const
Definition: messageparser_base.h:61
PJ::PlotDataMapRef::getOrCreateStringSeries
StringSeries & getOrCreateStringSeries(const std::string &name, PlotGroup::Ptr group={})
Definition: plotdata.cpp:81
PJ::PlotDataMapRef
Definition: plotdata.h:34
PJ::MessageParser::clampLargeArray
bool clampLargeArray() const
Definition: messageparser_base.h:101
PJ::MessageRef::_size
size_t _size
Definition: messageparser_base.h:68
PJ::StringSeries
Definition: stringseries.h:17
PJ::MessageParser::~MessageParser
virtual ~MessageParser()=default


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:45