"The seven of the big Talk QT" QT serialization operation

Source: Internet
Author: User

Application Requirements:

There is such a requirement in the development of the network disk. That is, the file version number control, that is, the record file version number of the replacement information, where the replacement information is not recorded in a different time file changes, that is, the file of the increase, deletion, alteration, renaming and other operations. A file is saved under each folder to be monitored. Record incremental information for file changes. Each time the low version number to the higher version of the upgrade can be achieved through the elimination of the combined operation at high speed. A detailed implementation of the file version number control is listed after the development is intact. This only indicates how it is saved, and the instance object of the file operation is serialized and saved in the file.

The implementation of serialization:

Here we use Qdatastream for serialization, and QT has different requirements for different instanced objects. There are two main types of this. That is, QT native data type. For example: QString, Qmap, Qhash, and so on, this is the serialization of native data types. We do not need to do any additional operations and can serialize to the file directly. Another type of special is our custom data structure or class, in such a way that using Qdatastream cannot be serialized directly, we have to overload the << and >> operators, only after overloading is enough to be able to serialize according to our requirements. Here are some examples. How our custom data structures or classes should be serialized:

The class you define: Lhtfileversionitem, which is used to record an operation. It is defined as:

#ifndef lht_fileversionitem_h#define lht_fileversionitem_h#include <qdatastream>struct FileVersionItem{    QString M_sfileabsolutepath;    QString M_sfileorgname;    QString M_sfilenowname;    int m_sfiletype;    QString M_sfilemovefromabsolutepath; QString M_sfilemovetoabsolutepath;};    Class Lhtfileversionitem{public:lhtfileversionitem ();    ~lhtfileversionitem ();    void setversion (int version);    void Setop (int op);    int getversion ();    int Getop ();    fileversionitem* Getfileversionpointer ();    Friend Qdatastream &operator<< (Qdatastream &, const Lhtfileversionitem &); Friend Qdatastream &operator>> (Qdatastream &, Lhtfileversionitem &);p rivate:int m    _iversion;    !-1:delete 1:crate 2:change//!3:rename 4:move int m_iop; Fileversionitem *m_hfileversionpointer;}; #endiF//Lht_fileversionitem_h 
Among them, friend Qdatastream &operator << and friend Qdatastream &operator >> is the declared overload of the operator. Note: Here we use friend to declare a friend function. Here are some introductions to Friendkeyword:

What is friend? Why do you want to make a UF dollar? We know that the use of the mechanism of the class to achieve the data hiding and encapsulation. A data member of a class is generally defined as a private member. member functions are generally defined as public. This provides an interface between the class and the outside world. However, sometimes it is necessary to define some functions, Note that these functions are not part of the class (so when implementing the function in a CPP file). The function is not required before use. Class Name: The way the function nameis, but it is frequently necessary to access private data members of the class, which is the ability to define these functions as friend functions. In addition to the friend function is also a friend class, the two collectively referred to as friend. The role of friends is to improve the execution efficiency of the program (that is, to reduce the type checking and security checks, and so on, these operations require time overhead), but it also destroys the class's encapsulation and concealment at the same time, so that non-member functions can access the private members of the class.

In fact, it is possible to understand friend's concern for friend, and friend means "friends" and "Friendly Relations". There are only two (functions and classes are friendly to form a friend function. Class is friendly to the class, and I agree that it will visit my private members.

The class you define: the implementation of Lhtfileversionitem:

#include "lht_fileversionitem.h" Lhtfileversionitem::lhtfileversionitem () {m_hfileversionpointer = new Fileversionitem ();} Lhtfileversionitem::~lhtfileversionitem () {}void lhtfileversionitem::setversion (int version) {this->m_iversion = version;} void Lhtfileversionitem::setop (int op) {this->m_iop = op;} int lhtfileversionitem::getversion () {return this->m_iversion;} int Lhtfileversionitem::getop () {return this->m_iop;} fileversionitem* Lhtfileversionitem::getfileversionpointer () {return this->m_hfileversionpointer;} //! Implementation of overloaded operator << Qdatastream &operator<< (qdatastream &output, const Lhtfileversionitem & Item) {OUTP               UT << item.m_iversion << item.m_iop << item.m_hfileversionpointer->m_sfileabsolutepath << Item.m_hfileversionpointer->m_sfilemovefromabsolutepath << Item.m_hfileversionpointer->m_sfilemoveto Absolutepath << Item.m_hfileversionpointer->m_sfilenowname << Item.m_hfileversionpointer->m_sfileorgname; return output;} //! Implementation of overloaded operator >> Qdatastream &operator>> (Qdatastream & Input, Lhtfileversionitem & Item) {Input >& Gt Item.m_iversion >> item.m_iop >> item.m_hfileversionpointer->m_sfileabsolutepath >> Item . M_hfileversionpointer->m_sfilemovefromabsolutepath >> Item.m_hfileversionpointer->m_ Sfilemovetoabsolutepath >> item.m_hfileversionpointer->m_sfilenowname >> Item.m_hfileversionpo    Inter->m_sfileorgname; return input;}
problems that occur after inheriting from object:

Assuming that our own defined classes inherit from Qobject, this compilation error may occur when used: Error C2248 ' Qobject::qobject ': Cannot access private member declared in class ' Qob Ject ' see below for example:

Why add to inherit from Qobject will appear this problem, certainly is qobject problem, view source code in private has such a sentence:

Private:    q_disable_copy (qobject)    Q_private_slot (D_func (), void _q_reregistertimers (void *))/* Some classes Permit copies to is made of an object. These classes contains a private copy constructor and assignment operator to disable copying (the compiler gives an ER     ROR message). */#define Q_DISABLE_COPY (Class) Class (const class &); Class &operator= (const class &);
From the above gaze and implementation can see inherit from Qobject after it does not agree with the object assignment operation, that is =. I'm looking for all the functions I've called and I don't see direct assignment, so why is this happening? The reason is that the pass-through of a function call is also considered an assignment operation. Therefore, the problem arises, the direct removal of qobject inheritance can be, we do not use to qobject unique specific.

The impact of local variable usage on performance and the heap and stack of processes:

Because in the code I used qmulithash<qstring, lhfilteversionitem> tmp; This local variable to hold a file under a folder. Because I used the loop to simulate 500,000 of data serialization while writing the test code, I saved it in the file, and during the execution I found that reading the function took a very long time. And the most time-consuming read operation in the function only takes a very short time, but the function can not exit immediately, after waiting for about 30s talent exit, the relevant code such as the following:

void Lhtworkflow::readalldatafromfile (qmultihash<qstring, lhtfileitem> &m_hfileiteminfo) {    if (NULL = = M_ffileinfohandle)    {        OpenFile (m_sfileiteminfoabsolutepath, 0);    }    M_ffileinfohandle->seek (0);    Qdatastream input (M_ffileinfohandle); Qmultihash<qstring, lhtfileitem> final;    while (!input.atend ())    {        qmultihash<qstring, lhtfileitem> tmp;        Input >> tmp;        Final + = tmp;    } M_hfileiteminfo = final;    CloseFile (M_ffileinfohandle);}
After careful analysis and thinking, the problem is found in the local variable final, because it is a local variable, so when the function is completed after the local variable will be destroyed, because it is a variable of type Qmultihash, we know that hash compared to the array is a big advantage of the data address is not contiguous. The memory address that the element occupies in the memory control is discontinuous, and the amount of data is large. Therefore, in the process of destruction should be a step through to release the memory pointer to go. The assumption is that a continuous data structure such as an array. Release will be very fast, just need to set the block memory flag to useless it can be recycled by the system. This is the first time I have met. Feel very interesting. I also have a certain sense of the future code to write. So I looked at the relevant contents of the heap and stack.

The following links: http://blog.csdn.net/hairetz/article/details/4141043. In order to deepen my understanding, I'll make a list here.

1> Prep Knowledge-memory allocation for programs

A program compiled by C + + takes up a few pieces of memory:

1) Stack-The release is voluntarily allocated by the compiler itself. The parameters of the stored function, the value of the local variable, and so on. It operates in a manner similar to a stack in a data structure.

2) heap Area (heap)-generally released by the program ape allocation, if the program Ape is not released, the program may end up with the recovery of the operating system.

3) Global Zone (static zone)-the storage of global variables and static variables is put together, initialized global variables and static variables in an area. Uninitialized global variables and uninitialized static variables are adjacent to an area. The program ends and is released by the system.

4) literal constant area-the constant string is placed here. Released by the system after the program is finished.

5) Program code area-binary code that holds the body of the function.

Comparison of 2> stacks and stacks

1) How to apply

Stack is assigned voluntarily by the system itself. Like what. Declare a local variable int b in the function; The system itself proactively opens up space for B in the stack.

The heap (heap) requires the program ape to apply himself. and indicates the size, in C, of the malloc function such as: P1 = (char *) malloc (10); In C + + with the new operator such as: P2 = new CHAR[10];

But note: P1, p2 itself is in the stack, only the space allocated by malloc and new is in the heap.

3> System response after application

Stack: Only the remaining space on the stack is larger than the requested space, the system will provide memory for the program. Otherwise, the exception prompt stack is reported to overflow.

Heap: You should first know that the operating system has a list of records spare memory addresses. When the system receives a request for a program, it iterates through the list, finds the first heap node that is larger than the requested space, and then removes the node from the list of spare nodes and assigns the node's space to the program, in addition. For most systems, the size of this allocation is recorded at the first address in this memory space. This allows the DELETE statement in the code to properly release the memory space.

In addition, because the size of the found heap node does not necessarily equal the size of the request, the system will voluntarily put the extra part into the spare chain list.

4> Limit of application size

Stack: Under Windows, the stack is the data structure to the low address extension, which is a contiguous area of memory. This means that the maximum capacity of the top and the stack is pre-defined by the system. Under Windows. The stack size is 2M, assuming that the requested space exceeds the remaining space on the stack and will prompt for overflow. Therefore, the space available from the stack is small.

Heap: A heap is a data structure that extends to a high address, and is a discontinuous area of memory. This is because the system uses the linked list to store the spare memory address, which is naturally discontinuous, while the traversal direction of the list is addressed by the low address to the high address. The size of the heap is limited by the valid virtual memory in the computer system. This shows that the heap is more flexible and larger than the space available.

Comparison of 5> application efficiency

Stack: The system itself is assigned voluntarily. Faster. But the program ape is uncontrollable.

Heap: Is the memory allocated by new, the best way is to allocate virtual memory with VirtualAlloc, it is not in the heap, not on the stack, but directly in the process's address space to retain a piece of memory. Although it is most inconvenient to use. But the speed is also the most flexible.

6> storage content in heaps and stacks

Stack: When a function is called, the first stack is the address of the next instruction (the next executable statement of the function call statement) in the main function, followed by the individual parameters of the function. In most C compilers, the arguments are in the right-to-left stack, followed by local variables in the function. Note: Static variables are not in the stack. When the function call ends, the local variable is first out of the stack, followed by the argument, and the last stack pointer points to the most current address. The next specified in the main function. The program continues execution from that point.

Heap: The size of the heap is usually stored on the heap's head with one byte.

The detailed contents of the heap are arranged by the program Ape.

Summarize:

Through the analysis of this content, there is a clearer understanding of several aspects that affect the performance of the program, as well as a deeper understanding of the stack of processes. Feel like you're beginning to focus on what's real. This I feel is very good, refuels, to each does not understand the question to be careful summary.


"The seven of the big Talk QT" QT serialization operation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.