nlohmann_parsers.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 #ifndef NLOHMANN_PARSERS_H
8 #define NLOHMANN_PARSERS_H
9 
10 #include "nlohmann/json.hpp"
12 #include <QDebug>
13 
14 using namespace PJ;
15 
17 {
18 public:
19  NlohmannParser(const std::string& topic_name, PlotDataMapRef& data, bool use_msg_stamp,
20  const std::string& stamp_fieldname)
21  : MessageParser(topic_name, data)
22  , _use_message_stamp(use_msg_stamp)
23  , _stamp_fieldname(stamp_fieldname)
24  {
25  }
26 
27 protected:
28  bool parseMessageImpl(double& timestamp);
29 
32  std::string _stamp_fieldname;
33 };
34 
36 {
37 public:
38  JSON_Parser(const std::string& topic_name, PlotDataMapRef& data, bool use_msg_stamp,
39  const std::string& stamp_fieldname)
40  : NlohmannParser(topic_name, data, use_msg_stamp, stamp_fieldname)
41  {
42  }
43 
44  bool parseMessage(const MessageRef msg, double& timestamp) override;
45 };
46 
48 {
49 public:
50  CBOR_Parser(const std::string& topic_name, PlotDataMapRef& data, bool use_msg_stamp,
51  const std::string& stamp_fieldname)
52  : NlohmannParser(topic_name, data, use_msg_stamp, stamp_fieldname)
53  {
54  }
55 
56  bool parseMessage(const MessageRef msg, double& timestamp) override;
57 };
58 
60 {
61 public:
62  BSON_Parser(const std::string& topic_name, PlotDataMapRef& data, bool use_msg_stamp,
63  const std::string& stamp_fieldname)
64  : NlohmannParser(topic_name, data, use_msg_stamp, stamp_fieldname)
65  {
66  }
67 
68  bool parseMessage(const MessageRef msg, double& timestamp) override;
69 };
70 
72 {
73 public:
74  MessagePack_Parser(const std::string& topic_name, PlotDataMapRef& data,
75  bool use_msg_stamp, const std::string& stamp_fieldname)
76  : NlohmannParser(topic_name, data, use_msg_stamp, stamp_fieldname)
77  {
78  }
79 
80  bool parseMessage(const MessageRef msg, double& timestamp) override;
81 };
82 
83 //------------------------------------------
84 
85 #include <QGroupBox>
86 #include <QVBoxLayout>
87 #include <QLineEdit>
88 
89 class QCheckBoxClose : public QGroupBox
90 {
91 public:
92  QLineEdit* lineedit;
93  QGroupBox* groupbox;
94  QCheckBoxClose(QString text) : QGroupBox(text)
95  {
96  QGroupBox::setCheckable(true);
97  QGroupBox::setChecked(false);
98  lineedit = new QLineEdit(this);
99  QVBoxLayout* vbox = new QVBoxLayout;
100  vbox->addSpacing(20);
101  vbox->addWidget(lineedit);
102  QGroupBox::setLayout(vbox);
103  }
104  ~QCheckBoxClose() override
105  {
106  qDebug() << "Destroying QCheckBoxClose";
107  }
108 };
109 
111 {
112 public:
114  {
115  _checkbox_use_timestamp = new QCheckBoxClose("use field as timestamp if available");
116  }
117 
118  template <typename ParserT>
119  MessageParserPtr createParserImpl(const std::string& topic_name,
121  {
122  std::string timestamp_name = _checkbox_use_timestamp->lineedit->text().toStdString();
123  return std::make_shared<ParserT>(
124  topic_name, data, _checkbox_use_timestamp->isChecked(), timestamp_name);
125  }
126 
127  virtual QWidget* optionsWidget()
128  {
129  return _checkbox_use_timestamp;
130  }
131 
132 protected:
134 };
135 
137 {
138 public:
139  MessageParserPtr createParser(const std::string& topic_name,
140  const std::string& /*type_name*/,
141  const std::string& /*schema*/,
142  PlotDataMapRef& data) override
143  {
144  return createParserImpl<JSON_Parser>(topic_name, data);
145  }
146  const char* name() const override
147  {
148  return "JSON_ParserFactory";
149  }
150  const char* encoding() const override
151  {
152  return "json";
153  }
154 };
155 
157 {
158 public:
159  MessageParserPtr createParser(const std::string& topic_name,
160  const std::string&,
161  const std::string&,
162  PlotDataMapRef& data) override
163  {
164  return createParserImpl<CBOR_Parser>(topic_name, data);
165  }
166  const char* name() const override
167  {
168  return "CBOR_ParserFactory";
169  }
170  const char* encoding() const override
171  {
172  return "cbor";
173  }
174 };
175 
177 {
178 public:
179  MessageParserPtr createParser(const std::string& topic_name,
180  const std::string&,
181  const std::string&,
182  PlotDataMapRef& data) override
183  {
184  return createParserImpl<BSON_Parser>(topic_name, data);
185  }
186  const char* name() const override
187  {
188  return "BSON_ParserFactory";
189  }
190  const char* encoding() const override
191  {
192  return "bson";
193  }
194 };
195 
197 {
198 public:
199  MessageParserPtr createParser(const std::string& topic_name,
200  const std::string& ,
201  const std::string& ,
202  PlotDataMapRef& data) override
203  {
204  return createParserImpl<MessagePack_Parser>(topic_name, data);
205  }
206  const char* name() const override
207  {
208  return "MessagePack_ParserFactory";
209  }
210  const char* encoding() const override
211  {
212  return "msgpack";
213  }
214 };
215 
216 #endif // NLOHMANN_PARSERS_H
const char * name() const override
Name of the plugin type, NOT the particular instance.
a class to store JSON values
Definition: json.hpp:3326
NlohmannParser(const std::string &topic_name, PlotDataMapRef &data, bool use_msg_stamp, const std::string &stamp_fieldname)
MessagePack_Parser(const std::string &topic_name, PlotDataMapRef &data, bool use_msg_stamp, const std::string &stamp_fieldname)
const char * name() const override
Name of the plugin type, NOT the particular instance.
MessageParserPtr createParser(const std::string &topic_name, const std::string &, const std::string &, PlotDataMapRef &data) override
MessageParserPtr createParser(const std::string &topic_name, const std::string &, const std::string &, PlotDataMapRef &data) override
virtual QWidget * optionsWidget()
optionsWidget pointer to a persistent widget used to set the plugin options .
const char * encoding() const override
nlohmann::json _json
std::shared_ptr< MessageParser > MessageParserPtr
MessageParserPtr createParserImpl(const std::string &topic_name, PlotDataMapRef &data)
const char * encoding() const override
QCheckBoxClose * _checkbox_use_timestamp
MessageParserPtr createParser(const std::string &topic_name, const std::string &, const std::string &, PlotDataMapRef &data) override
QCheckBoxClose(QString text)
CBOR_Parser(const std::string &topic_name, PlotDataMapRef &data, bool use_msg_stamp, const std::string &stamp_fieldname)
BSON_Parser(const std::string &topic_name, PlotDataMapRef &data, bool use_msg_stamp, const std::string &stamp_fieldname)
QLineEdit * lineedit
MessageParserPtr createParser(const std::string &topic_name, const std::string &, const std::string &, PlotDataMapRef &data) override
QGroupBox * groupbox
const char * name() const override
Name of the plugin type, NOT the particular instance.
The MessageParser is the base class used to parse a message with a specific encoding+schema.
std::string _stamp_fieldname
~QCheckBoxClose() override
dictionary data
Definition: mqtt_test.py:22
const char * encoding() const override
const char * name() const override
Name of the plugin type, NOT the particular instance.
Definition: format.h:895
const char * encoding() const override
JSON_Parser(const std::string &topic_name, PlotDataMapRef &data, bool use_msg_stamp, const std::string &stamp_fieldname)


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