Mercurial > projects > qtd
comparison d2/qtd/Marshal.d @ 350:31520b2c0b3c
Removed dependency on parent trait and stringof
author | Max Samukha <maxter@spambox.com> |
---|---|
date | Thu, 20 May 2010 15:49:08 +0300 |
parents | 925386e0e780 |
children | 9784459f0750 |
comparison
equal
deleted
inserted
replaced
349:925386e0e780 | 350:31520b2c0b3c |
---|---|
1 module qtd.Marshal; | 1 module qtd.Marshal; |
2 | 2 |
3 import std.traits; | 3 import |
4 std.traits, | |
5 qtd.meta.Compiletime, | |
6 qtd.ctfe.Format; | |
4 | 7 |
5 import qtd.Meta; | 8 import std.string : startsWith; |
6 | 9 |
7 template isQObjectType(T) // is a QObject type that belongs to the library | 10 |
11 template isQObjectType(T) | |
8 { | 12 { |
9 enum isQObjectType = is(T.__isQObjectType); | 13 enum isQObjectType = is(T.__isQObjectType); |
10 } | 14 } |
11 | 15 |
12 template isObjectType(T) // is a Qt Object type that belongs to the library | 16 template isObjectType(T) |
13 { | 17 { |
14 enum isObjectType = is(T.__isObjectType); | 18 enum isObjectType = is(T.__isObjectType); |
15 } | 19 } |
16 | 20 |
17 template isValueType(T) // is a Qt Value type that belongs to the library | 21 template isValueType(T) |
18 { | 22 { |
19 enum isValueType = is(T.__isValueType); | 23 enum isValueType = is(T.__isValueType); |
20 } | 24 } |
21 | 25 |
22 template isQtType(T) | 26 template isQtType(T) |
39 enum isStringType = is(T == string); | 43 enum isStringType = is(T == string); |
40 } | 44 } |
41 | 45 |
42 template isQList(T) | 46 template isQList(T) |
43 { | 47 { |
44 enum isQList = ctfeStartsWith(Unqual!(T).stringof, "QList!"); | 48 enum isQList = startsWith(Unqual!(T).stringof, "QList!"); |
45 } | 49 } |
46 | 50 |
47 // returns full name of enum: | 51 // returns full name of enum: |
48 // for Qt enum it is in the form of QPaintDevice::PaintDeviceMetric | 52 // for Qt enum it is in the form of QPaintDevice::PaintDeviceMetric |
49 // for pure D enums it is Foo.Bar | 53 // for pure D enums it is Foo.Bar |
56 else | 60 else |
57 enum enumFullName = qualifiedDName!T; | 61 enum enumFullName = qualifiedDName!T; |
58 } | 62 } |
59 else | 63 else |
60 enum enumFullName = qualifiedDName!T; | 64 enum enumFullName = qualifiedDName!T; |
61 | |
62 } | 65 } |
63 | 66 |
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 | 67 |
82 // converts a D argument type to C++ for registering in Qt meta system | 68 // converts a D argument type to C++ for registering in Qt meta system |
83 string qtDeclArg(T)() | 69 string qtDeclArg(T)() |
84 { | 70 { |
85 static if (isQObjectType!T || isObjectType!T) | 71 static if (isQObjectType!T || isObjectType!T) |
102 return Unqual!T.stringof; | 88 return Unqual!T.stringof; |
103 else | 89 else |
104 return T.stringof; | 90 return T.stringof; |
105 } | 91 } |
106 | 92 |
107 // converts an argument from D to C++ in a signal emitter | 93 /** |
108 string convertSignalArgument(T)(string arg) | 94 Generates C++-to-D conversion code for the |
95 argument argIndex. | |
96 */ | |
97 string generateConvToD(uint argIndex) | |
109 { | 98 { |
110 static if (isQObjectType!T || isObjectType!T) | 99 string res = format_ctfe(q{ |
111 return arg ~ " ? " "&" ~ arg ~ ".__nativeId : cast(void**) &" ~ arg; // since it is a pointer type check arg for null | 100 |
112 else static if (isValueType!T) | 101 static if (isQObjectType!(Args[${0}]) || isObjectType!(Args[${0}])) |
113 return arg ~ ".__nativeId"; | 102 auto _out${0} = Args[${0}].__getObject(*cast(void**)_a[${0}]); |
114 else static if (isStringType!T) | 103 else static if (isValueType!(Args[${0}])) |
115 return "&_qt" ~ arg; | 104 { |
116 else static if (isNativeType!T) | 105 // COMPILER BUG: 'new' chokes on Args[argIndex], hence the alias |
117 return "&" ~ arg; | 106 alias Args[${0}] Args${0}; |
118 else | 107 auto _out${0} = new Args${0}(Args[${0}].__constructNativeCopy(_a[${0}])); |
119 return "&" ~ arg; | 108 } |
109 else static if (isStringType!(Args[${0}])) | |
110 auto _out${0} = QStringUtil.toNativeString(_a[${0}]); | |
111 else | |
112 { | |
113 auto _out${0} = *cast(Args[${0}]*)_a[${0}]; | |
114 } | |
115 | |
116 }, argIndex); | |
117 | |
118 return res; | |
120 } | 119 } |
121 | 120 |
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 } |