dotherside/D/DQml/qslot.d

63 lines
1.2 KiB
D

import std.stdio;
import std.container;
import std.conv;
import std.typecons;
import std.traits;
import qvariant;
import qmetatype;
public class ISlot
{
void Execute(QVariant[] arguments) {}
}
public QSlot!(T) CreateQSlot(T)(T t) if (isCallable!(T))
{
return new QSlot!(T)(t);
}
public class QSlot(T) : ISlot
{
alias ReturnType!T SlotReturnType;
alias ParameterTypeTuple!T Arguments;
public this(T callable)
{
_callable = callable;
_parameterMetaTypes[0] = GetMetaType!SlotReturnType();
foreach (i, arg; Arguments) {
_parameterMetaTypes[i+1] = GetMetaType!arg();
}
}
public override void Execute(QVariant[] arguments)
{
Arguments argumentsTuple;
foreach (i, arg; argumentsTuple) {
arguments[i + 1].getValue(argumentsTuple[i]);
}
static if (is(SlotReturnType == void))
{
opCall(argumentsTuple);
}
else
{
auto result = opCall(argumentsTuple);
arguments[0].setValue(result);
}
}
public ReturnType!T opCall(Arguments arguments)
{
return _callable(arguments);
}
public int[] GetParameterMetaTypes() { return _parameterMetaTypes; }
private T _callable;
private int[] _parameterMetaTypes = new int[Arguments.length + 1];
};