changeset 3:3c155e4c3d56

Fixed structs
author Jacob Carlborg <doob@me.com>
date Mon, 31 May 2010 18:51:56 +0200
parents ea37a9470e3e
children 470ab5270d0c
files orange/serialization/Serializer.d
diffstat 1 files changed, 43 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/orange/serialization/Serializer.d	Mon May 31 17:44:23 2010 +0200
+++ b/orange/serialization/Serializer.d	Mon May 31 18:51:56 2010 +0200
@@ -423,7 +423,7 @@
 		});
 	}
 	
-	private void objectStructSerializeHelper (T) (T value)
+	private void objectStructSerializeHelper (T : Object) (T value)
 	{
 		const nonSerializedFields = collectAnnotations!(nonSerializedField)(value);
 		
@@ -439,11 +439,28 @@
 			}				
 		}
 		
-		static if (is(T : Object) && !is(T == Object))
-			serializeBaseTypes(value);
+		serializeBaseTypes(value);
 	}
 	
-	private void objectStructDeserializeHelper (T) (T value)
+	private void objectStructSerializeHelper (T) (ref T value)
+	{
+		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.`));
+		const nonSerializedFields = collectAnnotations!(nonSerializedField)(value);
+		
+		foreach (i, dummy ; typeof(T.tupleof))
+		{
+			const field = nameOfFieldAt!(T, i);
+			
+			static if (!internalFields.ctfeContains(field) && !nonSerializedFields.ctfeContains(field))
+			{
+				alias typeof(T.tupleof[i]) Type;				
+				Type v = value.tupleof[i];
+				serialize(v, toDataType(field));
+			}				
+		}
+	}
+	
+	private void objectStructDeserializeHelper (T : Object) (T value)
 	{		
 		const nonSerializedFields = collectAnnotations!(nonSerializedField)(value);
 		
@@ -454,13 +471,30 @@
 			static if (!internalFields.ctfeContains(field) && !nonSerializedFields.ctfeContains(field))
 			{
 				alias TypeOfField!(T, field) Type;
-				auto fieldValue = deserializeInternal!(Type)(toDataType(field));				
+				auto fieldValue = deserializeInternal!(Type)(toDataType(field));
 				value.tupleof[i] = fieldValue;
 			}			
 		}
 		
-		static if (is(T : Object) && !is(T == Object))
-			deserializeBaseTypes(value);
+		deserializeBaseTypes(value);
+	}
+	
+	private void objectStructDeserializeHelper (T) (ref T value)
+	{		
+		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.`));
+		const nonSerializedFields = collectAnnotations!(nonSerializedField)(value);
+		
+		foreach (i, dummy ; typeof(T.tupleof))
+		{
+			const field = nameOfFieldAt!(T, i);
+						
+			static if (!internalFields.ctfeContains(field) && !nonSerializedFields.ctfeContains(field))
+			{
+				alias TypeOfField!(T, field) Type;
+				auto fieldValue = deserializeInternal!(Type)(toDataType(field));
+				value.tupleof[i] = fieldValue;
+			}			
+		}
 	}
 	
 	private void serializeBaseTypes (T : Object) (T value)
@@ -554,7 +588,7 @@
 	
 	private void triggerEvent (string name, T) (T value)
 	{
-		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.`));
+		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.`));
 		
 		foreach (i, dummy ; typeof(T.tupleof))
 		{
@@ -588,7 +622,7 @@
 	
 	private static string[] collectAnnotations (string name, T) (T value)
 	{
-		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.`));
+		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.`));
 		
 		string[] annotations;