view dstep/internal/Traits.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 19885b43130e
line wrap: on
line source

/**
 * Copyright: Copyright (c) 2009 Jacob Carlborg.
 * Authors: Jacob Carlborg
 * Version: Initial created: Apr 28, 2009
 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
 */
module dstep.internal.Traits;

/**
 * Returns the name of the given function
 * 
 * Params:
 *     func = the function alias to get the name of
 *     
 * Returns: the name of the function 
 */
template functionNameOf (alias func)
{
	version(LDC)
		const functionNameOf = (&func).stringof[1 .. $];
	
	else
		const functionNameOf = (&func).stringof[2 .. $];
}

/**
 * Returns the parameter names of the given function
 * 
 * Params:
 *     func = the function alias to get the parameter names of
 *     
 * Returns: an array of strings containing the parameter names 
 */
template parameterNamesOf (alias func)
{
	const parameterNamesOf = parameterNamesOfImpl!(func);
}

private char[][] parameterNamesOfImpl (alias func) ()
{
	char[] funcStr = typeof(&func).stringof;
	
	auto start = funcStr.indexOf('(');
	auto end = funcStr.indexOf(')');
	
	const firstPattern = ' ';
	const secondPattern = ',';
	
	funcStr = funcStr[start + 1 .. end];
	
	if (funcStr == "")
		return null;
		
	funcStr ~= secondPattern;
	
	char[] token;
	char[][] arr;
	
	foreach (c ; funcStr)
	{		
		if (c != firstPattern && c != secondPattern)
			token ~= c;
		
		else
		{			
			if (token)
				arr ~= token;
			
			token = null;
		}			
	}
	
	char[][] result;
	bool skip = false;
	
	foreach (str ; arr)
	{
		skip = !skip;
		
		if (skip)
			continue;
		
		result ~= str;
	}
	
	return result;
}

/**
 * Compile-time function to get the index of the give element.
 * 
 * Performs a linear scan, returning the index of the first occurrence 
 * of the specified element in the array, or U.max if the array does 
 * not contain the element.
 * 
 * Params:
 *     arr = the array to get the index of the element from
 *     element = the element to find
 *     
 * Returns: the index of the element or size_t.max if the element was not found.
 */
private size_t indexOf (T) (T[] arr, T element)
{
	foreach (i, e ; arr)
		if (e == element)
			return i;
	
	return size_t.max;
}