OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkTArray_DEFINED | 8 #ifndef SkTArray_DEFINED |
9 #define SkTArray_DEFINED | 9 #define SkTArray_DEFINED |
10 | 10 |
(...skipping 14 matching lines...) Expand all Loading... |
25 inline void copy(SkTArray<T, true>* self, const T* array) { | 25 inline void copy(SkTArray<T, true>* self, const T* array) { |
26 memcpy(self->fMemArray, array, self->fCount * sizeof(T)); | 26 memcpy(self->fMemArray, array, self->fCount * sizeof(T)); |
27 } | 27 } |
28 template<typename T> | 28 template<typename T> |
29 inline void copyAndDelete(SkTArray<T, true>* self, char* newMemArray) { | 29 inline void copyAndDelete(SkTArray<T, true>* self, char* newMemArray) { |
30 memcpy(newMemArray, self->fMemArray, self->fCount * sizeof(T)); | 30 memcpy(newMemArray, self->fMemArray, self->fCount * sizeof(T)); |
31 } | 31 } |
32 | 32 |
33 template<typename T> | 33 template<typename T> |
34 inline void copy(SkTArray<T, false>* self, int dst, int src) { | 34 inline void copy(SkTArray<T, false>* self, int dst, int src) { |
35 SkNEW_PLACEMENT_ARGS(&self->fItemArray[dst], T, (self->fItemArray[src])); | 35 new (&self->fItemArray[dst]) T(self->fItemArray[src]); |
36 } | 36 } |
37 template<typename T> | 37 template<typename T> |
38 inline void copy(SkTArray<T, false>* self, const T* array) { | 38 inline void copy(SkTArray<T, false>* self, const T* array) { |
39 for (int i = 0; i < self->fCount; ++i) { | 39 for (int i = 0; i < self->fCount; ++i) { |
40 SkNEW_PLACEMENT_ARGS(self->fItemArray + i, T, (array[i])); | 40 new (self->fItemArray + i) T(array[i]); |
41 } | 41 } |
42 } | 42 } |
43 template<typename T> | 43 template<typename T> |
44 inline void copyAndDelete(SkTArray<T, false>* self, char* newMemArray) { | 44 inline void copyAndDelete(SkTArray<T, false>* self, char* newMemArray) { |
45 for (int i = 0; i < self->fCount; ++i) { | 45 for (int i = 0; i < self->fCount; ++i) { |
46 SkNEW_PLACEMENT_ARGS(newMemArray + sizeof(T) * i, T, (self->fItemArray[i
])); | 46 new (newMemArray + sizeof(T) * i) T(self->fItemArray[i]); |
47 self->fItemArray[i].~T(); | 47 self->fItemArray[i].~T(); |
48 } | 48 } |
49 } | 49 } |
50 | 50 |
51 } | 51 } |
52 | 52 |
53 template <typename T, bool MEM_COPY> void* operator new(size_t, SkTArray<T, MEM_
COPY>*, int); | 53 template <typename T, bool MEM_COPY> void* operator new(size_t, SkTArray<T, MEM_
COPY>*, int); |
54 | 54 |
55 /** When MEM_COPY is true T will be bit copied when moved. | 55 /** When MEM_COPY is true T will be bit copied when moved. |
56 When MEM_COPY is false, T will be copy constructed / destructed. | 56 When MEM_COPY is false, T will be copy constructed / destructed. |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 void reset(int n) { | 128 void reset(int n) { |
129 SkASSERT(n >= 0); | 129 SkASSERT(n >= 0); |
130 for (int i = 0; i < fCount; ++i) { | 130 for (int i = 0; i < fCount; ++i) { |
131 fItemArray[i].~T(); | 131 fItemArray[i].~T(); |
132 } | 132 } |
133 // set fCount to 0 before calling checkRealloc so that no copy cons. are
called. | 133 // set fCount to 0 before calling checkRealloc so that no copy cons. are
called. |
134 fCount = 0; | 134 fCount = 0; |
135 this->checkRealloc(n); | 135 this->checkRealloc(n); |
136 fCount = n; | 136 fCount = n; |
137 for (int i = 0; i < fCount; ++i) { | 137 for (int i = 0; i < fCount; ++i) { |
138 SkNEW_PLACEMENT(fItemArray + i, T); | 138 new (fItemArray + i) T; |
139 } | 139 } |
140 } | 140 } |
141 | 141 |
142 /** | 142 /** |
143 * Resets to a copy of a C array. | 143 * Resets to a copy of a C array. |
144 */ | 144 */ |
145 void reset(const T* array, int count) { | 145 void reset(const T* array, int count) { |
146 for (int i = 0; i < fCount; ++i) { | 146 for (int i = 0; i < fCount; ++i) { |
147 fItemArray[i].~T(); | 147 fItemArray[i].~T(); |
148 } | 148 } |
(...skipping 24 matching lines...) Expand all Loading... |
173 */ | 173 */ |
174 bool empty() const { return !fCount; } | 174 bool empty() const { return !fCount; } |
175 | 175 |
176 /** | 176 /** |
177 * Adds 1 new default-initialized T value and returns it by reference. Note | 177 * Adds 1 new default-initialized T value and returns it by reference. Note |
178 * the reference only remains valid until the next call that adds or removes | 178 * the reference only remains valid until the next call that adds or removes |
179 * elements. | 179 * elements. |
180 */ | 180 */ |
181 T& push_back() { | 181 T& push_back() { |
182 T* newT = reinterpret_cast<T*>(this->push_back_raw(1)); | 182 T* newT = reinterpret_cast<T*>(this->push_back_raw(1)); |
183 SkNEW_PLACEMENT(newT, T); | 183 new (newT) T; |
184 return *newT; | 184 return *newT; |
185 } | 185 } |
186 | 186 |
187 /** | 187 /** |
188 * Version of above that uses a copy constructor to initialize the new item | 188 * Version of above that uses a copy constructor to initialize the new item |
189 */ | 189 */ |
190 T& push_back(const T& t) { | 190 T& push_back(const T& t) { |
191 T* newT = reinterpret_cast<T*>(this->push_back_raw(1)); | 191 T* newT = reinterpret_cast<T*>(this->push_back_raw(1)); |
192 SkNEW_PLACEMENT_ARGS(newT, T, (t)); | 192 new (newT) T(t); |
193 return *newT; | 193 return *newT; |
194 } | 194 } |
195 | 195 |
196 /** | 196 /** |
197 * Allocates n more default-initialized T values, and returns the address of | 197 * Allocates n more default-initialized T values, and returns the address of |
198 * the start of that new range. Note: this address is only valid until the | 198 * the start of that new range. Note: this address is only valid until the |
199 * next API call made on the array that might add or remove elements. | 199 * next API call made on the array that might add or remove elements. |
200 */ | 200 */ |
201 T* push_back_n(int n) { | 201 T* push_back_n(int n) { |
202 SkASSERT(n >= 0); | 202 SkASSERT(n >= 0); |
203 T* newTs = reinterpret_cast<T*>(this->push_back_raw(n)); | 203 T* newTs = reinterpret_cast<T*>(this->push_back_raw(n)); |
204 for (int i = 0; i < n; ++i) { | 204 for (int i = 0; i < n; ++i) { |
205 SkNEW_PLACEMENT(newTs + i, T); | 205 new (newTs + i) T; |
206 } | 206 } |
207 return newTs; | 207 return newTs; |
208 } | 208 } |
209 | 209 |
210 /** | 210 /** |
211 * Version of above that uses a copy constructor to initialize all n items | 211 * Version of above that uses a copy constructor to initialize all n items |
212 * to the same T. | 212 * to the same T. |
213 */ | 213 */ |
214 T* push_back_n(int n, const T& t) { | 214 T* push_back_n(int n, const T& t) { |
215 SkASSERT(n >= 0); | 215 SkASSERT(n >= 0); |
216 T* newTs = reinterpret_cast<T*>(this->push_back_raw(n)); | 216 T* newTs = reinterpret_cast<T*>(this->push_back_raw(n)); |
217 for (int i = 0; i < n; ++i) { | 217 for (int i = 0; i < n; ++i) { |
218 SkNEW_PLACEMENT_ARGS(newTs[i], T, (t)); | 218 new (newTs[i]) T(t); |
219 } | 219 } |
220 return newTs; | 220 return newTs; |
221 } | 221 } |
222 | 222 |
223 /** | 223 /** |
224 * Version of above that uses a copy constructor to initialize the n items | 224 * Version of above that uses a copy constructor to initialize the n items |
225 * to separate T values. | 225 * to separate T values. |
226 */ | 226 */ |
227 T* push_back_n(int n, const T t[]) { | 227 T* push_back_n(int n, const T t[]) { |
228 SkASSERT(n >= 0); | 228 SkASSERT(n >= 0); |
229 this->checkRealloc(n); | 229 this->checkRealloc(n); |
230 for (int i = 0; i < n; ++i) { | 230 for (int i = 0; i < n; ++i) { |
231 SkNEW_PLACEMENT_ARGS(fItemArray + fCount + i, T, (t[i])); | 231 new (fItemArray + fCount + i) T(t[i]); |
232 } | 232 } |
233 fCount += n; | 233 fCount += n; |
234 return fItemArray + fCount - n; | 234 return fItemArray + fCount - n; |
235 } | 235 } |
236 | 236 |
237 /** | 237 /** |
238 * Removes the last element. Not safe to call when count() == 0. | 238 * Removes the last element. Not safe to call when count() == 0. |
239 */ | 239 */ |
240 void pop_back() { | 240 void pop_back() { |
241 SkASSERT(fCount > 0); | 241 SkASSERT(fCount > 0); |
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
541 SkSTArray& operator= (const INHERITED& array) { | 541 SkSTArray& operator= (const INHERITED& array) { |
542 INHERITED::operator=(array); | 542 INHERITED::operator=(array); |
543 return *this; | 543 return *this; |
544 } | 544 } |
545 | 545 |
546 private: | 546 private: |
547 SkAlignedSTStorage<N,T> fStorage; | 547 SkAlignedSTStorage<N,T> fStorage; |
548 }; | 548 }; |
549 | 549 |
550 #endif | 550 #endif |
OLD | NEW |