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 | 16 |
17 /** | 17 /** |
18 * Maintains a set of ptrs, assigning each a unique ID [1...N]. Duplicate ptrs | 18 * 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() | 19 * 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 | 20 * 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. | 21 * set. decPtr() is called on each ptr when the set is destroyed or reset. |
22 */ | 22 */ |
23 class SkPtrSet : public SkRefCnt { | 23 class SkPtrSet : public SkRefCnt { |
24 public: | 24 public: |
25 | 25 |
26 | 26 |
27 /** | 27 /** |
28 * Search for the specified ptr in the set. If it is found, return its | 28 * 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. | 29 * 32bit ID [1..N], or if not found, return 0. Always returns 0 for nullptr
. |
30 */ | 30 */ |
31 uint32_t find(void*) const; | 31 uint32_t find(void*) const; |
32 | 32 |
33 /** | 33 /** |
34 * Add the specified ptr to the set, returning a unique 32bit ID for it | 34 * Add the specified ptr to the set, returning a unique 32bit ID for it |
35 * [1...N]. Duplicate ptrs will return the same ID. | 35 * [1...N]. Duplicate ptrs will return the same ID. |
36 * | 36 * |
37 * If the ptr is NULL, it is not added, and 0 is returned. | 37 * If the ptr is nullptr, it is not added, and 0 is returned. |
38 */ | 38 */ |
39 uint32_t add(void*); | 39 uint32_t add(void*); |
40 | 40 |
41 /** | 41 /** |
42 * Return the number of (non-null) ptrs in the set. | 42 * Return the number of (non-null) ptrs in the set. |
43 */ | 43 */ |
44 int count() const { return fList.count(); } | 44 int count() const { return fList.count(); } |
45 | 45 |
46 /** | 46 /** |
47 * Copy the ptrs in the set into the specified array (allocated by the | 47 * Copy the ptrs in the set into the specified array (allocated by the |
(...skipping 16 matching lines...) Expand all Loading... |
64 class Iter { | 64 class Iter { |
65 public: | 65 public: |
66 Iter(const SkPtrSet& set) | 66 Iter(const SkPtrSet& set) |
67 : fSet(set) | 67 : fSet(set) |
68 , fIndex(0) {} | 68 , fIndex(0) {} |
69 | 69 |
70 /** | 70 /** |
71 * Return the next ptr in the set or null if the end was reached. | 71 * Return the next ptr in the set or null if the end was reached. |
72 */ | 72 */ |
73 void* next() { | 73 void* next() { |
74 return fIndex < fSet.fList.count() ? fSet.fList[fIndex++].fPtr : NUL
L; | 74 return fIndex < fSet.fList.count() ? fSet.fList[fIndex++].fPtr : nul
lptr; |
75 } | 75 } |
76 | 76 |
77 private: | 77 private: |
78 const SkPtrSet& fSet; | 78 const SkPtrSet& fSet; |
79 int fIndex; | 79 int fIndex; |
80 }; | 80 }; |
81 | 81 |
82 protected: | 82 protected: |
83 virtual void incPtr(void*) {} | 83 virtual void incPtr(void*) {} |
84 virtual void decPtr(void*) {} | 84 virtual void decPtr(void*) {} |
85 | 85 |
86 private: | 86 private: |
87 struct Pair { | 87 struct Pair { |
88 void* fPtr; // never NULL | 88 void* fPtr; // never nullptr |
89 uint32_t fIndex; // 1...N | 89 uint32_t fIndex; // 1...N |
90 }; | 90 }; |
91 | 91 |
92 // we store the ptrs in sorted-order (using Cmp) so that we can efficiently | 92 // we store the ptrs in sorted-order (using Cmp) so that we can efficiently |
93 // detect duplicates when add() is called. Hence we need to store the | 93 // detect duplicates when add() is called. Hence we need to store the |
94 // ptr and its ID/fIndex explicitly, since the ptr's position in the array | 94 // ptr and its ID/fIndex explicitly, since the ptr's position in the array |
95 // is not related to its "index". | 95 // is not related to its "index". |
96 SkTDArray<Pair> fList; | 96 SkTDArray<Pair> fList; |
97 | 97 |
98 static bool Less(const Pair& a, const Pair& b); | 98 static bool Less(const Pair& a, const Pair& b); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 const char* getNextAddedFactoryName(); | 163 const char* getNextAddedFactoryName(); |
164 private: | 164 private: |
165 int fNextAddedFactory; | 165 int fNextAddedFactory; |
166 SkFactorySet fFactorySet; | 166 SkFactorySet fFactorySet; |
167 SkTDArray<const char*> fNames; | 167 SkTDArray<const char*> fNames; |
168 | 168 |
169 typedef SkRefCnt INHERITED; | 169 typedef SkRefCnt INHERITED; |
170 }; | 170 }; |
171 | 171 |
172 #endif | 172 #endif |
OLD | NEW |