view dstep/objc/bridge/TypeEncoding.d @ 2:9fd439a28ce3

Adapted the scripts for the new bridge + a lot more
author Jacob Carlborg <doob@me.com>
date Sun, 05 Jul 2009 17:16:19 +0200
parents 033d260cfc9b
children 07194b026fa4
line wrap: on
line source

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

version (Tango)
	import tango.core.Traits;

else
	import std.traits;

import dstep.internal.String;
import dstep.internal.Traits;
import dstep.objc.objc;
import dstep.objc.runtime;

/**
 * Creates the encoded (mangled) form of a type for the
 * Objective-C runtime. 
 */
template encode (TL...)
{
	const string encode = encodeImpl!(TL)();
}

private string encodeImpl (TL...) ()
{
	char[] s;
	
	foreach (T ; TL)
	{
		static if (is (T == id) || is (T == class))
			s ~= _C_ID;

		else static if (is (T == Class))
			s ~= _C_CLASS;

		else static if (is (T == SEL))
			s ~= _C_SEL;
		
		else static if (is (T == byte))
			s ~= _C_CHR;

		else static if (is (T == ubyte))
			s ~= _C_UCHR;

		else static if (is (T == short))
			s ~= _C_SHT;

		else static if (is (T == ushort))
			s ~= _C_USHT;

		else static if (is (T == int))
			s ~= _C_INT;

		else static if (is (T == uint))
			s ~= _C_UINT;

		else static if (is (T == long))
			s ~= _C_LNG_LNG;

		else static if (is (T == ulong))
			s ~= _C_ULNG_LNG;

		else static if (is (T == float))
			s ~= _C_FLT;

		else static if (is (T == double))
			s ~= _C_DBL;

		else static if (is (T == bool))
			s ~= _C_BOOL;

		else static if (is (T == void))
			s ~= _C_VOID;

		else static if (is (T U : T*))
			s ~= _C_VOID ~ encode!(U);

		else static if (is (T : char*))
			s ~= _C_CHARPTR;

		else static if (is (T U : T))
			s ~= _C_ARY_B ~ encode!(U) ~ _C_ARY_E;
		
		else static if (is (T == union))
			s ~= _C_UNION_B ~ encode!(typeof(T.tupleof)) ~ _C_UNION_E;

		else static if (is (T == struct))
			s ~= _C_STRUCT_B ~ encode!(typeof(T.tupleof)) ~ _C_STRUCT_E;

		else
			s ~= _C_UNDEF;
	}
	
	return s;
}

/**
 * Create the encoded (mangled) form of a type for the
 * Objective-C runtime out of the given callable type.
 * This is a shortcut for encode
 */
template encodeCallable (alias Callable)
{
	static assert(isCallableType!(typeof(Callable)), "dstep.objc.bridge.TypeCoding.encodeCallable: The given type is not a callable type");
	
	const encodeCallable = encode!(ReturnTypeOf!(typeof(Callable)), id, SEL, ParameterTupleOf!(typeof(Callable)));
}

/**
 * Builds a string representing a selector out of the given function
 * 
 * It will build the string like this:
 * 
 * ---
 * foo (int x, int y); // foo:y:
 * bar (); // bar
 * fooBar (int x); // fooBar:
 * ---
 * 
 * Params:
 *     func = the function alias to build the selector of
 *     
 * Returns: a string representing the selector
 */
template buildSelector (alias method)
{
	const buildSelector = buildSelectorImpl!(method);
}

private string buildSelectorImpl (alias method) ()
{
	string result = functionNameOf!(method);
	string[] parameterNames = parameterNamesOf!(method);
	
	if (parameterNames.length == 0)
		return result;
	
	else if (parameterNames.length == 1)
		return result ~ ':';
	
	else if (parameterNames.length >= 2)
		parameterNames = parameterNames[1 .. $];
	
	result ~= ':';
	
	foreach (str ; parameterNames)
		result ~= str ~ ':';		
	
	return result;
}