288
|
1 module qt.qtd.MetaMarshall;
|
|
2
|
|
3 import std.traits;
|
|
4
|
309
|
5 // utilities
|
302
|
6 // shouldn't be here
|
|
7
|
309
|
8 public bool startsWith(T)(T[] source, T[] pattern)
|
|
9 {
|
|
10 return source.length >= pattern.length && source[0 .. pattern.length] == pattern[];
|
|
11 }
|
|
12
|
302
|
13 string __toString(long v)
|
|
14 {
|
|
15 if (v == 0)
|
|
16 return "0";
|
|
17
|
|
18 string ret;
|
|
19
|
|
20 bool neg;
|
|
21 if (v < 0)
|
|
22 {
|
|
23 neg = true;
|
|
24 v = -v;
|
|
25 }
|
|
26
|
|
27 while (v != 0)
|
|
28 {
|
|
29 ret = cast(char)(v % 10 + '0') ~ ret;
|
|
30 v = cast(long)(v / 10);
|
|
31 }
|
|
32
|
|
33 if (neg)
|
|
34 ret = "-" ~ ret;
|
|
35
|
|
36 return ret;
|
|
37 }
|
|
38
|
309
|
39 template templateParam(U : V!(U), alias V)
|
|
40 {
|
|
41 alias U templateParam;
|
|
42 }
|
|
43
|
|
44
|
|
45 /*
|
|
46 * actual stuff
|
|
47 */
|
|
48
|
288
|
49 template isQObjectType(T) // is a QObject type that belongs to the library
|
|
50 {
|
292
|
51 enum isQObjectType = is(T.__isQObjectType);
|
288
|
52 }
|
|
53
|
293
|
54 template isObjectType(T) // is a Qt Object type that belongs to the library
|
288
|
55 {
|
292
|
56 enum isObjectType = is(T.__isObjectType);
|
288
|
57 }
|
|
58
|
293
|
59 template isValueType(T) // is a Qt Value type that belongs to the library
|
288
|
60 {
|
293
|
61 enum isValueType = is(T.__isValueType);
|
288
|
62 }
|
|
63
|
|
64 template isNativeType(T) // type that doesn't require conversion i.e. is the same in C++ and D
|
|
65 {
|
302
|
66 enum isNativeType = isNumeric!T || is(T == bool) || is(T == struct);
|
|
67 }
|
|
68
|
|
69 template isStringType(T) // string type
|
|
70 {
|
|
71 enum isStringType = is(T == string);
|
288
|
72 }
|
|
73
|
309
|
74 template isQList(T)
|
|
75 {
|
|
76 enum isQList = startsWith(Unqual!(T).stringof, "QList!");
|
|
77 }
|
|
78
|
288
|
79 // converts an argumnent from C++ to D in qt_metacall
|
|
80 string metaCallArgument(T)(string ptr)
|
|
81 {
|
303
|
82 static if (isQObjectType!T || isObjectType!T)
|
288
|
83 return T.stringof ~ ".__getObject(*cast(void**)(" ~ ptr ~ "))";
|
318
|
84 else static if (isValueType!T)
|
|
85 return "new " ~ T.stringof ~ "(" ~ T.stringof ~ ".__constructNativeCopy(" ~ ptr ~ "))";
|
288
|
86 else static if (isNativeType!T)
|
|
87 return "*(cast(" ~ T.stringof ~ "*)" ~ ptr ~ ")";
|
302
|
88 else static if (isStringType!T)
|
|
89 return "QStringUtil.toNativeString(" ~ ptr ~ ")";
|
288
|
90 else
|
|
91 return "*(cast(" ~ T.stringof ~ "*)" ~ ptr ~ ")";
|
|
92 //res = T.stringof;
|
|
93 }
|
|
94
|
|
95 // converts a D argument type to C++ for registering in Qt meta system
|
|
96 string qtDeclArg(T)()
|
|
97 {
|
303
|
98 static if (isQObjectType!T || isObjectType!T)
|
288
|
99 return T.stringof ~ "*";
|
318
|
100 else static if (isValueType!T)
|
|
101 return T.stringof;
|
302
|
102 else static if (isStringType!T)
|
|
103 return "QString";
|
309
|
104 else static if (isQList!T)
|
318
|
105 {
|
|
106 alias templateParam!T ElementType;
|
|
107 static if (is(ElementType == string))
|
|
108 return "QStringList";
|
|
109 else
|
|
110 return "QList<" ~ qtDeclArg!(templateParam!T)() ~ ">";
|
|
111 }
|
288
|
112 else static if (isNativeType!T)
|
302
|
113 return Unqual!T.stringof;
|
288
|
114 else
|
|
115 return T.stringof;
|
|
116 }
|
|
117
|
|
118 // converts an argument from D to C++ in a signal emitter
|
|
119 string convertSignalArgument(T)(string arg)
|
|
120 {
|
303
|
121 static if (isQObjectType!T || isObjectType!T)
|
318
|
122 return "&" ~ arg ~ ".__nativeId";
|
|
123 else static if (isValueType!T)
|
288
|
124 return arg ~ ".__nativeId";
|
302
|
125 else static if (isStringType!T)
|
318
|
126 return "&_qt" ~ arg;
|
288
|
127 else static if (isNativeType!T)
|
318
|
128 return "&" ~ arg;
|
288
|
129 else
|
318
|
130 return "&" ~ arg;
|
288
|
131 }
|
302
|
132
|
|
133 string prepareSignalArguments(Args...)()
|
|
134 {
|
|
135 string res;
|
|
136 foreach(i, _; Args)
|
|
137 static if (isStringType!(Args[i]))
|
|
138 res ~= "auto _qt_t" ~ __toString(i) ~ " = QString(_t" ~ __toString(i) ~ ");\n";
|
|
139 return res;
|
|
140 }
|