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 |
11 #include "../private/SkTLogic.h" | 11 #include "../private/SkTLogic.h" |
12 #include "../private/SkTemplates.h" | 12 #include "../private/SkTemplates.h" |
13 #include "SkTypes.h" | 13 #include "SkTypes.h" |
14 | 14 |
15 #include <new> | 15 #include <new> |
16 #include <utility> | 16 #include <utility> |
17 | 17 |
18 template <typename T, bool MEM_COPY = false> class SkTArray; | |
19 template <typename T, bool MEM_COPY> void* operator new(size_t, SkTArray<T, MEM_
COPY>*, int); | |
20 | |
21 /** When MEM_COPY is true T will be bit copied when moved. | 18 /** When MEM_COPY is true T will be bit copied when moved. |
22 When MEM_COPY is false, T will be copy constructed / destructed. | 19 When MEM_COPY is false, T will be copy constructed / destructed. |
23 In all cases T will be default-initialized on allocation, | 20 In all cases T will be default-initialized on allocation, |
24 and its destructor will be called from this object's destructor. | 21 and its destructor will be called from this object's destructor. |
25 */ | 22 */ |
26 template <typename T, bool MEM_COPY> class SkTArray { | 23 template <typename T, bool MEM_COPY = false> class SkTArray { |
27 public: | 24 public: |
28 /** | 25 /** |
29 * Creates an empty array with no initial storage | 26 * Creates an empty array with no initial storage |
30 */ | 27 */ |
31 SkTArray() { | 28 SkTArray() { |
32 fCount = 0; | 29 fCount = 0; |
33 fReserveCount = gMIN_ALLOC_COUNT; | 30 fReserveCount = gMIN_ALLOC_COUNT; |
34 fAllocCount = 0; | 31 fAllocCount = 0; |
35 fMemArray = NULL; | 32 fMemArray = NULL; |
36 fPreAllocMemArray = NULL; | 33 fPreAllocMemArray = NULL; |
(...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 | 454 |
458 this->move(newMemArray); | 455 this->move(newMemArray); |
459 | 456 |
460 if (fMemArray != fPreAllocMemArray) { | 457 if (fMemArray != fPreAllocMemArray) { |
461 sk_free(fMemArray); | 458 sk_free(fMemArray); |
462 } | 459 } |
463 fMemArray = newMemArray; | 460 fMemArray = newMemArray; |
464 } | 461 } |
465 } | 462 } |
466 | 463 |
467 friend void* operator new<T>(size_t, SkTArray*, int); | |
468 | |
469 int fReserveCount; | 464 int fReserveCount; |
470 int fCount; | 465 int fCount; |
471 int fAllocCount; | 466 int fAllocCount; |
472 void* fPreAllocMemArray; | 467 void* fPreAllocMemArray; |
473 union { | 468 union { |
474 T* fItemArray; | 469 T* fItemArray; |
475 void* fMemArray; | 470 void* fMemArray; |
476 }; | 471 }; |
477 }; | 472 }; |
478 | 473 |
479 // Use the below macro (SkNEW_APPEND_TO_TARRAY) rather than calling this directl
y | |
480 template <typename T, bool MEM_COPY> | |
481 void* operator new(size_t, SkTArray<T, MEM_COPY>* array, int SkDEBUGCODE(atIndex
)) { | |
482 // Currently, we only support adding to the end of the array. When the array
class itself | |
483 // supports random insertion then this should be updated. | |
484 // SkASSERT(atIndex >= 0 && atIndex <= array->count()); | |
485 SkASSERT(atIndex == array->count()); | |
486 return array->push_back_raw(1); | |
487 } | |
488 | |
489 // Skia doesn't use C++ exceptions but it may be compiled with them enabled. Hav
ing an op delete | |
490 // to match the op new silences warnings about missing op delete when a construc
tor throws an | |
491 // exception. | |
492 template <typename T, bool MEM_COPY> | |
493 void operator delete(void*, SkTArray<T, MEM_COPY>* /*array*/, int /*atIndex*/) { | |
494 SK_ABORT("Invalid Operation"); | |
495 } | |
496 | |
497 // Constructs a new object as the last element of an SkTArray. | |
498 #define SkNEW_APPEND_TO_TARRAY(array_ptr, type_name, args) \ | |
499 (new ((array_ptr), (array_ptr)->count()) type_name args) | |
500 | |
501 | |
502 /** | 474 /** |
503 * Subclass of SkTArray that contains a preallocated memory block for the array. | 475 * Subclass of SkTArray that contains a preallocated memory block for the array. |
504 */ | 476 */ |
505 template <int N, typename T, bool MEM_COPY = false> | 477 template <int N, typename T, bool MEM_COPY = false> |
506 class SkSTArray : public SkTArray<T, MEM_COPY> { | 478 class SkSTArray : public SkTArray<T, MEM_COPY> { |
507 private: | 479 private: |
508 typedef SkTArray<T, MEM_COPY> INHERITED; | 480 typedef SkTArray<T, MEM_COPY> INHERITED; |
509 | 481 |
510 public: | 482 public: |
511 SkSTArray() : INHERITED(&fStorage) { | 483 SkSTArray() : INHERITED(&fStorage) { |
(...skipping 22 matching lines...) Expand all Loading... |
534 SkSTArray& operator= (const INHERITED& array) { | 506 SkSTArray& operator= (const INHERITED& array) { |
535 INHERITED::operator=(array); | 507 INHERITED::operator=(array); |
536 return *this; | 508 return *this; |
537 } | 509 } |
538 | 510 |
539 private: | 511 private: |
540 SkAlignedSTStorage<N,T> fStorage; | 512 SkAlignedSTStorage<N,T> fStorage; |
541 }; | 513 }; |
542 | 514 |
543 #endif | 515 #endif |
OLD | NEW |