changeset 30:9d1a8023bb89 experimental

Added IDs to every serialized value.
author Jacob Carlborg <doob@me.com>
date Sun, 21 Nov 2010 17:12:24 +0100
parents c422ff6477dd
children c68d29967c9f
files orange/_.d orange/serialization/Serializer.d orange/serialization/archives/XMLArchive.d orange/util/collection/_.d
diffstat 4 files changed, 110 insertions(+), 108 deletions(-) [+]
line wrap: on
line diff
--- a/orange/_.d	Sun Nov 21 16:53:46 2010 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,14 +0,0 @@
-/**
- * Copyright: Copyright (c) 2010 Jacob Carlborg.
- * Authors: Jacob Carlborg
- * Version: Initial created: Jan 26, 2010
- * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
- */
-module orange._;
-
-public:
-
-import orange.core._;
-import orange.serialization._;
-import orange.serialization.archives._;
-import orange.util._;
\ No newline at end of file
--- a/orange/serialization/Serializer.d	Sun Nov 21 16:53:46 2010 +0100
+++ b/orange/serialization/Serializer.d	Sun Nov 21 17:12:24 2010 +0100
@@ -138,33 +138,36 @@
 		return archive.untypedData;
 	}
 	
-	private void serializeInternal (T) (T value, string key = null)
+	private void serializeInternal (T) (T value, string key = null, Id id = Id.max)
 	{
 		if (!key)
 			key = nextKey;
 		
+		if (id == Id.max)
+			id = nextId;
+		
 		archive.beginArchiving();
 		
 		static if ( is(T == typedef) )
-			serializeTypedef(value, key);
+			serializeTypedef(value, key, id);
 		
 		else static if (isObject!(T))
-			serializeObject(value, key);
+			serializeObject(value, key, id);
 
 		else static if (isStruct!(T))
-			serializeStruct(value, key);
+			serializeStruct(value, key, id);
 
 		else static if (isString!(T))
-			serializeString(value, key);
+			serializeString(value, key, id);
 		
 		else static if (isArray!(T))
-			serializeArray(value, key);
+			serializeArray(value, key, id);
 
 		else static if (isAssociativeArray!(T))
-			serializeAssociativeArray(value, key);
+			serializeAssociativeArray(value, key, id);
 
 		else static if (isPrimitive!(T))
-			serializePrimitive(value, key);
+			serializePrimitive(value, key, id);
 
 		else static if (isPointer!(T))
 		{
@@ -172,11 +175,11 @@
 				goto error;
 				
 			else
-				serializePointer(value, key);
+				serializePointer(value, key, id);
 		}			
 		
 		else static if (isEnum!(T))
-			serializeEnum(value, key);
+			serializeEnum(value, key, id);
 		
 		else
 		{
@@ -185,7 +188,7 @@
 		}
 	}
 
-	private void serializeObject (T) (T value, string key)
+	private void serializeObject (T) (T value, string key, Id id)
 	{
 		if (!value)
 			return archive.archiveNull(T.stringof, key);
@@ -197,7 +200,6 @@
 		
 		auto runtimeType = value.classinfo.name;
 		
-		Id id = nextId;
 		addSerializedReference(value, id);
 
 		triggerEvents(serializing, value, {			
@@ -222,12 +224,12 @@
 		});
 	}
 	
-	private void serializeStruct (T) (T value, string key)
+	private void serializeStruct (T) (T value, string key, Id id)
 	{			
 		string type = T.stringof;
 		
 		triggerEvents(serializing, value, {
-			archive.archiveStruct(type, key, nextId, {
+			archive.archiveStruct(type, key, id, {
 				if (type in serializers)
 				{
 					auto wrapper = getSerializerWrapper!(T)(type);
@@ -246,19 +248,17 @@
 		});
 	}
 	
-	private void serializeString (T) (T value, string key)
+	private void serializeString (T) (T value, string key, Id id)
 	{
-		auto id = nextId;
 		auto array = Array(value.ptr, value.length, ElementTypeOfArray!(T).sizeof);
 		
 		archive.archive(value, key, id);			
 		addSerializedArray(array, id);
 	}
 	
-	private void serializeArray (T) (T value, string key)
+	private void serializeArray (T) (T value, string key, Id id)
 	{
 		auto array = Array(value.ptr, value.length, ElementTypeOfArray!(T).sizeof);
-		auto id = nextId;
 
 		archive.archiveArray(array, arrayToString!(T), key, id, {
 			foreach (i, e ; value)
@@ -268,14 +268,13 @@
 		addSerializedArray(array, id);
 	}
 	
-	private void serializeAssociativeArray (T) (T value, string key)
+	private void serializeAssociativeArray (T) (T value, string key, Id id)
 	{
 		auto reference = getSerializedReference(value);
 		
 		if (reference != Id.max)
 			return archive.archiveReference(key, reference);
-		
-		Id id = nextId;
+
 		addSerializedReference(value, id);
 		
 		string keyType = KeyTypeOfAssociativeArray!(T).stringof;
@@ -299,13 +298,11 @@
 		});
 	}
 	
-	private void serializePointer (T) (T value, string key)
+	private void serializePointer (T) (T value, string key, Id id)
 	{
 		if (!value)
 			return archive.archiveNull(T.stringof, key);
 		
-		Id id = nextId;
-		
 		archive.archivePointer(key, id, {
 			if (key in serializers)
 			{
@@ -329,24 +326,24 @@
 		addSerializedPointer(value, id);
 	}
 	
-	private void serializeEnum (T) (T value, string key)
+	private void serializeEnum (T) (T value, string key, Id id)
 	{
 		alias BaseTypeOfEnum!(T) EnumBaseType;
 		auto val = cast(EnumBaseType) value;
 		string type = T.stringof;
 		
-		archive.archiveEnum(val, type, key, nextId);
+		archive.archiveEnum(val, type, key, id);
 	}
 	
-	private void serializePrimitive (T) (T value, string key)
+	private void serializePrimitive (T) (T value, string key, Id id)
 	{	
-		archive.archive(value, key, nextId);
+		archive.archive(value, key, id);
 	}
 	
-	private void serializeTypedef (T) (T value, string key)
+	private void serializeTypedef (T) (T value, string key, Id id)
 	{
 		archive.archiveTypedef(T.stringof, key, nextId, {
-			serializeInternal!(BaseTypeOfTypedef!(T))(value, nextKey);
+			serializeInternal!(BaseTypeOfTypedef!(T))(value, id);
 		});
 	}
 	
@@ -665,11 +662,12 @@
 			static if (!internalFields.ctfeContains(field) && !nonSerializedFields.ctfeContains(field))
 			{
 				alias typeof(T.tupleof[i]) Type;				
-				Type v = value.tupleof[i];
+				Type v = value.tupleof[i];				
+				auto id = nextId;
 				
-				addSerializedValue(value.tupleof[i], nextId);
-				serializeInternal(v, toData(field));
-			}				
+				addSerializedValue(value.tupleof[i], id);
+				serializeInternal(v, toData(field), id);
+			}
 		}
 		
 		static if (isObject!(T) && !is(T == Object))
--- a/orange/serialization/archives/XMLArchive.d	Sun Nov 21 16:53:46 2010 +0100
+++ b/orange/serialization/archives/XMLArchive.d	Sun Nov 21 17:12:24 2010 +0100
@@ -273,7 +273,8 @@
 		lastElement.element(Tags.enumTag, toData(value))
 		.attribute(Attributes.typeAttribute, toData(type))
 		.attribute(Attributes.baseTypeAttribute, toData(T.stringof))
-		.attribute(Attributes.keyAttribute, toData(key));
+		.attribute(Attributes.keyAttribute, toData(key))
+		.attribute(Attributes.idAttribute, toData(id));
 	}
 	
 	void archiveBaseClass (string type, string key, Id id)
@@ -281,7 +282,8 @@
 		restore(lastElement) in {
 			lastElement = lastElement.element(Tags.baseTag)
 			.attribute(Attributes.typeAttribute, toData(type))
-			.attribute(Attributes.keyAttribute, toData(key)); 
+			.attribute(Attributes.keyAttribute, toData(key))
+			.attribute(Attributes.idAttribute, toData(id)); 
 		};
 	}
 	
@@ -324,8 +326,10 @@
 	{
 		if (auto pointerNode = getArchivedPointer(pointerId))
 		{
-			pointerNode.parent.element(Tags.referenceTag, toData(pointeeId)).
-			attribute(Attributes.keyAttribute, toData(pointerNode.key));
+			pointerNode.parent.element(Tags.pointerTag)
+			.attribute(Attributes.keyAttribute, toData(pointerNode.key))
+			.attribute(Attributes.idAttribute, toData(pointerId))
+			.element(Tags.referenceTag, toData(pointeeId));
 		}
 	}
 	
@@ -354,7 +358,8 @@
 		restore(lastElement) in {
 			lastElement = lastElement.element(Tags.structTag)
 			.attribute(Attributes.typeAttribute, toData(type))
-			.attribute(Attributes.keyAttribute, toData(key));
+			.attribute(Attributes.keyAttribute, toData(key))
+			.attribute(Attributes.idAttribute, toData(id));
 			
 			dg();
 		};
@@ -365,7 +370,8 @@
 		restore(lastElement) in {
 			lastElement = lastElement.element(Tags.typedefTag)
 			.attribute(Attributes.typeAttribute, toData(type))
-			.attribute(Attributes.keyAttribute, toData(key));
+			.attribute(Attributes.keyAttribute, toData(key))
+			.attribute(Attributes.idAttribute, toData(id));
 			
 			dg();
 		};
@@ -398,131 +404,132 @@
 	
 	void archive (bool value, string key, Id id)
 	{
-		archivePrimitive(value, key);
+		archivePrimitive(value, key, id);
 	}
-	
+
 	void archive (byte value, string key, Id id)
 	{
-		archivePrimitive(value, key);
+		archivePrimitive(value, key, id);
 	}
-	
-	// currently not suppported by to!()
+
+	//currently not suppported by to!()
 	/*void archive (cdouble value, string key, Id id)
 	{
-		archivePrimitive(value, key);
+		archivePrimitive(value, key, id);
 	}*/
-	
+
 	//currently not implemented but a reserved keyword
 	/*void archive (cent value, string key, Id id)
 	{
-		archivePrimitive(value, key);
+		archivePrimitive(value, key, id);
 	}*/
-	
+
 	//currently not suppported by to!()
 	/*void archive (cfloat value, string key, Id id)
 	{
-		archivePrimitive(value, key);
+		archivePrimitive(value, key, id);
 	}*/
-	
+
 	void archive (char value, string key, Id id)
 	{
-		archivePrimitive(value, key);
+		archivePrimitive(value, key, id);
 	}
-	
+
 	//currently not suppported by to!()
 	/*void archive (creal value, string key, Id id)
 	{
-		archivePrimitive(value, key);
+		archivePrimitive(value, key, id);
 	}*/
-	
+
 	void archive (dchar value, string key, Id id)
 	{
-		archivePrimitive(value, key);
+		archivePrimitive(value, key, id);
 	}
-	
+
 	void archive (double value, string key, Id id)
 	{
-		archivePrimitive(value, key);
+		archivePrimitive(value, key, id);
 	}
-	
+
 	void archive (float value, string key, Id id)
 	{
-		archivePrimitive(value, key);
+		archivePrimitive(value, key, id);
 	}
-	
+
 	//currently not suppported by to!()
 	/*void archive (idouble value, string key, Id id)
 	{
-		archivePrimitive(value, key);
+		archivePrimitive(value, key, id);
 	}*/
-	
+
 	//currently not suppported by to!()
 	/*void archive (ifloat value, string key, Id id)
 	{
-		archivePrimitive(value, key);
+		archivePrimitive(value, key, id);
 	}*/
-	
+
 	void archive (int value, string key, Id id)
 	{
-		archivePrimitive(value, key);
+		archivePrimitive(value, key, id);
 	}
-	
+
 	//currently not suppported by to!()
 	/*void archive (ireal value, string key, Id id)
 	{
-		archivePrimitive(value, key);
+		archivePrimitive(value, key, id);
 	}*/
-	
+
 	void archive (long value, string key, Id id)
 	{
-		archivePrimitive(value, key);
+		archivePrimitive(value, key, id);
 	}
-	
+
 	void archive (real value, string key, Id id)
 	{
-		archivePrimitive(value, key);
+		archivePrimitive(value, key, id);
 	}
-	
+
 	void archive (short value, string key, Id id)
 	{
-		archivePrimitive(value, key);
+		archivePrimitive(value, key, id);
 	}
-	
+
 	void archive (ubyte value, string key, Id id)
 	{
-		archivePrimitive(value, key);
+		archivePrimitive(value, key, id);
 	}
-	
+
 	//currently not implemented but a reserved keyword
 	/*void archive (ucent value, string key, Id id)
 	{
-		archivePrimitive(value, key);
+		archivePrimitive(value, key, id);
 	}*/
-	
+
 	void archive (uint value, string key, Id id)
 	{
-		archivePrimitive(value, key);
+		archivePrimitive(value, key, id);
 	}
-	
+
 	void archive (ulong value, string key, Id id)
 	{
-		archivePrimitive(value, key);
+		archivePrimitive(value, key, id);
+	}
+
+	void archive (ushort value, string key, Id id)
+	{
+		archivePrimitive(value, key, id);
+	}
+
+	void archive (wchar value, string key, Id id)
+	{
+		archivePrimitive(value, key, id);
 	}
 	
-	void archive (ushort value, string key, Id id)
-	{
-		archivePrimitive(value, key);
-	}
-	
-	void archive (wchar value, string key, Id id)
-	{
-		archivePrimitive(value, key);
-	}
-	
-	private void archivePrimitive (T) (T value, string key)
+	private void archivePrimitive (T) (T value, string key, Id id)
 	{
 		lastElement.element(toData(T.stringof), toData(value))
-		.attribute(Attributes.keyAttribute, toData(key));
+		.attribute(Attributes.keyAttribute, toData(key))
+		.attribute(Attributes.idAttribute, toData(id));
 	}
 	
 	Id unarchiveArray (string key, void delegate (size_t) dg)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/orange/util/collection/_.d	Sun Nov 21 17:12:24 2010 +0100
@@ -0,0 +1,11 @@
+/**
+ * Copyright: Copyright (c) 2010 Jacob Carlborg. All rights reserved.
+ * Authors: Jacob Carlborg
+ * Version: Initial created: Nov 21, 2010
+ * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
+ */
+module orange.util.collection._;
+
+public:
+	
+import orange.util.collection.Array;
\ No newline at end of file