QtPdCom  1.0.0
ScalarVariable.h
Go to the documentation of this file.
1/*****************************************************************************
2 *
3 * Copyright (C) 2009-2022 Florian Pose <fp@igh.de>
4 *
5 * This file is part of the QtPdCom library.
6 *
7 * The QtPdCom library is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * The QtPdCom library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with the QtPdCom Library. If not, see <http://www.gnu.org/licenses/>.
19 *
20 ****************************************************************************/
21
22#ifndef QTPDCOM_SCALARVARIABLE_H
23#define QTPDCOM_SCALARVARIABLE_H
24
25#include "ScalarSubscriber.h"
26#include "Export.h"
27
28#include <QObject>
29
30namespace QtPdCom {
31
32/****************************************************************************/
33
37 public QObject, public ScalarSubscriber
38{
39 Q_OBJECT
40
41 public:
42 using QObject::QObject;
43
44 struct Exception {
47 Exception(const QString &msg):
48 msg(msg) {}
49 QString msg;
50 };
51
52 template <typename T>
53 typename std::enable_if<std::is_arithmetic<T>::value, void>::type
54 copyData(T &dest) const
55 {
56 PdCom::details::copyData(
57 &dest, PdCom::details::TypeInfoTraits<T>::type_info.type,
58 getData(),
59 getVariable().getTypeInfo().type, 1);
60 }
61
62 signals:
63 void valueChanged();
65};
66
67/****************************************************************************/
68
71template <class T>
74{
75 public:
76 ScalarVariable(QObject *parent = nullptr);
77 virtual ~ScalarVariable();
78
79 void clearData(); // pure-virtual from ScalarSubscriber
80 bool hasData() const;
81
82 T getValue() const;
83 std::chrono::nanoseconds getMTime() const;
84 void inc();
85
86 private:
89 std::chrono::nanoseconds mTime;
92 // pure-virtual from ScalarSubscriber
93 void newValues(std::chrono::nanoseconds) override;
94};
95
96/****************************************************************************/
97
101
102/****************************************************************************/
103
106template <class T>
109 value((T) 0),
110 dataPresent(false)
111{
112}
113
114/****************************************************************************/
115
118template <class T>
120{
121}
122
123/****************************************************************************/
124
125template <class T>
127{
128 value = ((T) 0);
129 dataPresent = false;
130 emit valueChanged();
131}
132
133/****************************************************************************/
134
138template <class T>
139inline bool ScalarVariable<T>::hasData() const
140{
141 return dataPresent;
142}
143
144/****************************************************************************/
145
149template <class T>
151{
152 return value;
153}
154
155/****************************************************************************/
156
160template <class T>
161inline std::chrono::nanoseconds
163{
164 return mTime;
165}
166
167/****************************************************************************/
168
173template <class T>
175{
176 writeValue(value + 1);
177}
178
179/****************************************************************************/
180
184template <class T>
185void ScalarVariable<T>::newValues(std::chrono::nanoseconds ts)
186{
187 T newValue;
188
189 copyData(newValue);
190 newValue = newValue * scale + offset;
191 mTime = std::chrono::nanoseconds(ts);
192
193 if (newValue != value || !dataPresent) {
194 value = newValue;
195 dataPresent = true;
196 emit valueChanged();
197 }
198}
199
200/****************************************************************************/
201
202} // namespace
203
204#endif
#define QTPDCOM_PUBLIC
Definition: Export.h:30
Abstract Scalar Value.
Definition: ScalarVariable.h:38
void valueChanged()
Emitted, when the value changes, or the variable is disconnected.
std::enable_if< std::is_arithmetic< T >::value, void >::type copyData(T &dest) const
Definition: ScalarVariable.h:54
Subscriber of a single scalar value.
Definition: ScalarSubscriber.h:40
Scalar Value Template.
Definition: ScalarVariable.h:74
void clearData()
Definition: ScalarVariable.h:126
std::chrono::nanoseconds mTime
Modification Time of Current value.
Definition: ScalarVariable.h:89
T value
Current value.
Definition: ScalarVariable.h:87
bool hasData() const
Definition: ScalarVariable.h:139
void newValues(std::chrono::nanoseconds) override
This virtual method is called by the ProcessVariable, if its value changes.
Definition: ScalarVariable.h:185
std::chrono::nanoseconds getMTime() const
Definition: ScalarVariable.h:162
virtual ~ScalarVariable()
Destructor.
Definition: ScalarVariable.h:119
bool dataPresent
There is a process value to display.
Definition: ScalarVariable.h:90
void inc()
Increments the current value and writes it to the process.
Definition: ScalarVariable.h:174
ScalarVariable(QObject *parent=nullptr)
Constructor.
Definition: ScalarVariable.h:107
T getValue() const
Definition: ScalarVariable.h:150
Definition: Message.h:31
ScalarVariable< bool > BoolVariable
Definition: ScalarVariable.h:98
ScalarVariable< int > IntVariable
Definition: ScalarVariable.h:99
ScalarVariable< double > DoubleVariable
Definition: ScalarVariable.h:100
Definition: ScalarVariable.h:44
QString msg
Exception message.
Definition: ScalarVariable.h:49
Exception(const QString &msg)
Constructor.
Definition: ScalarVariable.h:47