QtPdCom  1.6.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 
30 namespace 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 
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;
92  std::chrono::nanoseconds mTime;
93  bool dataPresent;
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 
106 
107 /****************************************************************************/
108 
111 template <class T> ScalarVariable<T>::ScalarVariable(QObject *parent):
112  AbstractScalarVariable(parent),
113  value((T) 0),
114  dataPresent(false)
115 {}
116 
117 /****************************************************************************/
118 
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 
138 template <class T> inline bool ScalarVariable<T>::hasData() const
139 {
140  return dataPresent;
141 }
142 
143 /****************************************************************************/
144 
148 template <class T> inline T ScalarVariable<T>::getValue() const
149 {
150  return value;
151 }
152 
153 /****************************************************************************/
154 
158 template <class T>
159 inline std::chrono::nanoseconds ScalarVariable<T>::getMTime() const
160 {
161  return mTime;
162 }
163 
164 /****************************************************************************/
165 
170 template <class T> void ScalarVariable<T>::inc()
171 {
172  writeValue(value + 1);
173 }
174 
175 /****************************************************************************/
176 
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 
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
Exception(const QString &msg)
Constructor.
Definition: ScalarVariable.h:49
Abstract Scalar Value.
Definition: ScalarVariable.h:36
T getValue() const
Definition: ScalarVariable.h:148
virtual ~ScalarVariable()
Destructor.
Definition: ScalarVariable.h:121
Definition: BroadcastModel.h:32
QString msg
Exception message.
Definition: ScalarVariable.h:52
void newValues(std::chrono::nanoseconds) override
This virtual method is called if the process variable&#39;s value changed.
Definition: ScalarVariable.h:180
std::chrono::nanoseconds mTime
Modification Time of Current value.
Definition: ScalarVariable.h:92
T value
Current value.
Definition: ScalarVariable.h:90
Subscriber of a single scalar value.
Definition: ScalarSubscriber.h:39
bool hasData() const
Definition: ScalarVariable.h:138
#define QTPDCOM_PUBLIC
Definition: Export.h:30
void inc()
Increments the current value and writes it to the process.
Definition: ScalarVariable.h:170
void clearData()
Definition: ScalarVariable.h:126
void stateChange(PdCom::Subscription::State) override
Invalidates the cached value if the subscription is not active anymore.
Definition: ScalarVariable.h:205
Scalar Value Template.
Definition: ScalarVariable.h:76
ScalarVariable< double > DoubleVariable
Definition: ScalarVariable.h:105
ScalarVariable< int > IntVariable
Definition: ScalarVariable.h:104
bool dataPresent
There is a process value to display.
Definition: ScalarVariable.h:93
ScalarVariable(QObject *parent=nullptr)
Constructor.
Definition: ScalarVariable.h:111
std::chrono::nanoseconds getMTime() const
Definition: ScalarVariable.h:159
Definition: ScalarVariable.h:45
ScalarVariable< bool > BoolVariable
Definition: ScalarVariable.h:103
std::enable_if< std::is_arithmetic< T >::value, void >::type copyData(T &dest) const
Definition: ScalarVariable.h:57