| 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.detach(); |
| 150 | data->sourcePicoseconds = sourcePicoseconds; |
| 151 | } |
| 152 | |
| 153 | /*! |
| 154 | Returns the server timestamp for \l value(). |
| 155 | */ |
| 156 | QDateTime QOpcUaDataValue::serverTimestamp() const |
| 157 | { |
| 158 | return data->serverTimestamp; |
| 159 | } |
| 160 | |
| 161 | /*! |
| 162 | Sets the server timestamp to \a serverTimestamp. |
| 163 | */ |
| 164 | void QOpcUaDataValue::setServerTimestamp(const QDateTime &serverTimestamp) |
| 165 | { |
| 166 | data.detach(); |
| 167 | data->serverTimestamp = serverTimestamp; |
| 168 | } |
| 169 | |
| 170 | /*! |
| 171 | \since 6.7 |
| 172 | |
| 173 | Returns the number of 10 picosecond intervals for the server timestamp. |
| 174 | */ |
| 175 | quint16 QOpcUaDataValue::serverPicoseconds() const |
| 176 | { |
| 177 | return data->serverPicoseconds; |
| 178 | } |
| 179 | |
| 180 | /*! |
| 181 | \since 6.7 |
| 182 | |
| 183 | Sets the number of 10 picosecond intervals for the server timestamp to \a serverPicoseconds. |
| 184 | */ |
| 185 | void QOpcUaDataValue::setServerPicoseconds(quint16 serverPicoseconds) |
| 186 | { |
| 187 | data.detach(); |
| 188 | data->serverPicoseconds = serverPicoseconds; |
| 189 | } |
| 190 | |
| 191 | /*! |
| 192 | \fn bool QOpcUaDataValue::operator!=(const QOpcUaDataValue &lhs, const QOpcUaDataValue &rhs) noexcept |
| 193 | \since 6.7 |
| 194 | |
| 195 | Returns \c true if \a lhs is not equal to \a rhs. |
| 196 | */ |
| 197 | |
| 198 | /*! |
| 199 | \fn bool QOpcUaDataValue::operator==(const QOpcUaDataValue &lhs, const QOpcUaDataValue &rhs) noexcept |
| 200 | \since 6.7 |
| 201 | |
| 202 | Returns \c true if \a lhs is equal to \a rhs. |
| 203 | */ |
| 204 | bool comparesEqual(const QOpcUaDataValue &lhs, const QOpcUaDataValue &rhs) noexcept |
| 205 | { |
| 206 | return lhs.data->serverTimestamp == rhs.data->serverTimestamp && |
| 207 | lhs.data->serverPicoseconds == rhs.data->serverPicoseconds && |
| 208 | lhs.data->sourceTimestamp == rhs.data->sourceTimestamp && |
| 209 | lhs.data->sourcePicoseconds == rhs.data->sourcePicoseconds && |
| 210 | lhs.data->statusCode == rhs.data->statusCode && |
| 211 | lhs.data->value == rhs.data->value; |
| 212 | } |
| 213 | |
| 214 | QT_END_NAMESPACE |
| 215 |
