GCC Code Coverage Report


Directory: ./
File: QtPdCom1/ScalarVariable.h
Date: 2025-09-18 10:27:59
Exec Total Coverage
Lines: 26 26 100.0%
Branches: 6 14 42.9%

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 24 class QTPDCOM_PUBLIC AbstractScalarVariable:
37 public QObject,
38 public ScalarSubscriber
39 {
40 Q_OBJECT
41
42 public:
43
1/2
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
24 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 7 copyData(T &dest) const
58 {
59
2/4
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 7 times.
✗ Branch 6 not taken.
21 PdCom::details::copyData(
60 &dest,
61 7 PdCom::details::TypeInfoTraits<T>::type_info.type,
62 getData(),
63 14 getVariable().getTypeInfo().type,
64 1);
65 7 }
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 };
98
99 /****************************************************************************/
100
101 typedef ScalarVariable<bool> BoolVariable;
102 typedef ScalarVariable<int> IntVariable;
103 typedef ScalarVariable<double> DoubleVariable;
104
105 /****************************************************************************/
106
107 /** Constructor.
108 */
109 24 template <class T> ScalarVariable<T>::ScalarVariable(QObject *parent):
110 AbstractScalarVariable(parent),
111 value((T) 0),
112 24 dataPresent(false)
113 24 {}
114
115 /****************************************************************************/
116
117 /** Destructor.
118 */
119 24 template <class T> ScalarVariable<T>::~ScalarVariable()
120 24 {}
121
122 /****************************************************************************/
123
124 template <class T> void ScalarVariable<T>::clearData()
125 {
126 value = ((T) 0);
127 dataPresent = false;
128 emit valueChanged();
129 }
130
131 /****************************************************************************/
132
133 /**
134 * \return \a true, if data are present.
135 */
136 7 template <class T> inline bool ScalarVariable<T>::hasData() const
137 {
138 7 return dataPresent;
139 }
140
141 /****************************************************************************/
142
143 /**
144 * \return The current #value.
145 */
146 7 template <class T> inline T ScalarVariable<T>::getValue() const
147 {
148 7 return value;
149 }
150
151 /****************************************************************************/
152
153 /**
154 * \return The current Modification Time.
155 */
156 template <class T>
157 inline std::chrono::nanoseconds ScalarVariable<T>::getMTime() const
158 {
159 return mTime;
160 }
161
162 /****************************************************************************/
163
164 /** Increments the current #value and writes it to the process.
165 *
166 * This does \a not update #value directly.
167 */
168 template <class T> void ScalarVariable<T>::inc()
169 {
170 writeValue(value + 1);
171 }
172
173 /****************************************************************************/
174
175 /** This virtual method is called if the process variable's value changed.
176 */
177 template <class T>
178 7 void ScalarVariable<T>::newValues(std::chrono::nanoseconds ts)
179 {
180 7 T newValue;
181
182
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 copyData(newValue);
183 7 newValue = newValue * scale + offset;
184 7 mTime = std::chrono::nanoseconds(ts);
185
186
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7 if (newValue != value || !dataPresent) {
187 7 value = newValue;
188 7 dataPresent = true;
189
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 emit valueChanged();
190 }
191 7 }
192
193 /****************************************************************************/
194
195 } // namespace QtPdCom
196
197 #endif
198