epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2008 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
mike@reedtribe.org | e9e08cc | 2011-04-29 01:44:52 +0000 | [diff] [blame] | 10 | #ifndef SkPtrSet_DEFINED |
| 11 | #define SkPtrSet_DEFINED |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | |
| 13 | #include "SkRefCnt.h" |
mike@reedtribe.org | 227b516 | 2012-08-12 19:25:08 +0000 | [diff] [blame] | 14 | #include "SkFlattenable.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | #include "SkTDArray.h" |
| 16 | |
mike@reedtribe.org | e8f754a | 2011-04-29 01:02:43 +0000 | [diff] [blame] | 17 | /** |
| 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() |
| 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. |
| 22 | */ |
mike@reedtribe.org | e9e08cc | 2011-04-29 01:44:52 +0000 | [diff] [blame] | 23 | class SkPtrSet : public SkRefCnt { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | public: |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 25 | SK_DECLARE_INST_COUNT(SkPtrSet) |
| 26 | |
mike@reedtribe.org | e8f754a | 2011-04-29 01:02:43 +0000 | [diff] [blame] | 27 | /** |
reed@google.com | 8d90eeb | 2011-05-04 18:03:00 +0000 | [diff] [blame] | 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. |
| 30 | */ |
| 31 | uint32_t find(void*) const; |
| 32 | |
| 33 | /** |
mike@reedtribe.org | e8f754a | 2011-04-29 01:02:43 +0000 | [diff] [blame] | 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. |
| 36 | * |
mike@reedtribe.org | e9e08cc | 2011-04-29 01:44:52 +0000 | [diff] [blame] | 37 | * If the ptr is NULL, it is not added, and 0 is returned. |
mike@reedtribe.org | e8f754a | 2011-04-29 01:02:43 +0000 | [diff] [blame] | 38 | */ |
mike@reedtribe.org | e9e08cc | 2011-04-29 01:44:52 +0000 | [diff] [blame] | 39 | uint32_t add(void*); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame^] | 40 | |
mike@reedtribe.org | e8f754a | 2011-04-29 01:02:43 +0000 | [diff] [blame] | 41 | /** |
| 42 | * Return the number of (non-null) ptrs in the set. |
| 43 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 44 | int count() const { return fList.count(); } |
mike@reedtribe.org | e8f754a | 2011-04-29 01:02:43 +0000 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * 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 |
| 49 | * ID. e.g. array[ptr.ID - 1] = ptr. |
| 50 | * |
| 51 | * incPtr() and decPtr() are not called during this operation. |
| 52 | */ |
mike@reedtribe.org | e9e08cc | 2011-04-29 01:44:52 +0000 | [diff] [blame] | 53 | void copyToArray(void* array[]) const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | |
mike@reedtribe.org | e8f754a | 2011-04-29 01:02:43 +0000 | [diff] [blame] | 55 | /** |
| 56 | * Call decPtr() on each ptr in the set, and the reset the size of the set |
| 57 | * to 0. |
| 58 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | void reset(); |
| 60 | |
| 61 | protected: |
| 62 | virtual void incPtr(void* ptr) {} |
| 63 | virtual void decPtr(void* ptr) {} |
| 64 | |
| 65 | private: |
| 66 | struct Pair { |
mike@reedtribe.org | e8f754a | 2011-04-29 01:02:43 +0000 | [diff] [blame] | 67 | void* fPtr; // never NULL |
| 68 | uint32_t fIndex; // 1...N |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | }; |
mike@reedtribe.org | e8f754a | 2011-04-29 01:02:43 +0000 | [diff] [blame] | 70 | |
| 71 | // we store the ptrs in sorted-order (using Cmp) so that we can efficiently |
mike@reedtribe.org | e9e08cc | 2011-04-29 01:44:52 +0000 | [diff] [blame] | 72 | // detect duplicates when add() is called. Hence we need to store the |
mike@reedtribe.org | e8f754a | 2011-04-29 01:02:43 +0000 | [diff] [blame] | 73 | // ptr and its ID/fIndex explicitly, since the ptr's position in the array |
| 74 | // is not related to its "index". |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | SkTDArray<Pair> fList; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame^] | 76 | |
bsalomon@google.com | c967795 | 2012-04-10 21:03:23 +0000 | [diff] [blame] | 77 | static int Cmp(const Pair* a, const Pair* b); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame^] | 78 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | typedef SkRefCnt INHERITED; |
| 80 | }; |
| 81 | |
mike@reedtribe.org | e9e08cc | 2011-04-29 01:44:52 +0000 | [diff] [blame] | 82 | /** |
| 83 | * Templated wrapper for SkPtrSet, just meant to automate typecasting |
| 84 | * parameters to and from void* (which the base class expects). |
| 85 | */ |
| 86 | template <typename T> class SkTPtrSet : public SkPtrSet { |
| 87 | public: |
reed@google.com | 6bac947 | 2011-06-21 19:24:00 +0000 | [diff] [blame] | 88 | uint32_t find(T ptr) { |
| 89 | return this->INHERITED::find((void*)ptr); |
| 90 | } |
mike@reedtribe.org | e9e08cc | 2011-04-29 01:44:52 +0000 | [diff] [blame] | 91 | uint32_t add(T ptr) { |
| 92 | return this->INHERITED::add((void*)ptr); |
| 93 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame^] | 94 | |
mike@reedtribe.org | e9e08cc | 2011-04-29 01:44:52 +0000 | [diff] [blame] | 95 | void copyToArray(T* array) const { |
| 96 | this->INHERITED::copyToArray((void**)array); |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | typedef SkPtrSet INHERITED; |
| 101 | }; |
| 102 | |
mike@reedtribe.org | 227b516 | 2012-08-12 19:25:08 +0000 | [diff] [blame] | 103 | /** |
| 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(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame^] | 111 | |
mike@reedtribe.org | 227b516 | 2012-08-12 19:25:08 +0000 | [diff] [blame] | 112 | protected: |
| 113 | // overrides |
| 114 | virtual void incPtr(void*); |
| 115 | virtual void decPtr(void*); |
| 116 | }; |
| 117 | |
| 118 | class SkFactorySet : public SkTPtrSet<SkFlattenable::Factory> {}; |
| 119 | |
| 120 | /** |
| 121 | * Similar to SkFactorySet, but only allows Factorys that have registered names. |
| 122 | * Also has a function to return the next added Factory's name. |
| 123 | */ |
| 124 | class SkNamedFactorySet : public SkRefCnt { |
| 125 | public: |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 126 | SK_DECLARE_INST_COUNT(SkNamedFactorySet) |
| 127 | |
mike@reedtribe.org | 227b516 | 2012-08-12 19:25:08 +0000 | [diff] [blame] | 128 | SkNamedFactorySet(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame^] | 129 | |
mike@reedtribe.org | 227b516 | 2012-08-12 19:25:08 +0000 | [diff] [blame] | 130 | /** |
| 131 | * Find the specified Factory in the set. If it is not already in the set, |
| 132 | * and has registered its name, add it to the set, and return its index. |
| 133 | * If the Factory has no registered name, return 0. |
| 134 | */ |
| 135 | uint32_t find(SkFlattenable::Factory); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame^] | 136 | |
mike@reedtribe.org | 227b516 | 2012-08-12 19:25:08 +0000 | [diff] [blame] | 137 | /** |
| 138 | * If new Factorys have been added to the set, return the name of the first |
| 139 | * Factory added after the Factory name returned by the last call to this |
| 140 | * function. |
| 141 | */ |
| 142 | const char* getNextAddedFactoryName(); |
| 143 | private: |
| 144 | int fNextAddedFactory; |
| 145 | SkFactorySet fFactorySet; |
| 146 | SkTDArray<const char*> fNames; |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 147 | |
| 148 | typedef SkRefCnt INHERITED; |
mike@reedtribe.org | 227b516 | 2012-08-12 19:25:08 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | #endif |