OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2008 The Android Open Source Project | 3 * Copyright 2008 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 SkPtrSet_DEFINED | 10 #ifndef SkPtrSet_DEFINED |
11 #define SkPtrSet_DEFINED | 11 #define SkPtrSet_DEFINED |
12 | 12 |
13 #include "SkRefCnt.h" | 13 #include "SkRefCnt.h" |
14 #include "SkFlattenable.h" | 14 #include "SkFlattenable.h" |
15 #include "SkTDArray.h" | 15 #include "SkTDArray.h" |
| 16 #include "SkTSearch.h" |
16 | 17 |
17 /** | 18 /** |
18 * Maintains a set of ptrs, assigning each a unique ID [1...N]. Duplicate ptrs | 19 * Maintains a set of ptrs, assigning each a unique ID [1...N]. Duplicate ptrs |
19 * return the same ID (since its a set). Subclasses can override inPtr() | 20 * return the same ID (since its a set). Subclasses can override inPtr() |
20 * and decPtr(). incPtr() is called each time a unique ptr is added ot the | 21 * and decPtr(). incPtr() is called each time a unique ptr is added ot the |
21 * set. decPtr() is called on each ptr when the set is destroyed or reset. | 22 * set. decPtr() is called on each ptr when the set is destroyed or reset. |
22 */ | 23 */ |
23 class SkPtrSet : public SkRefCnt { | 24 template <typename T> |
| 25 class SkTPtrSet : public SkRefCnt { |
24 public: | 26 public: |
25 SK_DECLARE_INST_COUNT(SkPtrSet) | 27 SK_DECLARE_INST_COUNT(SkTPtrSet) |
26 | 28 |
27 /** | 29 /** |
28 * Search for the specified ptr in the set. If it is found, return its | 30 * Search for the specified ptr in the set. If it is found, return its |
29 * 32bit ID [1..N], or if not found, return 0. Always returns 0 for NULL. | 31 * 32bit ID [1..N], or if not found, return 0. Always returns 0 for NULL. |
30 */ | 32 */ |
31 uint32_t find(void*) const; | 33 uint32_t find(T* ptr) const { |
| 34 if (NULL == ptr) { |
| 35 return 0; |
| 36 } |
| 37 |
| 38 int count = fList.count(); |
| 39 Pair pair; |
| 40 pair.fPtr = ptr; |
| 41 |
| 42 int index = SkTSearch<Pair, SkTPtrSet<T>::Less>(fList.begin(), count, pa
ir, sizeof(pair)); |
| 43 if (index < 0) { |
| 44 return 0; |
| 45 } |
| 46 return fList[index].fIndex; |
| 47 } |
32 | 48 |
33 /** | 49 /** |
34 * Add the specified ptr to the set, returning a unique 32bit ID for it | 50 * Add the specified ptr to the set, returning a unique 32bit ID for it |
35 * [1...N]. Duplicate ptrs will return the same ID. | 51 * [1...N]. Duplicate ptrs will return the same ID. |
36 * | 52 * |
37 * If the ptr is NULL, it is not added, and 0 is returned. | 53 * If the ptr is NULL, it is not added, and 0 is returned. |
38 */ | 54 */ |
39 uint32_t add(void*); | 55 uint32_t add(T* ptr) { |
| 56 if (NULL == ptr) { |
| 57 return 0; |
| 58 } |
| 59 |
| 60 int count = fList.count(); |
| 61 Pair pair; |
| 62 pair.fPtr = ptr; |
| 63 |
| 64 int index = SkTSearch<Pair, SkTPtrSet<T>::Less>(fList.begin(), count, pa
ir, sizeof(pair)); |
| 65 if (index < 0) { |
| 66 index = ~index; // turn it back into an index for insertion |
| 67 this->incPtr(ptr); |
| 68 pair.fIndex = count + 1; |
| 69 *fList.insert(index) = pair; |
| 70 return count + 1; |
| 71 } else { |
| 72 return fList[index].fIndex; |
| 73 } |
| 74 } |
40 | 75 |
41 /** | 76 /** |
42 * Return the number of (non-null) ptrs in the set. | 77 * Return the number of (non-null) ptrs in the set. |
43 */ | 78 */ |
44 int count() const { return fList.count(); } | 79 int count() const { return fList.count(); } |
45 | 80 |
46 /** | 81 /** |
47 * Copy the ptrs in the set into the specified array (allocated by the | 82 * Copy the ptrs in the set into the specified array (allocated by the |
48 * caller). The ptrs are assgined to the array based on their corresponding | 83 * caller). The ptrs are assgined to the array based on their corresponding |
49 * ID. e.g. array[ptr.ID - 1] = ptr. | 84 * ID. e.g. array[ptr.ID - 1] = ptr. |
50 * | 85 * |
51 * incPtr() and decPtr() are not called during this operation. | 86 * incPtr() and decPtr() are not called during this operation. |
52 */ | 87 */ |
53 void copyToArray(void* array[]) const; | 88 void copyToArray(T* array[]) const { |
| 89 int count = fList.count(); |
| 90 if (count > 0) { |
| 91 SkASSERT(array); |
| 92 const Pair* p = fList.begin(); |
| 93 // p->fIndex is base-1, so we need to subtract to find its slot |
| 94 for (int i = 0; i < count; i++) { |
| 95 int index = p[i].fIndex - 1; |
| 96 SkASSERT((unsigned)index < (unsigned)count); |
| 97 array[index] = p[i].fPtr; |
| 98 } |
| 99 } |
| 100 } |
54 | 101 |
55 /** | 102 /** |
56 * Call decPtr() on each ptr in the set, and the reset the size of the set | 103 * Call decPtr() on each ptr in the set, and the reset the size of the set |
57 * to 0. | 104 * to 0. |
58 */ | 105 */ |
59 void reset(); | 106 void reset() { |
| 107 Iter iter(*this); |
| 108 for (T* ptr = iter.next(); NULL != ptr; ptr = iter.next()) { |
| 109 this->decPtr(ptr); |
| 110 } |
60 | 111 |
61 protected: | 112 fList.reset(); |
62 virtual void incPtr(void*) {} | 113 } |
63 virtual void decPtr(void*) {} | 114 |
| 115 class Iter { |
| 116 public: |
| 117 Iter(const SkTPtrSet<T>& set) |
| 118 : fSet(set) |
| 119 , fIndex(0) {} |
| 120 |
| 121 T* next() { |
| 122 if (fIndex >= fSet.fList.count()) { |
| 123 return NULL; |
| 124 } |
| 125 return fSet.fList[fIndex++].fPtr; |
| 126 } |
| 127 |
| 128 private: |
| 129 const SkTPtrSet<T>& fSet; |
| 130 int fIndex; |
| 131 }; |
64 | 132 |
65 private: | 133 private: |
| 134 void incPtr(T*) {} |
| 135 void decPtr(T*) {} |
| 136 |
66 struct Pair { | 137 struct Pair { |
67 void* fPtr; // never NULL | 138 T* fPtr; // never NULL |
68 uint32_t fIndex; // 1...N | 139 uint32_t fIndex; // 1...N |
69 }; | 140 }; |
70 | 141 |
71 // we store the ptrs in sorted-order (using Cmp) so that we can efficiently | 142 // we store the ptrs in sorted-order (using Cmp) so that we can efficiently |
72 // detect duplicates when add() is called. Hence we need to store the | 143 // detect duplicates when add() is called. Hence we need to store the |
73 // ptr and its ID/fIndex explicitly, since the ptr's position in the array | 144 // ptr and its ID/fIndex explicitly, since the ptr's position in the array |
74 // is not related to its "index". | 145 // is not related to its "index". |
75 SkTDArray<Pair> fList; | 146 SkTDArray<Pair> fList; |
76 | 147 |
77 static bool Less(const Pair& a, const Pair& b); | 148 static bool Less(const Pair& a, const Pair& b) { |
| 149 return (char*)a.fPtr < (char*)b.fPtr; |
| 150 } |
78 | 151 |
79 typedef SkRefCnt INHERITED; | 152 typedef SkRefCnt INHERITED; |
80 }; | 153 }; |
81 | 154 |
82 /** | 155 template <> |
83 * Templated wrapper for SkPtrSet, just meant to automate typecasting | 156 inline void SkTPtrSet<SkRefCnt>::incPtr(SkRefCnt* ptr) { ptr->ref(); } |
84 * parameters to and from void* (which the base class expects). | 157 |
85 */ | 158 template <> |
86 template <typename T> class SkTPtrSet : public SkPtrSet { | 159 inline void SkTPtrSet<SkRefCnt>::decPtr(SkRefCnt* ptr) { ptr->unref(); } |
| 160 |
| 161 class SkRefCntSet : public SkTPtrSet<SkRefCnt> { |
87 public: | 162 public: |
88 uint32_t find(T ptr) { | 163 virtual ~SkRefCntSet() { |
89 return this->INHERITED::find((void*)ptr); | 164 this->reset(); |
90 } | 165 } |
91 uint32_t add(T ptr) { | |
92 return this->INHERITED::add((void*)ptr); | |
93 } | |
94 | |
95 void copyToArray(T* array) const { | |
96 this->INHERITED::copyToArray((void**)array); | |
97 } | |
98 | |
99 private: | |
100 typedef SkPtrSet INHERITED; | |
101 }; | 166 }; |
102 | 167 |
103 /** | 168 class SkFactorySet : public SkTPtrSet<SkFlattenable::FactoryRef> {}; |
104 * Subclass of SkTPtrSet specialed to call ref() and unref() when the | |
105 * base class's incPtr() and decPtr() are called. This makes it a valid owner | |
106 * of each ptr, which is released when the set is reset or destroyed. | |
107 */ | |
108 class SkRefCntSet : public SkTPtrSet<SkRefCnt*> { | |
109 public: | |
110 virtual ~SkRefCntSet(); | |
111 | |
112 protected: | |
113 // overrides | |
114 virtual void incPtr(void*); | |
115 virtual void decPtr(void*); | |
116 }; | |
117 | |
118 class SkFactorySet : public SkTPtrSet<SkFlattenable::Factory> {}; | |
119 | 169 |
120 /** | 170 /** |
121 * Similar to SkFactorySet, but only allows Factorys that have registered names. | 171 * Similar to SkFactorySet, but only allows Factorys that have registered names. |
122 * Also has a function to return the next added Factory's name. | 172 * Also has a function to return the next added Factory's name. |
123 */ | 173 */ |
124 class SkNamedFactorySet : public SkRefCnt { | 174 class SkNamedFactorySet : public SkRefCnt { |
125 public: | 175 public: |
126 SK_DECLARE_INST_COUNT(SkNamedFactorySet) | 176 SK_DECLARE_INST_COUNT(SkNamedFactorySet) |
127 | 177 |
128 SkNamedFactorySet(); | 178 SkNamedFactorySet(); |
(...skipping 13 matching lines...) Expand all Loading... |
142 const char* getNextAddedFactoryName(); | 192 const char* getNextAddedFactoryName(); |
143 private: | 193 private: |
144 int fNextAddedFactory; | 194 int fNextAddedFactory; |
145 SkFactorySet fFactorySet; | 195 SkFactorySet fFactorySet; |
146 SkTDArray<const char*> fNames; | 196 SkTDArray<const char*> fNames; |
147 | 197 |
148 typedef SkRefCnt INHERITED; | 198 typedef SkRefCnt INHERITED; |
149 }; | 199 }; |
150 | 200 |
151 #endif | 201 #endif |
OLD | NEW |