Mercurial > projects > qtd
comparison qt/core/QList.d @ 292:19498f420252 signals
more QList goodness
author | eldar |
---|---|
date | Tue, 10 Nov 2009 19:29:42 +0000 |
parents | 0d2094800bdb |
children | 8627891e4556 |
comparison
equal
deleted
inserted
replaced
291:0d2094800bdb | 292:19498f420252 |
---|---|
1 module qt.core.QList; | 1 module qt.core.QList; |
2 | 2 |
3 import qt.QGlobal; | 3 import qt.QGlobal; |
4 import qt.qtd.Atomic; | 4 import qt.qtd.Atomic; |
5 //import qt.core.QTypeInfo; | |
6 | |
7 import qt.qtd.MetaMarshall; | |
5 | 8 |
6 import core.stdc.stdlib : qRealloc = realloc, qFree = free, qMalloc = malloc; | 9 import core.stdc.stdlib : qRealloc = realloc, qFree = free, qMalloc = malloc; |
7 import core.stdc.string : memcpy, memmove; | 10 import core.stdc.string : memcpy, memmove; |
8 | 11 |
12 import std.traits; | |
13 | |
9 enum INT_MAX = int.max; | 14 enum INT_MAX = int.max; |
15 | |
16 bool isComplex(T)() | |
17 if (is(typeof(T.QTypeInfo))) | |
18 { | |
19 return T.QTypeInfo.isComplex(); | |
20 } | |
21 | |
22 bool isStatic(T)() | |
23 if (is(typeof(T.QTypeInfo))) | |
24 { | |
25 return T.QTypeInfo.isStatic(); | |
26 } | |
27 | |
28 bool isLarge(T)() | |
29 if (is(typeof(T.QTypeInfo))) | |
30 { | |
31 return T.QTypeInfo.isLarge(); | |
32 } | |
33 | |
10 | 34 |
11 int qAllocMore(int alloc, int extra) | 35 int qAllocMore(int alloc, int extra) |
12 { | 36 { |
13 if (alloc == 0 && extra == 0) | 37 if (alloc == 0 && extra == 0) |
14 return 0; | 38 return 0; |
32 } | 56 } |
33 | 57 |
34 private int grow(int size) | 58 private int grow(int size) |
35 { | 59 { |
36 // dear compiler: don't optimize me out. | 60 // dear compiler: don't optimize me out. |
37 synchronized { | 61 // synchronized { |
38 int x = qAllocMore(size * (void*).sizeof, QListData.DataHeaderSize) / (void*).sizeof; | 62 int x = qAllocMore(size * (void*).sizeof, QListData.DataHeaderSize) / (void*).sizeof; |
39 return x; | 63 return x; |
40 } | 64 // } |
41 } | 65 } |
42 | 66 |
43 struct QListData { | 67 struct QListData { |
44 struct Data { | 68 struct Data { |
45 Atomic!int ref_; | 69 Atomic!int ref_; |
269 struct QList(T) | 293 struct QList(T) |
270 { | 294 { |
271 struct Node | 295 struct Node |
272 { | 296 { |
273 void *v; | 297 void *v; |
274 | 298 |
275 ref T t() | 299 static if (isQObjectType!T) |
276 { return *cast(T*)(&this); } | 300 { |
277 // { return *cast(T*)(QTypeInfo!T.isLarge || QTypeInfo!T.isStatic | 301 T t() |
278 // ? v : &this); } } | 302 { |
303 return T.__getObject( *cast(void**)(&this) ); | |
304 } | |
305 } | |
306 else | |
307 { | |
308 ref T t() | |
309 { | |
310 return *cast(T*)(&this); | |
311 } | |
312 // { return *cast(T*)(QTypeInfo!T.isLarge || QTypeInfo!T.isStatic | |
313 // ? v : &this); } } | |
314 } | |
279 } | 315 } |
280 | 316 |
281 union { | 317 union { |
282 QListData p; | 318 QListData p; |
283 QListData.Data* d; | 319 QListData.Data* d; |
327 detach_helper(); | 363 detach_helper(); |
328 } | 364 } |
329 return this; | 365 return this; |
330 } | 366 } |
331 | 367 |
368 int length() const { return p.size(); } | |
369 int size() const { return length; } | |
370 | |
332 void detach() { if (d.ref_.load() != 1) detach_helper(); } | 371 void detach() { if (d.ref_.load() != 1) detach_helper(); } |
333 | 372 |
334 private void detach_helper() | 373 private void detach_helper() |
335 { | 374 { |
336 Node *n = cast(Node*)(p.begin()); | 375 Node *n = cast(Node*)(p.begin()); |
345 detach(); | 384 detach(); |
346 /* static if (QTypeInfo!T.isLarge || QTypeInfo!T.isStatic) | 385 /* static if (QTypeInfo!T.isLarge || QTypeInfo!T.isStatic) |
347 { | 386 { |
348 node_construct(cast(Node*)(p.append()), t); | 387 node_construct(cast(Node*)(p.append()), t); |
349 } | 388 } |
350 else*/ | 389 else*/ static if (isQObjectType!T) |
390 { | |
391 auto n = cast(Node*)(p.append()); | |
392 *cast(void**)(n) = cast(Node*) t.__nativeId; | |
393 } | |
394 else | |
351 { | 395 { |
352 const T cpy = t; | 396 const T cpy = t; |
353 node_construct(cast(Node*)(p.append()), cpy); | 397 node_construct(cast(Node*)(p.append()), cpy); |
354 } | 398 } |
355 } | 399 } |
356 | 400 |
357 ref const (T) at(int i) const | 401 static if (isQObjectType!T) |
358 { | 402 { |
359 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range"); | 403 T at(int i) const |
360 return (cast(Node*)(p.at(i))).t(); | 404 { |
361 } | 405 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range"); |
362 | 406 return (cast(Node*)(p.at(i))).t(); |
363 void node_construct(Node *n, const ref T t) | 407 } |
364 { | 408 } |
365 /* TODO static if (QTypeInfo!T.isLarge || QTypeInfo!T.isStatic) | 409 else |
366 n.v = new T(t); | 410 { |
367 else static if (QTypeInfo!T.isComplex) | 411 ref const (T) at(int i) const |
368 new (n) T(t); | 412 { |
369 else*/ | 413 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range"); |
370 *cast(T*)(n) = t; | 414 return (cast(Node*)(p.at(i))).t(); |
371 } | 415 } |
416 } | |
417 | |
418 static if (isQObjectType!T) | |
419 { } | |
420 else | |
421 void node_construct(Node *n, const ref T t) | |
422 { | |
423 /* TODO static if (QTypeInfo!T.isLarge || QTypeInfo!T.isStatic) | |
424 n.v = new T(t); | |
425 else static if (QTypeInfo!T.isComplex) | |
426 new (n) T(t); | |
427 else*/ | |
428 { *cast(T*)(n) = cast(T)(t); } | |
429 } | |
372 | 430 |
373 void node_copy(Node *from, Node *to, Node *src) | 431 void node_copy(Node *from, Node *to, Node *src) |
374 { | 432 { |
375 /* TODO if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) | 433 /* TODO if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) |
376 while(from != to) | 434 while(from != to) |
381 */ | 439 */ |
382 } | 440 } |
383 | 441 |
384 void free(QListData.Data* data) | 442 void free(QListData.Data* data) |
385 { | 443 { |
444 writeln("QList data destroyed"); | |
386 node_destruct(cast(Node*)(data.array.ptr + data.begin), | 445 node_destruct(cast(Node*)(data.array.ptr + data.begin), |
387 cast(Node*)(data.array.ptr + data.end)); | 446 cast(Node*)(data.array.ptr + data.end)); |
388 if (data.ref_.load() == 0) | 447 if (data.ref_.load() == 0) |
389 {} // qFree(data); TODO | 448 {} // qFree(data); TODO |
390 } | 449 } |
398 */ | 457 */ |
399 } | 458 } |
400 } | 459 } |
401 | 460 |
402 extern(C) void qtd_create_QList(void *nativeId); | 461 extern(C) void qtd_create_QList(void *nativeId); |
462 extern(C) void qtd_create_QList_QObject(void *nativeId); |