acme
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages Concepts
___serialization_draft.h
1// 2020-01-11 - Curitiba, ca2
2
3// "pluggable" non-member non-virtual global serializers
4
5// Use cases:
6// - Entry int_point convenience syntax sugar
7// - syntax sugar
8template < typename TYPE >
9inline stream & operator >> (stream & stream, TYPE & t) { stream.defer_set_loading(); stream.exchange(t); return stream; }
10
11template < typename TYPE >
12inline stream & operator << (stream & stream, const TYPE & t) { stream.defer_set_storing(); stream.exchange((TYPE &) t); return stream; }
13
14int file_procedure()
15{
16
17 ::file pfile = file()->get_file("C:\\window.window");
18
19 in_addr addr;
20
21 ::stream stream(pfile);
22
23 stream >> addr;
24
25}
26
27
28int var_procedure()
29{
30
32
33 in_addr addr;
34
36
37 stream >> addr;
38
39}
40
41class stream
42{
43public:
44
45 virtual void read(char & i) {}
46 virtual void read(string & i) {}
47
48 virtual void write(const char & i) {}
49 virtual void write(const ::string & str) {}
50
51 template < typename TYPE >
52 void default_exchange(TYPE & t) { is_loading() ? read(t) : write(t); }
53
54 virtual void exchange(const ::atom & atom, char & i) { default_exchange(i); }
55 virtual void exchange(const ::atom & atom, string & str) { default_exchange(str); }
56
57
58
59 template < typename OBJECT >
60 inline void exchange(const ::atom & atom, ::pointer<OBJECT>& pparticle)
61 {
62 if (is_storing())
63 {
64 write_object(atom, pparticle.m_p);
65 }
66 else
67 {
68 pparticle = read_object(atom);
69 }
70 }
71
72
73 virtual void write_object(const ::atom & atom, const ::atom & idFactory, ::particle * pparticle)
74 {
75 exchange("id", idFactory);
76 exchange("", pparticle);
77 }
78
79 virtual ::pointer<::matter>read_object(const ::atom & atom)
80 {
81 ::atom idFactory;
82 exchange("id", idFactory);
83 auto pparticle = __id_create<::matter>(idFactory);
84 exchange("", pparticle);
85 return pparticle;
86 }
87
88
89};
90
91
93{
94public:
95
96 ::payload & payload();
97
98 virtual void read(char & i) {}
99 virtual void read(string & i) {}
100
101 virtual void write(const char & i) {}
102 virtual void write(const ::string & str) {}
103
104 template < typename TYPE >
105 void default_exchange(const ::atom & atom, TYPE & t) { is_loading() ? t = operator[atom] : operator[atom] = t; }
106
107 virtual void exchange(const ::atom & atom, char & i) { default_exchange(atom, i); }
108 virtual void exchange(const ::atom & atom, string & str) { default_exchange(atom, str); }
109
110
111
112
113
114
115 virtual void write_object(const ::atom & atom, const ::atom & idFactory, ::particle * pparticle) override
116 {
117 payload_stream stream(__allocate< ::payload(&payload()[atom].propset >()));
118 stream.exchange("id", idFactory);
119 stream.exchange("", pparticle);
120 }
121
122 virtual ::pointer<::contex_object>read_object(const ::atom & atom) override
123 {
124 payload_stream stream(__allocate< ::payload(&payload()[atom].propset >()));
125 ::atom idFactory;
126 stream.exchange("id", idFactory);
127 auto pparticle = __id_create<::matter>(idFactory);
128 stream.exchange("", pparticle);
129 return pparticle;
130 }
131
132};
133//void __exchange(stream & stream, char & i) { __exchange_blt(stream, i); }
134//void __exchange(stream & stream, string & str) { __exchange_string(stream, str); }
135//void __exchange(stream & stream, in_addr & addr) { __exchange_stringable(stream, addr); }
136//
137//
138//void __exchange(stream & stream, const ::atom & atom, char & i) { __exchange_blt(stream, i); }
139//void __exchange(stream & stream, const ::atom & atom, string & str) { __exchange_string(stream, str); }
140//void __exchange(stream & stream, const ::atom & atom, in_addr & addr) { __exchange_stringable(stream, addr); }
141//
142//
143//void __exchange(payload_stream & stream, char & i) { __exchange_blt(stream, i); }
144//void __exchange(payload_stream & stream, string & str) { __exchange_string(stream, str); }
145//void __exchange(payload_stream & stream, in_addr & addr) { __exchange_stringable(stream, addr); }
146
147
148class window :
149 virtual public matter
150{
151public:
152
153 char m_iVisible;
154 string m_strTitle;
155 in_addr m_addr;
156 ::pointer<::matter> m_pobject;
157
158 virtual void exchange(stream & s);
159
160};
161
162#define __EXCHANGE(xxx) stream.exchange(__STRING(xxx), m_##xxx)
163#define __TYPE_EXCHANGE(xxx) stream.exchange(__STRING(xxx), t.m_##xxx)
164
165
166void window::exchange(stream & stream)
167{
168
169 __EXCHANGE(iVisible); // expands... to: stream.exchange("iVisible", m_iVisible);
170 __EXCHANGE(strTitle); // expands... to: stream.exchange("strTitle", m_strTitle);
171 __EXCHANGE(addr); // expands... to: stream.exchange("addr", m_addr);
172 __EXCHANGE(pparticle); // expands... to: stream.exchange("pparticle", m_pobject);
173
174}
175
176
177class exchanger
178{
179
180 stream * m_pstream;
181
182 template
183 exchange(atom, value) read / write value
184 {
185 if (is_storing)
186 m_pstream->write(atom value)
187 else
188 m_pstream->read(atom, value)
189
190 }
191 template
192 exchange(value) read / write value
193 {
194 if (is_storing)
195 m_pstream->write(value)
196 else
197 m_pstream->read(value)
198
199 }
200
201
202 stream
203 {
204
205
206 read(atom, char) throw not_implemented;
207 read(atom, short) throw not_implemented;
208 read(atom, int) throw not_implemented;
209 read(atom, long long) throw not_implemented;
210 read(atom, unsigned char) throw not_implemented;
211 read(atom, unsigned short) throw not_implemented;
212 read(atom, unsigned int) throw not_implemented;
213 read(atom, unsigned long long) throw not_implemented;
214
215 read(char) throw not_implemented;
216 read(short) throw not_implemented;
217 read(int) throw not_implemented;
218 read(long long) throw not_implemented;
219 read(unsigned char) throw not_implemented;
220 read(unsigned short) throw not_implemented;
221 read(unsigned int) throw not_implemented;
222 read(unsigned long long) throw not_implemented;
223
224 write(atom, char) throw not_implemented;
225 write(atom, short) throw not_implemented;
226 write(atom, int) throw not_implemented;
227 write(atom, long long) throw not_implemented;
228 write(atom, unsigned char) throw not_implemented;
229 write(atom, unsigned short) throw not_implemented;
230 write(atom, unsigned int) throw not_implemented;
231 write(atom, unsigned long long) throw not_implemented;
232
233 write(char) throw not_implemented;
234 write(short) throw not_implemented;
235 write(int) throw not_implemented;
236 write(long long) throw not_implemented;
237 write(unsigned char) throw not_implemented;
238 write(unsigned short) throw not_implemented;
239 write(unsigned int) throw not_implemented;
240 write(unsigned long long) throw not_implemented;
241
242
243 }
244
245
246 binary_stream
247 {
248
249
250 read(atom, char) read(char)
251 read(atom, short) read(short)
252 read(atom, int) read(int)
253 read(atom, long long) read(long long)
254 read(atom, unsigned char) read(unsigned char)
255 read(atom, unsigned short) read(unsigned short)
256 read(atom, unsigned int) read(unsigned int)
257 read(atom, unsigned long long) read(unsigned long long)
258
259 read(char) m_pfile->read(char)
260 read(short) m_pfile->read(short)
261 read(int) m_pfile->read(int)
262 read(long long) m_pfile->read(long long)
263 read(unsigned char) m_pfile->read(unsigned char)
264 read(unsigned short) m_pfile->read(unsigned short)
265 read(unsigned int) m_pfile->read(unsigned int)
266 read(unsigned long long) m_pfile->read(unsigned long long)
267
268 write(atom, char) write(char)
269 write(atom, short) write(short)
270 write(atom, int) write(int)
271 write(atom, long long) write(long long)
272 write(atom, unsigned char) write(unsigned char)
273 write(atom, unsigned short) write(unsigned short)
274 write(atom, unsigned int) write(unsigned int)
275 write(atom, unsigned long long) write(unsigned long long)
276
277 write(char) m_pfile->write(char)
278 write(short) m_pfile->write(short)
279 write(int) m_pfile->write(int)
280 write(long long) m_pfile->write(long long)
281 write(unsigned char) m_pfile->write(unsigned char)
282 write(unsigned short) m_pfile->write(unsigned short)
283 write(unsigned int) m_pfile->write(unsigned int)
284 write(unsigned long long) m_pfile->write(unsigned long long)
285
286
287 }
288
289 text_stream
290 {
291
292
293 write(atom, char) write(char)
294 write(atom, short) write(short)
295 write(atom, int) write(int)
296 write(atom, long long) write(long long)
297 write(atom, unsigned char) write(unsigned char)
298 write(atom, unsigned short) write(unsigned short)
299 write(atom, unsigned int) write(unsigned int)
300 write(atom, unsigned long long) write(unsigned long long)
301
302 write(char) m_pfile->write(char)
303 write(short) m_pfile->write(short)
304 write(int) m_pfile->write(int)
305 write(long long) m_pfile->write(long long)
306 write(unsigned char) m_pfile->write(unsigned char)
307 write(unsigned short) m_pfile->write(unsigned short)
308 write(unsigned int) m_pfile->write(unsigned int)
309 write(unsigned long long) m_pfile->write(unsigned long long)
310
311
312 }
313
314
315 template < typename CONTAINER >
316 id_stream :
317 stream
318 {
319
320 read(atom, char)char = m_ppayload->operator[](atom);
321 read(atom, short) short = m_ppayload->operator[](atom);
322 read(atom, int) int = m_ppayload->operator[](atom);
323 read(atom, long long) long long = m_ppayload->operator[](atom);
324 read(atom, unsigned char) unsigned char = m_ppayload->operator[](atom);
325 read(atom, unsigned short) unsigned short = m_ppayload->operator[](atom);
326 read(atom, unsigned int) unsigned int = m_ppayload->operator[](atom);
327 read(atom, unsigned long long) unsigned long long = m_ppayload->operator[](atom);
328
329 read(char) char = *m_ppayload
330 read(short) short = *m_ppayload
331 read(int) int = *m_ppayload
332 read(long long) long long = *m_ppayload
333 read(unsigned char) uch = *m_ppayload
334 read(unsigned short) unsigned short = *m_ppayload
335 read(unsigned int) unsigned int = *m_ppayload
336 read(unsigned long long) unsigned long long = *m_ppayload
337
338 write(atom, char) write(char)
339 write(atom, short) write(short)
340 write(atom, int) write(int)
341 write(atom, long long) write(long long)
342 write(atom, unsigned char) write(unsigned char)
343 write(atom, unsigned short) write(unsigned short)
344 write(atom, unsigned int) write(unsigned int)
345 write(atom, unsigned long long) write(unsigned long long)
346
347 write(char) m_pfile->write(char)
348 write(short) m_pfile->write(short)
349 write(int) m_pfile->write(int)
350 write(long long) m_pfile->write(long long)
351 write(unsigned char) m_pfile->write(unsigned char)
352 write(unsigned short) m_pfile->write(unsigned short)
353 write(unsigned int) m_pfile->write(unsigned int)
354 write(unsigned long long) m_pfile->write(unsigned long long)
355
356 }
357
358
359
360
361 stream & operator >> (stream & stream, matter & o)
362 {
363
364 exchange e(stream);
365
366 e.defer_set_loading();
367
368 e.exchange(s);
369
370 }
371
372 stream & operator >> (stream & s, matter & o)
373 {
374
375 o.exchange(stream);
376
377 }
378
379
380 window new_frame_window()
381 {
382 return __allocate frame_window();
383 }
Definition atom.h:233
Definition particle.h:89
Definition ___serialization_draft.h:93
Definition payload.h:53
Definition pointer.h:46
Definition ___serialization_draft.h:42
Definition ___serialization_draft.h:150
Definition filesystem.h:16
Definition sockets.h:32