1 #include<stdio.h>
2 #include<iostream>
3 #include<fcntl.h>
4 #include<sys/types.h>
5 #include<sys/stat.h>
6 #include<vector>
7 #include <unistd.h>
8 using namespace std;
9
10 class A
11 {
12 private:
13 int i;
14 public:
15 A(){
16 i = 0;
17 }
18 explicit A(int j){
19 i = j;
20 }
21 virtual ~A(){
22
23 }
24 public:
25 void f(){
26 cout<<"in f() "<<i<<endl;
27 }
28 public:
29 bool Serialize(const char* pFilePath){
30 int fd = open(pFilePath,O_RDWR | O_CREAT | O_TRUNC, 0);
31 cout << "begin Serialize"<< endl;
32 if(fd == -1){
33 cout << "Serialize open error" << endl;
34 return false;
35 }
36 if (write(fd,&i,sizeof(int)) == -1)
37 {
38 cout << "Serialize write error" << endl;
39 close(fd);
40 return false;
41 }
42 cout << "Serialize finish" << endl;
43 return true;
44 }
45 bool Deserialize(const char* pFilePath){
46 int fd = open(pFilePath,O_RDWR);
47 cout << "Deserialize begin" << endl;
48 if (fd == -1)
49 {
50 cout <<"Deserialize open error"<<endl;
51 return false;
52 }
53 int r = read(fd,&i,sizeof(int));
54 if (r == -1)
55 {
56 cout << "Deserialize read error";
57 close(fd);
58 return false;
59 }
60 if (close(fd) == -1){
61 cout << "Deserialize close error" << endl;
62 return false;
63 }
64 cout << "Deserialize finish" << endl;
65 return true;
66 }
67 bool Serialize(int fd) const
68 {
69 if(-1==fd)
70 return false;
71
72 if(write(fd,&i,sizeof(int))==-1)
73 return false;
74
75 return true;
76 }
77 bool Deserialize(int fd)
78 {
79 if(-1==fd)
80 return false;
81
82 int r=read(fd,&i,sizeof(int));
83 if((0==r)||(-1==r))
84 return false;
85
86 return true;
87 }
88 };
89 class B
90 {
91 public:
92 B()
93 {
94 i=0;
95 j=0;
96 }
97 explicit B(int k)
98 {
99 i=k;
100 j=k+1;
101 }
102 virtual ~B()
103 {
104 }
105 public:
106 void f()
107 {
108 cout<<"in f() "<<i<<" "<<j<<endl;
109 }
110 public:
111 bool Serialize(int fd)
112 {
113 if(-1==fd)
114 return false;
115
116 if(write(fd,&i,sizeof(int))==-1)
117 return false;
118
119 if(write(fd,&j,sizeof(int))==-1)
120 return false;
121
122 return true;
123 }
124 bool Deserialize(int fd)
125 {
126 if(-1==fd)
127 return false;
128
129 int r=read(fd,&i,sizeof(int));
130 if((0==r)||(-1==r))
131 return false;
132
133 r=read(fd,&j,sizeof(int));
134 if((0==r)||(-1==r))
135 return false;
136
137 return true;
138 }
139 private:
140 int i;
141 int j;
142 };
143 struct Serialized
144 {
145 int nType;
146 void *pObj;
147 };
148
149 class Serializer
150 {
151 public:
152 bool Serialize(const char *pFilePath, std::vector<Serialized>& v)
153 {
154 int fd = open(pFilePath,O_RDWR | O_CREAT | O_TRUNC, 0);
155 if(-1==fd)
156 return false;
157
158 for(int i=0;i<v.size();i++)
159 {
160 if(write(fd,&(v[i].nType),0)==-1)
161 {
162 close(fd);
163 return false;
164 }
165 if(0==v[i].nType)
166 {
167 A *p=(A *)(v[i].pObj);
168
169 if(p->Serialize(fd)==false)
170 return false;
171 }
172 else if(1==v[i].nType)
173 {
174 B *p=(B *)(v[i].pObj);
175
176 if(p->Serialize(fd)==false)
177 return false;
178 }
179
180 }
181 if(close(fd)==-1)
182 return false;
183
184 return true;
185 }
186 bool Deserialize(const char *pFilePath, std::vector<Serialized>& v)
187 {
188 int fd=open(pFilePath,O_RDWR);
189 if(-1==fd)
190 return false;
191 for(;;)
192 {
193 int nType;
194 int r=read(fd,&nType,0);
195 if((-1==r)||(0==r))
196 break;
197
198 if(0==nType)
199 {
200 A *p;
201 p=new A();
202 p->Deserialize(fd);
203
204 Serialized s;
205 s.nType =nType;
206 s.pObj=p;
207
208 v.push_back(s);
209 }
210 else if(1==nType)
211 {
212 B *p;
213 p=new B();
214 p->Deserialize(fd);
215
216 Serialized s;
217 s.nType =nType;
218 s.pObj=p;
219
220 v.push_back(s);
221 }
222 }
223 if(close(fd)==-1)
224 return false;
225
226 return true;
227 }
228
229 };
230 int main()
231 {
232 A a1(2);
233 Serialized s1;
234 s1.nType=0;
235 s1.pObj=&a1;
236
237 B b1(3);
238 Serialized s2;
239 s2.nType=1;
240 s2.pObj=&b1;
241
242 B b2(4);
243 Serialized s3;
244 s3.nType=1;
245 s3.pObj=&b2;
246
247 A a2(5);
248 Serialized s4;
249 s4.nType=0;
250 s4.pObj=&a2;
251
252 std::vector<Serialized>v;
253 v.push_back(s1);
254 v.push_back(s2);
255 v.push_back(s3);
256 v.push_back(s4);
257
258 Serializer s;
259 s.Serialize("data2.txt",v);
260
261 s.Deserialize("a2.txt",v);
262 for(int i=0;i<v.size();i++)
263 {
264 if(v[i].nType==0)
265 {
266 A *p=(A *)(v[i].pObj);
267 p->f();
268 }
269 else if(v[i].nType==1)
270 {
271 B *p=(B *)(v[i].pObj);
272 p->f();
273 }
274 }
275 return 0;
276 }277 原文链接:https://blog.csdn.net/weixin_43234093/article/details/116667871