| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 #ifndef SkTemplates_DEFINED | 10 #ifndef SkTemplates_DEFINED |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 | 221 |
| 222 private: | 222 private: |
| 223 T* fArray; | 223 T* fArray; |
| 224 SkDEBUGCODE(int fCount;) | 224 SkDEBUGCODE(int fCount;) |
| 225 }; | 225 }; |
| 226 | 226 |
| 227 /** Wraps SkAutoTArray, with room for up to N elements preallocated | 227 /** Wraps SkAutoTArray, with room for up to N elements preallocated |
| 228 */ | 228 */ |
| 229 template <size_t N, typename T> class SkAutoSTArray : SkNoncopyable { | 229 template <size_t N, typename T> class SkAutoSTArray : SkNoncopyable { |
| 230 public: | 230 public: |
| 231 /** Initialize with no objects */ |
| 232 SkAutoSTArray() { |
| 233 fArray = NULL; |
| 234 fCount = 0; |
| 235 } |
| 236 |
| 231 /** Allocate count number of T elements | 237 /** Allocate count number of T elements |
| 232 */ | 238 */ |
| 233 SkAutoSTArray(size_t count) { | 239 SkAutoSTArray(size_t count) { |
| 234 fCount = count; | 240 fArray = NULL; |
| 235 if (count > N) { | 241 fCount = 0; |
| 236 fArray = (T*) sk_malloc_throw(count * sizeof(T)); | 242 this->reset(count); |
| 237 } else if (count > 0) { | 243 } |
| 238 fArray = (T*) fStorage; | 244 |
| 239 } else { | 245 ~SkAutoSTArray() { |
| 240 fArray = NULL; | 246 this->reset(0); |
| 241 return; | 247 } |
| 248 |
| 249 /** Destroys previous objects in the array and default constructs count numb
er of objects */ |
| 250 void reset(size_t count) { |
| 251 T* start = fArray; |
| 252 T* iter = start + fCount; |
| 253 while (iter > start) { |
| 254 (--iter)->~T(); |
| 242 } | 255 } |
| 243 T* iter = fArray; | 256 |
| 257 if (fCount != count) { |
| 258 if (count > N) { |
| 259 sk_free(fArray); |
| 260 } |
| 261 |
| 262 if (count > N) { |
| 263 fArray = (T*) sk_malloc_throw(count * sizeof(T)); |
| 264 } else if (count > 0) { |
| 265 fArray = (T*) fStorage; |
| 266 } else { |
| 267 fArray = NULL; |
| 268 return; |
| 269 } |
| 270 |
| 271 fCount = count; |
| 272 } |
| 273 |
| 274 iter = fArray; |
| 244 T* stop = fArray + count; | 275 T* stop = fArray + count; |
| 245 while (iter < stop) { | 276 while (iter < stop) { |
| 246 SkNEW_PLACEMENT(iter++, T); | 277 SkNEW_PLACEMENT(iter++, T); |
| 247 } | 278 } |
| 248 } | 279 } |
| 249 | 280 |
| 250 ~SkAutoSTArray() { | |
| 251 T* start = fArray; | |
| 252 T* iter = start + fCount; | |
| 253 while (iter > start) { | |
| 254 (--iter)->~T(); | |
| 255 } | |
| 256 if (fCount > N) { | |
| 257 sk_free(fArray); | |
| 258 } | |
| 259 } | |
| 260 | |
| 261 /** Return the number of T elements in the array | 281 /** Return the number of T elements in the array |
| 262 */ | 282 */ |
| 263 size_t count() const { return fCount; } | 283 size_t count() const { return fCount; } |
| 264 | 284 |
| 265 /** Return the array of T elements. Will be NULL if count == 0 | 285 /** Return the array of T elements. Will be NULL if count == 0 |
| 266 */ | 286 */ |
| 267 T* get() const { return fArray; } | 287 T* get() const { return fArray; } |
| 268 | 288 |
| 269 /** Return the nth element in the array | 289 /** Return the nth element in the array |
| 270 */ | 290 */ |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 /** | 452 /** |
| 433 * Returns void* because this object does not initialize the | 453 * Returns void* because this object does not initialize the |
| 434 * memory. Use placement new for types that require a cons. | 454 * memory. Use placement new for types that require a cons. |
| 435 */ | 455 */ |
| 436 void* get() { return fStorage.get(); } | 456 void* get() { return fStorage.get(); } |
| 437 private: | 457 private: |
| 438 SkAlignedSStorage<sizeof(T)*N> fStorage; | 458 SkAlignedSStorage<sizeof(T)*N> fStorage; |
| 439 }; | 459 }; |
| 440 | 460 |
| 441 #endif | 461 #endif |
| OLD | NEW |