How to combine STL to MFC (1)

zhaozj2021-02-08  236

How to combine STL to MFC (1)

Tang Hao

1.1 Serialization of the STL Continuous Container

How to make STL easy to use in the MFC, it is indeed a problem that VC programmers cannot avoid, I will combine some of my actual work and program, I hope to communicate and discuss with you.

The first time I want to explore how to use the serialization function of the MFC to the STL container, that is, how the STL container is serialized as a member of the class.

1.1.1 MFC practice

We know that MFC itself has three containers with Carray, Clist, and CMAP. Their parent class is cobject and implements Void Serialize (CARCHIVE & AR) method, so it is very easy when these containers act as members of the class. Serialization, for example, when we declare the following members:

Carray m_aint;

CLIST m_lint;

CMAP m_mint;

The serialization function is as follows:

Void Cexampledoc :: Serialize (CARCHIVE & A)

{

IF (ar.isstoring ())

{

}

Else

{

}

M_Aint.Serialize (ar);

m_lint.serialize (ar);

M_Mint.Serialize (ar);

}

But when our STL container is serialized as a member variable, how can we do it?

1.1.2 sequence

General practice

We know that the STL container is divided into two consecutive and non-continuous. The position of the insertion of the object and the value of the object is related, so it should be handled separately. Then let's talk about the serialization of the continuous container. They include Vector Deque And List, etc.

In fact, serialization is a fairly simple operation. If we applaud a member is as follows:

Std :: vector m_vint;

Then we can serialize this like this.

Void Cexampledoc :: Serialize (CARCHIVE & A)

{

IF (ar.isstoring ())

{

Ar << m_vint.size ();

For (Vector :: itrator it = m_vint.begin ()

IT! = m_vint.end ()

; IT) {

Ar << * IT;

}

}

Else

{

Long nsize;

AR >> NSIZE;

m_vint.resize (nsize);

For (Vector :: itrator it = m_vint.begin ()

IT! = m_vint.end ()

; IT) {

Ar >> * it;

}

}

}

1.1.3 Practice of the function object can be introduced

It can be seen that it is difficult to do serialize the serialization of each Item in each container object.

So we can do this first define a function object:

Template Struct MySerialize

: public st :: unary_function

{

MySerialize (CARCHIVE & A): M_AR (ar) {}

Void Operator () (T & INFO)

{

IF (m_ar.isstoring ())

{

m_ar << info;

}

Else

{

M_AR >> INFO;

}

}

CARCHIVE & M_AR;

}

Then use it when serialization:

Void Cexampledoc :: Serialize (CARCHIVE & A)

{

IF (ar.isstoring ())

{

Ar << m_vint.size ();

Else

{

Long nsize;

AR >> NSIZE;

m_vint.resize (nsize);

}

For_each (m_vint.begin (), m_vint.end (), MySerialize (ar));

}

1.1.4 All work is done

However, there is still a part of the free code to deal with the size of the container size.

How to combine the part of the code? In fact, you can add a template function, as shown below.

Template

Void ContainerSerialize (CARCHIVE & AR, Container_Type & Con, Type TMP)

{

IF (ar.isstoring ())

{

Ar << con.size ();

}

Else

{

Long nsize;

AR >> NSIZE;

Con.Resize (nsize);

}

For_each (con.begin (), con.nd (), myserialize (ar));

}

Then our serialization code can be written this:

Void Cexampledoc :: Serialize (CARCHIVE & A)

{

IF (ar.isstoring ())

{

}

Else

{

}

ContainerSerialize (Ar, m_vint, int ());

}

Ok, I will explain the serialization method of the non-continuous container in the next time.

转载请注明原文地址:https://www.9cbs.com/read-1233.html

New Post(0)