Mercurial > projects > qtd
comparison qt/core/QVariant.d @ 140:6cbe4f5f8fb7
D: autoregistration of new types in metasystem for QVariant.
D: implement QVariant.canConvert!(Type).
author | SokoL_SD |
---|---|
date | Sun, 07 Jun 2009 18:30:09 +0000 |
parents | cbdfc92eac7f |
children | 7ae51fb20f61 |
comparison
equal
deleted
inserted
replaced
139:825ac6364d2f | 140:6cbe4f5f8fb7 |
---|---|
94 LastType = 0xffffffff // need this so that gcc >= 3.4 allocates 32 bits for Type | 94 LastType = 0xffffffff // need this so that gcc >= 3.4 allocates 32 bits for Type |
95 } | 95 } |
96 | 96 |
97 // Functions | 97 // Functions |
98 | 98 |
99 private template getMetaId() | |
100 { | |
101 const char[] getMetaId = " | |
102 int i = qtd_MetatypeId(toStringz(name)); | |
103 if(i <= 0) | |
104 i = qRegisterMetaType!(T)(name);"; | |
105 } | |
106 | |
99 static public QVariant fromValue(T)(T obj) | 107 static public QVariant fromValue(T)(T obj) |
100 { | 108 { |
101 QVariant var; | 109 QVariant var; |
102 static if (is(T == class) || is(T == interface)) | 110 static if (is(T == class) || is(T == interface)) |
103 { | 111 { |
104 int i = qtd_MetatypeId(toStringz(obj.classinfo.name)); | 112 string name = obj.classinfo.name; |
113 mixin(getMetaId!()); | |
105 var = new QVariant(i, cast(void*)(obj)); | 114 var = new QVariant(i, cast(void*)(obj)); |
106 } | 115 } |
107 else static if (isDynamicArrayType!(T) || isStaticArrayType!(T) ) | 116 else static if (isDynamicArrayType!(T) || isStaticArrayType!(T) ) |
108 { | 117 { |
109 int i = qtd_MetatypeId(toStringz(typeid(ElementTypeOfArray!(T)).toString ~ "[]")); | 118 string name = typeid(ElementTypeOfArray!(T)).toString ~ "[]"; |
119 mixin(getMetaId!()); | |
110 auto darray = new DArrayToC; | 120 auto darray = new DArrayToC; |
111 darray.array = obj.dup; | 121 darray.array = obj.dup; |
112 var = new QVariant(i, cast(void*)(darray)); | 122 var = new QVariant(i, cast(void*)(darray)); |
113 } | 123 } |
114 else | 124 else |
115 { | 125 { |
116 int i = qtd_MetatypeId(toStringz(typeid(T).toString)); | 126 string name = typeid(T).toString; |
127 mixin(getMetaId!()); | |
117 auto data = new T; | 128 auto data = new T; |
118 *data = obj; | 129 *data = obj; |
119 var = new QVariant(i, cast(void*)(data)); | 130 var = new QVariant(i, cast(void*)(data)); |
120 } | 131 } |
121 return var; | 132 return var; |
297 public this(ulong ull) { | 308 public this(ulong ull) { |
298 void* __qt_return_value = qtd_QVariant_QVariant_ulong(ull); | 309 void* __qt_return_value = qtd_QVariant_QVariant_ulong(ull); |
299 super(__qt_return_value); | 310 super(__qt_return_value); |
300 } | 311 } |
301 | 312 |
302 public final bool canConvert(Type type) { | 313 private final bool canConvertImpl(char[] name) |
303 return qtd_QVariant_canConvert(nativeId, type); | 314 { |
315 int i = qtd_MetatypeId(toStringz(name)); | |
316 assert(i > 0); | |
317 return qtd_QVariant_canConvert(nativeId, i); | |
318 } | |
319 | |
320 public final bool canConvert(Type)() { | |
321 static if ( is(Type == QBitArray) ) | |
322 return canConvertImpl("QBitArray"); | |
323 else static if ( is(Type == bool) ) | |
324 return canConvertImpl("bool"); | |
325 else static if ( is(Type == QByteArray) ) | |
326 return canConvertImpl("QByteArray"); | |
327 else static if ( is(Type == QDate) ) | |
328 return canConvertImpl("QDate"); | |
329 else static if ( is(Type == QDateTime) ) | |
330 return canConvertImpl("QDateTime"); | |
331 else static if ( is(Type == double) ) | |
332 return canConvertImpl("double"); | |
333 else static if ( is(Type == int) ) | |
334 return canConvertImpl("int"); | |
335 else static if ( is(Type == QLine) ) | |
336 return canConvertImpl("QLine"); | |
337 else static if ( is(Type == QLineF) ) | |
338 return canConvertImpl("QLineF"); | |
339 else static if ( is(Type == QLocale) ) | |
340 return canConvertImpl("QLocale"); | |
341 else static if ( is(Type == long) ) | |
342 return canConvertImpl("long"); | |
343 else static if ( is(Type == QPoint) ) | |
344 return canConvertImpl("QPoint"); | |
345 else static if ( is(Type == QPointF) ) | |
346 return canConvertImpl("QPointF"); | |
347 else static if ( is(Type == QRect) ) | |
348 return canConvertImpl("QRect"); | |
349 else static if ( is(Type == QRectF) ) | |
350 return canConvertImpl("QRectF"); | |
351 else static if ( is(Type == QRegExp) ) | |
352 return canConvertImpl("QRegExp"); | |
353 else static if ( is(Type == QSize) ) | |
354 return canConvertImpl("QSize"); | |
355 else static if ( is(Type == QSizeF) ) | |
356 return canConvertImpl("QSizeF"); | |
357 else static if ( is(Type == string) ) | |
358 return canConvertImpl("QString"); | |
359 else static if ( is(Type == QTime) ) | |
360 return canConvertImpl("QTime"); | |
361 else static if ( is(Type == uint) ) | |
362 return canConvertImpl("unsigned int"); // TODO: | |
363 else static if ( is(Type == ulong) ) | |
364 return canConvertImpl("unsigned long long"); // TODO: | |
365 else static if ( is(Type == QUrl) ) | |
366 return canConvertImpl("QUrl"); | |
367 else | |
368 { | |
369 static if( is( Type == class ) || is( Type == interface ) ) | |
370 { | |
371 Object object = cast(Object)qtd_QVariant_data(nativeId); | |
372 if(object) | |
373 return cast(Type)(object) !is null; | |
374 return false; | |
375 } | |
376 else static if (isDynamicArrayType!(Type) || isStaticArrayType!(Type) ) | |
377 { | |
378 auto array = cast(DArrayToC*)qtd_QVariant_data(nativeId); | |
379 return cast(Type)(array.array) !is null; | |
380 } | |
381 else | |
382 { | |
383 int i = qtd_MetatypeId(toStringz(typeid(Type).toString)); | |
384 return qtd_QVariant_canConvert(nativeId, i); | |
385 } | |
386 } | |
304 } | 387 } |
305 | 388 |
306 public final Type value(Type)() { | 389 public final Type value(Type)() { |
307 static if ( is(Type == QBitArray) ) | 390 static if ( is(Type == QBitArray) ) |
308 return toBitArra; | 391 return toBitArra; |