comparison orange/serialization/archives/XMLArchive.d @ 33:4fea56a5849f experimental

Now both internal and external pointers work.
author Jacob Carlborg <doob@me.com>
date Sun, 31 Jul 2011 17:56:44 +0200
parents c68d29967c9f
children 068e853b9c07
comparison
equal deleted inserted replaced
32:9df3b7a46a51 33:4fea56a5849f
43 static const Data valueTag = "value"; 43 static const Data valueTag = "value";
44 } 44 }
45 45
46 private struct Attributes 46 private struct Attributes
47 { 47 {
48 static const Data invalidAttribute = "\0";
48 static const Data typeAttribute = "type"; 49 static const Data typeAttribute = "type";
49 static const Data versionAttribute = "version"; 50 static const Data versionAttribute = "version";
50 static const Data lengthAttribute = "length"; 51 static const Data lengthAttribute = "length";
51 static const Data keyAttribute = "key"; 52 static const Data keyAttribute = "key";
52 static const Data runtimeTypeAttribute = "runtimeType"; 53 static const Data runtimeTypeAttribute = "runtimeType";
319 addArchivedPointer(id, parent, lastElement, key); 320 addArchivedPointer(id, parent, lastElement, key);
320 dg(); 321 dg();
321 }; 322 };
322 } 323 }
323 324
324 void archivePointer (Id pointerId, Id pointeeId) 325 void archivePointer (Id pointeeId, string key, Id id)
325 { 326 {
326 if (auto pointerNode = getArchivedPointer(pointerId)) 327 if (auto pointerNode = getArchivedPointer(id))
327 { 328 {
328 pointerNode.parent.element(Tags.pointerTag) 329 pointerNode.parent.element(Tags.pointerTag)
329 .attribute(Attributes.keyAttribute, toData(pointerNode.key)) 330 .attribute(Attributes.keyAttribute, toData(pointerNode.key))
330 .attribute(Attributes.idAttribute, toData(pointerId)) 331 .attribute(Attributes.idAttribute, toData(id))
331 .element(Tags.referenceTag, toData(pointeeId)); 332 .element(Tags.referenceTag, toData(pointeeId))
333 .attribute(Attributes.keyAttribute, toData(key));
332 } 334 }
333 } 335 }
334 336
335 void archiveReference (string key, Id id) 337 void archiveReference (string key, Id id)
336 { 338 {
729 result = newInstance(name); 731 result = newInstance(name);
730 dg(); 732 dg();
731 }; 733 };
732 } 734 }
733 735
736 /*
737 * Id unarchiveArray (string key, void delegate (size_t) dg)
738 {
739 return restore!(Id)(lastElement) in {
740 auto element = getElement(Tags.arrayTag, key);
741
742 if (!element.isValid)
743 return Id.max;
744
745 lastElement = element;
746 auto len = getValueOfAttribute(Attributes.lengthAttribute);
747
748 if (!len)
749 return Id.max;
750
751 auto length = fromData!(size_t)(len);
752 auto id = getValueOfAttribute(Attributes.idAttribute);
753
754 if (!id)
755 return Id.max;
756
757 dg(length);
758
759 return id.toId();
760 };
761 }
762 */
763
734 Id unarchivePointer (string key, void delegate () dg) 764 Id unarchivePointer (string key, void delegate () dg)
735 { 765 {
736 return restore!(Id)(lastElement) in { 766 return restore!(Id)(lastElement) in {
737 auto tmp = getElement(Tags.pointerTag, key, Attributes.keyAttribute, false); 767 auto tmp = getElement(Tags.pointerTag, key, Attributes.keyAttribute, false);
738 768
739 if (!tmp.isValid) 769 if (!tmp.isValid)
740 { 770 {
741 lastElement = getElement(Tags.nullTag, key); 771 lastElement = getElement(Tags.nullTag, key);
742 return Id.max; 772 return Id.max;
743 } 773 }
744 774
745 lastElement = tmp; 775 lastElement = tmp;
746 auto stringId = getValueOfAttribute(Attributes.idAttribute); 776 auto id = getValueOfAttribute(Attributes.idAttribute);
747 777
748 if (!stringId) 778 if (!id)
749 return Id.max; 779 return Id.max;
750 780
751 dg(); 781 dg();
752 782
753 return stringId.toId(); 783 return id.toId();
754 }; 784 };
755 } 785 }
756 786
757 Id unarchiveReference (string key) 787 Id unarchiveReference (string key)
758 { 788 {
941 971
942 int unarchiveInt (string key) 972 int unarchiveInt (string key)
943 { 973 {
944 return unarchivePrimitive!(int)(key); 974 return unarchivePrimitive!(int)(key);
945 } 975 }
976
977 int unarchiveInt (Id id)
978 {
979 return unarchivePrimitive!(int)(id);
980 }
946 981
947 // currently not suppported by to!() 982 // currently not suppported by to!()
948 /*ireal unarchiveIreal (string key) 983 /*ireal unarchiveIreal (string key)
949 { 984 {
950 return unarchivePrimitive!(ireal)(key); 985 return unarchivePrimitive!(ireal)(key);
994 wchar unarchiveWchar (string key) 1029 wchar unarchiveWchar (string key)
995 { 1030 {
996 return unarchivePrimitive!(wchar)(key); 1031 return unarchivePrimitive!(wchar)(key);
997 } 1032 }
998 1033
999 T unarchivePrimitive (T) (string key) 1034 T unarchivePrimitive (T, U) (U keyOrId)
1000 { 1035 {
1001 auto element = getElement(toData(T.stringof), key); 1036 auto element = getElement(toData(T.stringof), keyOrId);
1002 1037
1003 if (!element.isValid) 1038 if (!element.isValid)
1004 return T.init; 1039 return T.init;
1005 1040
1006 return fromData!(T)(element.value); 1041 return fromData!(T)(element.value);
1048 errorCallback(new ArchiveException(`Could not continue archiving due to no pointer with the Id "` ~ to!(string)(id) ~ `" was found.`, __FILE__, __LINE__), [to!(string)(id)]); 1083 errorCallback(new ArchiveException(`Could not continue archiving due to no pointer with the Id "` ~ to!(string)(id) ~ `" was found.`, __FILE__, __LINE__), [to!(string)(id)]);
1049 1084
1050 return null; 1085 return null;
1051 } 1086 }
1052 1087
1053 private doc.Node getElement (Data tag, string k, Data attribute = Attributes.keyAttribute, bool throwOnError = true) 1088 private doc.Node getElement (T) (Data tag, T keyOrID, Data attribute = Attributes.invalidAttribute, bool throwOnError = true)
1054 { 1089 {
1055 auto key = toData(k); 1090 if (attribute == Attributes.invalidAttribute)
1091 {
1092 static if (is(T : Id))
1093 attribute = Attributes.idAttribute;
1094
1095 else
1096 attribute = Attributes.keyAttribute;
1097 }
1098
1099 auto key = toData(keyOrID);
1056 1100
1057 auto set = lastElement.query[tag].attribute((doc.Node node) { 1101 auto set = lastElement.query[tag].attribute((doc.Node node) {
1058 if (node.name == attribute && node.value == key) 1102 if (node.name == attribute && node.value == key)
1059 return true; 1103 return true;
1060 1104
1061 return false; 1105 return false;
1062 }); 1106 });
1063 1107
1064 if (set.nodes.length == 1) 1108 if (set.nodes.length == 1)
1065 return set.nodes[0].parent; 1109 return set.nodes[0].parent;
1066 1110
1067 else 1111 else
1068 { 1112 {