diff --git a/AdvancedDockingSystem/AdvancedDockingSystem.pri b/AdvancedDockingSystem/AdvancedDockingSystem.pri index f6cf92b..76e9016 100644 --- a/AdvancedDockingSystem/AdvancedDockingSystem.pri +++ b/AdvancedDockingSystem/AdvancedDockingSystem.pri @@ -1,24 +1,24 @@ SOURCES += \ - src/API.cpp \ - src/ContainerWidget.cpp \ - src/SectionWidget.cpp \ - src/SectionContent.cpp \ - src/SectionTitleWidget.cpp \ - src/SectionContentWidget.cpp \ - src/DropOverlay.cpp \ - src/FloatingWidget.cpp \ - src/Internal.cpp \ - src/Serialization.cpp + $$PWD/src/API.cpp \ + $$PWD/src/ContainerWidget.cpp \ + $$PWD/src/SectionWidget.cpp \ + $$PWD/src/SectionContent.cpp \ + $$PWD/src/SectionTitleWidget.cpp \ + $$PWD/src/SectionContentWidget.cpp \ + $$PWD/src/DropOverlay.cpp \ + $$PWD/src/FloatingWidget.cpp \ + $$PWD/src/Internal.cpp \ + $$PWD/src/Serialization.cpp HEADERS += \ - include/ads/API.h \ - include/ads/ContainerWidget.h \ - include/ads/SectionWidget.h \ - include/ads/SectionContent.h \ - include/ads/SectionTitleWidget.h \ - include/ads/SectionContentWidget.h \ - include/ads/DropOverlay.h \ - include/ads/FloatingWidget.h \ - include/ads/Internal.h \ - include/ads/Serialization.h + $$PWD/include/ads/API.h \ + $$PWD/include/ads/ContainerWidget.h \ + $$PWD/include/ads/SectionWidget.h \ + $$PWD/include/ads/SectionContent.h \ + $$PWD/include/ads/SectionTitleWidget.h \ + $$PWD/include/ads/SectionContentWidget.h \ + $$PWD/include/ads/DropOverlay.h \ + $$PWD/include/ads/FloatingWidget.h \ + $$PWD/include/ads/Internal.h \ + $$PWD/include/ads/Serialization.h diff --git a/AdvancedDockingSystem/AdvancedDockingSystem.pro b/AdvancedDockingSystem/AdvancedDockingSystem.pro index 1e119dc..24db460 100644 --- a/AdvancedDockingSystem/AdvancedDockingSystem.pro +++ b/AdvancedDockingSystem/AdvancedDockingSystem.pro @@ -4,6 +4,7 @@ CONFIG += adsBuildShared QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets +greaterThan(QT_MAJOR_VERSION, 4): DEFINES += ADS_NAMESPACE_ENABLED TEMPLATE = lib @@ -18,8 +19,6 @@ adsBuildShared { INCLUDEPATH += $$PWD/src INCLUDEPATH += $$PWD/include -greaterThan(QT_MAJOR_VERSION, 4): DEFINES += ADS_NAMESPACE_ENABLED - windows { # MinGW *-g++* { diff --git a/AdvancedDockingSystem/include/ads/API.h b/AdvancedDockingSystem/include/ads/API.h index c263979..30ffa04 100644 --- a/AdvancedDockingSystem/include/ads/API.h +++ b/AdvancedDockingSystem/include/ads/API.h @@ -7,7 +7,9 @@ class QSplitter; // DLL Export API #ifdef _WIN32 - #ifdef ADS_EXPORT + #if defined(ADS_IMPORT) + #define ADS_EXPORT_API + #elif defined(ADS_EXPORT) #define ADS_EXPORT_API __declspec(dllexport) #else #define ADS_EXPORT_API __declspec(dllimport) diff --git a/AdvancedDockingSystem/include/ads/Serialization.h b/AdvancedDockingSystem/include/ads/Serialization.h index 9189c80..2aad26c 100644 --- a/AdvancedDockingSystem/include/ads/Serialization.h +++ b/AdvancedDockingSystem/include/ads/Serialization.h @@ -129,9 +129,10 @@ class InMemoryWriter { public: InMemoryWriter(); - bool write(OffsetsHeaderEntry::Type type, const QByteArray& data); + bool write(qint32 entryType, const QByteArray& data); bool write(const SectionIndexData& data); QByteArray toByteArray() const; + qint32 offsetsCount() const { return _offsetsHeader.entriesCount; } private: QBuffer _contentBuffer; @@ -146,12 +147,11 @@ class InMemoryReader public: InMemoryReader(const QByteArray& data); bool initReadHeader(); - bool read(OffsetsHeaderEntry::Type type, QByteArray &data); + bool read(qint32 entryType, QByteArray &data); + qint32 offsetsCount() const { return _offsetsHeader.entriesCount; } private: QByteArray _data; - QBuffer _buff; - OffsetsHeader _offsetsHeader; }; diff --git a/AdvancedDockingSystem/src/Serialization.cpp b/AdvancedDockingSystem/src/Serialization.cpp index 12b76de..daba9f3 100644 --- a/AdvancedDockingSystem/src/Serialization.cpp +++ b/AdvancedDockingSystem/src/Serialization.cpp @@ -276,12 +276,13 @@ QDataStream& operator>>(QDataStream& in, SectionIndexData& data) InMemoryWriter::InMemoryWriter() { + _contentBuffer.open(QIODevice::ReadWrite); } -bool InMemoryWriter::write(OffsetsHeaderEntry::Type type, const QByteArray& data) +bool InMemoryWriter::write(qint32 entryType, const QByteArray& data) { OffsetsHeaderEntry entry; - entry.type = type; + entry.type = entryType; entry.offset = _contentBuffer.pos(); // Relative offset! entry.contentSize = data.size(); @@ -314,9 +315,7 @@ bool InMemoryWriter::write(const SectionIndexData& data) QByteArray InMemoryWriter::toByteArray() const { QByteArray data; - QBuffer buff(&data); - - QDataStream out(&buff); + QDataStream out(&data, QIODevice::ReadWrite); out.setVersion(QDataStream::Qt_4_5); // Basic format header. @@ -332,13 +331,13 @@ QByteArray InMemoryWriter::toByteArray() const // - Convert relative- to absolute-offsets // - Seek back to begin-pos and write OffsetsHeader again. // Use a copy of OffsetsHeader to keep the _offsetsHeader relative. - const qint64 posOffsetHeaders = buff.pos(); + const qint64 posOffsetHeaders = out.device()->pos(); OffsetsHeader offsetsHeader = _offsetsHeader; out << offsetsHeader; // Now we know the size of the entire header. // We can update the relative- to absolute-offsets now. - const qint64 allHeaderSize = buff.pos(); + const qint64 allHeaderSize = out.device()->pos(); for (int i = 0; i < offsetsHeader.entriesCount; ++i) { offsetsHeader.entries[i].offset += allHeaderSize; @@ -346,11 +345,11 @@ QByteArray InMemoryWriter::toByteArray() const // Seek back and write again with absolute offsets. // TODO Thats not nice, but it works... - buff.seek(posOffsetHeaders); + out.device()->seek(posOffsetHeaders); out << offsetsHeader; // Write contents. - buff.write(_contentBuffer.data()); + out.writeRawData(_contentBuffer.data().constData(), _contentBuffer.size()); return data; } @@ -358,13 +357,13 @@ QByteArray InMemoryWriter::toByteArray() const /////////////////////////////////////////////////////////////////////////////// InMemoryReader::InMemoryReader(const QByteArray& data) : - _data(data), _buff(&_data) + _data(data) { } bool InMemoryReader::initReadHeader() { - QDataStream in(&_buff); + QDataStream in(_data); in.setVersion(QDataStream::Qt_4_5); // Basic format header. @@ -388,13 +387,13 @@ bool InMemoryReader::initReadHeader() return !in.atEnd(); } -bool InMemoryReader::read(OffsetsHeaderEntry::Type type, QByteArray& data) +bool InMemoryReader::read(qint32 entryType, QByteArray& data) { // Find offset for "type". int index = -1; for (int i = 0; i < _offsetsHeader.entriesCount; ++i) { - if (_offsetsHeader.entries.at(index).type == type) + if (_offsetsHeader.entries.at(i).type == entryType) { index = i; break; @@ -406,8 +405,16 @@ bool InMemoryReader::read(OffsetsHeaderEntry::Type type, QByteArray& data) return false; const OffsetsHeaderEntry& entry = _offsetsHeader.entries.at(index); - _buff.seek(entry.offset); - data.append(_buff.read(entry.contentSize)); + + QDataStream in(_data); + in.setVersion(QDataStream::Qt_4_5); + in.device()->seek(entry.offset); + + char* buff = new char[entry.contentSize]; + in.readRawData(buff, entry.contentSize); + data.append(buff, entry.contentSize); + delete[] buff; + return true; } diff --git a/AdvancedDockingSystemUnitTests/AdvancedDockingSystemUnitTests.pri b/AdvancedDockingSystemUnitTests/AdvancedDockingSystemUnitTests.pri new file mode 100644 index 0000000..028a5de --- /dev/null +++ b/AdvancedDockingSystemUnitTests/AdvancedDockingSystemUnitTests.pri @@ -0,0 +1,7 @@ + +HEADERS += \ + $$PWD/src/TestCore.h + +SOURCES += \ + $$PWD/src/main.cpp \ + $$PWD/src/TestCore.cpp diff --git a/AdvancedDockingSystemUnitTests/AdvancedDockingSystemUnitTests.pro b/AdvancedDockingSystemUnitTests/AdvancedDockingSystemUnitTests.pro new file mode 100644 index 0000000..7f682ce --- /dev/null +++ b/AdvancedDockingSystemUnitTests/AdvancedDockingSystemUnitTests.pro @@ -0,0 +1,14 @@ +TARGET = AdvancedDockingSystemUnitTests + +QT += core gui testlib +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets +greaterThan(QT_MAJOR_VERSION, 4): DEFINES += ADS_NAMESPACE_ENABLED +DEFINES += ADS_IMPORT + +INCLUDEPATH += $$PWD/src + +INCLUDEPATH += $$PWD/../AdvancedDockingSystem/include +DEPENDPATH += $$PWD/../AdvancedDockingSystem/include + +include(AdvancedDockingSystemUnitTests.pri) +include(../AdvancedDockingSystem/AdvancedDockingSystem.pri) \ No newline at end of file diff --git a/AdvancedDockingSystemUnitTests/src/TestCore.cpp b/AdvancedDockingSystemUnitTests/src/TestCore.cpp new file mode 100644 index 0000000..da16474 --- /dev/null +++ b/AdvancedDockingSystemUnitTests/src/TestCore.cpp @@ -0,0 +1,35 @@ +#include "TestCore.h" + +#include "ads/API.h" +#include "ads/Serialization.h" + +void TestCore::serialization() +{ + QList datas; + datas.append(QByteArray("Custom Data Here!!!")); + datas.append(QByteArray("Even More...")); + datas.append(QByteArray("lalalaalalalalalalal").toBase64()); + + // Write some data. + ADS_NS_SER::InMemoryWriter writer; + for (int i = 0; i < datas.count(); ++i) + { + QVERIFY(writer.write(i + 1, datas.at(i))); + } + QVERIFY(writer.offsetsCount() == datas.count()); + const QByteArray writtenData = writer.toByteArray(); + QVERIFY(writtenData.size() > 0); + + // Read and validate written data. + ADS_NS_SER::InMemoryReader reader(writtenData); + QVERIFY(reader.initReadHeader()); + QVERIFY(reader.offsetsCount() == datas.count()); + for (int i = 0; i < datas.count(); ++i) + { + QByteArray readData; + QVERIFY(reader.read(i + 1, readData)); + QVERIFY(readData == datas.at(i)); + } +} + +QTEST_MAIN(TestCore) \ No newline at end of file diff --git a/AdvancedDockingSystemUnitTests/src/TestCore.h b/AdvancedDockingSystemUnitTests/src/TestCore.h new file mode 100644 index 0000000..fbe11d1 --- /dev/null +++ b/AdvancedDockingSystemUnitTests/src/TestCore.h @@ -0,0 +1,14 @@ +#ifndef TEST_CORE_H +#define TEST_CORE_H + +#include + +class TestCore : public QObject +{ + Q_OBJECT + +private slots: + void serialization(); +}; + +#endif \ No newline at end of file diff --git a/AdvancedDockingSystemUnitTests/src/main.cpp b/AdvancedDockingSystemUnitTests/src/main.cpp new file mode 100644 index 0000000..815c5e8 --- /dev/null +++ b/AdvancedDockingSystemUnitTests/src/main.cpp @@ -0,0 +1 @@ +#include diff --git a/build.pro b/build.pro index 63a2d87..b83748a 100644 --- a/build.pro +++ b/build.pro @@ -2,4 +2,5 @@ TEMPLATE = subdirs SUBDIRS = \ AdvancedDockingSystem \ - AdvancedDockingSystemDemo + AdvancedDockingSystemDemo \ + AdvancedDockingSystemUnitTests