Storing templates
Simen
simen.haugen at pandavre.com
Mon Mar 12 11:10:31 PDT 2007
Bill Baxter Wrote:
> Simen wrote:
> > Any pointers on how I can solve this? I'm sure this is not the best way to do this, and I'm always open for change. Perhaps I have taken the wrong approach here?
>
> So basically you want to put these functions in a big map and then later
> be able to query what their actual parameters and types are?
>
> How are you going to supply the necessary arguments in the end?
>
> You may be able to wrap the functions in a templated wrapper class with
> generic "call(...)" method implemented as a common base class/interface
> , and some kind of type info retrieval mechanism. Something like this:
>
> import std.traits;
> interface Callable {
> void call(...);
> TypeInfo argtypes();
> }
> class FuncWrapper(alias Func) : Callable
> {
> alias ParameterTypeTuple!(Func) ArgTup;
> alias ReturnType!(Func) RetT;
> override void call(...) {
> ArgTup args;
> // check to make sure that all _argument's are
> // of the type expected by ArgTup, and copy args
> // one by one to args[i]. This is a little tricky...
> Func(args);
> }
> override TypeInfo argtypes() { return typeid(typeof(Func)); }
> }
>
> But that introduces a fair amount of calling overhead. And it won't
> work 100% if you start throwing in inout parameters. I don't think
> TypeInfo really contains everything you need to know about Func's type
> either so you'll probably have to roll your own mechanism to return info
> about the args and return type.
>
> For the return value you'll probably have to make the return value be an
> out parameter of call().
>
> --bb
Thanks a lot for the reply. I actually don't need anything this fancy as I already know that all the parameters will have the correct type, but you at least set me off in another direction than the black hole I was digging for myself.
I looked at MiniD by Jarrett Billingsley and used ideas from there.
I now have a general data type, and all my functions return this datatype and takes a list of these as parameters:
MyDataType MyFunc(MyDataType[] params)
{
auto param1 = params[0].asDataType1;
auto param2 = params[1].asDataType2;
MyDataType param3 = param1 + param2;
return param3;
}
I store these functions in a small struct that also has the return value so I can look this up:
struct Function
{
MyDataType.Type returnType;
MyDataType function(MyDataType[] params) func;
}
Then I have my list of functions as Function[char[]] functions. I still guess this isn't the best way, but it at least solves my problem.
Again thanks for the reply.
-Simen
More information about the Digitalmars-d-learn
mailing list