计算对象在Qt中的序列化大小 [英] Calculate serialization size of objects in Qt

查看:200
本文介绍了计算对象在Qt中的序列化大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何知道qt数据类型的大小(以字节为单位)?如果这些数据类型写在某些QFile上,则包括QString对象.我必须在如下的Student类中实现sizeOf()函数;类似于我们在C语言中的sizeof(struct student).

How can I know the size of qt data types in bytes; including QString objects if these data types were written on some QFile. I have to implement sizeOf() function in the Student class as below; something like we sizeof(struct student) in C.

学生班

        #include<QtCore>

        class Student
        {
        public:

            QString name,fname;
            quint8 age,weight,clss;

            Student(){

            }

    /*the following function should return the size of this object in bytes; 
     I will use QDataStream & operator<<(QDataStream &out, Student &f)
     function  to write data to a QFile   */
        qint16 sizeOf()
        {
            // needs implementation;
        }

        };

        QDataStream & operator<<(QDataStream &out, Student &f)
        {

            out<<f.name<<f.fname<<f.age<<f.weight<<f.clss<<f.next;
            return out;


        }
        QDataStream & operator>>(QDataStream &in, Student &f)
        {

            in>>f.name>>f.fname>>f.age>>f.weight>>f.clss>>f.next;

            return in;
        }

我知道可以用QDataStream & operator>>(QDataStream &in, Student &f)读取数据;但是我还想知道其他情况下的尺寸.

I know that data can be read with QDataStream & operator>>(QDataStream &in, Student &f); but I want to know size also for some other cases.

这没有给我提供有效的档案大小.看来Qt在序列化时增加了一些额外的位.可能是因为在不同平台上的字节序独立性.实际大小总是大于sizeOf()函数

This does not give me a valid size on file. It seems Qt adds some extra bits while serializing; possibly for endian-ness independency on different platforms. Actual size is always more than returned by sizeOf() function

qint16 sizeOf()
            {
                qint16 size=0;
                size+=sizeof(quint8)*3; // size of age, weight and clss 
                                        //variables all have type quint8
                size+=name.size()*16;   // number of characters in string
                                         // multiply with 16 bit QChar
                size+=fname.size()*16;
                return size;
            }

我正在使用QFile,QDataStream api. Windows 8上的Qt版本4.8.​​

I am using QFile, QDataStream api. Qt version 4.8 on Windows 8.

推荐答案

sizeof将为您提供的大小不能反映对象可能具有的实际大小.例如,无论实际字符串有多长,在32位版本中的sizeofQString始终为4位.

The size which sizeof will give you does not reflect the actual size an object might have. For example, the sizeof a QString in a 32 bit build will always be 4 bites, regardless how long the actual string is.

sizeof运算符包含不需要序列化的内容,例如对象的vtable指针,并且不考虑为该对象动态分配的资源的大小.

This sizeof operator includes stuff that doesn't need to be serialized, like the object's vtable pointer, and does not account for the size of dynamically allocated resources for that object.

您可以轻松确定可序列化的大小,只需使用QDataStream,先从device()获取pos(),然后在数据流中输入对象,然后再与pos()进行比较.

You can easily determine the serializable size, just use a QDataStream, from the device() get the pos() before, input the object in the data stream and compare with the pos() afterwards.

此外,此行显然是错误的:size+=sizeof(quint8*3)它不会给您三倍的字节大小.它将为您提供int的大小,这是乘法后如何提升结果的方式.

Also, this line is clearly wrong: size+=sizeof(quint8*3) it will not give you three times the size of a byte. It will give you the size of an int, which is how the result is promoted after the multiplication.

这是一个漂亮的小类,您可以用于该任务:

Here is a nifty little class you can use for the task:

class SerialSize {
public:
  SerialSize() : stream(&data) { data.open(QIODevice::WriteOnly); }

  template <typename T>
  quint64 operator ()(const T & t) {
    data.seek(0);
    stream << t;
    return data.pos();
  }

private:
  QBuffer data;
  QDataStream stream;
};

然后使用它:

  SerialSize size;
  qDebug() << size(QString("a")); // 6
  qDebug() << size(QString("aa")); // 8

这篇关于计算对象在Qt中的序列化大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆