annotate qt/core/QList.d @ 296:5173835bb372 signals

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