diff options
author | Denis Shienkov <denis.shienkov@gmail.com> | 2015-07-08 10:43:55 +0300 |
---|---|---|
committer | Denis Shienkov <denis.shienkov@gmail.com> | 2015-07-08 13:38:17 +0000 |
commit | 3bcb73862cbb5022699e5d49a435652689795ccd (patch) | |
tree | 738fc331eefc6c8f20ab7f87a217c5ce547b7b26 | |
parent | a0d0e85fa9b0543564b469b762dc1541aae8b33e (diff) |
Add stream operators to serialize QCanFrame
Change-Id: I1be3bcea88e897b9ddecbfdd38738e0fb70a1266
Reviewed-by: Alex Blasche <alexander.blasche@theqtcompany.com>
-rw-r--r-- | src/serialbus/qcanframe.cpp | 80 | ||||
-rw-r--r-- | src/serialbus/qcanframe.h | 10 | ||||
-rw-r--r-- | src/serialbus/serialbus.pro | 1 | ||||
-rw-r--r-- | tests/auto/qcanframe/tst_qcanframe.cpp | 41 |
4 files changed, 130 insertions, 2 deletions
diff --git a/src/serialbus/qcanframe.cpp b/src/serialbus/qcanframe.cpp new file mode 100644 index 0000000..da6574f --- /dev/null +++ b/src/serialbus/qcanframe.cpp @@ -0,0 +1,80 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtSerialBus module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qcanframe.h" + +#include <QtCore/qdatastream.h> + +QT_BEGIN_NAMESPACE + +#ifndef QT_NO_DATASTREAM + +/*! \relates QCanFrame + + Writes frame \a frame to the stream \a out and returns a reference + to the stream. +*/ +QDataStream &operator<<(QDataStream &out, const QCanFrame &frame) +{ + out << frame.frameId(); + out << frame.payload(); + const QCanFrame::TimeStamp stamp = frame.timeStamp(); + out << stamp.seconds(); + out << stamp.microSeconds(); + return out; +} + +/*! \relates QCanFrame + + Reads a frame into \a frame from the stream \a in and returns a + reference to the stream. +*/ +QDataStream &operator>>(QDataStream &in, QCanFrame &frame) +{ + qint32 frameId; + QByteArray payload; + qint64 seconds; + qint64 microSeconds; + in >> frameId >> payload >> seconds >> microSeconds; + frame.setFrameId(frameId); + frame.setPayload(payload); + frame.setTimeStamp(QCanFrame::TimeStamp(seconds, microSeconds)); + return in; +} + +#endif // QT_NO_DATASTREAM + +QT_END_NAMESPACE diff --git a/src/serialbus/qcanframe.h b/src/serialbus/qcanframe.h index 8a56bc9..6964496 100644 --- a/src/serialbus/qcanframe.h +++ b/src/serialbus/qcanframe.h @@ -43,9 +43,10 @@ QT_BEGIN_NAMESPACE -//TODO: stream operator for this class +class QDataStream; + //TODO: review ctors for this class -class QCanFrame +class Q_SERIALBUS_EXPORT QCanFrame { public: class TimeStamp { @@ -80,6 +81,11 @@ private: Q_DECLARE_TYPEINFO(QCanFrame, Q_MOVABLE_TYPE); Q_DECLARE_TYPEINFO(QCanFrame::TimeStamp, Q_PRIMITIVE_TYPE); +#ifndef QT_NO_DATASTREAM +Q_SERIALBUS_EXPORT QDataStream &operator<<(QDataStream &, const QCanFrame &); +Q_SERIALBUS_EXPORT QDataStream &operator>>(QDataStream &, QCanFrame &); +#endif + QT_END_NAMESPACE #endif // QCANFRAME_H diff --git a/src/serialbus/serialbus.pro b/src/serialbus/serialbus.pro index 75c60c2..df89967 100644 --- a/src/serialbus/serialbus.pro +++ b/src/serialbus/serialbus.pro @@ -26,6 +26,7 @@ PRIVATE_HEADERS += \ SOURCES += \ qbusdummydevice.cpp \ qcanbusdevice.cpp \ + qcanframe.cpp \ qserialbus.cpp \ qserialbusdevice.cpp \ diff --git a/tests/auto/qcanframe/tst_qcanframe.cpp b/tests/auto/qcanframe/tst_qcanframe.cpp index e935a54..1e18d53 100644 --- a/tests/auto/qcanframe/tst_qcanframe.cpp +++ b/tests/auto/qcanframe/tst_qcanframe.cpp @@ -48,6 +48,9 @@ private slots: void id(); void payload(); void timeStamp(); + + void streaming_data(); + void streaming(); }; tst_QCanFrame::tst_QCanFrame() @@ -102,6 +105,44 @@ void tst_QCanFrame::timeStamp() QCOMPARE(timeStamp.microSeconds(), 5); } +void tst_QCanFrame::streaming_data() +{ + QTest::addColumn<qint32>("frameId"); + QTest::addColumn<QByteArray>("payload"); + QTest::addColumn<qint64>("seconds"); + QTest::addColumn<qint64>("microSeconds"); + + QTest::newRow("emptyFrame") << qint32(0) << QByteArray() << qint64(0) << qint64(0); + QTest::newRow("fullFrame") << qint32(123) << QByteArray("abcdef") << qint64(456) << qint64(789); +} + +void tst_QCanFrame::streaming() +{ + QFETCH(qint32, frameId); + QFETCH(QByteArray, payload); + QFETCH(qint64, seconds); + QFETCH(qint64, microSeconds); + + QCanFrame originalFrame(frameId, payload); + const QCanFrame::TimeStamp originalStamp(seconds, microSeconds); + originalFrame.setTimeStamp(originalStamp); + + QByteArray buffer; + QDataStream out(&buffer, QIODevice::WriteOnly); + out << originalFrame; + + QDataStream in(buffer); + QCanFrame restoredFrame; + in >> restoredFrame; + const QCanFrame::TimeStamp restoredStamp(restoredFrame.timeStamp()); + + QCOMPARE(restored.frameId(), original.frameId()); + QCOMPARE(restored.payload(), original.payload()); + + QCOMPARE(restoredStamp.seconds(), originalStamp.seconds()); + QCOMPARE(restoredStamp.microSeconds(), originalStamp.microSeconds()); +} + QTEST_MAIN(tst_QCanFrame) #include "tst_qcanframe.moc" |