Mercurial > projects > orange
comparison orange/serialization/Serializer.d @ 39:301476d40518
Made a couple of refactorings:
* Removed Serializable
* Added a Serializable interface
* Moved isSerializable and updated the implementation
* Added new deserialize methods for deserialize without data during deserializing
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Thu, 04 Aug 2011 21:29:56 +0200 |
parents | 511d1ef4e299 |
children | 02dbd18b7fe9 |
comparison
equal
deleted
inserted
replaced
38:9443bcddc699 | 39:301476d40518 |
---|---|
56 Id id; | 56 Id id; |
57 string key; | 57 string key; |
58 } | 58 } |
59 | 59 |
60 ErrorCallback errorCallback_; | 60 ErrorCallback errorCallback_; |
61 Archive archive; | 61 Archive archive_; |
62 | 62 |
63 size_t keyCounter; | 63 size_t keyCounter; |
64 Id idCounter; | 64 Id idCounter; |
65 | 65 |
66 RegisterBase[string] serializers; | 66 RegisterBase[string] serializers; |
85 void delegate (ArchiveException exception, string[] data) doNothingOnErrorCallback; | 85 void delegate (ArchiveException exception, string[] data) doNothingOnErrorCallback; |
86 } | 86 } |
87 | 87 |
88 this (Archive archive) | 88 this (Archive archive) |
89 { | 89 { |
90 this.archive = archive; | 90 this.archive_ = archive; |
91 | 91 |
92 throwOnErrorCallback = (ArchiveException exception, string[] data) { throw exception; }; | 92 throwOnErrorCallback = (ArchiveException exception, string[] data) { throw exception; }; |
93 doNothingOnErrorCallback = (ArchiveException exception, string[] data) { /* do nothing */ }; | 93 doNothingOnErrorCallback = (ArchiveException exception, string[] data) { /* do nothing */ }; |
94 | 94 |
95 setThrowOnErrorCallback(); | 95 setThrowOnErrorCallback(); |
96 } | |
97 | |
98 Archive archive () | |
99 { | |
100 return archive_; | |
96 } | 101 } |
97 | 102 |
98 ErrorCallback errorCallback () | 103 ErrorCallback errorCallback () |
99 { | 104 { |
100 return errorCallback_; | 105 return errorCallback_; |
218 { | 223 { |
219 auto wrapper = getSerializerWrapper!(T)(runtimeType); | 224 auto wrapper = getSerializerWrapper!(T)(runtimeType); |
220 wrapper(value, this, key); | 225 wrapper(value, this, key); |
221 } | 226 } |
222 | 227 |
223 else static if (isSerializable!(T, Serializer)) | 228 else static if (isSerializable!(T)) |
224 value.toData(this, key); | 229 value.toData(this, key); |
225 | 230 |
226 else | 231 else |
227 { | 232 { |
228 if (isBaseClass(value)) | 233 if (isBaseClass(value)) |
325 { | 330 { |
326 auto wrapper = getSerializerWrapper!(T)(key); | 331 auto wrapper = getSerializerWrapper!(T)(key); |
327 wrapper(value, this, key); | 332 wrapper(value, this, key); |
328 } | 333 } |
329 | 334 |
330 else static if (isSerializable!(T, Serializer)) | 335 else static if (isSerializable!(T)) |
331 value.toData(this, key); | 336 value.toData(this, key); |
332 | 337 |
333 else | 338 else |
334 { | 339 { |
335 static if (isVoid!(BaseTypeOfPointer!(T))) | 340 static if (isVoid!(BaseTypeOfPointer!(T))) |
362 archive.archiveTypedef(T.stringof, key, nextId, { | 367 archive.archiveTypedef(T.stringof, key, nextId, { |
363 serializeInternal!(BaseTypeOfTypedef!(T))(value, nextKey); | 368 serializeInternal!(BaseTypeOfTypedef!(T))(value, nextKey); |
364 }); | 369 }); |
365 } | 370 } |
366 | 371 |
367 T deserialize (T) (Data data, string key = null) | 372 T deserialize (T) (Data data, string key = "") |
368 { | 373 { |
369 if (hasBegunSerializing && !hasBegunDeserializing) | 374 if (hasBegunSerializing && !hasBegunDeserializing) |
370 resetCounters(); | 375 resetCounters(); |
371 | 376 |
372 if (!hasBegunDeserializing) | 377 if (!hasBegunDeserializing) |
373 hasBegunDeserializing = true; | 378 hasBegunDeserializing = true; |
374 | 379 |
375 if (!key) | 380 if (key.empty()) |
376 key = nextKey; | 381 key = nextKey; |
377 | 382 |
378 archive.beginUnarchiving(data); | 383 archive.beginUnarchiving(data); |
379 auto value = deserializeInternal!(T)(key); | 384 auto value = deserializeInternal!(T)(key); |
380 deserializingPostProcess; | 385 deserializingPostProcess; |
381 | 386 |
382 return value; | 387 return value; |
388 } | |
389 | |
390 T deserialize (T) (string key) | |
391 { | |
392 if (!hasBegunDeserializing) | |
393 throw new SerializationException("Cannot deserialize without any data, this method should only be called after deserialization has begun", __FILE__, __LINE__); | |
394 | |
395 return deserialize!(T)(archive.untypedData, key); | |
396 } | |
397 | |
398 T deserialize (T) () | |
399 { | |
400 return deserialize!(T)(""); | |
383 } | 401 } |
384 | 402 |
385 private T deserializeInternal (T, U) (U keyOrId) | 403 private T deserializeInternal (T, U) (U keyOrId) |
386 { | 404 { |
387 static if (isTypedef!(T)) | 405 static if (isTypedef!(T)) |
443 { | 461 { |
444 auto wrapper = getDeserializerWrapper!(T)(runtimeType); | 462 auto wrapper = getDeserializerWrapper!(T)(runtimeType); |
445 wrapper(value, this, keyOrId); | 463 wrapper(value, this, keyOrId); |
446 } | 464 } |
447 | 465 |
448 else static if (isSerializable!(T, Serializer)) | 466 else static if (isSerializable!(T)) |
449 value.fromData(this, keyOrId); | 467 value.fromData(this, keyOrId); |
450 | 468 |
451 else | 469 else |
452 { | 470 { |
453 if (isBaseClass(value)) | 471 if (isBaseClass(value)) |