1
0
mirror of https://gitee.com/drabel/LibQQt.git synced 2025-01-04 10:18:44 +08:00

move data serialization to highgrade library for easy modification.

This commit is contained in:
tianduanrui 2020-04-05 20:51:03 +08:00
parent 7113c23850
commit 05a3d3f824
21 changed files with 0 additions and 731 deletions

View File

@ -1,95 +0,0 @@
#include <qqtdataserialization.h>
#include <QDebug>
#include <qqtcore.h>
QQtDataSerialization::QQtDataSerialization ( QObject* parent )
: QQtMessage ( parent )
{
mIndex = 0;
}
void QQtDataSerialization::clear() { mDict.clear(); }
void QQtDataSerialization::seek ( int index ) { mIndex = index; }
int QQtDataSerialization::pos()
{
return mIndex;
}
int QQtDataSerialization::length() { return 2 + 2 + mDict.toJson ( QJsonDocument::Compact ).size() + 2 + 2; }
void QQtDataSerialization::dump()
{
quint16 header = 0x77EE;
quint16 size = 0;
QByteArray json_data;
quint16 checksum = 0;
quint16 tail = 0x77FF;
QByteArray bytes = mBytes;
bytes >> header >> size;
int json_size = size - ( 2 + 2 + 2 + 2 );
json_data.resize ( json_size );
bytes >> json_data;
bytes >> checksum >> tail;
QQtDictionary dict;
dict.fromJson ( json_data );
QByteArray data = dict.toJson ( QJsonDocument::Indented );
qDebug() << "{" << hex << header << ", "
<< oct << size << ", "
<< oct << json_data.size() << ", "
<< hex << checksum << ", "
<< hex << tail
<< "}";
qDebug() << qPrintable ( data );
}
void QQtDataSerialization::dump_dictionary()
{
QQtDictionary& dict = mDict;
qDebug() << dict["Root"];;
}
void QQtDataSerialization::packer ( QByteArray& l ) const
{
quint16 header = 0x77EE;
quint16 size = 0;
QByteArray json_data;
quint16 checksum = 0;
quint16 tail = 0x77FF;
json_data = mDict.toJson ( QJsonDocument::Compact );
size = 2 + 2 + json_data.size() + 2 + 2;
for ( int i = 0; i < json_data.size(); i++ )
{
char check = json_data.data() [i];
if ( checksum + check >= 0x77FF )
checksum = 0;
checksum = checksum + ( quint16 ) check;
}
QByteArray& bytes = l;
bytes << header << size << json_data << checksum << tail;
}
void QQtDataSerialization::parser ( const QByteArray& _l )
{
quint16 header = 0x77EE;
quint16 size = 0;
QByteArray json_data;
quint16 checksum = 0;
quint16 tail = 0x77FF;
QByteArray bytes = _l;
bytes >> header >> size;
int json_size = size - ( 2 + 2 + 2 + 2 );
json_data.resize ( json_size );
bytes >> json_data;
bytes >> checksum >> tail;
QQtDictionary& dict = mDict;
dict.fromJson ( json_data );
}

View File

@ -1,281 +0,0 @@
#ifndef QQTDATASERIALIZATION_H
#define QQTDATASERIALIZATION_H
#include <QObject>
#include <qqtmessage.h>
#include <qqtdictionary.h>
#include <qqt-local.h>
/**
* @brief The QQtDataSerialization class
* LibQQt提供的标准报文类
*
*
* Dict ---- Buffer
* QByteArray
* serialize/unserialize序列化到QByteArray里QByteArray里反序列化到内部字典里
*
* Buffer:
* |quint16 header|quint16 size|quint8* json_data|quint16 checksum|quint16 tail|
* :
* dict["Root"][0-N]=Value;
* data段保存的是压缩的json结构数据
* data段的字典
*
*/
class QQTSHARED_EXPORT QQtDataSerialization : public QQtMessage
{
Q_OBJECT
public:
explicit QQtDataSerialization ( QObject* parent = 0 );
virtual ~QQtDataSerialization() {}
QQtDataSerialization ( const QQtDataSerialization& other ) {
*this = other;
}
QQtDataSerialization& operator= ( const QQtDataSerialization& other ) {
this->buffer() = other.buffer();
this->dictionary() = other.dictionary();
return *this;
}
//设置value
template <typename T>
void addValue ( const T& value ) {
mDict["Root"][mIndex++].setValue<T> ( value );
}
/*自己本身没有孩子,是个叶子,添加值*/
void addValue ( const QVariant& value ) {
mDict["Root"][mIndex++].setValue ( value );
}
template <typename T>
QQtDataSerialization& operator = ( const T& value ) {
mDict["Root"][mIndex++].setValue<T> ( value );
return *this;
}
QQtDataSerialization& operator = ( const QVariant& value ) {
mDict["Root"][mIndex++].setValue ( value );
return *this;
}
bool operator == ( const QQtDataSerialization& other ) {
if ( mDict == other.dictionary() && mBytes == other.buffer() )
return true;
return false;
}
/**
* <<
**/
//QQtDataSerialization& operator<< ( std::nullptr_t ) { return *this; }
QQtDataSerialization& operator<< ( const bool& i ) {
mDict["Root"][mIndex++] = i;
return *this;
}
QQtDataSerialization& operator<< ( const float& f ) {
mDict["Root"][mIndex++] = f;
return *this;
}
QQtDataSerialization& operator<< ( const double& f ) {
mDict["Root"][mIndex++] = f;
return *this;
}
QQtDataSerialization& operator<< ( const quint8& i ) {
mDict["Root"][mIndex++] = i;
return *this;
}
QQtDataSerialization& operator<< ( const quint16& i ) {
mDict["Root"][mIndex++] = i;
return *this;
}
QQtDataSerialization& operator<< ( const quint32& i ) {
mDict["Root"][mIndex++] = i;
return *this;
}
QQtDataSerialization& operator<< ( const quint64& i ) {
mDict["Root"][mIndex++] = i;
return *this;
}
QQtDataSerialization& operator<< ( const qint8& i ) {
mDict["Root"][mIndex++] = i;
return *this;
}
QQtDataSerialization& operator<< ( const qint16& i ) {
mDict["Root"][mIndex++] = i;
return *this;
}
QQtDataSerialization& operator<< ( const qint32& i ) {
mDict["Root"][mIndex++] = i;
return *this;
}
QQtDataSerialization& operator<< ( const qint64& i ) {
mDict["Root"][mIndex++] = i;
return *this;
}
QQtDataSerialization& operator<< ( const char* b ) {
QByteArray bytes;
bytes.resize ( qstrlen ( b ) );
memcpy ( bytes.data(), b, qstrlen ( b ) );
mDict["Root"][mIndex++] = bytes;
return *this;
}
QQtDataSerialization& operator<< ( const QByteArray& b ) {
mDict["Root"][mIndex++] = b;
return *this;
}
QQtDataSerialization& operator<< ( const QString& b ) {
mDict["Root"][mIndex++] = b;
return *this;
}
QQtDataSerialization& operator<< ( const QImage& f ) {
QByteArray bytes;
bytes.resize ( f.byteCount() );
memcpy ( bytes.data(), f.bits(), f.byteCount() );
mDict["Root"][mIndex++] = bytes;
return *this;
}
template <typename T>
QQtDataSerialization& operator<< ( const T& b ) {
mDict["Root"][mIndex++] = b;
return *this;
}
QQtDataSerialization& operator<< ( const QVariant& b ) {
mDict["Root"][mIndex++] = b;
return *this;
}
/**
* >>
**/
//QQtDataSerialization& operator>> ( std::nullptr_t& ptr ) { ptr = nullptr; return *this; }
QQtDataSerialization& operator>> ( bool& i ) {
i = mDict["Root"][mIndex++].getValue().toBool();
return *this;
}
QQtDataSerialization& operator>> ( float& f ) {
f = mDict["Root"][mIndex++].getValue().toFloat();
return *this;
}
QQtDataSerialization& operator>> ( double& f ) {
f = mDict["Root"][mIndex++].getValue().toDouble();
return *this;
}
QQtDataSerialization& operator>> ( qint8& i ) {
i = mDict["Root"][mIndex++].getValue().toInt();
return *this;
}
QQtDataSerialization& operator>> ( qint16& i ) {
i = mDict["Root"][mIndex++].getValue().toInt();
return *this;
}
QQtDataSerialization& operator>> ( qint32& i ) {
i = mDict["Root"][mIndex++].getValue().toInt();
return *this;
}
QQtDataSerialization& operator>> ( qint64& i ) {
i = mDict["Root"][mIndex++].getValue().toLongLong();
return *this;
}
QQtDataSerialization& operator>> ( quint8& i ) {
i = mDict["Root"][mIndex++].getValue().toUInt();
return *this;
}
QQtDataSerialization& operator>> ( quint16& i ) {
i = mDict["Root"][mIndex++].getValue().toUInt();
return *this;
}
QQtDataSerialization& operator>> ( quint32& i ) {
i = mDict["Root"][mIndex++].getValue().toUInt();
return *this;
}
QQtDataSerialization& operator>> ( quint64& i ) {
i = mDict["Root"][mIndex++].getValue().toULongLong();
return *this;
}
QQtDataSerialization& operator>> ( QByteArray& b ) {
b = mDict["Root"][mIndex++].getValue().toByteArray();
return *this;
}
QQtDataSerialization& operator>> ( QString& b ) {
b = mDict["Root"][mIndex++].getValue().toString();
return *this;
}
QQtDataSerialization& operator>> ( QImage& f ) {
QByteArray bytes;
bytes = mDict["Root"][mIndex++].getValue().toByteArray();
f.loadFromData ( bytes );
return *this;
}
template <typename T>
QQtDataSerialization& operator>> ( T& b ) {
b = mDict["Root"][mIndex++].getValue().value<T>();
return *this;
}
QQtDataSerialization& operator>> ( QVariant& b ) {
b = mDict["Root"][mIndex++].getValue();
return *this;
}
/**
* operator [] json_data的数据字典
*/
QQtDictionary& operator[] ( int index ) { return mDict["Root"][index]; }
const QQtDictionary& operator[] ( int index ) const { return mDict["Root"][index]; }
//输入、输出
QByteArray& buffer() { return mBytes; }
const QByteArray& buffer() const { return mBytes; }
//直接操作 ["Root"][0-N][...] = value
QQtDictionary& dictionary() { return mDict; }
const QQtDictionary& dictionary() const { return mDict; }
//手动packer()/parser()
void serialize() {
packer ( mBytes );
}
void unserialize() {
parser ( mBytes );
}
void clear();
void seek ( int index );
int pos();
int length();
void dump();
void dump_dictionary();
public:
virtual quint16 minlength() const override {
return 0x04;
}
virtual quint16 maxlength() const override {
return 0x77FF;
}
virtual void packer ( QByteArray& l ) const override;
virtual void parser ( const QByteArray& _l ) override;
protected:
private:
QQtDictionary mDict;
quint32 mIndex;
QByteArray mBytes;
};
#endif // QQTDATASERIALIZATION_H

View File

@ -1,2 +0,0 @@
#include <qqtstandardprotocol.h>

View File

@ -1,75 +0,0 @@
#ifndef QQTSTANDARDPROTOCOL_H
#define QQTSTANDARDPROTOCOL_H
#include <QObject>
#include <qqtdataserialization.h>
#include <qqtprotocol.h>
#include <qqt-local.h>
/**
* @brief The QQtStandardProtocol class
* LibQQt提供的标准协议类
*/
class QQTSHARED_EXPORT QQtStandardProtocol : public QQtProtocol
{
Q_OBJECT
public:
explicit QQtStandardProtocol ( QObject* parent = 0 )
: QQtProtocol ( parent ) {}
virtual ~QQtStandardProtocol() {}
virtual void sendMessage ( const QQtDataSerialization& data ) {
//已经序列化好buffer
#if 0
QQtDataSerialization ds0 = data;
ds0.serialize(); //数据本地化,补充一次序列化。
#else
const QQtDataSerialization& ds0 = data;
#endif
QByteArray bytes = ds0.buffer();
write ( bytes );
}
virtual void recvMessage ( const QQtDataSerialization& data ) {
//已经反序列化好buffer
emit notifyToProtocolManager ( this, &data );
emit readyRead ( data );
}
signals:
void readyRead ( const QQtDataSerialization& data );
protected:
private:
// QQtProtocol interface
public:
virtual quint16 minlength() override {
QQtDataSerialization ds0;
return ds0.minlength();
}
virtual quint16 maxlength() override {
QQtDataSerialization ds0;
return ds0.maxlength();
}
virtual quint16 splitter ( const QByteArray& _l ) override {
QByteArray bytes = _l.left ( minlength() );
quint16 header, size;
bytes >> header >> size;
return size;
}
virtual bool dispatcher ( const QByteArray& _l ) override {
QQtDataSerialization ds1;
ds1.buffer() = _l;
ds1.unserialize();
recvMessage ( ds1 );
}
virtual void initializer() override {
}
};
#endif // QQTSTANDARDPROTOCOL_H

View File

@ -690,18 +690,6 @@ contains (DEFINES, __HIGHGRADE__) {
$$PWD/highgrade/qqtdatapersistence.cpp
HEADERS += \
$$PWD/highgrade/qqtdatapersistence.h
add_object_class(QQtDataSerialization, $$PWD/highgrade)
SOURCES += \
$$PWD/highgrade/qqtdataserialization.cpp
HEADERS += \
$$PWD/highgrade/qqtdataserialization.h
add_object_class(QQtStandardProtocol, $$PWD/highgrade)
SOURCES += \
$$PWD/highgrade/qqtstandardprotocol.cpp
HEADERS += \
$$PWD/highgrade/qqtstandardprotocol.h
}
include ($$PWD/qqt_3rdparty.pri)

View File

@ -1,24 +0,0 @@
/*spinbox 抬起样式*/
QTimeEdit::up-button,QDoubleSpinBox::up-button,QSpinBox::up-button {
subcontrol-origin:border;
subcontrol-position:right;
width: 12px;
}
QTimeEdit::down-button,QDoubleSpinBox::down-button,QSpinBox::down-button {
subcontrol-origin:border;
subcontrol-position:left;
width: 12px;
}
/*按钮按下样式*/
QTimeEdit::up-button:pressed,QDoubleSpinBox::up-button:pressed,QSpinBox::up-button:pressed{
subcontrol-origin:border;
subcontrol-position:right;
width: 12px;
}
QTimeEdit::down-button:pressed,QDoubleSpinBox::down-button:pressed,QSpinBox::down-button:pressed,QSpinBox::down-button:pressed{
subcontrol-position:left;
width: 12px;
}

View File

@ -1,89 +0,0 @@
<?xml version="1.0"?>
<manifest package="org.qtproject.example.testdataserialization" xmlns:android="http://schemas.android.com/apk/res/android" android:versionName="1.0" android:versionCode="1" android:installLocation="auto">
<application android:hardwareAccelerated="true" android:name="org.qtproject.qt5.android.bindings.QtApplication" android:label="testdataserialization" android:icon="@drawable/icon">
<activity android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|layoutDirection|locale|fontScale|keyboard|keyboardHidden|navigation" android:name="org.qtproject.qt5.android.bindings.QtActivity" android:label="testdataserialization" android:screenOrientation="unspecified" android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<!-- Application arguments -->
<!-- meta-data android:name="android.app.arguments" android:value="arg1 arg2 arg3"/ -->
<!-- Application arguments -->
<meta-data android:name="android.app.lib_name" android:value="testdataserialization"/>
<meta-data android:name="android.app.qt_sources_resource_id" android:resource="@array/qt_sources"/>
<meta-data android:name="android.app.repository" android:value="default"/>
<meta-data android:name="android.app.qt_libs_resource_id" android:resource="@array/qt_libs"/>
<meta-data android:name="android.app.bundled_libs_resource_id" android:resource="@array/bundled_libs"/>
<!-- Deploy Qt libs as part of package -->
<meta-data android:name="android.app.bundle_local_qt_libs" android:value="1"/>
<meta-data android:name="android.app.bundled_in_lib_resource_id" android:resource="@array/bundled_in_lib"/>
<meta-data android:name="android.app.bundled_in_assets_resource_id" android:resource="@array/bundled_in_assets"/>
<!-- Run with local libs -->
<meta-data android:name="android.app.use_local_qt_libs" android:value="1"/>
<meta-data android:name="android.app.libs_prefix" android:value="/data/local/tmp/qt/"/>
<meta-data android:name="android.app.load_local_libs" android:value="plugins/platforms/android/libqtforandroid.so"/>
<meta-data android:name="android.app.load_local_jars" android:value="jar/QtAndroid.jar:jar/QtAndroid-bundled.jar"/>
<meta-data android:name="android.app.static_init_classes" android:value=""/>
<!-- Messages maps -->
<meta-data android:value="@string/ministro_not_found_msg" android:name="android.app.ministro_not_found_msg"/>
<meta-data android:value="@string/ministro_needed_msg" android:name="android.app.ministro_needed_msg"/>
<meta-data android:value="@string/fatal_error_msg" android:name="android.app.fatal_error_msg"/>
<!-- Messages maps -->
<!-- Splash screen -->
<!-- meta-data android:name="android.app.splash_screen_drawable" android:resource="@drawable/logo"/ -->
<!-- meta-data android:name="android.app.splash_screen_sticky" android:value="true"/ -->
<!-- Splash screen -->
<!-- Background running -->
<!-- Warning: changing this value to true may cause unexpected crashes if the
application still try to draw after
"applicationStateChanged(Qt::ApplicationSuspended)"
signal is sent! -->
<meta-data android:name="android.app.background_running" android:value="false"/>
<!-- Background running -->
<!-- auto screen scale factor -->
<meta-data android:name="android.app.auto_screen_scale_factor" android:value="false"/>
<!-- auto screen scale factor -->
<!-- extract android style -->
<!-- available android:values :
* full - useful QWidget & Quick Controls 1 apps
* minimal - useful for Quick Controls 2 apps, it is much faster than "full"
* none - useful for apps that don't use any of the above Qt modules
-->
<meta-data android:name="android.app.extract_android_style" android:value="full"/>
<!-- extract android style -->
</activity>
<!-- For adding service(s) please check: https://wiki.qt.io/AndroidServices -->
</application>
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="16"/>
<supports-screens android:largeScreens="true" android:normalScreens="true" android:anyDensity="true" android:smallScreens="true"/>
<!-- The following comment will be replaced upon deployment with default permissions based on the dependencies of the application.
Remove the comment if you do not require these default permissions. -->
<!-- The following comment will be replaced upon deployment with default features based on the dependencies of the application.
Remove the comment if you do not require these default features. -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!-- %%INSERT_PERMISSIONS -->
<!-- %%INSERT_FEATURES -->
<uses-permission android:name="android.permission.ACCESS_CHECKIN_PROPERTIES"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
</manifest>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 82 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 82 KiB

View File

@ -1,11 +0,0 @@
#include "mainwindow.h"
#include <QQtApplication>
int main ( int argc, char* argv[] )
{
QQtApplication a ( argc, argv );
MainWindow w;
w.show();
return a.exec();
}

View File

@ -1,14 +0,0 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}

View File

@ -1,22 +0,0 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

View File

@ -1,40 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>testdataserialization</string>
</property>
<widget class="QWidget" name="centralWidget"/>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>17</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

View File

@ -1,66 +0,0 @@
#-------------------------------------------------
#
# Project created by QtCreator 2018-10-06T08:16:10
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = testdataserialization
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
include($${PWD}/../../multi-link/add_base_manager.pri)
#-------------------------------------------------
#用户工程配置
#-------------------------------------------------
add_version(1,0,0,0)
add_deploy()
add_deploy_config($${PWD}/AppRoot)
add_dependent_manager(QQt)
system(touch main.cpp)
#-------------------------------------------------
#用户工程配置
#-------------------------------------------------
equals(QSYS_PRIVATE, macOS) {
CONFIG += app_bundle
}
contains(QSYS_PRIVATE, Android|AndroidX86) {
CONFIG += mobility
MOBILITY =
DISTFILES += \
android/AndroidManifest.xml
ANDROID_PACKAGE_SOURCE_DIR = $${PWD}/android
}
message ($${TARGET} config $${CONFIG})
message ($${TARGET} DEFINE $${DEFINES})
message ($${TARGET} prelink $${QMAKE_PRE_LINK})
message ($${TARGET} postlink $${QMAKE_POST_LINK})