Mercurial > projects > qtd
annotate qt/core/QList.d @ 330:115130499d78 signals
fix a const violation spotted by a newer dmd, thanks Rainer
author | eldar1@eldar1-laptop |
---|---|
date | Sat, 16 Jan 2010 12:20:50 +0000 |
parents | 7a3c43424dca |
children | 5896535a03cd |
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: | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
318
diff
changeset
|
371 /* |
291 | 372 void output() |
373 { | |
374 writeln("QList atomic ", d.ref_.load()); | |
375 } | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
318
diff
changeset
|
376 */ |
291 | 377 |
378 static QList!T opCall() | |
379 { | |
380 QList!T res; | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
318
diff
changeset
|
381 // writeln("QList opCall"); |
291 | 382 |
383 res.d = &QListData.shared_null; | |
384 res.d.ref_.increment(); | |
385 | |
386 return res; | |
387 } | |
388 | |
389 this(this) | |
390 { | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
318
diff
changeset
|
391 // writeln("QList postblit"); |
291 | 392 d.ref_.increment(); |
393 if (!d.sharable) | |
394 detach_helper(); | |
395 } | |
396 | |
397 ~this() | |
398 { | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
318
diff
changeset
|
399 // writeln("QList ~this"); |
291 | 400 if (d && !d.ref_.decrement()) |
401 free(d); | |
402 } | |
403 | |
404 ref QList!T opAssign(const ref QList!T l) | |
405 { | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
318
diff
changeset
|
406 // writeln("QList opAssign"); |
291 | 407 if (d != l.d) { |
330
115130499d78
fix a const violation spotted by a newer dmd, thanks Rainer
eldar1@eldar1-laptop
parents:
323
diff
changeset
|
408 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
|
409 nd.ref_.increment(); |
291 | 410 if (!d.ref_.decrement()) |
411 free(d); | |
330
115130499d78
fix a const violation spotted by a newer dmd, thanks Rainer
eldar1@eldar1-laptop
parents:
323
diff
changeset
|
412 d = nd; |
291 | 413 if (!d.sharable) |
414 detach_helper(); | |
415 } | |
416 return this; | |
417 } | |
418 | |
292 | 419 int length() const { return p.size(); } |
420 int size() const { return length; } | |
421 | |
291 | 422 void detach() { if (d.ref_.load() != 1) detach_helper(); } |
423 | |
424 private void detach_helper() | |
425 { | |
426 Node *n = cast(Node*)(p.begin()); | |
427 QListData.Data* x = p.detach2(); | |
428 node_copy(cast(Node*)(p.begin()), cast(Node*)(p.end()), n); | |
429 if (!x.ref_.decrement()) | |
430 free(x); | |
431 } | |
432 | |
433 void append(const T t) // fix to const ref for complex types TODO | |
434 { | |
435 detach(); | |
293 | 436 static if (isQObjectType!T || isObjectType!T || isValueType!T) |
291 | 437 { |
438 node_construct(cast(Node*)(p.append()), t); | |
439 } | |
292 | 440 else |
291 | 441 { |
442 const T cpy = t; | |
443 node_construct(cast(Node*)(p.append()), cpy); | |
444 } | |
445 } | |
318 | 446 |
447 alias append opCatAssign; | |
448 | |
296 | 449 static if (isQObjectType!T || isObjectType!T || isValueType!T || is(T == string)) |
291 | 450 { |
292 | 451 T at(int i) const |
452 { | |
453 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range"); | |
454 return (cast(Node*)(p.at(i))).t(); | |
455 } | |
296 | 456 T opIndex(int i) |
457 { | |
458 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range"); | |
459 return (cast(Node*)(p.at(i))).t(); | |
460 } | |
291 | 461 } |
292 | 462 else |
291 | 463 { |
299 | 464 const (T) at(int i) const // DMD BUG |
292 | 465 { |
466 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range"); | |
467 return (cast(Node*)(p.at(i))).t(); | |
468 } | |
296 | 469 ref T opIndex(int i) |
470 { | |
471 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range"); | |
472 return (cast(Node*)(p.at(i))).t(); | |
473 } | |
292 | 474 } |
475 | |
293 | 476 static if (isQObjectType!T || isObjectType!T || isValueType!T) //binded types |
477 void node_construct(Node *n, const T t) | |
478 { | |
479 static if (isValueType!T) | |
480 { | |
481 if (isLarge!T() || isStatic!T()) // TODO should be static if | |
294 | 482 n.v = T.__constructNativeCopy(t.__nativeId); // n.v = new T(t); |
293 | 483 else if (isComplex!T()) |
294 | 484 T.__constructPlacedNativeCopy(t.__nativeId, n); // new (n) T(t); |
293 | 485 else |
294 | 486 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 | 487 } |
488 else // in case of QObject or Object Qt types we place a pointer to a native object in the node | |
489 n = cast(Node*) t.__nativeId; | |
490 } | |
296 | 491 else static if (is(T == string)) |
492 { | |
493 void node_construct(Node *n, T t) | |
494 { | |
495 QString.__constructPlacedQString(n, t); | |
496 } | |
497 } | |
293 | 498 else // native types |
292 | 499 void node_construct(Node *n, const ref T t) |
500 { | |
296 | 501 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
|
502 n.v = q_new!T(t); // n.v = new T(t); |
296 | 503 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
|
504 q_new_at(n, t); // new (n) T(t); |
296 | 505 else |
293 | 506 *cast(T*)(n) = cast(T)(t); |
292 | 507 } |
291 | 508 |
509 void node_copy(Node *from, Node *to, Node *src) | |
510 { | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
318
diff
changeset
|
511 // 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
|
512 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
|
513 {} // ensure to do nothing. copying only a pointer |
296 | 514 else static if(is(T == string)) |
515 { | |
516 while(from != to) // TODO when porting to Qt 5 ensure that QTypeInfo<QString>.isLarge and .isStatic == false | |
517 QString.__constructPlacedNativeCopy(src++, from++); // new (from++) T(*reinterpret_cast<T*>(src++)); | |
518 } | |
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
|
519 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
|
520 { |
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 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
|
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 (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
|
524 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
|
525 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
|
526 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
|
527 } |
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.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
|
529 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
|
530 (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
|
531 else static if (TI.isComplex) |
291 | 532 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
|
533 q_new_at(from++, *cast(T*)(src++)); |
291 | 534 } |
297 | 535 |
298
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
536 T[] toArray() |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
537 { |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
538 T[] res; |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
539 res.length = this.length; |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
540 for(int i = 0; i < res.length; ++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 static if (isValueType!T) |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
543 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
|
544 else |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
545 res[i] = this.opIndex(i); |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
546 } |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
547 return res; |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
548 } |
adae77fdc1ea
Native QList implementation is now used throughout QtD
eldar
parents:
297
diff
changeset
|
549 |
291 | 550 void free(QListData.Data* data) |
551 { | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
318
diff
changeset
|
552 // writeln("QList data destroyed"); |
291 | 553 node_destruct(cast(Node*)(data.array.ptr + data.begin), |
554 cast(Node*)(data.array.ptr + data.end)); | |
555 if (data.ref_.load() == 0) | |
293 | 556 qFree(data); |
291 | 557 } |
558 | |
559 void node_destruct(Node *from, Node *to) | |
293 | 560 { |
561 static if (isQObjectType!T || isObjectType!T) //binded types | |
562 {} // removing just pointers, do nothing | |
296 | 563 else static if (is(T == string)) |
564 { | |
565 while (from != to) | |
566 --to, QString.__callNativeDestructor(to); | |
567 } | |
568 else static if (isValueType!T) //binded value types | |
293 | 569 { |
570 if (isLarge!T() || isStatic!T()) // TODO should be static if | |
571 while (from != to) | |
294 | 572 --to, T.__deleteNativeObject(to.v); |
573 else if (isComplex!T()) | |
574 while (from != to) | |
575 --to, T.__callNativeDestructor(to); | |
293 | 576 } |
577 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
|
578 { |
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 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
|
580 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
|
581 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
|
582 while (from != to) --to, cast(T*)(to).__dtor(); |
293 | 583 } |
291 | 584 } |
296 | 585 |
586 //iteration support | |
587 int opApply(int delegate(ref T) dg) | |
588 { | |
589 int result = 0; | |
590 int sz = this.length; | |
591 for (int i = 0; i < sz; i++) | |
592 { | |
593 static if (isQtReference!T) | |
594 { | |
595 T t = this[i]; // hack to avoid "is not an lvalue" error, since dg accepts ref T | |
596 result = dg(t); | |
597 } | |
598 else | |
599 result = dg(this[i]); | |
600 | |
601 if (result) | |
602 break; | |
603 } | |
604 return result; | |
605 } | |
291 | 606 } |
607 | |
299 | 608 alias QList!string QStringList; |
609 | |
610 QList!T toQList(T)(T[] src) | |
611 { | |
612 auto res = QList!T.opCall(); | |
613 foreach(elem; src) | |
614 res.append(elem); | |
615 return res; | |
616 } | |
617 | |
618 QList!T qList(T)() | |
619 { | |
620 return QList!T.opCall(); | |
621 } | |
622 | |
291 | 623 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
|
624 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
|
625 |
330
115130499d78
fix a const violation spotted by a newer dmd, thanks Rainer
eldar1@eldar1-laptop
parents:
323
diff
changeset
|
626 extern(C) void qtd_create_QList_QObject(void *nativeId); |