Mercurial > projects > qtd
annotate d1/qt/core/QList.d @ 344:96a75b1e5b26
project structure changes
author | Max Samukha <maxter@spambox.com> |
---|---|
date | Fri, 14 May 2010 12:14:37 +0300 |
parents | qt/core/QList.d@5896535a03cd |
children |
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 { | |
337 | 94 private: |
291 | 95 struct Data { |
96 Atomic!int ref_; | |
97 int alloc, begin, end; | |
98 uint sharable; | |
99 void*[1] array; | |
100 } | |
101 | |
102 enum { DataHeaderSize = Data.sizeof - (void*).sizeof } | |
103 | |
104 static Data shared_null; | |
105 Data *d; | |
106 | |
107 static this() | |
108 { | |
109 shared_null = Data(Atomic!int(1), 0, 0, 0, true, [null]); | |
110 } | |
111 | |
112 | |
113 // Data *detach(); // remove in 5.0 | |
114 | |
115 Data* detach2() | |
116 { | |
117 Data* x = d; | |
118 d = cast(Data*)(qMalloc(DataHeaderSize + x.alloc * (void*).sizeof)); | |
119 if (!d) | |
120 qFatal("QList: Out of memory"); | |
121 | |
122 memcpy(d, x, DataHeaderSize + x.alloc * (void*).sizeof); | |
123 d.alloc = x.alloc; | |
124 d.ref_.store(1); | |
125 d.sharable = true; | |
126 if (!d.alloc) | |
127 d.begin = d.end = 0; | |
128 | |
129 return x; | |
130 } | |
131 | |
132 void realloc(int alloc) | |
133 { | |
134 // assert(d.ref_ == 1); | |
135 Data* x = cast(Data*)(qRealloc(d, DataHeaderSize + alloc * (void*).sizeof)); | |
136 if (!x) | |
137 qFatal("QList: Out of memory"); | |
138 | |
139 d = x; | |
140 d.alloc = alloc; | |
141 if (!alloc) | |
142 d.begin = d.end = 0; | |
143 } | |
144 | |
145 void** append() | |
146 { | |
147 // #TODO Q_ASSERT(d.ref_ == 1); | |
148 if (d.end == d.alloc) { | |
149 int n = d.end - d.begin; | |
150 if (d.begin > 2 * d.alloc / 3) { | |
151 memcpy(d.array.ptr + n, d.array.ptr + d.begin, n * (void*).sizeof); | |
152 d.begin = n; | |
153 d.end = n * 2; | |
154 } else { | |
155 realloc(grow(d.alloc + 1)); | |
156 } | |
157 } | |
158 return d.array.ptr + d.end++; | |
159 } | |
160 | |
161 void **append(const ref QListData l) | |
162 { | |
163 // Q_ASSERT(d.ref_ == 1); | |
164 int e = d.end; | |
165 int n = l.d.end - l.d.begin; | |
166 if (n) { | |
167 if (e + n > d.alloc) | |
168 realloc(grow(e + l.d.end - l.d.begin)); | |
169 memcpy(d.array.ptr + d.end, l.d.array.ptr + l.d.begin, n * (void*).sizeof); | |
170 d.end += n; | |
171 } | |
172 return d.array.ptr + e; | |
173 } | |
174 | |
175 void **prepend() | |
176 { | |
177 // Q_ASSERT(d.ref_ == 1); | |
178 if (d.begin == 0) { | |
179 if (d.end >= d.alloc / 3) | |
180 realloc(grow(d.alloc + 1)); | |
181 | |
182 if (d.end < d.alloc / 3) | |
183 d.begin = d.alloc - 2 * d.end; | |
184 else | |
185 d.begin = d.alloc - d.end; | |
186 | |
187 memmove(d.array.ptr + d.begin, d.array.ptr, d.end * (void*).sizeof); | |
188 d.end += d.begin; | |
189 } | |
190 return d.array.ptr + --d.begin; | |
191 } | |
192 | |
193 void **insert(int i) | |
194 { | |
195 // Q_ASSERT(d.ref_ == 1); | |
196 if (i <= 0) | |
197 return prepend(); | |
198 if (i >= d.end - d.begin) | |
199 return append(); | |
200 | |
201 bool leftward = false; | |
202 int size = d.end - d.begin; | |
203 | |
204 if (d.begin == 0) { | |
205 if (d.end == d.alloc) { | |
206 // If the array is full, we expand it and move some items rightward | |
207 realloc(grow(d.alloc + 1)); | |
208 } else { | |
209 // If there is free space at the end of the array, we move some items rightward | |
210 } | |
211 } else { | |
212 if (d.end == d.alloc) { | |
213 // If there is free space at the beginning of the array, we move some items leftward | |
214 leftward = true; | |
215 } else { | |
216 // If there is free space at both ends, we move as few items as possible | |
217 leftward = (i < size - i); | |
218 } | |
219 } | |
220 | |
221 if (leftward) { | |
222 --d.begin; | |
223 memmove(d.array.ptr + d.begin, d.array.ptr + d.begin + 1, i * (void*).sizeof); | |
224 } else { | |
225 memmove(d.array.ptr + d.begin + i + 1, d.array.ptr + d.begin + i, | |
226 (size - i) * (void*).sizeof); | |
227 ++d.end; | |
228 } | |
229 return d.array.ptr + d.begin + i; | |
230 } | |
231 | |
232 void remove(int i) | |
233 { | |
234 // Q_ASSERT(d.ref_ == 1); | |
235 i += d.begin; | |
236 if (i - d.begin < d.end - i) { | |
237 if (int offset = i - d.begin) | |
238 memmove(d.array.ptr + d.begin + 1, d.array.ptr + d.begin, offset * (void*).sizeof); | |
239 d.begin++; | |
240 } else { | |
241 if (int offset = d.end - i - 1) | |
242 memmove(d.array.ptr + i, d.array.ptr + i + 1, offset * (void*).sizeof); | |
243 d.end--; | |
244 } | |
245 } | |
246 | |
247 void remove(int i, int n) | |
248 { | |
249 // Q_ASSERT(d.ref_ == 1); | |
250 i += d.begin; | |
251 int middle = i + n/2; | |
252 if (middle - d.begin < d.end - middle) { | |
253 memmove(d.array.ptr + d.begin + n, d.array.ptr + d.begin, | |
254 (i - d.begin) * (void*).sizeof); | |
255 d.begin += n; | |
256 } else { | |
257 memmove(d.array.ptr + i, d.array.ptr + i + n, | |
258 (d.end - i - n) * (void*).sizeof); | |
259 d.end -= n; | |
260 } | |
261 } | |
262 | |
263 void move(int from, int to) | |
264 { | |
265 // Q_ASSERT(d.ref_ == 1); | |
266 if (from == to) | |
267 return; | |
268 | |
269 from += d.begin; | |
270 to += d.begin; | |
271 void *t = d.array.ptr[from]; | |
272 | |
273 if (from < to) { | |
274 if (d.end == d.alloc || 3 * (to - from) < 2 * (d.end - d.begin)) { | |
275 memmove(d.array.ptr + from, d.array.ptr + from + 1, (to - from) * (void*).sizeof); | |
276 } else { | |
277 // optimization | |
278 if (int offset = from - d.begin) | |
279 memmove(d.array.ptr + d.begin + 1, d.array.ptr + d.begin, offset * (void*).sizeof); | |
280 if (int offset = d.end - (to + 1)) | |
281 memmove(d.array.ptr + to + 2, d.array.ptr + to + 1, offset * (void*).sizeof); | |
282 ++d.begin; | |
283 ++d.end; | |
284 ++to; | |
285 } | |
286 } else { | |
287 if (d.begin == 0 || 3 * (from - to) < 2 * (d.end - d.begin)) { | |
288 memmove(d.array.ptr + to + 1, d.array.ptr + to, (from - to) * (void*).sizeof); | |
289 } else { | |
290 // optimization | |
291 if (int offset = to - d.begin) | |
292 memmove(d.array.ptr + d.begin - 1, d.array.ptr + d.begin, offset * (void*).sizeof); | |
293 if (int offset = d.end - (from + 1)) | |
294 memmove(d.array.ptr + from, d.array.ptr + from + 1, offset * (void*).sizeof); | |
295 --d.begin; | |
296 --d.end; | |
297 --to; | |
298 } | |
299 } | |
300 d.array.ptr[to] = t; | |
301 } | |
302 | |
303 void **erase(void **xi) | |
304 { | |
305 // Q_ASSERT(d.ref_ == 1); | |
306 int i = xi - (d.array.ptr + d.begin); | |
307 remove(i); | |
308 return d.array.ptr + d.begin + i; | |
309 } | |
310 | |
311 int size() const { return d.end - d.begin; } | |
312 bool isEmpty() const { return d.end == d.begin; } | |
313 const (void*)* at(int i) const { return d.array.ptr + d.begin + i; } | |
314 const (void*)* begin() const { return d.array.ptr + d.begin; } | |
315 const (void*)* end() const { return d.array.ptr + d.end; } | |
316 } | |
317 | |
318 import std.stdio; | |
298
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
319 import std.conv; |
291 | 320 |
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
|
321 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
|
322 |
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
|
323 struct QList(T, alias Default = Dummy) |
291 | 324 { |
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
|
325 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
|
326 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
|
327 else |
298
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
328 alias Default TI; |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
329 |
291 | 330 struct Node |
331 { | |
332 void *v; | |
292 | 333 |
296 | 334 static if (isQObjectType!T || isObjectType!T || isValueType!T || is(T == string)) // binded Qt types |
292 | 335 { |
336 T t() | |
337 { | |
296 | 338 static if(is(T == string)) |
339 { | |
340 void* ptr = cast(void*)(TI.isLarge || TI.isStatic ? v : &this); | |
302 | 341 return QStringUtil.toNativeString(ptr); |
296 | 342 } |
343 else static if (isValueType!T) | |
293 | 344 { |
345 void* ptr = cast(void*)(isLarge!T() || isStatic!T() ? v : &this); | |
346 return new T(ptr, QtdObjectFlags.nativeOwnership); | |
347 } | |
348 else | |
349 { | |
350 return T.__getObject( *cast(void**)(&this) ); | |
351 } | |
292 | 352 } |
353 } | |
293 | 354 else // native types |
292 | 355 { |
356 ref T t() | |
357 { | |
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
|
358 static if(TI.isLarge || TI.isStatic) |
337 | 359 return *cast(T*)(this.v); |
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
|
360 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
|
361 return *cast(T*)(&this); |
292 | 362 } |
363 } | |
291 | 364 } |
365 | |
366 union { | |
367 QListData p; | |
368 QListData.Data* d; | |
369 } | |
370 | |
371 public: | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
318
diff
changeset
|
372 /* |
291 | 373 void output() |
374 { | |
375 writeln("QList atomic ", d.ref_.load()); | |
376 } | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
318
diff
changeset
|
377 */ |
291 | 378 |
379 static QList!T opCall() | |
380 { | |
381 QList!T res; | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
318
diff
changeset
|
382 // writeln("QList opCall"); |
291 | 383 |
384 res.d = &QListData.shared_null; | |
385 res.d.ref_.increment(); | |
386 | |
387 return res; | |
388 } | |
389 | |
390 this(this) | |
391 { | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
318
diff
changeset
|
392 // writeln("QList postblit"); |
291 | 393 d.ref_.increment(); |
394 if (!d.sharable) | |
395 detach_helper(); | |
396 } | |
397 | |
398 ~this() | |
399 { | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
318
diff
changeset
|
400 // writeln("QList ~this"); |
291 | 401 if (d && !d.ref_.decrement()) |
402 free(d); | |
403 } | |
404 | |
405 ref QList!T opAssign(const ref QList!T l) | |
406 { | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
318
diff
changeset
|
407 // writeln("QList opAssign"); |
291 | 408 if (d != l.d) { |
330
115130499d78
fix a const violation spotted by a newer dmd, thanks Rainer
eldar1@eldar1-laptop
parents:
323
diff
changeset
|
409 QListData.Data* nd = cast(QListData.Data*)l.d; |
115130499d78
fix a const violation spotted by a newer dmd, thanks Rainer
eldar1@eldar1-laptop
parents:
323
diff
changeset
|
410 nd.ref_.increment(); |
291 | 411 if (!d.ref_.decrement()) |
412 free(d); | |
330
115130499d78
fix a const violation spotted by a newer dmd, thanks Rainer
eldar1@eldar1-laptop
parents:
323
diff
changeset
|
413 d = nd; |
291 | 414 if (!d.sharable) |
415 detach_helper(); | |
416 } | |
417 return this; | |
418 } | |
419 | |
292 | 420 int length() const { return p.size(); } |
421 int size() const { return length; } | |
422 | |
291 | 423 void detach() { if (d.ref_.load() != 1) detach_helper(); } |
424 | |
425 private void detach_helper() | |
426 { | |
427 Node *n = cast(Node*)(p.begin()); | |
428 QListData.Data* x = p.detach2(); | |
429 node_copy(cast(Node*)(p.begin()), cast(Node*)(p.end()), n); | |
430 if (!x.ref_.decrement()) | |
431 free(x); | |
432 } | |
433 | |
434 void append(const T t) // fix to const ref for complex types TODO | |
435 { | |
436 detach(); | |
293 | 437 static if (isQObjectType!T || isObjectType!T || isValueType!T) |
291 | 438 { |
439 node_construct(cast(Node*)(p.append()), t); | |
440 } | |
292 | 441 else |
291 | 442 { |
443 const T cpy = t; | |
444 node_construct(cast(Node*)(p.append()), cpy); | |
445 } | |
446 } | |
318 | 447 |
448 alias append opCatAssign; | |
449 | |
296 | 450 static if (isQObjectType!T || isObjectType!T || isValueType!T || is(T == string)) |
291 | 451 { |
292 | 452 T at(int i) const |
453 { | |
454 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range"); | |
455 return (cast(Node*)(p.at(i))).t(); | |
456 } | |
296 | 457 T opIndex(int i) |
458 { | |
459 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range"); | |
460 return (cast(Node*)(p.at(i))).t(); | |
461 } | |
291 | 462 } |
292 | 463 else |
291 | 464 { |
299 | 465 const (T) at(int i) const // DMD BUG |
292 | 466 { |
467 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range"); | |
468 return (cast(Node*)(p.at(i))).t(); | |
469 } | |
296 | 470 ref T opIndex(int i) |
471 { | |
472 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range"); | |
473 return (cast(Node*)(p.at(i))).t(); | |
474 } | |
292 | 475 } |
476 | |
293 | 477 static if (isQObjectType!T || isObjectType!T || isValueType!T) //binded types |
478 void node_construct(Node *n, const T t) | |
479 { | |
480 static if (isValueType!T) | |
481 { | |
482 if (isLarge!T() || isStatic!T()) // TODO should be static if | |
294 | 483 n.v = T.__constructNativeCopy(t.__nativeId); // n.v = new T(t); |
293 | 484 else if (isComplex!T()) |
294 | 485 T.__constructPlacedNativeCopy(t.__nativeId, n); // new (n) T(t); |
293 | 486 else |
294 | 487 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 | 488 } |
489 else // in case of QObject or Object Qt types we place a pointer to a native object in the node | |
490 n = cast(Node*) t.__nativeId; | |
491 } | |
296 | 492 else static if (is(T == string)) |
493 { | |
494 void node_construct(Node *n, T t) | |
495 { | |
496 QString.__constructPlacedQString(n, t); | |
497 } | |
498 } | |
293 | 499 else // native types |
292 | 500 void node_construct(Node *n, const ref T t) |
501 { | |
296 | 502 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
|
503 n.v = q_new!T(t); // n.v = new T(t); |
296 | 504 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
|
505 q_new_at(n, t); // new (n) T(t); |
296 | 506 else |
293 | 507 *cast(T*)(n) = cast(T)(t); |
292 | 508 } |
291 | 509 |
510 void node_copy(Node *from, Node *to, Node *src) | |
511 { | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
318
diff
changeset
|
512 // 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
|
513 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
|
514 {} // ensure to do nothing. copying only a pointer |
296 | 515 else static if(is(T == string)) |
516 { | |
517 while(from != to) // TODO when porting to Qt 5 ensure that QTypeInfo<QString>.isLarge and .isStatic == false | |
518 QString.__constructPlacedNativeCopy(src++, from++); // new (from++) T(*reinterpret_cast<T*>(src++)); | |
519 } | |
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
|
520 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
|
521 { |
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 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
|
523 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
|
524 (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
|
525 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
|
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 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
|
528 } |
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
|
529 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
|
530 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
|
531 (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
|
532 else static if (TI.isComplex) |
291 | 533 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
|
534 q_new_at(from++, *cast(T*)(src++)); |
291 | 535 } |
297 | 536 |
298
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
537 T[] toArray() |
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 T[] res; |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
540 res.length = this.length; |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
541 for(int i = 0; i < res.length; ++i) |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
542 { |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
543 static if (isValueType!T) |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
544 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
|
545 else |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
546 res[i] = this.opIndex(i); |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
547 } |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
548 return res; |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
549 } |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
550 |
291 | 551 void free(QListData.Data* data) |
552 { | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
318
diff
changeset
|
553 // writeln("QList data destroyed"); |
291 | 554 node_destruct(cast(Node*)(data.array.ptr + data.begin), |
555 cast(Node*)(data.array.ptr + data.end)); | |
556 if (data.ref_.load() == 0) | |
293 | 557 qFree(data); |
291 | 558 } |
559 | |
560 void node_destruct(Node *from, Node *to) | |
293 | 561 { |
562 static if (isQObjectType!T || isObjectType!T) //binded types | |
563 {} // removing just pointers, do nothing | |
296 | 564 else static if (is(T == string)) |
565 { | |
566 while (from != to) | |
567 --to, QString.__callNativeDestructor(to); | |
568 } | |
569 else static if (isValueType!T) //binded value types | |
293 | 570 { |
571 if (isLarge!T() || isStatic!T()) // TODO should be static if | |
572 while (from != to) | |
294 | 573 --to, T.__deleteNativeObject(to.v); |
574 else if (isComplex!T()) | |
575 while (from != to) | |
576 --to, T.__callNativeDestructor(to); | |
293 | 577 } |
578 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
|
579 { |
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
|
580 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
|
581 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
|
582 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
|
583 while (from != to) --to, cast(T*)(to).__dtor(); |
293 | 584 } |
291 | 585 } |
296 | 586 |
587 //iteration support | |
588 int opApply(int delegate(ref T) dg) | |
589 { | |
590 int result = 0; | |
591 int sz = this.length; | |
592 for (int i = 0; i < sz; i++) | |
593 { | |
594 static if (isQtReference!T) | |
595 { | |
596 T t = this[i]; // hack to avoid "is not an lvalue" error, since dg accepts ref T | |
597 result = dg(t); | |
598 } | |
599 else | |
600 result = dg(this[i]); | |
601 | |
602 if (result) | |
603 break; | |
604 } | |
605 return result; | |
606 } | |
291 | 607 } |
608 | |
299 | 609 alias QList!string QStringList; |
610 | |
611 QList!T toQList(T)(T[] src) | |
612 { | |
613 auto res = QList!T.opCall(); | |
614 foreach(elem; src) | |
615 res.append(elem); | |
616 return res; | |
617 } | |
618 | |
619 QList!T qList(T)() | |
620 { | |
621 return QList!T.opCall(); | |
622 } | |
623 | |
291 | 624 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
|
625 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
|
626 |
330
115130499d78
fix a const violation spotted by a newer dmd, thanks Rainer
eldar1@eldar1-laptop
parents:
323
diff
changeset
|
627 extern(C) void qtd_create_QList_QObject(void *nativeId); |