GCC Code Coverage Report


Directory: ./
File: QtPdCom1/ScalarVariable.h
Date: 2024-01-26 10:40:55
Exec Total Coverage
Lines: 26 26 100.0%
Branches: 6 14 42.9%

Line Branch Exec Source
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
30 namespace QtPdCom {
31
32 /****************************************************************************/
33
34 /** Abstract Scalar Value.
35 */
36 19 class QTPDCOM_PUBLIC AbstractScalarVariable:
37 public QObject, public ScalarSubscriber
38 {
39 Q_OBJECT
40
41 public:
42
1/2
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
19 using QObject::QObject;
43
44 struct Exception {
45 /** Constructor.
46 */
47 Exception(const QString &msg):
48 msg(msg) {}
49 QString msg; /**< Exception message. */
50 };
51
52 template <typename T>
53 typename std::enable_if<std::is_arithmetic<T>::value, void>::type
54 7 copyData(T &dest) const
55 {
56
2/4
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 7 times.
✗ Branch 6 not taken.
21 PdCom::details::copyData(
57 7 &dest, PdCom::details::TypeInfoTraits<T>::type_info.type,
58 getData(),
59 14 getVariable().getTypeInfo().type, 1);
60 7 }
61
62 signals:
63 void valueChanged(); /**< Emitted, when the value changes, or the
64 variable is disconnected. */
65 };
66
67 /****************************************************************************/
68
69 /** Scalar Value Template.
70 */
71 template <class T>
72 class ScalarVariable:
73 public AbstractScalarVariable
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:
87 T value; /**< Current value. */
88 /** Modification Time of Current value. */
89 std::chrono::nanoseconds mTime;
90 bool dataPresent; /**< There is a process value to display. */
91
92 // pure-virtual from ScalarSubscriber
93 void newValues(std::chrono::nanoseconds) override;
94 };
95
96 /****************************************************************************/
97
98 typedef ScalarVariable<bool> BoolVariable;
99 typedef ScalarVariable<int> IntVariable;
100 typedef ScalarVariable<double> DoubleVariable;
101
102 /****************************************************************************/
103
104 /** Constructor.
105 */
106 template <class T>
107 19 ScalarVariable<T>::ScalarVariable(QObject *parent):
108 AbstractScalarVariable(parent),
109 value((T) 0),
110 19 dataPresent(false)
111 {
112 19 }
113
114 /****************************************************************************/
115
116 /** Destructor.
117 */
118 template <class T>
119 19 ScalarVariable<T>::~ScalarVariable()
120 {
121 19 }
122
123 /****************************************************************************/
124
125 template <class T>
126 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>
139 7 inline bool ScalarVariable<T>::hasData() const
140 {
141 7 return dataPresent;
142 }
143
144 /****************************************************************************/
145
146 /**
147 * \return The current #value.
148 */
149 template <class T>
150 7 inline T ScalarVariable<T>::getValue() const
151 {
152 7 return value;
153 }
154
155 /****************************************************************************/
156
157 /**
158 * \return The current Modification Time.
159 */
160 template <class T>
161 inline std::chrono::nanoseconds
162 ScalarVariable<T>::getMTime() const
163 {
164 return mTime;
165 }
166
167 /****************************************************************************/
168
169 /** Increments the current #value and writes it to the process.
170 *
171 * This does \a not update #value directly.
172 */
173 template <class T>
174 void ScalarVariable<T>::inc()
175 {
176 writeValue(value + 1);
177 }
178
179 /****************************************************************************/
180
181 /** This virtual method is called by the ProcessVariable, if its value
182 * changes.
183 */
184 template <class T>
185 7 void ScalarVariable<T>::newValues(std::chrono::nanoseconds ts)
186 {
187 7 T newValue;
188
189
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 copyData(newValue);
190 7 newValue = newValue * scale + offset;
191 7 mTime = std::chrono::nanoseconds(ts);
192
193
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7 if (newValue != value || !dataPresent) {
194 7 value = newValue;
195 7 dataPresent = true;
196
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 emit valueChanged();
197 }
198 7 }
199
200 /****************************************************************************/
201
202 } // namespace
203
204 #endif
205