PdCom  5.3
Process data communication client
Loading...
Searching...
No Matches
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
26
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
45namespace PdCom {
46namespace impl {
47class Variable;
48}
49struct Exception;
50class Process;
52
67class 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 =
79 std::chrono::nanoseconds>;
80
81 using SetValueFuture = Future<PdCom::Exception const &>;
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 }
121
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(
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
194 PdCom::Variable::SetValueFuture setValue(
195 const void *src,
196 TypeInfo::DataType src_type,
197 size_t count,
198 size_t offset) const;
199
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
274
275 private:
276 std::weak_ptr<const impl::Variable> pimpl_;
277};
278
285class 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
Callback management handle.
Definition Future.h:50
Base class for PdCom protocol handler.
Definition Process.h:87
Size of a Variable.
Definition SizeTypeInfo.h:67
Result of Variable::poll()
Definition Variable.h:287
PdCom Variable interface.
Definition Variable.h:68
SetValueFuture setValue(const void *src, TypeInfo::DataType src_type, size_t count, const Selector &selector={nullptr}) const
Write to a variable.
PdCom::Variable::SetValueFuture setValue(const void *src, TypeInfo::DataType src_type, size_t count, size_t offset) const
Write to a variable.
bool isWriteable() const
std::string getPath() const
The Path of the variable.
Process * getProcess() const
Get the assigned Process.
Variable()=default
Constructs an empty variable.
std::chrono::duration< double > getSampleTime() const
PollFuture poll() const
Read a variable without subscription.
TypeInfo getTypeInfo() const
Get details about the variable type.
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
std::string getName() const
The name of the variable.
SizeInfo getSizeInfo() const
Get details about the variable shape.
std::string getAlias() const
The alias of the variable, if set.
bool empty() const noexcept
Checks whether this instance is empty.
Definition Variable.h:245
int getTaskId() const
The task id of the variable.
SetValueFuture setValue(const T(&data)[M][N], const Selector &selector={nullptr}) const
Write to a variable.
Definition Variable.h:155
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
Data Deserialisation helper.
Definition DataDeserializer.h:51
Definition Exception.h:34
Selector base class for creating views on multidimensional data.
Definition Selector.h:48
Type of a Variable.
Definition SizeTypeInfo.h:34
size_t element_size
Size of one element in bytes.
Definition SizeTypeInfo.h:56
Definition details.h:44
Definition details.h:184