Mercurial > projects > orange
comparison orange/serialization/Serializer.d @ 4:470ab5270d0c
Simplified the implementation of RegisterWrapper. Fixed: the base class of an object was not serialized
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Mon, 31 May 2010 19:20:14 +0200 |
parents | 3c155e4c3d56 |
children | 613a0bb20207 |
comparison
equal
deleted
inserted
replaced
3:3c155e4c3d56 | 4:470ab5270d0c |
---|---|
421 return archive.unarchive!(T)(key, (T value) { | 421 return archive.unarchive!(T)(key, (T value) { |
422 return deserializeInternal!(BaseTypeOfTypeDef!(T))(key); | 422 return deserializeInternal!(BaseTypeOfTypeDef!(T))(key); |
423 }); | 423 }); |
424 } | 424 } |
425 | 425 |
426 private void objectStructSerializeHelper (T : Object) (T value) | 426 private void objectStructSerializeHelper (T) (ref T value) |
427 { | 427 { |
428 static assert(isStruct!(T) || isObject!(T), format!(`The given value of the type "`, T, `" is not a valid type, the only valid types for this method are objects and structs.`)); | |
428 const nonSerializedFields = collectAnnotations!(nonSerializedField)(value); | 429 const nonSerializedFields = collectAnnotations!(nonSerializedField)(value); |
429 | 430 |
430 foreach (i, dummy ; typeof(T.tupleof)) | 431 foreach (i, dummy ; typeof(T.tupleof)) |
431 { | 432 { |
432 const field = nameOfFieldAt!(T, i); | 433 const field = nameOfFieldAt!(T, i); |
437 Type v = value.tupleof[i]; | 438 Type v = value.tupleof[i]; |
438 serialize(v, toDataType(field)); | 439 serialize(v, toDataType(field)); |
439 } | 440 } |
440 } | 441 } |
441 | 442 |
442 serializeBaseTypes(value); | 443 static if (isObject!(T) && !is(T == Object)) |
443 } | 444 serializeBaseTypes(value); |
444 | 445 } |
445 private void objectStructSerializeHelper (T) (ref T value) | 446 |
446 { | 447 private void objectStructDeserializeHelper (T) (ref T value) |
448 { | |
447 static assert(isStruct!(T) || isObject!(T), format!(`The given value of the type "`, T, `" is not a valid type, the only valid types for this method are objects and structs.`)); | 449 static assert(isStruct!(T) || isObject!(T), format!(`The given value of the type "`, T, `" is not a valid type, the only valid types for this method are objects and structs.`)); |
448 const nonSerializedFields = collectAnnotations!(nonSerializedField)(value); | |
449 | |
450 foreach (i, dummy ; typeof(T.tupleof)) | |
451 { | |
452 const field = nameOfFieldAt!(T, i); | |
453 | |
454 static if (!internalFields.ctfeContains(field) && !nonSerializedFields.ctfeContains(field)) | |
455 { | |
456 alias typeof(T.tupleof[i]) Type; | |
457 Type v = value.tupleof[i]; | |
458 serialize(v, toDataType(field)); | |
459 } | |
460 } | |
461 } | |
462 | |
463 private void objectStructDeserializeHelper (T : Object) (T value) | |
464 { | |
465 const nonSerializedFields = collectAnnotations!(nonSerializedField)(value); | 450 const nonSerializedFields = collectAnnotations!(nonSerializedField)(value); |
466 | 451 |
467 foreach (i, dummy ; typeof(T.tupleof)) | 452 foreach (i, dummy ; typeof(T.tupleof)) |
468 { | 453 { |
469 const field = nameOfFieldAt!(T, i); | 454 const field = nameOfFieldAt!(T, i); |
474 auto fieldValue = deserializeInternal!(Type)(toDataType(field)); | 459 auto fieldValue = deserializeInternal!(Type)(toDataType(field)); |
475 value.tupleof[i] = fieldValue; | 460 value.tupleof[i] = fieldValue; |
476 } | 461 } |
477 } | 462 } |
478 | 463 |
479 deserializeBaseTypes(value); | 464 static if (isObject!(T) && !is(T == Object)) |
480 } | 465 deserializeBaseTypes(value); |
481 | 466 } |
482 private void objectStructDeserializeHelper (T) (ref T value) | 467 |
483 { | 468 private void serializeBaseTypes (T : Object) (T value) |
484 static assert(isStruct!(T) || isObject!(T), format!(`The given value of the type "`, T, `" is not a valid type, the only valid types for this method are objects and structs.`)); | 469 { |
485 const nonSerializedFields = collectAnnotations!(nonSerializedField)(value); | 470 alias BaseTypeTupleOf!(T)[0] Base; |
486 | 471 |
487 foreach (i, dummy ; typeof(T.tupleof)) | 472 static if (!is(Base == Object)) |
488 { | 473 { |
489 const field = nameOfFieldAt!(T, i); | 474 archive.archiveBaseClass!(Base)(nextKey); |
490 | 475 Base base = value; |
491 static if (!internalFields.ctfeContains(field) && !nonSerializedFields.ctfeContains(field)) | 476 objectStructSerializeHelper(base); |
492 { | |
493 alias TypeOfField!(T, field) Type; | |
494 auto fieldValue = deserializeInternal!(Type)(toDataType(field)); | |
495 value.tupleof[i] = fieldValue; | |
496 } | |
497 } | 477 } |
498 } | 478 } |
499 | 479 |
500 private void serializeBaseTypes (T : Object) (T value) | 480 private void deserializeBaseTypes (T : Object) (T value) |
501 { | 481 { |
502 alias BaseTypeTupleOf!(T)[0] Base; | 482 alias BaseTypeTupleOf!(T)[0] Base; |
503 | 483 |
504 static if (!is(Base == Object)) | 484 static if (!is(Base == Object)) |
505 { | 485 { |
506 archive.archiveBaseClass!(Base)(nextKey); | |
507 objectStructSerializeHelper!(Base)(value); | |
508 } | |
509 } | |
510 | |
511 private void deserializeBaseTypes (T : Object) (T value) | |
512 { | |
513 alias BaseTypeTupleOf!(T)[0] Base; | |
514 | |
515 static if (!is(Base == Object)) | |
516 { | |
517 archive.unarchiveBaseClass!(Base)(nextKey); | 486 archive.unarchiveBaseClass!(Base)(nextKey); |
518 objectStructDeserializeHelper!(Base)(value); | 487 Base base = value; |
488 objectStructDeserializeHelper(base); | |
519 } | 489 } |
520 } | 490 } |
521 | 491 |
522 private SerializeRegisterWrapper!(T, ArchiveType) getSerializerWrapper (T) (string type) | 492 private SerializeRegisterWrapper!(T, ArchiveType) getSerializerWrapper (T) (string type) |
523 { | 493 { |