QtPdCom  1.5.2
ScalarVariable.h
Go to the documentation of this file.
1/*****************************************************************************
2 *
3 * Copyright (C) 2009-2025 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,
38 public ScalarSubscriber
39{
40 Q_OBJECT
41
42 public:
43 using QObject::QObject;
44
45 struct Exception
46 {
49 Exception(const QString &msg):
50 msg(msg)
51 {}
52 QString msg;
53 };
54
55 template <typename T>
56 typename std::enable_if<std::is_arithmetic<T>::value, void>::type
57 copyData(T &dest) const
58 {
59 PdCom::details::copyData(
60 &dest,
61 PdCom::details::TypeInfoTraits<T>::type_info.type,
62 getData(),
63 getVariable().getTypeInfo().type,
64 1);
65 }
66
67 signals:
68 void valueChanged();
70};
71
72/****************************************************************************/
73
76template <class T> class ScalarVariable: public AbstractScalarVariable
77{
78 public:
79 ScalarVariable(QObject *parent = nullptr);
80 virtual ~ScalarVariable();
81
82 void clearData();
83 bool hasData() const;
84
85 T getValue() const;
86 std::chrono::nanoseconds getMTime() const;
87 void inc();
88
89 private:
92 std::chrono::nanoseconds mTime;
95 // pure-virtual from ScalarSubscriber
96 void newValues(std::chrono::nanoseconds) override;
97};
98
99/****************************************************************************/
100
104
105/****************************************************************************/
106
109template <class T> ScalarVariable<T>::ScalarVariable(QObject *parent):
111 value((T) 0),
112 dataPresent(false)
113{}
114
115/****************************************************************************/
116
120{}
121
122/****************************************************************************/
123
124template <class T> void ScalarVariable<T>::clearData()
125{
126 value = ((T) 0);
127 dataPresent = false;
128 emit valueChanged();
129}
130
131/****************************************************************************/
132
136template <class T> inline bool ScalarVariable<T>::hasData() const
137{
138 return dataPresent;
139}
140
141/****************************************************************************/
142
146template <class T> inline T ScalarVariable<T>::getValue() const
147{
148 return value;
149}
150
151/****************************************************************************/
152
156template <class T>
157inline std::chrono::nanoseconds ScalarVariable<T>::getMTime() const
158{
159 return mTime;
160}
161
162/****************************************************************************/
163
168template <class T> void ScalarVariable<T>::inc()
169{
170 writeValue(value + 1);
171}
172
173/****************************************************************************/
174
177template <class T>
178void ScalarVariable<T>::newValues(std::chrono::nanoseconds ts)
179{
180 T newValue;
181
182 copyData(newValue);
183 newValue = newValue * scale + offset;
184 mTime = std::chrono::nanoseconds(ts);
185
186 if (newValue != value || !dataPresent) {
187 value = newValue;
188 dataPresent = true;
189 emit valueChanged();
190 }
191}
192
193/****************************************************************************/
194
195} // namespace QtPdCom
196
197#endif
#define QTPDCOM_PUBLIC
Definition Export.h:30
Abstract Scalar Value.
Definition ScalarVariable.h:39
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:57
Subscriber of a single scalar value.
Definition ScalarSubscriber.h:40
Scalar Value Template.
Definition ScalarVariable.h:77
void inc()
Increments the current value and writes it to the process.
Definition ScalarVariable.h:168
void clearData()
Definition ScalarVariable.h:124
std::chrono::nanoseconds mTime
Modification Time of Current value.
Definition ScalarVariable.h:92
T value
Current value.
Definition ScalarVariable.h:90
std::chrono::nanoseconds getMTime() const
Definition ScalarVariable.h:157
T getValue() const
Definition ScalarVariable.h:146
bool dataPresent
There is a process value to display.
Definition ScalarVariable.h:93
ScalarVariable(QObject *parent=nullptr)
Constructor.
Definition ScalarVariable.h:109
void newValues(std::chrono::nanoseconds) override
This virtual method is called if the process variable's value changed.
Definition ScalarVariable.h:178
virtual ~ScalarVariable()
Destructor.
Definition ScalarVariable.h:119
bool hasData() const
Definition ScalarVariable.h:136
Definition BroadcastModel.h:32
ScalarVariable< bool > BoolVariable
Definition ScalarVariable.h:101
ScalarVariable< int > IntVariable
Definition ScalarVariable.h:102
ScalarVariable< double > DoubleVariable
Definition ScalarVariable.h:103
Definition ScalarVariable.h:46
QString msg
Exception message.
Definition ScalarVariable.h:52
Exception(const QString &msg)
Constructor.
Definition ScalarVariable.h:49