transform_function.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 PJ_TRANSFORM_FUNCTION_H
8 #define PJ_TRANSFORM_FUNCTION_H
9 
10 #include <QApplication>
11 #include <set>
12 #include <functional>
13 #include "PlotJuggler/plotdata.h"
14 #include "PlotJuggler/pj_plugin.h"
15 
16 namespace PJ
17 {
23 {
24  Q_OBJECT
25 
26 public:
27  using Ptr = std::shared_ptr<TransformFunction>;
28 
30 
31  virtual ~TransformFunction() = default;
32 
33  virtual const char* name() const = 0;
34 
45  virtual int numInputs() const = 0;
46 
50  virtual int numOutputs() const = 0;
51 
53  virtual void reset()
54  {
55  }
56 
58  {
59  return _data;
60  }
61 
62  std::vector<const PlotData*>& dataSources();
63 
64  virtual void setData(PlotDataMapRef* data, const std::vector<const PlotData*>& src_vect,
65  std::vector<PlotData*>& dst_vect);
66 
67  virtual void calculate() = 0;
68 
69  unsigned order() const
70  {
71  return _order;
72  }
73 
74 signals:
75  void parametersChanged();
76 
77 protected:
78  std::vector<const PlotData*> _src_vector;
79  std::vector<PlotData*> _dst_vector;
81 
82  unsigned _order;
83 };
84 
85 using TransformsMap = std::unordered_map<std::string, std::shared_ptr<TransformFunction>>;
86 
89 {
90  Q_OBJECT
91 public:
92  TransformFunction_SISO() = default;
93 
94  void reset() override;
95 
96  int numInputs() const override
97  {
98  return 1;
99  }
100 
101  int numOutputs() const override
102  {
103  return 1;
104  }
105 
106  void calculate() override;
107 
110  virtual std::optional<PlotData::Point> calculateNextPoint(size_t index) = 0;
111 
112  const PlotData* dataSource() const;
113 
114 protected:
115  double _last_timestamp = std::numeric_limits<double>::lowest();
116 };
117 
119 
120 class TransformFactory : public QObject
121 {
122 public:
124  {
125  }
126 
127 private:
128  TransformFactory(const TransformFactory&) = delete;
129  TransformFactory& operator=(const TransformFactory&) = delete;
130 
131  std::map<std::string, std::function<TransformFunction::Ptr()>> creators_;
132  std::map<std::string, QDomDocument> default_params_;
133  std::set<std::string> names_;
134 
135  static TransformFactory* instance();
136 
137 public:
138  static const std::set<std::string>& registeredTransforms();
139 
140  template <typename T>
141  static void registerTransform()
142  {
143  const std::string name = T::transformName();
144  instance()->names_.insert(name);
145  auto doc_it = instance()->default_params_.insert({ name, {} }).first;
146  QDomDocument* document = &(doc_it->second);
147  instance()->creators_[name] = [document]() {
148  auto trans = std::make_shared<T>();
149  T* transform = trans.get();
150 
151  connect(transform, &TransformFunction_SISO::parametersChanged,
152  [transform, document]() {
153  *document = {};
154  auto root = document->createElement("root");
155  document->appendChild(root);
156  transform->xmlSaveState(*document, root);
157  });
158  auto root = document->firstChildElement("root");
159  if (!root.isNull())
160  {
161  QSignalBlocker block(trans.get());
162  trans->xmlLoadState(root);
163  }
164  return trans;
165  };
166  }
167 
168  static TransformFunction::Ptr create(const std::string& name);
169 };
170 
171 } // namespace PJ
172 
173 Q_DECLARE_OPAQUE_POINTER(PJ::TransformFactory*)
174 Q_DECLARE_METATYPE(PJ::TransformFactory*)
175 Q_GLOBAL_STATIC(PJ::TransformFactory, _transform_factory_ptr_from_macro)
176 
177 QT_BEGIN_NAMESPACE
178 
179 #define TransformFunction_iid "facontidavide.PlotJuggler3.TransformFunction"
180 Q_DECLARE_INTERFACE(PJ::TransformFunction, TransformFunction_iid)
181 
182 #define TransformFunctionSISO_iid "facontidavide.PlotJuggler3.TransformFunctionSISO"
184 
185 QT_END_NAMESPACE
186 
187 #endif
detail::first
auto first(const T &value, const Tail &...) -> const T &
Definition: compile.h:60
PJ::TransformFunction::setData
virtual void setData(PlotDataMapRef *data, const std::vector< const PlotData * > &src_vect, std::vector< PlotData * > &dst_vect)
Definition: transform_function.cpp:22
PJ::TimeseriesBase
Definition: timeseries.h:16
PJ::TransformFunction::numOutputs
virtual int numOutputs() const =0
PJ::TransformFunction::dataSources
std::vector< const PlotData * > & dataSources()
Definition: transform_function.cpp:17
PJ::TransformFunction::~TransformFunction
virtual ~TransformFunction()=default
PJ::TransformFactory::TransformFactory
TransformFactory()
Definition: transform_function.h:123
PJ::TransformFunction::numInputs
virtual int numInputs() const =0
PJ::TransformFactory
---— The factory to create instances of a SeriesTransform ----------—
Definition: transform_function.h:120
PJ::TransformFunction::order
unsigned order() const
Definition: transform_function.h:69
PJ::TransformFunction::Ptr
std::shared_ptr< TransformFunction > Ptr
Definition: transform_function.h:27
PJ::TransformFunction_SISO::calculate
void calculate() override
Definition: transform_function.cpp:45
TransformFunctionSISO_iid
#define TransformFunctionSISO_iid
Definition: transform_function.h:182
PJ::TransformFactory::default_params_
std::map< std::string, QDomDocument > default_params_
Definition: transform_function.h:132
PJ::TransformFactory::instance
static TransformFactory * instance()
Definition: transform_function.cpp:89
PJ::TransformFunction_SISO::reset
void reset() override
Definition: transform_function.cpp:40
PJ::TransformsMap
std::unordered_map< std::string, std::shared_ptr< TransformFunction > > TransformsMap
Definition: transform_function.h:85
PJ::TransformFunction_SISO::numOutputs
int numOutputs() const override
Definition: transform_function.h:101
PJ::PlotJugglerPlugin
The PlotJugglerPlugin is the base class of all the plugins.
Definition: pj_plugin.h:22
PJ::TransformFunction::_src_vector
std::vector< const PlotData * > _src_vector
Definition: transform_function.h:78
PJ::TransformFactory::operator=
TransformFactory & operator=(const TransformFactory &)=delete
PJ::TransformFactory::registeredTransforms
static const std::set< std::string > & registeredTransforms()
Definition: transform_function.cpp:108
PJ::TransformFunction::_order
unsigned _order
Definition: transform_function.h:82
PJ::TransformFunction_SISO
Simplified version with Single input and Single output.
Definition: transform_function.h:88
PJ::TransformFunction::calculate
virtual void calculate()=0
PJ::TransformFunction::name
virtual const char * name() const =0
Name of the plugin type, NOT the particular instance.
PJ::TransformFunction
Generic interface for a multi input - multi output transformation function. Contrariwise to other plu...
Definition: transform_function.h:22
TransformFunction_iid
#define TransformFunction_iid
Definition: transform_function.h:179
PJ::TransformFunction::TransformFunction
TransformFunction()
Definition: transform_function.cpp:11
PJ::TransformFunction::_data
PlotDataMapRef * _data
Definition: transform_function.h:80
PJ::TransformFunction_SISO::TransformFunction_SISO
TransformFunction_SISO()=default
PJ::TransformFunction_SISO::calculateNextPoint
virtual std::optional< PlotData::Point > calculateNextPoint(size_t index)=0
plotdata.h
pj_plugin.h
PJ::TransformFactory::names_
std::set< std::string > names_
Definition: transform_function.h:133
block
static void block(LexState *ls)
Definition: lparser.c:1293
PJ::TransformFunction::parametersChanged
void parametersChanged()
PJ::TransformFunction_SISO::numInputs
int numInputs() const override
Definition: transform_function.h:96
PJ
Definition: dataloader_base.h:16
mqtt_test.data
dictionary data
Definition: mqtt_test.py:22
PJ::TransformFunction_SISO::dataSource
const PlotData * dataSource() const
Definition: transform_function.cpp:113
PJ::TransformFunction::plotData
PlotDataMapRef * plotData()
Definition: transform_function.h:57
PJ::TransformFactory::registerTransform
static void registerTransform()
Definition: transform_function.h:141
PJ::TransformFactory::creators_
std::map< std::string, std::function< TransformFunction::Ptr()> > creators_
Definition: transform_function.h:131
PJ::PlotDataMapRef
Definition: plotdata.h:34
PJ::TransformFunction::_dst_vector
std::vector< PlotData * > _dst_vector
Definition: transform_function.h:79
PJ::TransformFunction_SISO::_last_timestamp
double _last_timestamp
Definition: transform_function.h:115
PJ::TransformFactory::create
static TransformFunction::Ptr create(const std::string &name)
Definition: transform_function.cpp:79
PJ::TransformFunction::reset
virtual void reset()
Definition: transform_function.h:53


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