1 | // Copyright (C) 2021 basysKom GmbH, opensource@basyskom.com |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qopcuadatavalue.h" |
5 | |
6 | QT_BEGIN_NAMESPACE |
7 | |
8 | /*! |
9 | \class QOpcUaDataValue |
10 | \inmodule QtOpcUa |
11 | \brief This class stores OPC UA value data and associated metadata. |
12 | \since 6.3 |
13 | |
14 | This class corresponds to the OPC UA DataValue type. |
15 | */ |
16 | class QOpcUaDataValueData : public QSharedData |
17 | { |
18 | public: |
19 | QDateTime serverTimestamp; |
20 | quint16 serverPicoseconds = 0; |
21 | QDateTime sourceTimestamp; |
22 | quint16 sourcePicoseconds = 0; |
23 | QOpcUa::UaStatusCode statusCode {QOpcUa::UaStatusCode::Good}; |
24 | QVariant value; |
25 | }; |
26 | |
27 | /*! |
28 | Constructs an invalid data value. |
29 | */ |
30 | QOpcUaDataValue::QOpcUaDataValue() |
31 | : data(new QOpcUaDataValueData) |
32 | { |
33 | } |
34 | |
35 | /*! |
36 | Constructs a data value from \a other. |
37 | */ |
38 | QOpcUaDataValue::QOpcUaDataValue(const QOpcUaDataValue &other) |
39 | : data(other.data) |
40 | { |
41 | } |
42 | |
43 | /*! |
44 | Sets the values from \a other in this data value. |
45 | */ |
46 | QOpcUaDataValue &QOpcUaDataValue::operator=(const QOpcUaDataValue &other) |
47 | { |
48 | if (this != &other) |
49 | data.operator=(o: other.data); |
50 | return *this; |
51 | } |
52 | |
53 | /*! |
54 | Destroys the data value. |
55 | */ |
56 | QOpcUaDataValue::~QOpcUaDataValue() |
57 | { |
58 | } |
59 | |
60 | /*! |
61 | \fn QOpcUaDataValue::swap(QOpcUaDataValue &other) |
62 | |
63 | Swaps this data value instance with \a other. This function is very |
64 | fast and never fails. |
65 | */ |
66 | |
67 | /*! |
68 | Returns the value. |
69 | */ |
70 | QVariant QOpcUaDataValue::value() const |
71 | { |
72 | return data->value; |
73 | } |
74 | |
75 | /*! |
76 | Sets the value to \a value. |
77 | |
78 | If this data value is to be used with \l QOpcUaBinaryDataEncoding or |
79 | \l QOpcUaGenericStructHandler, the value's type must be \l QOpcUaVariant. |
80 | */ |
81 | void QOpcUaDataValue::setValue(const QVariant &value) |
82 | { |
83 | data.detach(); |
84 | data->value = value; |
85 | } |
86 | |
87 | /*! |
88 | Returns a QVariant containing this data value. |
89 | |
90 | \since 6.7 |
91 | */ |
92 | QOpcUaDataValue::operator QVariant() const |
93 | { |
94 | return QVariant::fromValue(value: *this); |
95 | } |
96 | |
97 | /*! |
98 | Returns the status code for this data value. If the status code is not \l {QOpcUa::UaStatusCode} {Good}, the |
99 | value and the timestamps are invalid. |
100 | */ |
101 | QOpcUa::UaStatusCode QOpcUaDataValue::statusCode() const |
102 | { |
103 | return data->statusCode; |
104 | } |
105 | |
106 | /*! |
107 | Sets the status code to \a statusCode. |
108 | */ |
109 | void QOpcUaDataValue::setStatusCode(QOpcUa::UaStatusCode statusCode) |
110 | { |
111 | data.detach(); |
112 | data->statusCode = statusCode; |
113 | } |
114 | |
115 | /*! |
116 | Returns the source timestamp for \l value(). |
117 | */ |
118 | QDateTime QOpcUaDataValue::sourceTimestamp() const |
119 | { |
120 | return data->sourceTimestamp; |
121 | } |
122 | |
123 | /*! |
124 | Sets the source timestamp to \a sourceTimestamp. |
125 | */ |
126 | void QOpcUaDataValue::setSourceTimestamp(const QDateTime &sourceTimestamp) |
127 | { |
128 | data.detach(); |
129 | data->sourceTimestamp = sourceTimestamp; |
130 | } |
131 | |
132 | /*! |
133 | \since 6.7 |
134 | |
135 | Returns the number of 10 picosecond intervals for the source timestamp. |
136 | */ |
137 | quint16 QOpcUaDataValue::sourcePicoseconds() const |
138 | { |
139 | return data->sourcePicoseconds; |
140 | } |
141 | |
142 | /*! |
143 | \since 6.7 |
144 | |
145 | Sets the number of 10 picosecond intervals for the source timestamp to \a sourcePicoseconds. |
146 | */ |
147 | void QOpcUaDataValue::setSourcePicoseconds(quint16 sourcePicoseconds) |
148 | { |
149 | data->sourcePicoseconds = sourcePicoseconds; |
150 | } |
151 | |
152 | /*! |
153 | Returns the server timestamp for \l value(). |
154 | */ |
155 | QDateTime QOpcUaDataValue::serverTimestamp() const |
156 | { |
157 | return data->serverTimestamp; |
158 | } |
159 | |
160 | /*! |
161 | Sets the server timestamp to \a serverTimestamp. |
162 | */ |
163 | void QOpcUaDataValue::setServerTimestamp(const QDateTime &serverTimestamp) |
164 | { |
165 | data.detach(); |
166 | data->serverTimestamp = serverTimestamp; |
167 | } |
168 | |
169 | /*! |
170 | \since 6.7 |
171 | |
172 | Returns the number of 10 picosecond intervals for the server timestamp. |
173 | */ |
174 | quint16 QOpcUaDataValue::serverPicoseconds() const |
175 | { |
176 | return data->serverPicoseconds; |
177 | } |
178 | |
179 | /*! |
180 | \since 6.7 |
181 | |
182 | Sets the number of 10 picosecond intervals for the server timestamp to \a serverPicoseconds. |
183 | */ |
184 | void QOpcUaDataValue::setServerPicoseconds(quint16 serverPicoseconds) |
185 | { |
186 | data->serverPicoseconds = serverPicoseconds; |
187 | } |
188 | |
189 | /*! |
190 | \fn bool QOpcUaDataValue::operator!=(const QOpcUaDataValue &lhs, const QOpcUaDataValue &rhs) noexcept |
191 | \since 6.7 |
192 | |
193 | Returns \c true if \a lhs is not equal to \a rhs. |
194 | */ |
195 | |
196 | /*! |
197 | \fn bool QOpcUaDataValue::operator==(const QOpcUaDataValue &lhs, const QOpcUaDataValue &rhs) noexcept |
198 | \since 6.7 |
199 | |
200 | Returns \c true if \a lhs is equal to \a rhs. |
201 | */ |
202 | bool comparesEqual(const QOpcUaDataValue &lhs, const QOpcUaDataValue &rhs) noexcept |
203 | { |
204 | return lhs.data->serverTimestamp == rhs.data->serverTimestamp && |
205 | lhs.data->serverPicoseconds == rhs.data->serverPicoseconds && |
206 | lhs.data->sourceTimestamp == rhs.data->sourceTimestamp && |
207 | lhs.data->sourcePicoseconds == rhs.data->sourcePicoseconds && |
208 | lhs.data->statusCode == rhs.data->statusCode && |
209 | lhs.data->value == rhs.data->value; |
210 | } |
211 | |
212 | QT_END_NAMESPACE |
213 | |