changeset 44:1fef41162966

Fixed deserialization of whcar for D2. Fixed events for D2.
author Jacob Carlborg <doob@me.com>
date Tue, 09 Aug 2011 10:11:14 +0200
parents 6f35fb47ca24
children 3cd22957e411
files orange/serialization/Events.d orange/serialization/archives/Archive.d orange/serialization/archives/XMLArchive.d orange/util/Reflection.d tests/Primitive.d
diffstat 5 files changed, 43 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/orange/serialization/Events.d	Tue Aug 09 09:26:57 2011 +0200
+++ b/orange/serialization/Events.d	Tue Aug 09 10:11:14 2011 +0200
@@ -30,7 +30,11 @@
 
 struct Event (alias m)
 {
-	private const method = &m;
+	version (Tango)
+		private const method = &m;
+		
+	else
+		mixin("private enum method = &m;");
 	
 	void opCall (T) (T value)
 	{
--- a/orange/serialization/archives/Archive.d	Tue Aug 09 09:26:57 2011 +0200
+++ b/orange/serialization/archives/Archive.d	Tue Aug 09 10:11:14 2011 +0200
@@ -11,7 +11,10 @@
 
 else
 {
+	import std.array;
 	import std.conv;
+	import std.utf;
+
 	alias ConvException ConversionException;
 }
 
@@ -206,8 +209,14 @@
 	protected T fromData (T) (Data value)
 	{
 		try
-			return to!(T)(value);
-		
+		{
+			static if (is(T == wchar))
+				return toWchar(value);
+
+			else
+				return to!(T)(value);
+		}
+
 		catch (ConversionException e)
 			throw new ArchiveException(e);
 	}
@@ -224,4 +233,22 @@
 		
 		return aPtr >= bPtr && aPtr + a.length * T.sizeof <= bPtr + b.length * U.sizeof;
 	}
+	
+	private wchar toWchar (Data value)
+	{
+		version (Tango)
+			return to!(wchar)(value);
+		
+		else
+		{
+			auto c = value.front;
+
+			if (codeLength!(wchar)(c) > 2)
+				throw new ConversionException("Could not convert `" ~
+					to!(string)(value) ~ "` of type " ~
+					Data.stringof ~ " to type wchar.");
+
+			return cast(wchar) c;
+		}
+	}
 }
\ No newline at end of file
--- a/orange/serialization/archives/XMLArchive.d	Tue Aug 09 09:26:57 2011 +0200
+++ b/orange/serialization/archives/XMLArchive.d	Tue Aug 09 10:11:14 2011 +0200
@@ -997,11 +997,7 @@
 	
 	wchar unarchiveWchar (string key)
 	{
-		version (Tango)
-			return unarchivePrimitive!(wchar)(key);
-			
-		else
-			return wchar.init;
+		return unarchivePrimitive!(wchar)(key);
 	}
 	
 	T unarchivePrimitive (T) (string key)
--- a/orange/util/Reflection.d	Tue Aug 09 09:26:57 2011 +0200
+++ b/orange/util/Reflection.d	Tue Aug 09 10:11:14 2011 +0200
@@ -327,6 +327,8 @@
 		static if (f == field)
 			return t.tupleof[i];
 	}
+	
+	assert(0);
 }
 
 /**
--- a/tests/Primitive.d	Tue Aug 09 09:26:57 2011 +0200
+++ b/tests/Primitive.d	Tue Aug 09 10:11:14 2011 +0200
@@ -39,15 +39,13 @@
 	uint uint_;
 	ulong ulong_;
 	ushort ushort_;
-	
-	version (Tango)
-		wchar wchar_; // Phobos to!() function can't handle string -> wchar
+	wchar wchar_;
 	
 	equals_t opEquals (Object other)
 	{
 		if (auto o =  cast(H) other)
 		{
-			auto result = bool_ == o.bool_ &&
+			return bool_ == o.bool_ &&
 				   byte_ == o.byte_ &&
 				   //cdouble_ == o.cdouble_ && // currently not suppported by to!()
 				   //cent_ == o.cent_ && // currently not implemented but a reserved keyword
@@ -68,13 +66,8 @@
 				   //ucent_ == o.ucent_ && // currently not implemented but a reserved keyword
 				   uint_ == o.uint_ &&
 				   ulong_ == o.ulong_ &&
-				   ushort_ == o.ushort_;
-				
-				   version (Tango)
-				   		return result && wchar_ == o.wchar_;
-
-				   else
-						return result;
+				   ushort_ == o.ushort_ &&
+				   wchar_ == o.wchar_;
 		}
 		
 		return false;
@@ -109,9 +102,7 @@
 	h.uint_ = 1U;
 	h.ulong_ = 1LU;
 	h.ushort_ = 1U;
-
-	version (Tango)
-		h.wchar_ = 'c';
+	h.wchar_ = 'c';
 
 	describe("serialize primitives") in {
 		it("should return serialized primitives") in {
@@ -134,9 +125,7 @@
 			assert(archive.data().containsXmlTag("uint", `key="uint_" id="12"`, "1"));
 			assert(archive.data().containsXmlTag("ulong", `key="ulong_" id="13"`, "1"));
 			assert(archive.data().containsXmlTag("ushort", `key="ushort_" id="14"`, "1"));
-			
-			version (Tango)
-				assert(archive.data().containsXmlTag("wchar", `key="wchar_" id="15"`, "c"));
+			assert(archive.data().containsXmlTag("wchar", `key="wchar_" id="15"`, "c"));
 		};
 	};