diff dstep/objc/bridge/Type.d @ 16:19885b43130e

Huge update, the bridge actually works now
author Jacob Carlborg <doob@me.com>
date Sun, 03 Jan 2010 22:06:11 +0100
parents 9fd439a28ce3
children
line wrap: on
line diff
--- a/dstep/objc/bridge/Type.d	Mon Aug 03 15:31:48 2009 +0200
+++ b/dstep/objc/bridge/Type.d	Sun Jan 03 22:06:11 2010 +0100
@@ -7,15 +7,24 @@
 module dstep.objc.bridge.Type;
 
 version (Tango)
-	import tango.core.Tuple;
+	import tango.core.Tuple : Tuple;
 
 else
-	import std.typetuple;
+	import std.typetuple : Tuple;
 
 import dstep.objc.objc;
 
+/**
+ * Evaluates to the Objective-C equivalent type for $(D_PARAM T).
+ * 
+ * Examples:
+ * ---
+ * static assert(is(ObjcType!(Object) == id));
+ * static assert(is(ObjcType!(int) == int));
+ * ---
+ */
 template ObjcType (T)
-{
+{	
 	static if (needsEncapsulation!(T))
 		alias id ObjcType;
 	
@@ -23,32 +32,73 @@
 		alias T ObjcType;
 }
 
+/**
+ * Evaluates to $(D_KEYWORD true) if $(D_PARAM T) needs to be encapsulated.
+ * 
+ * Examples:
+ * ---
+ * static assert(needsEncapsulation!(Object));
+ * static assert(!needsEncapsulation!(int));
+ * ---
+ */
 template needsEncapsulation (T)
 {
-	static if (is(T == class))
+	static if (is(T == class) || is (T : Object) || is(T == interface))
 		const needsEncapsulation = true;
 	
 	else
 		const needsEncapsulation = false;
 }
 
-template ObjcTypeTuple (TList...)
+/**
+ * Evaluates to a tuple of Objective-C equivalent types.
+ * 
+ * Examples:
+ * ---
+ * static assert(is(typeof(ObjcTypes!(id, Object, id)) == typeof(Tuple!(id, id, id))));
+ * static assert(is(typeof(ObjcTypes!(id, id, id)) == typeof(Tuple!(id, id, id))));
+ * ---
+ */ 
+template ObjcTypes (Types...)
 {
-	alias replaceClasses!(TList).types ObjcTypeTuple;
-}
-
-private struct Wrap (TList...)
-{
-	TList types;
+	alias EncapsulateTypes!(id, Types) ObjcTypes;
 }
 
-private Wrap!(TList) replaceClasses (TList...) ()
+/**
+ * Evaluates to $(D_KEYWORD true) if $(D_PARAM T) has a constructor that takes one
+ * argument of the type $(D_PSYMBOL id).
+ * 
+ * Examples:
+ * ---
+ * class NSString : NSObject
+ * {
+ * 		this (id object)
+ * 		{
+ * 			super(object);
+ * 		}
+ * }
+ * 
+ * static assert(hasIdConstructor!(NSString));
+ * static assert(!hasIdConstructor!(Object));
+ * ---
+ */
+template hasIdConstructor (T)
 {
-	alias Tuple!(TList) Types;
+	const hasIdConstructor = is(typeof(
+	{
+		objc_object o;
+		new T(&o);
+	}));
+}
+
+private template EncapsulateTypes (U, TList... )
+{
+	static if( TList.length == 0 )
+	    alias TList EncapsulateTypes;
 	
-	foreach (ref type ; Types)
-		static if (is(type == class))
-			type = id;
+	else static if(needsEncapsulation!(TList[0]))
+	    alias Tuple!( U, EncapsulateTypes!(U, TList[1 .. $] ) ) EncapsulateTypes;
 	
-	return Wrap!(Types);
+	else
+	    alias Tuple!( TList[0], EncapsulateTypes!( U, TList[1 .. $] ) ) EncapsulateTypes;
 }
\ No newline at end of file