comparison d2/qtd/Marshal.d @ 349:925386e0e780

renamed MetaMarshall.d
author maxter <spambox@d-coding.com>
date Mon, 17 May 2010 21:48:15 +0300
parents d2/qtd/MetaMarshall.d@96a75b1e5b26
children 31520b2c0b3c
comparison
equal deleted inserted replaced
348:29ea6511681f 349:925386e0e780
1 module qtd.Marshal;
2
3 import std.traits;
4
5 import qtd.Meta;
6
7 template isQObjectType(T) // is a QObject type that belongs to the library
8 {
9 enum isQObjectType = is(T.__isQObjectType);
10 }
11
12 template isObjectType(T) // is a Qt Object type that belongs to the library
13 {
14 enum isObjectType = is(T.__isObjectType);
15 }
16
17 template isValueType(T) // is a Qt Value type that belongs to the library
18 {
19 enum isValueType = is(T.__isValueType);
20 }
21
22 template isQtType(T)
23 {
24 mixin ("enum isQtType = is(T.__isQtType_" ~ T.stringof ~ ");");
25 }
26 /*
27 template isQtType(T)
28 {
29 enum isQtType = isQObjectType!(T) || isObjectType!(T) || isValueType!(T) || is(T.__isQtType);
30 }
31 */
32 template isNativeType(T) // type that doesn't require conversion i.e. is the same in C++ and D
33 {
34 enum isNativeType = isNumeric!T || is(T == bool) || is(T == struct);
35 }
36
37 template isStringType(T) // string type
38 {
39 enum isStringType = is(T == string);
40 }
41
42 template isQList(T)
43 {
44 enum isQList = ctfeStartsWith(Unqual!(T).stringof, "QList!");
45 }
46
47 // returns full name of enum:
48 // for Qt enum it is in the form of QPaintDevice::PaintDeviceMetric
49 // for pure D enums it is Foo.Bar
50 template enumFullName(T)
51 {
52 static if(!isModule(__traits(parent, T).stringof))
53 {
54 static if(isQtType!(__traits(parent, T)))
55 enum enumFullName = qualifiedCppName!T;
56 else
57 enum enumFullName = qualifiedDName!T;
58 }
59 else
60 enum enumFullName = qualifiedDName!T;
61
62 }
63
64 // converts an argumnent from C++ to D in qt_metacall
65 string metaCallArgument(T)(string ptr)
66 {
67 static if (isQObjectType!T || isObjectType!T)
68 return T.stringof ~ ".__getObject(*cast(void**)(" ~ ptr ~ "))";
69 else static if (isValueType!T)
70 return "new " ~ T.stringof ~ "(" ~ T.stringof ~ ".__constructNativeCopy(" ~ ptr ~ "))";
71 else static if (isNativeType!T)
72 return "*(cast(" ~ T.stringof ~ "*)" ~ ptr ~ ")";
73 else static if (isStringType!T)
74 return "QStringUtil.toNativeString(" ~ ptr ~ ")";
75 else static if (is(T == enum))
76 return "*(cast(" ~ qualifiedDName!T ~ "*)" ~ ptr ~ ")";
77 else
78 return "*(cast(" ~ T.stringof ~ "*)" ~ ptr ~ ")";
79 //res = T.stringof;
80 }
81
82 // converts a D argument type to C++ for registering in Qt meta system
83 string qtDeclArg(T)()
84 {
85 static if (isQObjectType!T || isObjectType!T)
86 return T.stringof ~ "*";
87 else static if (isValueType!T)
88 return T.stringof;
89 else static if (isStringType!T)
90 return "QString";
91 else static if (isQList!T)
92 {
93 alias templateParam!T ElementType;
94 static if (is(ElementType == string))
95 return "QStringList";
96 else
97 return "QList<" ~ qtDeclArg!(templateParam!T)() ~ ">";
98 }
99 else static if (is(T == enum))
100 return enumFullName!T;
101 else static if (isNativeType!T)
102 return Unqual!T.stringof;
103 else
104 return T.stringof;
105 }
106
107 // converts an argument from D to C++ in a signal emitter
108 string convertSignalArgument(T)(string arg)
109 {
110 static if (isQObjectType!T || isObjectType!T)
111 return arg ~ " ? " "&" ~ arg ~ ".__nativeId : cast(void**) &" ~ arg; // since it is a pointer type check arg for null
112 else static if (isValueType!T)
113 return arg ~ ".__nativeId";
114 else static if (isStringType!T)
115 return "&_qt" ~ arg;
116 else static if (isNativeType!T)
117 return "&" ~ arg;
118 else
119 return "&" ~ arg;
120 }
121
122 string prepareSignalArguments(Args...)()
123 {
124 string res;
125 foreach(i, _; Args)
126 static if (isStringType!(Args[i]))
127 res ~= "auto _qt_t" ~ __toString(i) ~ " = QString(_t" ~ __toString(i) ~ ");\n";
128 return res;
129 }