reed@google.com | 80ba796 | 2012-07-21 17:31:40 +0000 | [diff] [blame] | 1 | // |
| 2 | // SkTRefArray.h |
| 3 | // core |
| 4 | // |
| 5 | // Created by Mike Reed on 7/17/12. |
| 6 | // Copyright (c) 2012 __MyCompanyName__. All rights reserved. |
| 7 | // |
| 8 | |
| 9 | #ifndef SkTRefArray_DEFINED |
| 10 | #define SkTRefArray_DEFINED |
| 11 | |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame^] | 12 | #include "SkRefCnt.h" |
reed@google.com | 0c6a736 | 2012-07-21 19:39:24 +0000 | [diff] [blame] | 13 | #include <new> |
reed@google.com | 80ba796 | 2012-07-21 17:31:40 +0000 | [diff] [blame] | 14 | |
| 15 | /** |
| 16 | * Wrapper to manage thread-safe sharing of an array of T objects. The array |
| 17 | * cannot be grown or shrunk. |
| 18 | */ |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame^] | 19 | template <typename T> class SkTRefArray : public SkRefCnt { |
| 20 | /* |
| 21 | * Shared factory to allocate the space needed for our instance plus N |
| 22 | * T entries at the end. We call our constructor, but not the constructors |
| 23 | * for the elements. Those are called by the proper Create method. |
| 24 | */ |
| 25 | static SkTRefArray<T>* Alloc(int count) { |
| 26 | // space for us, and our [count] elements |
reed@google.com | 80ba796 | 2012-07-21 17:31:40 +0000 | [diff] [blame] | 27 | size_t size = sizeof(SkTRefArray<T>) + count * sizeof(T); |
| 28 | SkTRefArray<T>* obj = (SkTRefArray<T>*)sk_malloc_throw(size); |
| 29 | |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame^] | 30 | SkNEW_PLACEMENT(obj, SkTRefArray<T>); |
reed@google.com | 80ba796 | 2012-07-21 17:31:40 +0000 | [diff] [blame] | 31 | obj->fCount = count; |
reed@google.com | 80ba796 | 2012-07-21 17:31:40 +0000 | [diff] [blame] | 32 | return obj; |
| 33 | } |
| 34 | |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame^] | 35 | public: |
| 36 | /** |
| 37 | * Return a new array with 'count' elements, initialized to their default |
| 38 | * value. To change them to some other value, use writableBegin/End or |
| 39 | * writableAt(), but do that before this array is given to another thread. |
| 40 | */ |
| 41 | static SkTRefArray<T>* Create(int count) { |
| 42 | SkTRefArray<T>* obj = Alloc(count); |
| 43 | T* array = const_cast<T*>(obj->begin()); |
| 44 | for (int i = 0; i < count; ++i) { |
| 45 | SkNEW_PLACEMENT(&array[i], T); |
| 46 | } |
| 47 | return obj; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Return a new array with 'count' elements, initialized from the provided |
| 52 | * src array. To change them to some other value, use writableBegin/End or |
| 53 | * writableAt(), but do that before this array is given to another thread. |
| 54 | */ |
| 55 | static SkTRefArray<T>* Create(const T src[], int count) { |
| 56 | SkTRefArray<T>* obj = Alloc(count); |
| 57 | T* array = const_cast<T*>(obj->begin()); |
| 58 | for (int i = 0; i < count; ++i) { |
| 59 | SkNEW_PLACEMENT_ARGS(&array[i], T, (src[i])); |
| 60 | } |
| 61 | return obj; |
| 62 | } |
| 63 | |
reed@google.com | 80ba796 | 2012-07-21 17:31:40 +0000 | [diff] [blame] | 64 | int count() const { return fCount; } |
| 65 | const T* begin() const { return (const T*)(this + 1); } |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame^] | 66 | const T* end() const { return this->begin() + fCount; } |
| 67 | const T& at(int index) const { |
reed@google.com | 80ba796 | 2012-07-21 17:31:40 +0000 | [diff] [blame] | 68 | SkASSERT((unsigned)index < (unsigned)fCount); |
| 69 | return this->begin()[index]; |
| 70 | } |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame^] | 71 | const T& operator[](int index) const { return this->at(index); } |
reed@google.com | 80ba796 | 2012-07-21 17:31:40 +0000 | [diff] [blame] | 72 | |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame^] | 73 | // For the writable methods, we assert that we are the only owner if we |
| 74 | // call these, since other owners are not informed if we change an element. |
reed@google.com | 80ba796 | 2012-07-21 17:31:40 +0000 | [diff] [blame] | 75 | |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame^] | 76 | T* writableBegin() { |
| 77 | SkASSERT(1 == this->getRefCnt()); |
| 78 | return (T*)(this + 1); |
reed@google.com | 80ba796 | 2012-07-21 17:31:40 +0000 | [diff] [blame] | 79 | } |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame^] | 80 | T* writableEnd() { |
| 81 | return this->writableBegin() + fCount; |
| 82 | } |
| 83 | T& writableAt(int index) { |
| 84 | SkASSERT((unsigned)index < (unsigned)fCount); |
| 85 | return this->writableBegin()[index]; |
reed@google.com | 80ba796 | 2012-07-21 17:31:40 +0000 | [diff] [blame] | 86 | } |
| 87 | |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame^] | 88 | protected: |
| 89 | virtual void internal_dispose() const SK_OVERRIDE { |
reed@google.com | 80ba796 | 2012-07-21 17:31:40 +0000 | [diff] [blame] | 90 | T* array = const_cast<T*>(this->begin()); |
| 91 | int n = fCount; |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame^] | 92 | |
reed@google.com | 80ba796 | 2012-07-21 17:31:40 +0000 | [diff] [blame] | 93 | for (int i = 0; i < n; ++i) { |
| 94 | array->~T(); |
| 95 | array += 1; |
| 96 | } |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame^] | 97 | |
| 98 | this->internal_dispose_restore_refcnt_to_1(); |
| 99 | this->~SkTRefArray<T>(); |
| 100 | sk_free((void*)this); |
reed@google.com | 80ba796 | 2012-07-21 17:31:40 +0000 | [diff] [blame] | 101 | } |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame^] | 102 | |
| 103 | private: |
| 104 | int fCount; |
| 105 | |
| 106 | // hide this |
| 107 | virtual ~SkTRefArray() {} |
| 108 | |
| 109 | typedef SkRefCnt INHERITED; |
reed@google.com | 80ba796 | 2012-07-21 17:31:40 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | #endif |