advanced function binding

Moritz Warning moritzwarning at web.de
Wed Mar 28 15:15:07 PDT 2007


> http://paste.dprogramming.com/dpbaqugv.php

Hi,

thank you for your response! It was very helpful indeed.
Here is the example code I'm experimenting on:

import std.traits;
import std.conv;
import std.bind;

void main() {
	Wrapper!(A, Foo.getBar) x = new Wrapper!(A, Foo.getBar)(Foo.getBar);
}

class Foo {
	void getBar() {}
}
class Bar { }

class Wrapper(B, alias Func)
{
	alias ReturnType!(Func) R;
	alias typeof(Func) * FuncPtr;
	alias typeof(ReturnType!(std.bind.bindAlias!(Func)))  BindPtr; //type is DerefFunc!(R) or void?
	
	FuncPtr funcptr;
	BindPtr bindptr;
	
	//1. store pointer
	this(FuncPtr funcptr)
	{
		this.funcptr = funcptr;
	}
	
	//2. bind parameters
	void bind(char[][] u)
	{
		ParameterTypeTuple!(Func) t;
		foreach (i, arg; t) {
			t[i] = convert!(typeof(arg))(u[i]);
		}
		bindptr = std.bind.bindAlias!(Func)(t);
		//fn(t);
	}
	
	//3. call function on object
	R call(B b)
	{
		R delegate() dg; 
		dg.funcptr = this.funcptr; //need to assign bindptr!
		dg.ptr = cast(void*) b;
		return dg();
	}
}

T convert(T)(char[] u)
{
	static if (is(T == int)) {
		return std.conv.toInt(u);
	} else static if (is(T == float)) {
		return std.conv.toFloat(u);;
	} else static if (is(T == char[])) {
		return u;
	} else static assert(false, "Unsupported type: " ~ T.stringof);
}

It doesn't compile because I cannot assign a bindAlias!(Func)(t) to bindptr.
The compiler tells me the return type of bindAlias is void, when I look at the source
I would guess it's DerefFunc!(R). Anyway, somehow I must be able to store the binded
function and assign it to dg.funcptr in call().
What is going wrong?


More information about the Digitalmars-d-learn mailing list