PdCom  5.3
Process data communication client
Variable.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * vim:tw=78
3  *
4  * Copyright (C) 2021 Richard Hacker (lerichi at gmx dot net),
5  * Florian Pose (fp at igh dot de),
6  * Bjarne von Horn (vh at igh dot de).
7  *
8  * This file is part of the PdCom library.
9  *
10  * The PdCom library is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or (at your
13  * option) any later version.
14  *
15  * The PdCom library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with the PdCom library. If not, see <http://www.gnu.org/licenses/>.
22  *
23  *****************************************************************************/
24 
27 #ifndef PDCOM5_VARIABLE_H
28 #define PDCOM5_VARIABLE_H
29 
30 #include "DataDeserializer.h"
31 #include "Future.h"
32 #include "Selector.h"
33 #include "SizeTypeInfo.h"
34 #include "details.h"
35 
36 #include <chrono>
37 #include <istream>
38 #include <memory>
39 #include <ostream>
40 #include <pdcom5_export.h>
41 #include <stdint.h>
42 #include <string>
43 #include <vector>
44 
45 namespace PdCom {
46 namespace impl {
47 class Variable;
48 }
49 struct Exception;
50 class Process;
51 class VariablePollResult;
52 
67 class PDCOM5_PUBLIC Variable
68 {
69  friend class impl::Variable;
70 
71  explicit Variable(std::weak_ptr<const impl::Variable> pimpl) :
72  pimpl_(std::move(pimpl))
73  {}
74 
75  public:
76  using PollFuture =
77  Future<PdCom::Exception const &,
79  std::chrono::nanoseconds>;
80 
82 
83 
86  Variable() = default;
87 
102  template <typename T>
103  typename std::enable_if<!std::is_arithmetic<T>::value, SetValueFuture>::type
104  setValue(T const &data, const Selector &selector = {nullptr}) const
105  {
106  static_assert(
107  std::is_same<
108  decltype(data[0]),
109  typename std::add_lvalue_reference<
110  const typename T::value_type>::type>::value,
111  "Index operator does not return a lvalue reference of an "
112  "integral");
113  static_assert(
115  "Container must be contiguous");
116  return setValue(
117  &data[0],
119  data.size(), selector);
120  }
133  template <typename T>
134  typename std::enable_if<std::is_arithmetic<T>::value, SetValueFuture>::type
135  setValue(T const &data, const Selector &selector = {nullptr}) const
136  {
137  return setValue(
138  &data, details::TypeInfoTraits<T>::type_info.type, 1, selector);
139  }
140 
153  template <typename T, size_t M, size_t N>
154  SetValueFuture
155  setValue(const T (&data)[M][N], const Selector &selector = {nullptr}) const
156  {
157  return setValue(
158  data, details::TypeInfoTraits<T>::type_info.type, M * N,
159  selector);
160  }
161 
175  SetValueFuture setValue(
176  const void *src,
177  TypeInfo::DataType src_type,
178  size_t count,
179  const Selector &selector = {nullptr}) const;
180 
195  const void *src,
196  TypeInfo::DataType src_type,
197  size_t count,
198  size_t offset) const;
199 
204  TypeInfo getTypeInfo() const;
209  SizeInfo getSizeInfo() const;
214  std::string getPath() const;
219  std::string getName() const;
224  std::string getAlias() const;
229  int getTaskId() const;
230 
233  bool isWriteable() const;
236  std::chrono::duration<double> getSampleTime() const;
237 
238 
245  bool empty() const noexcept { return (pimpl_.expired()); }
246 
268  PollFuture poll() const;
269 
273  Process *getProcess() const;
274 
275  private:
276  std::weak_ptr<const impl::Variable> pimpl_;
277 };
278 
285 class PDCOM5_PUBLIC VariablePollResult :
286  public DataDeserializer<VariablePollResult>
287 {
288  std::vector<char> data_;
289  Variable variable_;
290 
291  public:
292  explicit VariablePollResult(Variable var) :
293  data_(var.getSizeInfo().totalElements()
294  * var.getTypeInfo().element_size),
295  variable_(var)
296  {}
297 
298  const void *getData() const noexcept { return data_.data(); }
299  void *getData() noexcept { return data_.data(); }
300  Variable getVariable() const noexcept { return variable_; }
301 };
302 
303 } // namespace PdCom
304 
305 #endif // PDCOM5_VARIABLE_H
Definition: Exception.h:33
Selector base class for creating views on multidimensional data.
Definition: Selector.h:47
SizeInfo getSizeInfo() const
Get details about the variable shape.
Callback management handle.
Definition: Future.h:49
std::enable_if<!std::is_arithmetic< T >::value, SetValueFuture >::type setValue(T const &data, const Selector &selector={nullptr}) const
Write to a variable.
Definition: Variable.h:104
Definition: details.h:43
SetValueFuture setValue(const T(&data)[M][N], const Selector &selector={nullptr}) const
Write to a variable.
Definition: Variable.h:155
bool empty() const noexcept
Checks whether this instance is empty.
Definition: Variable.h:245
TypeInfo getTypeInfo() const
Get details about the variable type.
Definition: details.h:183
size_t element_size
Size of one element in bytes.
Definition: SizeTypeInfo.h:56
std::enable_if< std::is_arithmetic< T >::value, SetValueFuture >::type setValue(T const &data, const Selector &selector={nullptr}) const
Write to a variable.
Definition: Variable.h:135
PdCom Variable interface.
Definition: Variable.h:67
Base class for PdCom protocol handler.
Definition: Process.h:86
Result of Variable::poll()
Definition: Variable.h:285
Data Deserialisation helper.
Definition: DataDeserializer.h:50
Definition: ClientStatistics.h:31