comparison orange/serialization/Serializer.d @ 3:3c155e4c3d56

Fixed structs
author Jacob Carlborg <doob@me.com>
date Mon, 31 May 2010 18:51:56 +0200
parents ea37a9470e3e
children 470ab5270d0c
comparison
equal deleted inserted replaced
2:ea37a9470e3e 3:3c155e4c3d56
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) (T value) 426 private void objectStructSerializeHelper (T : Object) (T value)
427 { 427 {
428 const nonSerializedFields = collectAnnotations!(nonSerializedField)(value); 428 const nonSerializedFields = collectAnnotations!(nonSerializedField)(value);
429 429
430 foreach (i, dummy ; typeof(T.tupleof)) 430 foreach (i, dummy ; typeof(T.tupleof))
431 { 431 {
437 Type v = value.tupleof[i]; 437 Type v = value.tupleof[i];
438 serialize(v, toDataType(field)); 438 serialize(v, toDataType(field));
439 } 439 }
440 } 440 }
441 441
442 static if (is(T : Object) && !is(T == Object)) 442 serializeBaseTypes(value);
443 serializeBaseTypes(value); 443 }
444 } 444
445 445 private void objectStructSerializeHelper (T) (ref T value)
446 private void objectStructDeserializeHelper (T) (T value) 446 {
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.`));
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)
447 { 464 {
448 const nonSerializedFields = collectAnnotations!(nonSerializedField)(value); 465 const nonSerializedFields = collectAnnotations!(nonSerializedField)(value);
449 466
450 foreach (i, dummy ; typeof(T.tupleof)) 467 foreach (i, dummy ; typeof(T.tupleof))
451 { 468 {
452 const field = nameOfFieldAt!(T, i); 469 const field = nameOfFieldAt!(T, i);
453 470
454 static if (!internalFields.ctfeContains(field) && !nonSerializedFields.ctfeContains(field)) 471 static if (!internalFields.ctfeContains(field) && !nonSerializedFields.ctfeContains(field))
455 { 472 {
456 alias TypeOfField!(T, field) Type; 473 alias TypeOfField!(T, field) Type;
457 auto fieldValue = deserializeInternal!(Type)(toDataType(field)); 474 auto fieldValue = deserializeInternal!(Type)(toDataType(field));
458 value.tupleof[i] = fieldValue; 475 value.tupleof[i] = fieldValue;
459 } 476 }
460 } 477 }
461 478
462 static if (is(T : Object) && !is(T == Object)) 479 deserializeBaseTypes(value);
463 deserializeBaseTypes(value); 480 }
481
482 private void objectStructDeserializeHelper (T) (ref T value)
483 {
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.`));
485 const nonSerializedFields = collectAnnotations!(nonSerializedField)(value);
486
487 foreach (i, dummy ; typeof(T.tupleof))
488 {
489 const field = nameOfFieldAt!(T, i);
490
491 static if (!internalFields.ctfeContains(field) && !nonSerializedFields.ctfeContains(field))
492 {
493 alias TypeOfField!(T, field) Type;
494 auto fieldValue = deserializeInternal!(Type)(toDataType(field));
495 value.tupleof[i] = fieldValue;
496 }
497 }
464 } 498 }
465 499
466 private void serializeBaseTypes (T : Object) (T value) 500 private void serializeBaseTypes (T : Object) (T value)
467 { 501 {
468 alias BaseTypeTupleOf!(T)[0] Base; 502 alias BaseTypeTupleOf!(T)[0] Base;
552 keyCounter = 0; 586 keyCounter = 0;
553 } 587 }
554 588
555 private void triggerEvent (string name, T) (T value) 589 private void triggerEvent (string name, T) (T value)
556 { 590 {
557 static assert (is(T == class) || is(T == struct), format!(`The given value of the type "`, T, `" is not a valid type, the only valid types for this method are classes and structs.`)); 591 static assert (isObject!(T) || isStruct!(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.`));
558 592
559 foreach (i, dummy ; typeof(T.tupleof)) 593 foreach (i, dummy ; typeof(T.tupleof))
560 { 594 {
561 const field = nameOfFieldAt!(T, i); 595 const field = nameOfFieldAt!(T, i);
562 596
586 triggerEvent!(onDeserializedField)(value); 620 triggerEvent!(onDeserializedField)(value);
587 } 621 }
588 622
589 private static string[] collectAnnotations (string name, T) (T value) 623 private static string[] collectAnnotations (string name, T) (T value)
590 { 624 {
591 static assert (is(T == class) || is(T == struct), format!(`The given value of the type "`, T, `" is not a valid type, the only valid types for this method are classes and structs.`)); 625 static assert (isObject!(T) || isStruct!(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.`));
592 626
593 string[] annotations; 627 string[] annotations;
594 628
595 foreach (i, dummy ; typeof(T.tupleof)) 629 foreach (i, dummy ; typeof(T.tupleof))
596 { 630 {