boilerplate generation

Dicebot public at dicebot.lv
Sun Dec 29 14:01:18 PST 2013


Does this look clean enough? (Not sure I have completely 
understood the question)

/* export extern(C) int f( uint argc, A* argv) { return Wrapper!f(
argc, argv); } */

struct A {}

int Wrapper(alias func, T...)(T args) { return 42; }

template ExternC(alias existingFunc)
{
	import std.string : format;
	import std.traits : ReturnType;
	
	static assert(is(typeof(&existingFunc) : int function(uint, 
A*)));
	
	mixin(format(
		"export extern(C) int %s(uint argc, A* argv) { return 
Wrapper!(%s)(argc, argv); }",
		__traits(identifier, existingFunc),
		__traits(identifier, existingFunc)
	));
}

// to avoid name clash, assuming you have different module in 
real code
struct X
{	
	static int fff(uint, A*)
	{
		return 42;
	}
}

mixin ExternC!(X.fff);

pragma(msg, typeof(fff));
// extern (C) int(uint argc, A* argv)

void main()
{
}


More information about the Digitalmars-d-learn mailing list