C++ 序列化 代码
1 #include <afx.h>
2 #include <iostream.h>
3 class CMyObject : public CObject
4 {
5 // ...Member functions
6 public:
7 CMyObject() { }
8 virtual void Serialize( CArchive& ar ){};
9
10 // Implementation
11 protected:
12 DECLARE_SERIAL( CMyObject )
13 };
14
15
16 class COtherObject : public CObject
17 {
18 // ...Member functions
19 public:
20 COtherObject() { c='t'; }
21 virtual void Serialize( CArchive& ar );//;
22
23 // Implementation
24 protected:
25 DECLARE_SERIAL( COtherObject )
26 public:
27 char c;
28 };
29
30
31 class CCompoundObject : public CObject
32 {
33 // ...Member functions
34 public:
35 CCompoundObject();
36 ~CCompoundObject();
37 virtual void Serialize( CArchive& ar );
38
39 // Implementation
40 public:
41 CMyObject m_myob; // Embedded object
42 COtherObject* m_pOther; // Object allocated in constructor
43 CObject* m_pObDyn; // Dynamically allocated object
44 //..Other member data and implementation
45
46 DECLARE_SERIAL( CCompoundObject )
47 };
48
49
50 ///////////////////////////////////////
51
52 IMPLEMENT_SERIAL(CMyObject,CObject,1)
53 IMPLEMENT_SERIAL(COtherObject,CObject,1)
54 IMPLEMENT_SERIAL(CCompoundObject,CObject,1)
55
56
57 CCompoundObject::CCompoundObject()
58 {
59 m_pOther = new COtherObject; // Exact type known and object already
60 //allocated.
61 m_pObDyn = NULL; // Will be allocated in another member function
62 // if needed, could be a derived class object.
63 }
64
65 CCompoundObject::~CCompoundObject()
66 {
67 delete m_pOther;
68 }
69
70 void CCompoundObject::Serialize( CArchive& ar )
71 {
72 CObject::Serialize( ar ); // Always call base class Serialize.
73 m_myob.Serialize( ar ); // Call Serialize on embedded member.
74 m_pOther->Serialize( ar ); // Call Serialize on objects of known exact type.
75
76 // Serialize dynamic members and other raw data
77 if ( ar.IsStoring() )
78 {
79 ar << m_pObDyn;
80 // Store other members
81 }
82 else
83 {
84 ar >> m_pObDyn; // Polymorphic reconstruction of persistent object
85 //load other members
86 }
87 }
88
89 void COtherObject::Serialize( CArchive& ar )
90 {
91 CObject::Serialize( ar );
92 if ( ar.IsStoring() )
93 {
94 cout<<"ar.IsStoring()"<<endl;
95 ar << c;
96 // Store other members
97 }
98 else
99 {
100 cout<<"No ar.IsStoring()"<<endl;
101 ar >> c; // Polymorphic reconstruction of persistent object
102 //load other members
103 }
104 }
105
106 ////////////////////////////////
107 void main()
108 {
109 CFile theFile;
110 theFile.Open(_T("CArchive__Test.txt"), CFile::modeCreate | CFile::modeWrite);
111 CArchive archiveWrite(&theFile, CArchive::store);
112 CArchive archiveRead(&theFile, CArchive::load);
113
114 CCompoundObject oCompound;
115 oCompound.Serialize(archiveWrite);//
116 //The library defines << and >> operators for CArchive as the first operand and
117 //the following data types and class types as the second operand:
118 // CObject*,SIZE and CSize,float,WORD,CString,POINT and CPoint,DWORD,BYTE,
119 // RECT and CRect,Double,LONG,CTime and CTimeSpan,Int,COleCurrency,COleVariant,
120 // COleDateTime,COleDateTimeSpan;
121 //如果用运算符<< and >>操作类,那么必须重载运算符<< and >>;
122
123 archiveWrite<<"hello";
124
125 CCompoundObject newoCompound;
126 //When you are finished storing or loading data to or from the CArchive object,
127 //close it. Although the CArchive (and CFile) objects will automatically close
128 //the archive (and file), it is good practice to explicitly do so since it makes
129 //recovery from errors easier. For more information about error handling, see the
130 //article Exceptions: Catching and Deleting Exceptions.
131 try
132 {
133 newoCompound.Serialize(archiveRead);
134 }
135 catch( CException* e )
136 {
137 // Handle the exception here.
138 // "e" contains information about the exception.
139 e->Delete();
140 }
141 cout<<newoCompound.m_pOther->c<<endl;
142 archiveWrite.Close();
143 archiveRead.Close();
144 theFile.Close();
145 }
2 #include <iostream.h>
3 class CMyObject : public CObject
4 {
5 // ...Member functions
6 public:
7 CMyObject() { }
8 virtual void Serialize( CArchive& ar ){};
9
10 // Implementation
11 protected:
12 DECLARE_SERIAL( CMyObject )
13 };
14
15
16 class COtherObject : public CObject
17 {
18 // ...Member functions
19 public:
20 COtherObject() { c='t'; }
21 virtual void Serialize( CArchive& ar );//;
22
23 // Implementation
24 protected:
25 DECLARE_SERIAL( COtherObject )
26 public:
27 char c;
28 };
29
30
31 class CCompoundObject : public CObject
32 {
33 // ...Member functions
34 public:
35 CCompoundObject();
36 ~CCompoundObject();
37 virtual void Serialize( CArchive& ar );
38
39 // Implementation
40 public:
41 CMyObject m_myob; // Embedded object
42 COtherObject* m_pOther; // Object allocated in constructor
43 CObject* m_pObDyn; // Dynamically allocated object
44 //..Other member data and implementation
45
46 DECLARE_SERIAL( CCompoundObject )
47 };
48
49
50 ///////////////////////////////////////
51
52 IMPLEMENT_SERIAL(CMyObject,CObject,1)
53 IMPLEMENT_SERIAL(COtherObject,CObject,1)
54 IMPLEMENT_SERIAL(CCompoundObject,CObject,1)
55
56
57 CCompoundObject::CCompoundObject()
58 {
59 m_pOther = new COtherObject; // Exact type known and object already
60 //allocated.
61 m_pObDyn = NULL; // Will be allocated in another member function
62 // if needed, could be a derived class object.
63 }
64
65 CCompoundObject::~CCompoundObject()
66 {
67 delete m_pOther;
68 }
69
70 void CCompoundObject::Serialize( CArchive& ar )
71 {
72 CObject::Serialize( ar ); // Always call base class Serialize.
73 m_myob.Serialize( ar ); // Call Serialize on embedded member.
74 m_pOther->Serialize( ar ); // Call Serialize on objects of known exact type.
75
76 // Serialize dynamic members and other raw data
77 if ( ar.IsStoring() )
78 {
79 ar << m_pObDyn;
80 // Store other members
81 }
82 else
83 {
84 ar >> m_pObDyn; // Polymorphic reconstruction of persistent object
85 //load other members
86 }
87 }
88
89 void COtherObject::Serialize( CArchive& ar )
90 {
91 CObject::Serialize( ar );
92 if ( ar.IsStoring() )
93 {
94 cout<<"ar.IsStoring()"<<endl;
95 ar << c;
96 // Store other members
97 }
98 else
99 {
100 cout<<"No ar.IsStoring()"<<endl;
101 ar >> c; // Polymorphic reconstruction of persistent object
102 //load other members
103 }
104 }
105
106 ////////////////////////////////
107 void main()
108 {
109 CFile theFile;
110 theFile.Open(_T("CArchive__Test.txt"), CFile::modeCreate | CFile::modeWrite);
111 CArchive archiveWrite(&theFile, CArchive::store);
112 CArchive archiveRead(&theFile, CArchive::load);
113
114 CCompoundObject oCompound;
115 oCompound.Serialize(archiveWrite);//
116 //The library defines << and >> operators for CArchive as the first operand and
117 //the following data types and class types as the second operand:
118 // CObject*,SIZE and CSize,float,WORD,CString,POINT and CPoint,DWORD,BYTE,
119 // RECT and CRect,Double,LONG,CTime and CTimeSpan,Int,COleCurrency,COleVariant,
120 // COleDateTime,COleDateTimeSpan;
121 //如果用运算符<< and >>操作类,那么必须重载运算符<< and >>;
122
123 archiveWrite<<"hello";
124
125 CCompoundObject newoCompound;
126 //When you are finished storing or loading data to or from the CArchive object,
127 //close it. Although the CArchive (and CFile) objects will automatically close
128 //the archive (and file), it is good practice to explicitly do so since it makes
129 //recovery from errors easier. For more information about error handling, see the
130 //article Exceptions: Catching and Deleting Exceptions.
131 try
132 {
133 newoCompound.Serialize(archiveRead);
134 }
135 catch( CException* e )
136 {
137 // Handle the exception here.
138 // "e" contains information about the exception.
139 e->Delete();
140 }
141 cout<<newoCompound.m_pOther->c<<endl;
142 archiveWrite.Close();
143 archiveRead.Close();
144 theFile.Close();
145 }