changeset 39:301476d40518

Made a couple of refactorings: * Removed Serializable * Added a Serializable interface * Moved isSerializable and updated the implementation * Added new deserialize methods for deserialize without data during deserializing
author Jacob Carlborg <doob@me.com>
date Thu, 04 Aug 2011 21:29:56 +0200
parents 9443bcddc699
children 02dbd18b7fe9
files dsss.conf orange/serialization/Serializable.d orange/serialization/Serializer.d orange/util/Traits.d
diffstat 4 files changed, 47 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/dsss.conf	Wed Aug 03 22:09:22 2011 +0200
+++ b/dsss.conf	Thu Aug 04 21:29:56 2011 +0200
@@ -1,8 +1,10 @@
-version (unittest) {
-	[tests/all.d]
-	buildflags += -unittest
-	target = unittest
-	version += OrangeUnitTest
-} else {
-	[orange]
-}
\ No newline at end of file
+# version (unittest) {
+# 	[tests/all.d]
+# 	buildflags += -unittest
+# 	target = unittest
+# 	version += OrangeUnitTest
+# } else {
+# 	[orange]
+# }
+
+[main.d]
\ No newline at end of file
--- a/orange/serialization/Serializable.d	Wed Aug 03 22:09:22 2011 +0200
+++ b/orange/serialization/Serializable.d	Thu Aug 04 21:29:56 2011 +0200
@@ -8,39 +8,20 @@
 
 import orange.serialization.archives.Archive;
 import orange.serialization.Events;
+import orange.serialization.Serializer;
 import orange.util.CTFE;
 
-template Serializable ()
+interface Serializable
 {
-	void toData (T) (T archive, T.DataType key)
-	{		
-		alias typeof(this) ThisType;
-		
-		foreach (i, dummy ; typeof(T.tupleof))
-		{
-			alias typeof(ThisType.tupleof[i]) FieldType;
-			
-			const field = nameOfFieldAt!(ThisType, i);			
-			auto value = getValueOfField!(ThisType, FieldType, field)(this);
-			
-			archive.archive(value, field);
-		}
-	}
-	
-	void fromData (T) (T archive, T.DataType key)
-	{
-		alias typeof(this) ThisType;
-		
-		foreach (i, dummy ; typeof(ThisType.tupleof))
-		{
-			alias typeof(ThisType.tupleof[i]) FieldType;
-			
-			const field = nameOfFieldAt!(ThisType, i);
-			auto value = archive.unarchive!(FieldType)(field);
-			
-			setValueOfField!(FieldType, ThisType, field)(this, value);
-		}
-	}
+	void toData (Serializer serializer, Serializer.Data key);
+	void fromData (Serializer serializer, Serializer.Data key);
+}
+
+template isSerializable (T)
+{
+	const isSerializable = is(T : Serializable) || (
+		is(typeof(T.toData(Serializer.init, Serializer.Data.init))) &&
+		is(typeof(T.fromData(Serializer.init, Serializer.Data.init))));
 }
 
 template NonSerialized (alias field)
--- a/orange/serialization/Serializer.d	Wed Aug 03 22:09:22 2011 +0200
+++ b/orange/serialization/Serializer.d	Thu Aug 04 21:29:56 2011 +0200
@@ -58,7 +58,7 @@
 		}
 		
 		ErrorCallback errorCallback_;		
-		Archive archive;
+		Archive archive_;
 		
 		size_t keyCounter;
 		Id idCounter;
@@ -87,7 +87,7 @@
 	
 	this (Archive archive)
 	{
-		this.archive = archive;
+		this.archive_ = archive;
 		
 		throwOnErrorCallback = (ArchiveException exception, string[] data) { throw exception; };
 		doNothingOnErrorCallback = (ArchiveException exception, string[] data) { /* do nothing */ };
@@ -95,6 +95,11 @@
 		setThrowOnErrorCallback();
 	}
 	
+	Archive archive ()
+	{
+		return archive_;
+	}
+	
 	ErrorCallback errorCallback ()
 	{
 		return errorCallback_;
@@ -220,7 +225,7 @@
 					wrapper(value, this, key);
 				}
 				
-				else static if (isSerializable!(T, Serializer))
+				else static if (isSerializable!(T))
 					value.toData(this, key);
 				
 				else
@@ -327,7 +332,7 @@
 				wrapper(value, this, key);
 			}
 			
-			else static if (isSerializable!(T, Serializer))
+			else static if (isSerializable!(T))
 				value.toData(this, key);
 			
 			else
@@ -364,15 +369,15 @@
 		});
 	}
 	
-	T deserialize (T) (Data data, string key = null)
-	{		
+	T deserialize (T) (Data data, string key = "")
+	{
 		if (hasBegunSerializing && !hasBegunDeserializing)
 			resetCounters();
 		
 		if (!hasBegunDeserializing)
 			hasBegunDeserializing = true;
 		
-		if (!key)
+		if (key.empty())
 			key = nextKey;
 
 		archive.beginUnarchiving(data);
@@ -381,6 +386,19 @@
 		
 		return value;
 	}
+
+	T deserialize (T) (string key)
+	{
+		if (!hasBegunDeserializing)
+			throw new SerializationException("Cannot deserialize without any data, this method should only be called after deserialization has begun", __FILE__, __LINE__);
+		
+		return deserialize!(T)(archive.untypedData, key);
+	}
+
+	T deserialize (T) ()
+	{
+		return deserialize!(T)("");
+	}
 	
 	private T deserializeInternal (T, U) (U keyOrId)
 	{		
@@ -445,7 +463,7 @@
 					wrapper(value, this, keyOrId);
 				}
 				
-				else static if (isSerializable!(T, Serializer))
+				else static if (isSerializable!(T))
 					value.fromData(this, keyOrId);
 				
 				else
--- a/orange/util/Traits.d	Wed Aug 03 22:09:22 2011 +0200
+++ b/orange/util/Traits.d	Thu Aug 04 21:29:56 2011 +0200
@@ -197,16 +197,6 @@
 	
 }
 
-template isSerializable (T, SerializerType)
-{
-	const isSerializable = is(typeof(T.toData(SerializerType.init, SerializerType.DataType.init))) && is(typeof(T.fromData(SerializerType.init, SerializerType.DataType.init)));
-}
-
-template isISerializable (T)
-{
-	const isISerializable = is(T : ISerializable);
-}
-
 template TypeOfDataType (T)
 {
 	alias T.DataType TypeOfDataType;