121 lines
2.9 KiB
C
121 lines
2.9 KiB
C
#pragma once
|
||
|
||
/*
|
||
说明:
|
||
1. 调整结构体成员顺序以实现内存对齐
|
||
2. 基本类型按大小降序排列(double > long long > int > bool)
|
||
3. 将QString等大对象放在末尾
|
||
4. 保持原有功能和接口不变
|
||
*/
|
||
// 应用层数据结构 - 内存对齐
|
||
|
||
#include <QJsonArray>
|
||
#include <QJsonObject>
|
||
#include <QJsonDocument>
|
||
#include <QVector>
|
||
|
||
// 订单详细信息
|
||
struct OrderDetail {
|
||
double volume = 0.0; // 8字节 (对齐到8)
|
||
long long orderId = 0; // 8字节
|
||
// 总大小16字节 (自然对齐)
|
||
};
|
||
|
||
// 每个挡位的订单
|
||
struct OrderBookEntry {
|
||
// 优先排列基本数据类型 (按大小降序)
|
||
double price = 0.0; // 8字节
|
||
double volume = 0.0; // 8字节
|
||
int orderCount = 0; // 4字节
|
||
// 此处有4字节填充 (满足QString对齐要求)
|
||
|
||
// 最后放置大对象
|
||
QString code = ""; // 24字节 (典型64位系统)
|
||
QVector<OrderDetail> details;// 24字节 (典型64位系统)
|
||
|
||
bool isValid() const {
|
||
return price > 0 && volume > 0 && !details.isEmpty();
|
||
}
|
||
|
||
// 总大小约72字节 (8+8+4+4填充+24+24)
|
||
};
|
||
|
||
// 摆盘数据
|
||
struct OrderBookData {
|
||
// 大对象分组放置
|
||
QString name = ""; // 24
|
||
QString code = ""; // 24
|
||
QString askTime = ""; // 24
|
||
QString bidTime = ""; // 24
|
||
|
||
// 容器对象
|
||
QVector<OrderBookEntry> bids; // 24
|
||
QVector<OrderBookEntry> asks; // 24
|
||
|
||
void clear() {
|
||
bids.clear();
|
||
asks.clear();
|
||
}
|
||
bool isEmpty() const { return bids.isEmpty() && asks.isEmpty(); }
|
||
|
||
// 总大小约144字节 + 向量内容
|
||
};
|
||
|
||
// 优化对齐的结构体
|
||
struct BigOrderInfo {
|
||
// 8字节类型优先
|
||
double price = 0.0; // 8
|
||
double volume = 0.0; // 8
|
||
long long orderId = 0; // 8
|
||
|
||
// 4字节类型
|
||
int nBigOrderType = 1; // 4 (大单类型:0空/1多)
|
||
int level = 1; // 4 (价格挡位)
|
||
|
||
// 1字节类型
|
||
bool isBigOrder = false; // 1
|
||
// 此处有3字节填充 (满足QString对齐要求)
|
||
|
||
// 最后放置QString对象
|
||
QString name = ""; // 24
|
||
QString code = ""; // 24
|
||
QString svrRecvTime = "";// 24 (交易所接收时间)
|
||
|
||
// 序列化/反序列化保持不变
|
||
QJsonObject toJson() const {
|
||
return{
|
||
{ "isBigOrder", isBigOrder },
|
||
{ "nBigOrderType", nBigOrderType },
|
||
{ "orderId", orderId },
|
||
{ "name", name },
|
||
{ "code", code },
|
||
{ "price", price },
|
||
{ "volume", volume },
|
||
{ "level", level },
|
||
{ "svrRecvTime", svrRecvTime }
|
||
};
|
||
}
|
||
|
||
static BigOrderInfo fromJson(const QJsonObject& json) {
|
||
BigOrderInfo info;
|
||
info.isBigOrder = json.value("isBigOrder").toBool(false);
|
||
info.nBigOrderType = json.value("nBigOrderType").toInt(1);
|
||
|
||
// 安全转换long long
|
||
bool ok = false;
|
||
info.orderId = json.value("orderId").toString().toLongLong(&ok);
|
||
if (!ok) info.orderId = 0;
|
||
|
||
info.name = json.value("name").toString("");
|
||
info.code = json.value("code").toString("");
|
||
info.price = json.value("price").toDouble(0.0);
|
||
info.volume = json.value("volume").toDouble(0.0);
|
||
info.level = json.value("level").toInt(1);
|
||
info.svrRecvTime = json.value("svrRecvTime").toString("");
|
||
|
||
return info;
|
||
}
|
||
};
|
||
|
||
// 在全局作用域声明元类型
|
||
Q_DECLARE_METATYPE(BigOrderInfo) |