PdCom  4.2
Process data communication client
Variable.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * vim:tw=78
3  * $Id$
4  *
5  * Copyright (C) 2016 Richard Hacker (lerichi at gmx dot net)
6  *
7  * This file is part of the PdCom library.
8  *
9  * The PdCom library is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or (at your
12  * option) any later version.
13  *
14  * The PdCom library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with the PdCom library. If not, see <http://www.gnu.org/licenses/>.
21  *
22  *****************************************************************************/
23 
26 #ifndef PDCOM_VARIABLE_H
27 #define PDCOM_VARIABLE_H
28 
29 #include <string>
30 #include <vector>
31 #include <ostream>
32 #include <istream>
33 #include <stdint.h>
34 
35 namespace PdCom {
36 
37 class Process;
38 
43 class Variable {
44  public:
46 
47  enum Type {
48  boolean_T = 0,
49  uint8_T, int8_T,
50  uint16_T, int16_T,
51  uint32_T, int32_T,
52  uint64_T, int64_T,
53  double_T, single_T
54  };
55 
57 
77  class Subscription {
78  public:
81  int id,
82  const Variable* v,
83  int decimation,
84  const uint64_t* const* time,
85  const char* const* data);
86 
87  Process* const process;
88  const int id;
89  const Variable* const variable;
90  const int decimation;
98  const uint64_t* const* const time_ns;
99 
105  virtual void cancel() const = 0;
106 
117  bool poll() const;
118 
126  template <typename T>
127  void getValue(T* val, size_t index, size_t nelem) const;
128 
133  std::string getStringValue(char delimiter = ',') const;
134 
142  void print(std::ostream& os, char delimiter) const;
143 
144  protected:
145  ~Subscription();
146 
147  private:
148 
149  const char* const * const data;
150  };
151 
153 
154  Variable( Type type,
155  double sampleTime,
156  size_t task,
157  bool writeable,
158  const std::string& alias,
159  size_t ndim,
160  const size_t *dims);
161 
162  virtual ~Variable();
163 
164  const Type type;
165  const char* const ctype;
178  const double sampleTime;
180  const size_t task;
184  const bool writeable;
186  const std::string alias;
189  virtual std::string name() const = 0;
190 
192  virtual std::string path() const = 0;
193 
194  const size_t nelem;
195  const std::vector<size_t> dim;
196  const size_t typeWidth;
197  const size_t bytes;
200  bool isScalar() const;
201 
207  size_t isVector() const;
208 
213  bool setStringValue(const std::string& value) const;
214 
215 
225  template <typename T>
226  bool setValue(const T* val,
227  size_t offset, size_t nelem) const;
228 
236  bool read(std::istream& is) const;
237 
238  protected:
239 
243  virtual bool poll(const Subscription*) const { return true; }
244 
246  virtual bool setValue(const void*, size_t /*UserTypeIdx*/,
247  size_t /*index*/, size_t /*nelem*/) const {
248  return true;
249  }
250 
252  void copyValue(char* dst, const void* src,
253  size_t UserTypeIdx, size_t n) const;
254 
256  void printValue(std::ostream& os, const char* buf, char delim) const;
257 
260  private:
261 
262  typedef void (Variable::*GetValueFunc_T)
263  (void* dst, size_t nelem, const char* src, size_t offset) const;
264  const GetValueFunc_T* const getValueFunc;
265 
266  typedef void (Variable::*CopyValueFunc_T)
267  (char* dst, const void* src, size_t nelem) const;
268  const CopyValueFunc_T* const copyValueFunc;
269 
270  typedef void (Variable::*PrintValueFunc_T)
271  (std::ostream& os, const char* data, char delim) const;
272  const PrintValueFunc_T printValueFunc;
273 
274  template <class CType>
275  void printFcn(
276  std::ostream& os, const char* buf, char delim) const;
277  static const PrintValueFunc_T printValueFuncList[];
278 
279  template <class CType>
280  bool readFcn(std::istream& is) const;
281  typedef bool (Variable::*ReadValueFunc_T)
282  (std::istream& is) const;
283  const ReadValueFunc_T readValueFunc;
284  static const ReadValueFunc_T readValueFuncList[];
285 
286  template <class CType, class UserType>
287  void copyValue(char* dst, const void* src, size_t nelem) const;
288  template <class CType, class UserType>
289  void getValue(void* dst, size_t nelem,
290  const char* src, size_t offset) const;
291  template <class CType>
292  struct DataConverter {
293  static const GetValueFunc_T getValueFunc[];
294  static const CopyValueFunc_T copyValueFunc[];
295  };
296  static const GetValueFunc_T* const getValueFuncList[];
297  static const CopyValueFunc_T* const copyValueFuncList[];
298 };
299 
300 }
301 
303 std::ostream& operator<< (std::ostream& os,
304  const PdCom::Variable::Subscription& subscription);
305 
307 std::istream& operator>> (std::istream& is,
308  const PdCom::Variable& variable);
309 
310 #endif //PDCOM_VARIABLE_H
Type
Variable&#39;s type.
Definition: Variable.h:47
bool setValue(const T *val, size_t offset, size_t nelem) const
Set a parameter&#39;s value.
Subscription(Process *process, int id, const Variable *v, int decimation, const uint64_t *const *time, const char *const *data)
Constructor.
Variable(Type type, double sampleTime, size_t task, bool writeable, const std::string &alias, size_t ndim, const size_t *dims)
Constructor.
const bool writeable
Can be written to, for parameters.
Definition: Variable.h:184
bool setStringValue(const std::string &value) const
Set a parameter&#39;s value.
const size_t bytes
memory required to store everything
Definition: Variable.h:197
std::istream & operator>>(std::istream &is, const PdCom::Variable &variable)
Stream operator to write a value to a Variable.
const size_t nelem
Number of elements.
Definition: Variable.h:194
const int id
id supplied during Process::subscribe()
Definition: Variable.h:88
size_t isVector() const
Test for a vector.
bool poll() const
Poll a subscription.
virtual std::string path() const =0
complete path, including name()
const size_t typeWidth
sizeof(datatype)
Definition: Variable.h:196
const size_t task
Task id of signals.
Definition: Variable.h:180
Process variable.
Definition: Variable.h:43
const double sampleTime
sample time of variable; 0 for parameters
Definition: Variable.h:178
Process *const process
pointer to process
Definition: Variable.h:87
const uint64_t *const *const time_ns
Time of value in nanoseconds since epoch.
Definition: Variable.h:98
Base class for PdCom protocol handler.
Definition: Process.h:93
const Type type
Data type.
Definition: Variable.h:164
Subscription class.
Definition: Variable.h:77
std::string getStringValue(char delimiter=',') const
Get the entire value as a linear string.
void print(std::ostream &os, char delimiter) const
Write the value to std::ostream.
const char *const ctype
c-type string One of:
Definition: Variable.h:165
bool isScalar() const
Scalar test.
const int decimation
Subscription decimation.
Definition: Variable.h:90
std::ostream & operator<<(std::ostream &os, const PdCom::Variable::Subscription &subscription)
Stream operator to dump a Subscription to std::ostream.
const std::string alias
Optional alias.
Definition: Variable.h:186
bool read(std::istream &is) const
Set a parameter&#39;s value.
virtual std::string name() const =0
Short name without leading path.
Definition: Process.h:37
const std::vector< size_t > dim
Dimension array.
Definition: Variable.h:195
virtual void cancel() const =0
Cancel subscription.
const Variable *const variable
Actual variable.
Definition: Variable.h:89
void getValue(T *val, size_t index, size_t nelem) const
Retrieve subscriptions value in any data type.