1
0
mirror of https://github.com/qt/qtmqtt.git synced 2026-10-25 16:16:26 +00:00

Refactor QMqttMessage

As the class is exported now, move the data members to a private class.
Furthermore payload should just return a const-ref in case no copy is
needed.

Change-Id: I273d235431b2f4d6e2834b2a9c6934a23daf3962
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
This commit is contained in:
Maurice Kalinowski 2017-11-13 13:30:52 +01:00
parent 6eca6f914d
commit 42c9128e48
2 changed files with 103 additions and 21 deletions

View File

@ -86,44 +86,117 @@ QT_BEGIN_NAMESPACE
live update. A broker can store only one retained message per topic.
*/
QByteArray QMqttMessage::payload() const
class QMqttMessagePrivate : public QSharedData
{
return m_payload;
public:
bool operator==(const QMqttMessagePrivate &other) {
return m_topic == other.m_topic &&
m_payload == other.m_payload &&
m_id == other.m_id &&
m_qos == other.m_qos &&
m_duplicate == other.m_duplicate &&
m_retain == other.m_retain;
}
QMqttTopicName m_topic;
QByteArray m_payload;
quint16 m_id{0};
quint8 m_qos{0};
bool m_duplicate{false};
bool m_retain{false};
};
/*!
Creates a new MQTT message.
*/
QMqttMessage::QMqttMessage()
: d(new QMqttMessagePrivate())
{
}
/*!
Constructs a new MQTT message that is a copy of \a other.
*/
QMqttMessage::QMqttMessage(const QMqttMessage &other)
: d(other.d)
{
}
QMqttMessage::~QMqttMessage()
{
}
/*!
Makes this object a copy of \a other and returns the new value of this object.
*/
QMqttMessage &QMqttMessage::operator=(const QMqttMessage &other)
{
d = other.d;
return *this;
}
/*!
Returns \c true if the message and \a other are equal, otherwise returns \c false.
*/
bool QMqttMessage::operator==(const QMqttMessage &other) const
{
return d == other.d;
}
/*!
Returns \c true if the message and \a other are not equal, otherwise returns \c false.
*/
bool QMqttMessage::operator!=(const QMqttMessage &other) const
{
return !(*this == other);
}
const QByteArray &QMqttMessage::payload() const
{
return d->m_payload;
}
quint8 QMqttMessage::qos() const
{
return m_qos;
return d->m_qos;
}
quint16 QMqttMessage::id() const
{
return m_id;
return d->m_id;
}
QMqttTopicName QMqttMessage::topic() const
{
return m_topic;
return d->m_topic;
}
bool QMqttMessage::duplicate() const
{
return m_duplicate;
return d->m_duplicate;
}
bool QMqttMessage::retain() const
{
return m_retain;
return d->m_retain;
}
/*!
\internal
Constructs a new MQTT message with \a content on the topic \a topic.
Furthermore the properties \a id, \a qos, \a dup, \a retain must be specified.
This constructor is mostly used internally to construct a QMqttMessage at message
receival.
*/
QMqttMessage::QMqttMessage(const QMqttTopicName &topic, const QByteArray &content, quint16 id, quint8 qos, bool dup, bool retain)
: m_topic(topic)
, m_payload(content)
, m_id(id)
, m_qos(qos)
, m_duplicate(dup)
, m_retain(retain)
: d(new QMqttMessagePrivate)
{
d->m_topic = topic;
d->m_payload = content;
d->m_id = id;
d->m_qos = qos;
d->m_duplicate = dup;
d->m_retain = retain;
}
QT_END_NAMESPACE

View File

@ -35,9 +35,12 @@
#include <QtMqtt/QMqttTopicName>
#include <QtCore/QObject>
#include <QtCore/QSharedDataPointer>
QT_BEGIN_NAMESPACE
class QMqttMessagePrivate;
class Q_MQTT_EXPORT QMqttMessage
{
Q_GADGET
@ -47,8 +50,17 @@ class Q_MQTT_EXPORT QMqttMessage
Q_PROPERTY(quint8 qos READ qos CONSTANT)
Q_PROPERTY(bool duplicate READ duplicate CONSTANT)
Q_PROPERTY(bool retain READ retain CONSTANT)
public:
QByteArray payload() const;
QMqttMessage();
QMqttMessage(const QMqttMessage& other);
~QMqttMessage();
QMqttMessage& operator=(const QMqttMessage &other);
bool operator==(const QMqttMessage &other) const;
inline bool operator!=(const QMqttMessage &other) const;
const QByteArray &payload() const;
quint8 qos() const;
quint16 id() const;
QMqttTopicName topic() const;
@ -57,17 +69,14 @@ public:
private:
friend class QMqttConnection;
explicit QMqttMessage(const QMqttTopicName &topic, const QByteArray &payload,
QMqttMessage(const QMqttTopicName &topic, const QByteArray &payload,
quint16 id, quint8 qos,
bool dup, bool retain);
QMqttTopicName m_topic;
QByteArray m_payload;
quint16 m_id;
quint8 m_qos;
bool m_duplicate;
bool m_retain;
QExplicitlySharedDataPointer<QMqttMessagePrivate> d;
};
QT_END_NAMESPACE
Q_DECLARE_METATYPE(QMqttMessage)
#endif // QMQTTMESSAGE_H