Mercurial > projects > qtd
annotate qt/core/QList.d @ 302:55ee4603365d signals
string arguments for signals
author | eldar_ins@eldar-laptop |
---|---|
date | Sat, 12 Dec 2009 03:22:54 +0500 |
parents | 87db643519a4 |
children | ce07227f00c1 |
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 } | |
292 | 443 |
296 | 444 static if (isQObjectType!T || isObjectType!T || isValueType!T || is(T == string)) |
291 | 445 { |
292 | 446 T at(int i) const |
447 { | |
448 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range"); | |
449 return (cast(Node*)(p.at(i))).t(); | |
450 } | |
296 | 451 T opIndex(int i) |
452 { | |
453 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range"); | |
454 return (cast(Node*)(p.at(i))).t(); | |
455 } | |
291 | 456 } |
292 | 457 else |
291 | 458 { |
299 | 459 const (T) at(int i) const // DMD BUG |
292 | 460 { |
461 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range"); | |
462 return (cast(Node*)(p.at(i))).t(); | |
463 } | |
296 | 464 ref T opIndex(int i) |
465 { | |
466 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range"); | |
467 return (cast(Node*)(p.at(i))).t(); | |
468 } | |
292 | 469 } |
470 | |
293 | 471 static if (isQObjectType!T || isObjectType!T || isValueType!T) //binded types |
472 void node_construct(Node *n, const T t) | |
473 { | |
474 static if (isValueType!T) | |
475 { | |
476 if (isLarge!T() || isStatic!T()) // TODO should be static if | |
294 | 477 n.v = T.__constructNativeCopy(t.__nativeId); // n.v = new T(t); |
293 | 478 else if (isComplex!T()) |
294 | 479 T.__constructPlacedNativeCopy(t.__nativeId, n); // new (n) T(t); |
293 | 480 else |
294 | 481 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 | 482 } |
483 else // in case of QObject or Object Qt types we place a pointer to a native object in the node | |
484 n = cast(Node*) t.__nativeId; | |
485 } | |
296 | 486 else static if (is(T == string)) |
487 { | |
488 void node_construct(Node *n, T t) | |
489 { | |
490 QString.__constructPlacedQString(n, t); | |
491 } | |
492 } | |
293 | 493 else // native types |
292 | 494 void node_construct(Node *n, const ref T t) |
495 { | |
296 | 496 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
|
497 n.v = q_new!T(t); // n.v = new T(t); |
296 | 498 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
|
499 q_new_at(n, t); // new (n) T(t); |
296 | 500 else |
293 | 501 *cast(T*)(n) = cast(T)(t); |
292 | 502 } |
291 | 503 |
504 void node_copy(Node *from, Node *to, Node *src) | |
505 { | |
294 | 506 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
|
507 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
|
508 {} // ensure to do nothing. copying only a pointer |
296 | 509 else static if(is(T == string)) |
510 { | |
511 while(from != to) // TODO when porting to Qt 5 ensure that QTypeInfo<QString>.isLarge and .isStatic == false | |
512 QString.__constructPlacedNativeCopy(src++, from++); // new (from++) T(*reinterpret_cast<T*>(src++)); | |
513 } | |
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
|
514 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
|
515 { |
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 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
|
517 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
|
518 (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
|
519 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
|
520 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
|
521 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
|
522 } |
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 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
|
524 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
|
525 (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
|
526 else static if (TI.isComplex) |
291 | 527 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
|
528 q_new_at(from++, *cast(T*)(src++)); |
291 | 529 } |
297 | 530 |
298
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
531 T[] toArray() |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
532 { |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
533 T[] res; |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
534 res.length = this.length; |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
535 for(int i = 0; i < res.length; ++i) |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
536 { |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
537 static if (isValueType!T) |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
538 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
|
539 else |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
540 res[i] = this.opIndex(i); |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
541 } |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
542 return res; |
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 |
291 | 545 void free(QListData.Data* data) |
546 { | |
292 | 547 writeln("QList data destroyed"); |
291 | 548 node_destruct(cast(Node*)(data.array.ptr + data.begin), |
549 cast(Node*)(data.array.ptr + data.end)); | |
550 if (data.ref_.load() == 0) | |
293 | 551 qFree(data); |
291 | 552 } |
553 | |
554 void node_destruct(Node *from, Node *to) | |
293 | 555 { |
556 static if (isQObjectType!T || isObjectType!T) //binded types | |
557 {} // removing just pointers, do nothing | |
296 | 558 else static if (is(T == string)) |
559 { | |
560 while (from != to) | |
561 --to, QString.__callNativeDestructor(to); | |
562 } | |
563 else static if (isValueType!T) //binded value types | |
293 | 564 { |
565 if (isLarge!T() || isStatic!T()) // TODO should be static if | |
566 while (from != to) | |
294 | 567 --to, T.__deleteNativeObject(to.v); |
568 else if (isComplex!T()) | |
569 while (from != to) | |
570 --to, T.__callNativeDestructor(to); | |
293 | 571 } |
572 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
|
573 { |
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
|
574 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
|
575 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
|
576 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
|
577 while (from != to) --to, cast(T*)(to).__dtor(); |
293 | 578 } |
291 | 579 } |
296 | 580 |
581 //iteration support | |
582 int opApply(int delegate(ref T) dg) | |
583 { | |
584 int result = 0; | |
585 int sz = this.length; | |
586 for (int i = 0; i < sz; i++) | |
587 { | |
588 static if (isQtReference!T) | |
589 { | |
590 T t = this[i]; // hack to avoid "is not an lvalue" error, since dg accepts ref T | |
591 result = dg(t); | |
592 } | |
593 else | |
594 result = dg(this[i]); | |
595 | |
596 if (result) | |
597 break; | |
598 } | |
599 return result; | |
600 } | |
291 | 601 } |
602 | |
299 | 603 alias QList!string QStringList; |
604 | |
605 QList!T toQList(T)(T[] src) | |
606 { | |
607 auto res = QList!T.opCall(); | |
608 foreach(elem; src) | |
609 res.append(elem); | |
610 return res; | |
611 } | |
612 | |
613 QList!T qList(T)() | |
614 { | |
615 return QList!T.opCall(); | |
616 } | |
617 | |
291 | 618 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
|
619 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
|
620 |
292 | 621 extern(C) void qtd_create_QList_QObject(void *nativeId); |