PdCom  5.0
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 #include "pdcom5_export.h"
36 
37 #include <chrono>
38 #include <istream>
39 #include <memory>
40 #include <ostream>
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 VariablePollResult;
51 
66 class PDCOM5_PUBLIC Variable
67 {
68  friend class impl::Variable;
69 
70  explicit Variable(std::weak_ptr<const impl::Variable> pimpl) :
71  pimpl_(std::move(pimpl))
72  {}
73 
74  public:
75  using PollFuture =
76  Future<PdCom::Exception const &,
78  std::chrono::nanoseconds>;
79 
81 
82 
85  Variable() = default;
86 
101  template <typename T>
102  typename std::enable_if<!std::is_arithmetic<T>::value, SetValueFuture>::type
103  setValue(T const &data, const Selector &selector = {nullptr}) const
104  {
105  static_assert(
106  std::is_same<
107  decltype(data[0]),
108  typename std::add_lvalue_reference<
109  const typename T::value_type>::type>::value,
110  "Index operator does not return a lvalue reference of an "
111  "integral");
112  static_assert(
114  "Container must be contiguous");
115  return setValue(
116  &data[0],
118  data.size(), selector);
119  }
132  template <typename T>
133  typename std::enable_if<std::is_arithmetic<T>::value, SetValueFuture>::type
134  setValue(T const &data, const Selector &selector = {nullptr}) const
135  {
136  return setValue(
137  &data, details::TypeInfoTraits<T>::type_info.type, 1, selector);
138  }
139 
152  template <typename T, size_t M, size_t N>
153  SetValueFuture
154  setValue(const T (&data)[M][N], const Selector &selector = {nullptr}) const
155  {
156  return setValue(
157  data, details::TypeInfoTraits<T>::type_info.type, M * N,
158  selector);
159  }
160 
174  SetValueFuture setValue(
175  const void *src,
176  TypeInfo::DataType src_type,
177  size_t count,
178  const Selector &selector = {nullptr}) const;
179 
194  const void *src,
195  TypeInfo::DataType src_type,
196  size_t count,
197  size_t offset) const;
198 
203  TypeInfo getTypeInfo() const;
208  SizeInfo getSizeInfo() const;
213  std::string getPath() const;
218  std::string getName() const;
223  std::string getAlias() const;
228  int getTaskId() const;
229 
232  bool isWriteable() const;
235  std::chrono::duration<double> getSampleTime() const;
236 
237 
244  bool empty() const noexcept { return (pimpl_.expired()); }
245 
267  PollFuture poll() const;
268 
269  private:
270  std::weak_ptr<const impl::Variable> pimpl_;
271 };
272 
279 class PDCOM5_PUBLIC VariablePollResult :
280  public DataDeserializer<VariablePollResult>
281 {
282  std::vector<char> data_;
283  Variable variable_;
284 
285  public:
286  explicit VariablePollResult(Variable var) :
287  data_(var.getSizeInfo().totalElements()
288  * var.getTypeInfo().element_size),
289  variable_(var)
290  {}
291 
292  const void *getData() const noexcept { return data_.data(); }
293  void *getData() noexcept { return data_.data(); }
294  Variable getVariable() const noexcept { return variable_; }
295 };
296 
297 } // namespace PdCom
298 
299 #endif // PDCOM5_VARIABLE_H
Definition: Exception.h:34
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:50
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:103
Definition: details.h:46
SetValueFuture setValue(const T(&data)[M][N], const Selector &selector={nullptr}) const
Write to a variable.
Definition: Variable.h:154
bool empty() const noexcept
Checks whether this instance is empty.
Definition: Variable.h:244
TypeInfo getTypeInfo() const
Get details about the variable type.
Definition: details.h:186
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:134
PdCom Variable interface.
Definition: Variable.h:66
Result of Variable::poll()
Definition: Variable.h:279
Data Deserialisation helper.
Definition: DataDeserializer.h:50
library version string as "major.minor.patch"
Definition: ClientStatistics.h:31