Mercurial > projects > qtd
annotate qt/core/QList.d @ 318:ce07227f00c1 signals
more signals and QList
author | eldar_ins@eldar-laptop |
---|---|
date | Thu, 24 Dec 2009 05:19:40 +0500 |
parents | 55ee4603365d |
children | 7a3c43424dca |
rev | line source |
---|---|
291 | 1 module qt.core.QList; |
2 | |
3 import qt.QGlobal; | |
293 | 4 import qt.QtdObject; |
291 | 5 import qt.qtd.Atomic; |
293 | 6 import qt.qtd.MetaMarshall; |
295
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
7 import qt.core.QTypeInfo; |
296 | 8 import qt.core.QString; |
292 | 9 |
291 | 10 import core.stdc.stdlib : qRealloc = realloc, qFree = free, qMalloc = malloc; |
11 import core.stdc.string : memcpy, memmove; | |
12 | |
292 | 13 import std.traits; |
14 | |
291 | 15 enum INT_MAX = int.max; |
16 | |
292 | 17 bool isComplex(T)() |
293 | 18 if (is(T.QTypeInfo)) |
292 | 19 { |
293 | 20 return T.QTypeInfo.isComplex; |
292 | 21 } |
22 | |
23 bool isStatic(T)() | |
293 | 24 if (is(T.QTypeInfo)) |
292 | 25 { |
293 | 26 return T.QTypeInfo.isStatic; |
292 | 27 } |
28 | |
29 bool isLarge(T)() | |
293 | 30 if (is(T.QTypeInfo)) |
292 | 31 { |
293 | 32 return T.QTypeInfo.isLarge; |
292 | 33 } |
34 | |
296 | 35 template isQtReference(T) |
36 { | |
37 enum isQtReference = isQObjectType!T || isObjectType!T || isValueType!T || is(T == string); | |
38 } | |
39 | |
291 | 40 int qAllocMore(int alloc, int extra) |
41 { | |
42 if (alloc == 0 && extra == 0) | |
43 return 0; | |
44 const int page = 1 << 12; | |
45 int nalloc; | |
46 alloc += extra; | |
47 if (alloc < 1<<6) { | |
48 nalloc = (1<<3) + ((alloc >>3) << 3); | |
49 } else { | |
50 // don't do anything if the loop will overflow signed int. | |
51 if (alloc >= INT_MAX/2) | |
52 return INT_MAX; | |
53 nalloc = (alloc < page) ? 1 << 3 : page; | |
54 while (nalloc < alloc) { | |
55 if (nalloc <= 0) | |
56 return INT_MAX; | |
57 nalloc *= 2; | |
58 } | |
59 } | |
60 return nalloc - extra; | |
61 } | |
62 | |
295
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
63 void q_new_at(T)(T* ptr, const ref T t) |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
64 { |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
65 memcpy(ptr, &t, T.sizeof); |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
66 /* static if (__traits(compiles, ptr.__postblit())) DMD bug #3539 |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
67 ptr.__postblit();*/ |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
68 } |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
69 |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
70 T* q_new(T)(const ref T t) |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
71 { |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
72 T* ptr = cast(T*) qMalloc(T.sizeof); |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
73 q_new_at!T(ptr, t); |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
74 return ptr; |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
75 } |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
76 |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
77 void q_delete(T)(T* t) |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
78 { |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
79 static if (__traits(compiles, t.__dtor())) |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
80 t.__dtor(); |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
81 qFree(t); |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
82 } |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
83 |
291 | 84 private int grow(int size) |
85 { | |
86 // dear compiler: don't optimize me out. | |
292 | 87 // synchronized { |
291 | 88 int x = qAllocMore(size * (void*).sizeof, QListData.DataHeaderSize) / (void*).sizeof; |
89 return x; | |
292 | 90 // } |
291 | 91 } |
92 | |
93 struct QListData { | |
94 struct Data { | |
95 Atomic!int ref_; | |
96 int alloc, begin, end; | |
97 uint sharable; | |
98 void*[1] array; | |
99 } | |
100 | |
101 enum { DataHeaderSize = Data.sizeof - (void*).sizeof } | |
102 | |
103 static Data shared_null; | |
104 Data *d; | |
105 | |
106 static this() | |
107 { | |
108 shared_null = Data(Atomic!int(1), 0, 0, 0, true, [null]); | |
109 } | |
110 | |
111 | |
112 // Data *detach(); // remove in 5.0 | |
113 | |
114 Data* detach2() | |
115 { | |
116 Data* x = d; | |
117 d = cast(Data*)(qMalloc(DataHeaderSize + x.alloc * (void*).sizeof)); | |
118 if (!d) | |
119 qFatal("QList: Out of memory"); | |
120 | |
121 memcpy(d, x, DataHeaderSize + x.alloc * (void*).sizeof); | |
122 d.alloc = x.alloc; | |
123 d.ref_.store(1); | |
124 d.sharable = true; | |
125 if (!d.alloc) | |
126 d.begin = d.end = 0; | |
127 | |
128 return x; | |
129 } | |
130 | |
131 void realloc(int alloc) | |
132 { | |
133 // assert(d.ref_ == 1); | |
134 Data* x = cast(Data*)(qRealloc(d, DataHeaderSize + alloc * (void*).sizeof)); | |
135 if (!x) | |
136 qFatal("QList: Out of memory"); | |
137 | |
138 d = x; | |
139 d.alloc = alloc; | |
140 if (!alloc) | |
141 d.begin = d.end = 0; | |
142 } | |
143 | |
144 void** append() | |
145 { | |
146 // #TODO Q_ASSERT(d.ref_ == 1); | |
147 if (d.end == d.alloc) { | |
148 int n = d.end - d.begin; | |
149 if (d.begin > 2 * d.alloc / 3) { | |
150 memcpy(d.array.ptr + n, d.array.ptr + d.begin, n * (void*).sizeof); | |
151 d.begin = n; | |
152 d.end = n * 2; | |
153 } else { | |
154 realloc(grow(d.alloc + 1)); | |
155 } | |
156 } | |
157 return d.array.ptr + d.end++; | |
158 } | |
159 | |
160 void **append(const ref QListData l) | |
161 { | |
162 // Q_ASSERT(d.ref_ == 1); | |
163 int e = d.end; | |
164 int n = l.d.end - l.d.begin; | |
165 if (n) { | |
166 if (e + n > d.alloc) | |
167 realloc(grow(e + l.d.end - l.d.begin)); | |
168 memcpy(d.array.ptr + d.end, l.d.array.ptr + l.d.begin, n * (void*).sizeof); | |
169 d.end += n; | |
170 } | |
171 return d.array.ptr + e; | |
172 } | |
173 | |
174 void **prepend() | |
175 { | |
176 // Q_ASSERT(d.ref_ == 1); | |
177 if (d.begin == 0) { | |
178 if (d.end >= d.alloc / 3) | |
179 realloc(grow(d.alloc + 1)); | |
180 | |
181 if (d.end < d.alloc / 3) | |
182 d.begin = d.alloc - 2 * d.end; | |
183 else | |
184 d.begin = d.alloc - d.end; | |
185 | |
186 memmove(d.array.ptr + d.begin, d.array.ptr, d.end * (void*).sizeof); | |
187 d.end += d.begin; | |
188 } | |
189 return d.array.ptr + --d.begin; | |
190 } | |
191 | |
192 void **insert(int i) | |
193 { | |
194 // Q_ASSERT(d.ref_ == 1); | |
195 if (i <= 0) | |
196 return prepend(); | |
197 if (i >= d.end - d.begin) | |
198 return append(); | |
199 | |
200 bool leftward = false; | |
201 int size = d.end - d.begin; | |
202 | |
203 if (d.begin == 0) { | |
204 if (d.end == d.alloc) { | |
205 // If the array is full, we expand it and move some items rightward | |
206 realloc(grow(d.alloc + 1)); | |
207 } else { | |
208 // If there is free space at the end of the array, we move some items rightward | |
209 } | |
210 } else { | |
211 if (d.end == d.alloc) { | |
212 // If there is free space at the beginning of the array, we move some items leftward | |
213 leftward = true; | |
214 } else { | |
215 // If there is free space at both ends, we move as few items as possible | |
216 leftward = (i < size - i); | |
217 } | |
218 } | |
219 | |
220 if (leftward) { | |
221 --d.begin; | |
222 memmove(d.array.ptr + d.begin, d.array.ptr + d.begin + 1, i * (void*).sizeof); | |
223 } else { | |
224 memmove(d.array.ptr + d.begin + i + 1, d.array.ptr + d.begin + i, | |
225 (size - i) * (void*).sizeof); | |
226 ++d.end; | |
227 } | |
228 return d.array.ptr + d.begin + i; | |
229 } | |
230 | |
231 void remove(int i) | |
232 { | |
233 // Q_ASSERT(d.ref_ == 1); | |
234 i += d.begin; | |
235 if (i - d.begin < d.end - i) { | |
236 if (int offset = i - d.begin) | |
237 memmove(d.array.ptr + d.begin + 1, d.array.ptr + d.begin, offset * (void*).sizeof); | |
238 d.begin++; | |
239 } else { | |
240 if (int offset = d.end - i - 1) | |
241 memmove(d.array.ptr + i, d.array.ptr + i + 1, offset * (void*).sizeof); | |
242 d.end--; | |
243 } | |
244 } | |
245 | |
246 void remove(int i, int n) | |
247 { | |
248 // Q_ASSERT(d.ref_ == 1); | |
249 i += d.begin; | |
250 int middle = i + n/2; | |
251 if (middle - d.begin < d.end - middle) { | |
252 memmove(d.array.ptr + d.begin + n, d.array.ptr + d.begin, | |
253 (i - d.begin) * (void*).sizeof); | |
254 d.begin += n; | |
255 } else { | |
256 memmove(d.array.ptr + i, d.array.ptr + i + n, | |
257 (d.end - i - n) * (void*).sizeof); | |
258 d.end -= n; | |
259 } | |
260 } | |
261 | |
262 void move(int from, int to) | |
263 { | |
264 // Q_ASSERT(d.ref_ == 1); | |
265 if (from == to) | |
266 return; | |
267 | |
268 from += d.begin; | |
269 to += d.begin; | |
270 void *t = d.array.ptr[from]; | |
271 | |
272 if (from < to) { | |
273 if (d.end == d.alloc || 3 * (to - from) < 2 * (d.end - d.begin)) { | |
274 memmove(d.array.ptr + from, d.array.ptr + from + 1, (to - from) * (void*).sizeof); | |
275 } else { | |
276 // optimization | |
277 if (int offset = from - d.begin) | |
278 memmove(d.array.ptr + d.begin + 1, d.array.ptr + d.begin, offset * (void*).sizeof); | |
279 if (int offset = d.end - (to + 1)) | |
280 memmove(d.array.ptr + to + 2, d.array.ptr + to + 1, offset * (void*).sizeof); | |
281 ++d.begin; | |
282 ++d.end; | |
283 ++to; | |
284 } | |
285 } else { | |
286 if (d.begin == 0 || 3 * (from - to) < 2 * (d.end - d.begin)) { | |
287 memmove(d.array.ptr + to + 1, d.array.ptr + to, (from - to) * (void*).sizeof); | |
288 } else { | |
289 // optimization | |
290 if (int offset = to - d.begin) | |
291 memmove(d.array.ptr + d.begin - 1, d.array.ptr + d.begin, offset * (void*).sizeof); | |
292 if (int offset = d.end - (from + 1)) | |
293 memmove(d.array.ptr + from, d.array.ptr + from + 1, offset * (void*).sizeof); | |
294 --d.begin; | |
295 --d.end; | |
296 --to; | |
297 } | |
298 } | |
299 d.array.ptr[to] = t; | |
300 } | |
301 | |
302 void **erase(void **xi) | |
303 { | |
304 // Q_ASSERT(d.ref_ == 1); | |
305 int i = xi - (d.array.ptr + d.begin); | |
306 remove(i); | |
307 return d.array.ptr + d.begin + i; | |
308 } | |
309 | |
310 int size() const { return d.end - d.begin; } | |
311 bool isEmpty() const { return d.end == d.begin; } | |
312 const (void*)* at(int i) const { return d.array.ptr + d.begin + i; } | |
313 const (void*)* begin() const { return d.array.ptr + d.begin; } | |
314 const (void*)* end() const { return d.array.ptr + d.end; } | |
315 } | |
316 | |
317 import std.stdio; | |
298
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
318 import std.conv; |
291 | 319 |
295
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
320 alias void Dummy; // DMD bug #3538 |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
321 |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
322 struct QList(T, alias Default = Dummy) |
291 | 323 { |
295
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
324 static if (is(Default == Dummy)) |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
325 alias QTypeInfo!T TI; |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
326 else |
298
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
327 alias Default TI; |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
328 |
291 | 329 struct Node |
330 { | |
331 void *v; | |
292 | 332 |
296 | 333 static if (isQObjectType!T || isObjectType!T || isValueType!T || is(T == string)) // binded Qt types |
292 | 334 { |
335 T t() | |
336 { | |
296 | 337 static if(is(T == string)) |
338 { | |
339 void* ptr = cast(void*)(TI.isLarge || TI.isStatic ? v : &this); | |
302 | 340 return QStringUtil.toNativeString(ptr); |
296 | 341 } |
342 else static if (isValueType!T) | |
293 | 343 { |
344 void* ptr = cast(void*)(isLarge!T() || isStatic!T() ? v : &this); | |
345 return new T(ptr, QtdObjectFlags.nativeOwnership); | |
346 } | |
347 else | |
348 { | |
349 return T.__getObject( *cast(void**)(&this) ); | |
350 } | |
292 | 351 } |
352 } | |
293 | 353 else // native types |
292 | 354 { |
355 ref T t() | |
356 { | |
295
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
357 static if(TI.isLarge || TI.isStatic) |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
358 return *cast(T*)(v); |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
359 else |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
360 return *cast(T*)(&this); |
292 | 361 } |
362 } | |
291 | 363 } |
364 | |
365 union { | |
366 QListData p; | |
367 QListData.Data* d; | |
368 } | |
369 | |
370 public: | |
371 void output() | |
372 { | |
373 writeln("QList atomic ", d.ref_.load()); | |
374 } | |
375 | |
376 static QList!T opCall() | |
377 { | |
378 QList!T res; | |
379 writeln("QList opCall"); | |
380 | |
381 res.d = &QListData.shared_null; | |
382 res.d.ref_.increment(); | |
383 | |
384 return res; | |
385 } | |
386 | |
387 this(this) | |
388 { | |
389 writeln("QList postblit"); | |
390 d.ref_.increment(); | |
391 if (!d.sharable) | |
392 detach_helper(); | |
393 } | |
394 | |
395 ~this() | |
396 { | |
397 writeln("QList ~this"); | |
398 if (d && !d.ref_.decrement()) | |
399 free(d); | |
400 } | |
401 | |
402 ref QList!T opAssign(const ref QList!T l) | |
403 { | |
404 writeln("QList opAssign"); | |
405 if (d != l.d) { | |
406 l.d.ref_.increment(); | |
407 if (!d.ref_.decrement()) | |
408 free(d); | |
409 d = cast(QListData.Data*)l.d; | |
410 if (!d.sharable) | |
411 detach_helper(); | |
412 } | |
413 return this; | |
414 } | |
415 | |
292 | 416 int length() const { return p.size(); } |
417 int size() const { return length; } | |
418 | |
291 | 419 void detach() { if (d.ref_.load() != 1) detach_helper(); } |
420 | |
421 private void detach_helper() | |
422 { | |
423 Node *n = cast(Node*)(p.begin()); | |
424 QListData.Data* x = p.detach2(); | |
425 node_copy(cast(Node*)(p.begin()), cast(Node*)(p.end()), n); | |
426 if (!x.ref_.decrement()) | |
427 free(x); | |
428 } | |
429 | |
430 void append(const T t) // fix to const ref for complex types TODO | |
431 { | |
432 detach(); | |
293 | 433 static if (isQObjectType!T || isObjectType!T || isValueType!T) |
291 | 434 { |
435 node_construct(cast(Node*)(p.append()), t); | |
436 } | |
292 | 437 else |
291 | 438 { |
439 const T cpy = t; | |
440 node_construct(cast(Node*)(p.append()), cpy); | |
441 } | |
442 } | |
318 | 443 |
444 alias append opCatAssign; | |
445 | |
296 | 446 static if (isQObjectType!T || isObjectType!T || isValueType!T || is(T == string)) |
291 | 447 { |
292 | 448 T at(int i) const |
449 { | |
450 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range"); | |
451 return (cast(Node*)(p.at(i))).t(); | |
452 } | |
296 | 453 T opIndex(int i) |
454 { | |
455 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range"); | |
456 return (cast(Node*)(p.at(i))).t(); | |
457 } | |
291 | 458 } |
292 | 459 else |
291 | 460 { |
299 | 461 const (T) at(int i) const // DMD BUG |
292 | 462 { |
463 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range"); | |
464 return (cast(Node*)(p.at(i))).t(); | |
465 } | |
296 | 466 ref T opIndex(int i) |
467 { | |
468 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range"); | |
469 return (cast(Node*)(p.at(i))).t(); | |
470 } | |
292 | 471 } |
472 | |
293 | 473 static if (isQObjectType!T || isObjectType!T || isValueType!T) //binded types |
474 void node_construct(Node *n, const T t) | |
475 { | |
476 static if (isValueType!T) | |
477 { | |
478 if (isLarge!T() || isStatic!T()) // TODO should be static if | |
294 | 479 n.v = T.__constructNativeCopy(t.__nativeId); // n.v = new T(t); |
293 | 480 else if (isComplex!T()) |
294 | 481 T.__constructPlacedNativeCopy(t.__nativeId, n); // new (n) T(t); |
293 | 482 else |
294 | 483 T.__constructPlacedNativeCopy(t.__nativeId, n); // TODO should be *cast(T*)(n) = cast(T)(t); as it is a primitive type. fix when they are implemented with structs |
293 | 484 } |
485 else // in case of QObject or Object Qt types we place a pointer to a native object in the node | |
486 n = cast(Node*) t.__nativeId; | |
487 } | |
296 | 488 else static if (is(T == string)) |
489 { | |
490 void node_construct(Node *n, T t) | |
491 { | |
492 QString.__constructPlacedQString(n, t); | |
493 } | |
494 } | |
293 | 495 else // native types |
292 | 496 void node_construct(Node *n, const ref T t) |
497 { | |
296 | 498 static if (TI.isLarge || TI.isStatic) |
295
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
499 n.v = q_new!T(t); // n.v = new T(t); |
296 | 500 else static if (TI.isComplex) |
295
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
501 q_new_at(n, t); // new (n) T(t); |
296 | 502 else |
293 | 503 *cast(T*)(n) = cast(T)(t); |
292 | 504 } |
291 | 505 |
506 void node_copy(Node *from, Node *to, Node *src) | |
507 { | |
294 | 508 writeln("QList node_copy"); |
295
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
509 static if (isQObjectType!T || isObjectType!T) |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
510 {} // ensure to do nothing. copying only a pointer |
296 | 511 else static if(is(T == string)) |
512 { | |
513 while(from != to) // TODO when porting to Qt 5 ensure that QTypeInfo<QString>.isLarge and .isStatic == false | |
514 QString.__constructPlacedNativeCopy(src++, from++); // new (from++) T(*reinterpret_cast<T*>(src++)); | |
515 } | |
295
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
516 else static if (isValueType!T) |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
517 { |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
518 if (TI.isLarge || TI.isStatic) // TODO should be static if |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
519 while(from != to) |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
520 (from++).v = T.__constructNativeCopy((src++).v); // (from++)->v = new T(*reinterpret_cast<T*>((src++)->v)); |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
521 else if (TI.isComplex) |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
522 while(from != to) |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
523 T.__constructPlacedNativeCopy(src++, from++); // new (from++) T(*reinterpret_cast<T*>(src++)); |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
524 } |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
525 else static if (TI.isLarge || TI.isStatic) |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
526 while(from != to) |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
527 (from++).v = q_new!T(*cast(T*)((src++).v)); |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
528 else static if (TI.isComplex) |
291 | 529 while(from != to) |
295
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
530 q_new_at(from++, *cast(T*)(src++)); |
291 | 531 } |
297 | 532 |
298
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
533 T[] toArray() |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
534 { |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
535 T[] res; |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
536 res.length = this.length; |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
537 for(int i = 0; i < res.length; ++i) |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
538 { |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
539 static if (isValueType!T) |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
540 res[i] = new T(T.__constructNativeCopy(this.at(i).__nativeId)); // Node should probably provide a ptr method to directly extract pointer to the native value stored in the list to avoid creating a dummy D object in t() |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
541 else |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
542 res[i] = this.opIndex(i); |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
543 } |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
544 return res; |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
545 } |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
546 |
291 | 547 void free(QListData.Data* data) |
548 { | |
292 | 549 writeln("QList data destroyed"); |
291 | 550 node_destruct(cast(Node*)(data.array.ptr + data.begin), |
551 cast(Node*)(data.array.ptr + data.end)); | |
552 if (data.ref_.load() == 0) | |
293 | 553 qFree(data); |
291 | 554 } |
555 | |
556 void node_destruct(Node *from, Node *to) | |
293 | 557 { |
558 static if (isQObjectType!T || isObjectType!T) //binded types | |
559 {} // removing just pointers, do nothing | |
296 | 560 else static if (is(T == string)) |
561 { | |
562 while (from != to) | |
563 --to, QString.__callNativeDestructor(to); | |
564 } | |
565 else static if (isValueType!T) //binded value types | |
293 | 566 { |
567 if (isLarge!T() || isStatic!T()) // TODO should be static if | |
568 while (from != to) | |
294 | 569 --to, T.__deleteNativeObject(to.v); |
570 else if (isComplex!T()) | |
571 while (from != to) | |
572 --to, T.__callNativeDestructor(to); | |
293 | 573 } |
574 else | |
295
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
575 { |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
576 static if (TI.isLarge || TI.isStatic) |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
577 while (from != to) --to, q_delete(cast(T*)(to.v)); |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
578 else static if (TI.isComplex) |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
579 while (from != to) --to, cast(T*)(to).__dtor(); |
293 | 580 } |
291 | 581 } |
296 | 582 |
583 //iteration support | |
584 int opApply(int delegate(ref T) dg) | |
585 { | |
586 int result = 0; | |
587 int sz = this.length; | |
588 for (int i = 0; i < sz; i++) | |
589 { | |
590 static if (isQtReference!T) | |
591 { | |
592 T t = this[i]; // hack to avoid "is not an lvalue" error, since dg accepts ref T | |
593 result = dg(t); | |
594 } | |
595 else | |
596 result = dg(this[i]); | |
597 | |
598 if (result) | |
599 break; | |
600 } | |
601 return result; | |
602 } | |
291 | 603 } |
604 | |
299 | 605 alias QList!string QStringList; |
606 | |
607 QList!T toQList(T)(T[] src) | |
608 { | |
609 auto res = QList!T.opCall(); | |
610 foreach(elem; src) | |
611 res.append(elem); | |
612 return res; | |
613 } | |
614 | |
615 QList!T qList(T)() | |
616 { | |
617 return QList!T.opCall(); | |
618 } | |
619 | |
291 | 620 extern(C) void qtd_create_QList(void *nativeId); |
295
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
621 extern(C) void qtd_create_QList_double(void *nativeId); |
463563fc9e17
more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents:
294
diff
changeset
|
622 |
292 | 623 extern(C) void qtd_create_QList_QObject(void *nativeId); |