mirror of
https://github.com/status-im/dotherside.git
synced 2025-02-12 04:26:43 +00:00
[DQml] Implemented signal body implementations
This commit is contained in:
parent
6c368e70fc
commit
bf65c83b58
@ -4,8 +4,19 @@ import std.algorithm;
|
||||
import std.string;
|
||||
import std.stdio;
|
||||
|
||||
struct Slot {};
|
||||
struct Signal {};
|
||||
struct QtProperty {
|
||||
public enum Type { Read = 0, Write = 1}
|
||||
public string name;
|
||||
public Type type;
|
||||
|
||||
this(string name, Type type)
|
||||
{
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
}
|
||||
struct QtSlot {};
|
||||
struct QtSignal {};
|
||||
|
||||
string GenerateVariantConversionCall(string typeName)
|
||||
{
|
||||
@ -33,7 +44,7 @@ string GenerateArgumentList(string[] typeNames)
|
||||
return result;
|
||||
}
|
||||
|
||||
string GenerateSlotCall(SlotInfo info)
|
||||
string GenerateSlotCall(FunctionInfo info)
|
||||
{
|
||||
auto args = GenerateArgumentList(info.parameterTypes);
|
||||
auto call = format("%s(%s)", info.name, args);
|
||||
@ -41,7 +52,7 @@ string GenerateSlotCall(SlotInfo info)
|
||||
return format(formatStr, call);
|
||||
}
|
||||
|
||||
string GenerateCaseBlock(SlotInfo info)
|
||||
string GenerateCaseBlock(FunctionInfo info)
|
||||
{
|
||||
string result = "";
|
||||
result ~= format("case \"%s\":\n", info.name);
|
||||
@ -56,14 +67,46 @@ string GenerateOnSlotCalled(T)()
|
||||
result ~= "{\n";
|
||||
result ~= "switch(slotName.toString())\n";
|
||||
result ~= "{\n";
|
||||
foreach (info; IterateUDA!(T)())
|
||||
result ~= GenerateCaseBlock(info);
|
||||
auto info = IterateUDA!(T)();
|
||||
foreach (slot; info.slots)
|
||||
result ~= GenerateCaseBlock(slot);
|
||||
result ~= "default: break;\n";
|
||||
result ~= "}\n"; //
|
||||
result ~= "}";
|
||||
return result;
|
||||
}
|
||||
|
||||
string GenerateSignalCall(FunctionInfo info)
|
||||
{
|
||||
string args = "";
|
||||
string vars = "";
|
||||
for (int i = 0; i < info.parameterTypes.length; ++i) {
|
||||
if (i > 0) {
|
||||
args ~= ",";
|
||||
vars ~= ",";
|
||||
}
|
||||
args ~= format("%s val%d", info.parameterTypes[i], i);
|
||||
vars ~= format("val%d", i);
|
||||
}
|
||||
|
||||
auto result = format("%s %s(%s) { emit(\"%s\", %s); }", info.returnType, info.name, args, info.name, vars);
|
||||
return result;
|
||||
}
|
||||
|
||||
string GenerateQtSignals(T)()
|
||||
{
|
||||
string result = "";
|
||||
|
||||
auto info = IterateUDA!(T)();
|
||||
|
||||
foreach (signal; info.signals)
|
||||
{
|
||||
result ~= GenerateSignalCall(signal) ~ "\n";
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
string GenerateMetaType(string typeName)
|
||||
{
|
||||
switch(typeName)
|
||||
@ -79,7 +122,7 @@ string GenerateMetaType(string typeName)
|
||||
}
|
||||
}
|
||||
|
||||
string GenerateMetaTypesList(SlotInfo info)
|
||||
string GenerateMetaTypesList(FunctionInfo info)
|
||||
{
|
||||
string result = GenerateMetaType(info.returnType);
|
||||
foreach (typeName; info.parameterTypes)
|
||||
@ -93,26 +136,35 @@ string GenerateQObjectInit(T)()
|
||||
result ~= "protected void qobjectInit()\n";
|
||||
result ~= "{\n";
|
||||
|
||||
foreach (info; IterateUDA!(T)())
|
||||
auto info = IterateUDA!(T)();
|
||||
|
||||
foreach (slot; info.slots)
|
||||
{
|
||||
auto metaTypes = GenerateMetaTypesList(info);
|
||||
result ~= format("registerSlot(\"%s\", [%s]);\n", info.name, metaTypes);
|
||||
auto metaTypes = GenerateMetaTypesList(slot);
|
||||
result ~= format("registerSlot(\"%s\", [%s]);\n", slot.name, metaTypes);
|
||||
}
|
||||
|
||||
result ~= "}";
|
||||
return result;
|
||||
}
|
||||
|
||||
struct SlotInfo
|
||||
struct FunctionInfo
|
||||
{
|
||||
string name;
|
||||
string returnType;
|
||||
string[] parameterTypes;
|
||||
}
|
||||
|
||||
SlotInfo[] IterateUDA(T)()
|
||||
struct QtInfo
|
||||
{
|
||||
SlotInfo[] result;
|
||||
FunctionInfo[] slots;
|
||||
FunctionInfo[] signals;
|
||||
QtProperty[] properties;
|
||||
}
|
||||
|
||||
QtInfo IterateUDA(T)()
|
||||
{
|
||||
QtInfo result;
|
||||
|
||||
foreach (member; __traits(allMembers, T))
|
||||
{
|
||||
@ -126,17 +178,25 @@ SlotInfo[] IterateUDA(T)()
|
||||
foreach (attribute; attributes)
|
||||
attributeNames ~= typeof(attribute).stringof;
|
||||
|
||||
bool isSlot = attributeNames.canFind("QtSlot");
|
||||
bool isSignal = attributeNames.canFind("QtSignal");
|
||||
bool isProperty = attributeNames.canFind("QtProperty");
|
||||
|
||||
// Extract the Function Return Type and Arguments
|
||||
if (attributeNames.canFind("Slot"))
|
||||
if (isSlot || isSignal)
|
||||
{
|
||||
SlotInfo slotInfo;
|
||||
slotInfo.name = member;
|
||||
slotInfo.returnType = ReturnType!(__traits(getMember, T, member)).stringof;
|
||||
FunctionInfo info;
|
||||
info.name = member;
|
||||
info.returnType = ReturnType!(__traits(getMember, T, member)).stringof;
|
||||
|
||||
foreach (param; ParameterTypeTuple!(__traits(getMember, T, member)))
|
||||
slotInfo.parameterTypes ~= param.stringof;
|
||||
info.parameterTypes ~= param.stringof;
|
||||
|
||||
result ~= slotInfo;
|
||||
if (isSlot)
|
||||
result.slots ~= info;
|
||||
|
||||
if (isSignal)
|
||||
result.signals ~= info;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -144,6 +204,7 @@ SlotInfo[] IterateUDA(T)()
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
class Contact : QObject
|
||||
{
|
||||
this()
|
||||
@ -153,19 +214,31 @@ class Contact : QObject
|
||||
registerSignal("nameChanged", [QMetaType.String]);
|
||||
registerProperty("name", QMetaType.String, "getName", "setName", "nameChanged");
|
||||
|
||||
auto info = IterateUDA!(Contact);
|
||||
|
||||
foreach(slot; info.slots)
|
||||
writeln("Slot:", slot.name);
|
||||
|
||||
foreach(signal; info.signals)
|
||||
writeln("Signals:", signal.name);
|
||||
|
||||
foreach(property; info.properties)
|
||||
writeln("Property:", property.name);
|
||||
|
||||
writeln(GenerateOnSlotCalled!(Contact));
|
||||
writeln(GenerateQObjectInit!(Contact));
|
||||
// writeln(GenerateQObjectInit!(Contact));
|
||||
writeln(GenerateQtSignals!(Contact));
|
||||
}
|
||||
|
||||
~this() {}
|
||||
|
||||
@Slot()
|
||||
@QtSlot()
|
||||
public string getName()
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
@Slot()
|
||||
@QtSlot()
|
||||
public void setName(string name)
|
||||
{
|
||||
if (m_name != name)
|
||||
@ -175,8 +248,28 @@ class Contact : QObject
|
||||
}
|
||||
}
|
||||
|
||||
@QtSignal()
|
||||
public void nameChanged(string name);
|
||||
|
||||
@QtSlot()
|
||||
public string getSurname()
|
||||
{
|
||||
return m_surname;
|
||||
}
|
||||
|
||||
@QtSlot()
|
||||
public void setSurname(string surname)
|
||||
{
|
||||
m_surname = surname;
|
||||
}
|
||||
|
||||
@QtSignal()
|
||||
void surnameChanged(string surname);
|
||||
|
||||
mixin(GenerateOnSlotCalled!(Contact));
|
||||
mixin(GenerateQObjectInit!(Contact));
|
||||
mixin(GenerateQtSignals!(Contact));
|
||||
|
||||
private string m_name;
|
||||
private string m_surname;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user