GCC Code Coverage Report


Directory: ./
File: qtpdcom/QtPdCom1/ScalarVariable.h
Date: 2026-11-29 04:09:07
Exec Total Coverage
Lines: 0 31 0.0%
Branches: 0 32 0.0%

Line Branch Exec Source
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
30 namespace QtPdCom {
31
32 /****************************************************************************/
33
34 /** Abstract Scalar Value.
35 */
36 ✗ class QTPDCOM_PUBLIC AbstractScalarVariable:
37 public QObject,
38 public ScalarSubscriber
39 {
40 Q_OBJECT
41
42 public:
43 ✗ using QObject::QObject;
44
45 struct Exception
46 {
47 /** Constructor.
48 */
49 Exception(const QString &msg):
50 msg(msg)
51 {}
52 QString msg; /**< Exception message. */
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(); /**< Emitted, when the value changes, or the
69 variable is disconnected. */
70 };
71
72 /****************************************************************************/
73
74 /** Scalar Value Template.
75 */
76 template <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:
90 T value; /**< Current value. */
91 /** Modification Time of Current value. */
92 std::chrono::nanoseconds mTime;
93 bool dataPresent; /**< There is a process value to display. */
94
95 // pure-virtual from ScalarSubscriber
96 void newValues(std::chrono::nanoseconds) override;
97 // virtual from ScalarSubscriber
98 void stateChange(PdCom::Subscription::State) override;
99 };
100
101 /****************************************************************************/
102
103 typedef ScalarVariable<bool> BoolVariable;
104 typedef ScalarVariable<int> IntVariable;
105 typedef ScalarVariable<double> DoubleVariable;
106
107 /****************************************************************************/
108
109 /** Constructor.
110 */
111 ✗ template <class T> ScalarVariable<T>::ScalarVariable(QObject *parent):
112 AbstractScalarVariable(parent),
113 value((T) 0),
114 ✗ dataPresent(false)
115 {}
116
117 /****************************************************************************/
118
119 /** Destructor.
120 */
121 ✗ template <class T> ScalarVariable<T>::~ScalarVariable()
122 {}
123
124 /****************************************************************************/
125
126 ✗ template <class T> void ScalarVariable<T>::clearData()
127 {
128 ✗ value = ((T) 0);
129 ✗ dataPresent = false;
130 ✗ emit valueChanged();
131 }
132
133 /****************************************************************************/
134
135 /**
136 * \return \a true, if data are present.
137 */
138 ✗ template <class T> inline bool ScalarVariable<T>::hasData() const
139 {
140 ✗ return dataPresent;
141 }
142
143 /****************************************************************************/
144
145 /**
146 * \return The current #value.
147 */
148 ✗ template <class T> inline T ScalarVariable<T>::getValue() const
149 {
150 ✗ return value;
151 }
152
153 /****************************************************************************/
154
155 /**
156 * \return The current Modification Time.
157 */
158 template <class T>
159 inline std::chrono::nanoseconds ScalarVariable<T>::getMTime() const
160 {
161 return mTime;
162 }
163
164 /****************************************************************************/
165
166 /** Increments the current #value and writes it to the process.
167 *
168 * This does \a not update #value directly.
169 */
170 template <class T> void ScalarVariable<T>::inc()
171 {
172 writeValue(value + 1);
173 }
174
175 /****************************************************************************/
176
177 /** This virtual method is called if the process variable's value changed.
178 */
179 template <class T>
180 ✗ void ScalarVariable<T>::newValues(std::chrono::nanoseconds ts)
181 {
182 ✗ T newValue;
183
184 ✗ copyData(newValue);
185 ✗ if (scale != 1.0 || offset != 0.0) {
186 ✗ newValue = newValue * scale + offset;
187 }
188 ✗ mTime = std::chrono::nanoseconds(ts);
189
190 ✗ if (newValue != value || !dataPresent) {
191 ✗ value = newValue;
192 ✗ dataPresent = true;
193 ✗ emit valueChanged();
194 }
195 }
196
197 /****************************************************************************/
198
199 /** Invalidates the cached value if the subscription is not active anymore.
200 *
201 * Otherwise, a new subscription (e.g. after a reconnect) that delivers the
202 * same value as before would not emit valueChanged().
203 */
204 template <class T>
205 ✗ void ScalarVariable<T>::stateChange(PdCom::Subscription::State state)
206 {
207 ✗ if (state != PdCom::Subscription::State::Active && dataPresent) {
208 ✗ clearData();
209 }
210 }
211
212 /****************************************************************************/
213
214 } // namespace QtPdCom
215
216 #endif
217