Help with reflection?

bitwise via Digitalmars-d digitalmars-d at puremagic.com
Fri Oct 10 09:59:59 PDT 2014


Hey, I am trying to put together a basic reflection system, but 
I've run into a problem. I'm not sure if this is a bug or not.

I am trying to store method information about a class. So 
basically, I am using Traits to go through all members of the 
class, and then trying to create delegates to each member. Then, 
at runtime, the function will be dynamically invoked by changing 
the ".ptr" property of the delegate, and calling it.

These are the errors I am getting from these 3 lines from the 
code below:

//del.ptr = null;
-Error: CTFE internal error: unsupported assignment del.ptr = 
null (TestD)

//del.funcptr = mixin("&T." ~ member);
-Error: CTFE internal error: unsupported assignment del.funcptr = 
& someFunction (TestD)
-Also, why has 'T' been removed after mixin?

//del.funcptr = mixin("&SomeClass." ~ member);
-Error: CTFE internal error: unsupported assignment del.funcptr = 
& someFunction (TestD)

Any insight here would be much appreciated!



abstract class Method_Info
{
	string name();
	void invoke(Object obj, ...);
}

class Method_Info_t(T, D) : Method_Info
{
	alias _paramTypes = ParameterTypeTuple!D;
	
	string _name;
	private D _del;
	
	this(string name, D del) {
		_name =  name;
		_del = del;
	}
	
	this(string name) {
		_name =  name;
	}
	
	override string name() {
		return _name;
	}
	
	override void invoke(Object obj, ...)
	{
		if(_arguments.length != _paramTypes.length)
			throw new Exception("Invoke Failed: Invalid Argument Count");
		
		if(obj is null || typeid(T) != typeid(obj))
			throw new Exception("Invoke Failed: Invalid Object");
		
		foreach(i, ty; _paramTypes)
		{
			if(_arguments[i] != typeid(_paramTypes[i]))
				throw new Exception("Invoke Failed: Invalid Argument Type - " 
~
				                    "Expected \'" ~ _paramTypes[i].stringof ~ 
"\', " ~
				"Received \'" ~ _arguments[i].toString ~ "\'.");
		}
		
		_paramTypes tup;
		
		foreach(i, ty; _paramTypes)
			tup[i] = va_arg!(_paramTypes[i])(_argptr);

		_del.ptr = cast(void*)obj;
		_del(tup);
	}
}

static Method_Info[] _createMethodInfo(T)()
{
	enum allMembers = __traits(allMembers, T);
	
	int ct = 0;
	
	Method_Info[] ret = new Method_Info[allMembers.length];
	
	foreach(member; allMembers)
	{
		enum compiled = __traits(compiles, {
			alias typeof(__traits(getMember, T, member)) func_type;
			alias ReturnType!func_type ret_type;
			alias ParameterTypeTuple!func_type params;
			ret_type delegate(params) del;
		});
		
		static if(compiled)
		{
			alias typeof(__traits(getMember, T, member)) func_type;
			
			enum callable = isCallable!func_type;
			
			static if(callable)
			{
				alias ReturnType!func_type return_type;
				alias ParameterTypeTuple!func_type params;
				
				return_type delegate(params) del;

				//del.ptr = null;
				//del.funcptr = mixin("&T." ~ member);
				//del.funcptr = mixin("&SomeClass." ~ member);

				ret[ct++] = new Method_Info_t!(T, typeof(del))(member, del);
			}
		}
	}
	
	return ret[0 .. ct];
}

class SomeClass
{
	int someFunction(int x)
	{
		writeln(x);
		return x * 2;
	}
	
	void otherFunction(string s, int a, float b)
	{
		writeln(s);
		writeln(a);
		writeln(b);
	}

	static Method_Info[] _methodInfo = 
_createMethodInfo!(SomeClass)();
}



More information about the Digitalmars-d mailing list