view 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 source

/**
 * Copyright: Copyright (c) 2009 Jacob Carlborg.
 * Authors: Jacob Carlborg
 * Version: Initial created: May 16, 2009
 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
 */
module dstep.objc.bridge.Type;

version (Tango)
	import tango.core.Tuple : Tuple;

else
	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;
	
	else
		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) || is (T : Object) || is(T == interface))
		const needsEncapsulation = true;
	
	else
		const needsEncapsulation = false;
}

/**
 * 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 EncapsulateTypes!(id, Types) ObjcTypes;
}

/**
 * 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)
{
	const hasIdConstructor = is(typeof(
	{
		objc_object o;
		new T(&o);
	}));
}

private template EncapsulateTypes (U, TList... )
{
	static if( TList.length == 0 )
	    alias TList EncapsulateTypes;
	
	else static if(needsEncapsulation!(TList[0]))
	    alias Tuple!( U, EncapsulateTypes!(U, TList[1 .. $] ) ) EncapsulateTypes;
	
	else
	    alias Tuple!( TList[0], EncapsulateTypes!( U, TList[1 .. $] ) ) EncapsulateTypes;
}