michael@0: michael@0: /* michael@0: * Copyright 2008 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #ifndef SkPtrSet_DEFINED michael@0: #define SkPtrSet_DEFINED michael@0: michael@0: #include "SkRefCnt.h" michael@0: #include "SkFlattenable.h" michael@0: #include "SkTDArray.h" michael@0: michael@0: /** michael@0: * Maintains a set of ptrs, assigning each a unique ID [1...N]. Duplicate ptrs michael@0: * return the same ID (since its a set). Subclasses can override inPtr() michael@0: * and decPtr(). incPtr() is called each time a unique ptr is added ot the michael@0: * set. decPtr() is called on each ptr when the set is destroyed or reset. michael@0: */ michael@0: class SkPtrSet : public SkRefCnt { michael@0: public: michael@0: SK_DECLARE_INST_COUNT(SkPtrSet) michael@0: michael@0: /** michael@0: * Search for the specified ptr in the set. If it is found, return its michael@0: * 32bit ID [1..N], or if not found, return 0. Always returns 0 for NULL. michael@0: */ michael@0: uint32_t find(void*) const; michael@0: michael@0: /** michael@0: * Add the specified ptr to the set, returning a unique 32bit ID for it michael@0: * [1...N]. Duplicate ptrs will return the same ID. michael@0: * michael@0: * If the ptr is NULL, it is not added, and 0 is returned. michael@0: */ michael@0: uint32_t add(void*); michael@0: michael@0: /** michael@0: * Return the number of (non-null) ptrs in the set. michael@0: */ michael@0: int count() const { return fList.count(); } michael@0: michael@0: /** michael@0: * Copy the ptrs in the set into the specified array (allocated by the michael@0: * caller). The ptrs are assgined to the array based on their corresponding michael@0: * ID. e.g. array[ptr.ID - 1] = ptr. michael@0: * michael@0: * incPtr() and decPtr() are not called during this operation. michael@0: */ michael@0: void copyToArray(void* array[]) const; michael@0: michael@0: /** michael@0: * Call decPtr() on each ptr in the set, and the reset the size of the set michael@0: * to 0. michael@0: */ michael@0: void reset(); michael@0: michael@0: protected: michael@0: virtual void incPtr(void*) {} michael@0: virtual void decPtr(void*) {} michael@0: michael@0: private: michael@0: struct Pair { michael@0: void* fPtr; // never NULL michael@0: uint32_t fIndex; // 1...N michael@0: }; michael@0: michael@0: // we store the ptrs in sorted-order (using Cmp) so that we can efficiently michael@0: // detect duplicates when add() is called. Hence we need to store the michael@0: // ptr and its ID/fIndex explicitly, since the ptr's position in the array michael@0: // is not related to its "index". michael@0: SkTDArray fList; michael@0: michael@0: static bool Less(const Pair& a, const Pair& b); michael@0: michael@0: typedef SkRefCnt INHERITED; michael@0: }; michael@0: michael@0: /** michael@0: * Templated wrapper for SkPtrSet, just meant to automate typecasting michael@0: * parameters to and from void* (which the base class expects). michael@0: */ michael@0: template class SkTPtrSet : public SkPtrSet { michael@0: public: michael@0: uint32_t find(T ptr) { michael@0: return this->INHERITED::find((void*)ptr); michael@0: } michael@0: uint32_t add(T ptr) { michael@0: return this->INHERITED::add((void*)ptr); michael@0: } michael@0: michael@0: void copyToArray(T* array) const { michael@0: this->INHERITED::copyToArray((void**)array); michael@0: } michael@0: michael@0: private: michael@0: typedef SkPtrSet INHERITED; michael@0: }; michael@0: michael@0: /** michael@0: * Subclass of SkTPtrSet specialed to call ref() and unref() when the michael@0: * base class's incPtr() and decPtr() are called. This makes it a valid owner michael@0: * of each ptr, which is released when the set is reset or destroyed. michael@0: */ michael@0: class SkRefCntSet : public SkTPtrSet { michael@0: public: michael@0: virtual ~SkRefCntSet(); michael@0: michael@0: protected: michael@0: // overrides michael@0: virtual void incPtr(void*); michael@0: virtual void decPtr(void*); michael@0: }; michael@0: michael@0: class SkFactorySet : public SkTPtrSet {}; michael@0: michael@0: /** michael@0: * Similar to SkFactorySet, but only allows Factorys that have registered names. michael@0: * Also has a function to return the next added Factory's name. michael@0: */ michael@0: class SkNamedFactorySet : public SkRefCnt { michael@0: public: michael@0: SK_DECLARE_INST_COUNT(SkNamedFactorySet) michael@0: michael@0: SkNamedFactorySet(); michael@0: michael@0: /** michael@0: * Find the specified Factory in the set. If it is not already in the set, michael@0: * and has registered its name, add it to the set, and return its index. michael@0: * If the Factory has no registered name, return 0. michael@0: */ michael@0: uint32_t find(SkFlattenable::Factory); michael@0: michael@0: /** michael@0: * If new Factorys have been added to the set, return the name of the first michael@0: * Factory added after the Factory name returned by the last call to this michael@0: * function. michael@0: */ michael@0: const char* getNextAddedFactoryName(); michael@0: private: michael@0: int fNextAddedFactory; michael@0: SkFactorySet fFactorySet; michael@0: SkTDArray fNames; michael@0: michael@0: typedef SkRefCnt INHERITED; michael@0: }; michael@0: michael@0: #endif