dotherside/D/dslot.d

60 lines
1.2 KiB
D
Raw Normal View History

2014-07-19 18:26:08 +02:00
import std.stdio;
import std.container;
import std.conv;
import std.typecons;
import std.traits;
2014-08-30 18:46:34 +02:00
import dotherside;
2014-07-19 18:26:08 +02:00
public class ISlot
{
2014-08-30 18:46:34 +02:00
void Execute(QVariant[] arguments) {}
2014-07-19 18:26:08 +02:00
}
2014-08-30 18:46:34 +02:00
public DSlot!(T) CreateDSlot(T)(T t) if (isCallable!(T))
2014-07-19 18:26:08 +02:00
{
2014-08-30 18:46:34 +02:00
return new DSlot!(T)(t);
2014-07-19 18:26:08 +02:00
}
public class DSlot(T) : ISlot
{
alias ReturnType!T SlotReturnType;
alias ParameterTypeTuple!T Arguments;
2014-08-30 18:46:34 +02:00
public this(T callable)
2014-07-19 18:26:08 +02:00
{
_callable = callable;
_parameterMetaTypes[0] = GetMetaType!SlotReturnType();
foreach (i, arg; Arguments)
_parameterMetaTypes[i+1] = GetMetaType!arg();
2014-07-19 18:26:08 +02:00
}
2014-08-30 18:46:34 +02:00
public override void Execute(QVariant[] arguments)
2014-07-19 18:26:08 +02:00
{
Arguments argumentsTuple;
foreach (i, arg; argumentsTuple) {
2014-08-30 18:46:34 +02:00
arguments[i + 1].getValue(argumentsTuple[i]);
}
2014-07-19 18:26:08 +02:00
static if (is(SlotReturnType == void))
{
opCall(argumentsTuple);
}
else
{
2014-08-30 18:46:34 +02:00
auto result = opCall(argumentsTuple);
arguments[0].setValue(result);
2014-07-19 18:26:08 +02:00
}
}
2014-08-30 18:46:34 +02:00
public ReturnType!T opCall(Arguments arguments)
2014-07-19 18:26:08 +02:00
{
return _callable(arguments);
}
2014-08-30 18:46:34 +02:00
public int[] GetParameterMetaTypes() { return _parameterMetaTypes; }
2014-07-19 18:26:08 +02:00
2014-08-30 18:46:34 +02:00
private T _callable;
private int[] _parameterMetaTypes = new int[Arguments.length + 1];
};