2011-07-28 14:26:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2008 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
|
2011-04-29 01:44:52 +00:00
|
|
|
#ifndef SkPtrSet_DEFINED
|
|
|
|
#define SkPtrSet_DEFINED
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
#include "SkRefCnt.h"
|
|
|
|
#include "SkTDArray.h"
|
|
|
|
|
2011-04-29 01:02:43 +00:00
|
|
|
/**
|
|
|
|
* Maintains a set of ptrs, assigning each a unique ID [1...N]. Duplicate ptrs
|
|
|
|
* return the same ID (since its a set). Subclasses can override inPtr()
|
|
|
|
* and decPtr(). incPtr() is called each time a unique ptr is added ot the
|
|
|
|
* set. decPtr() is called on each ptr when the set is destroyed or reset.
|
|
|
|
*/
|
2011-04-29 01:44:52 +00:00
|
|
|
class SkPtrSet : public SkRefCnt {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2011-05-04 18:03:00 +00:00
|
|
|
/**
|
|
|
|
* Search for the specified ptr in the set. If it is found, return its
|
|
|
|
* 32bit ID [1..N], or if not found, return 0. Always returns 0 for NULL.
|
|
|
|
*/
|
|
|
|
uint32_t find(void*) const;
|
|
|
|
|
2011-04-29 01:02:43 +00:00
|
|
|
/**
|
|
|
|
* Add the specified ptr to the set, returning a unique 32bit ID for it
|
|
|
|
* [1...N]. Duplicate ptrs will return the same ID.
|
|
|
|
*
|
2011-04-29 01:44:52 +00:00
|
|
|
* If the ptr is NULL, it is not added, and 0 is returned.
|
2011-04-29 01:02:43 +00:00
|
|
|
*/
|
2011-04-29 01:44:52 +00:00
|
|
|
uint32_t add(void*);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-04-29 01:02:43 +00:00
|
|
|
/**
|
|
|
|
* Return the number of (non-null) ptrs in the set.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
int count() const { return fList.count(); }
|
2011-04-29 01:02:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy the ptrs in the set into the specified array (allocated by the
|
|
|
|
* caller). The ptrs are assgined to the array based on their corresponding
|
|
|
|
* ID. e.g. array[ptr.ID - 1] = ptr.
|
|
|
|
*
|
|
|
|
* incPtr() and decPtr() are not called during this operation.
|
|
|
|
*/
|
2011-04-29 01:44:52 +00:00
|
|
|
void copyToArray(void* array[]) const;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-04-29 01:02:43 +00:00
|
|
|
/**
|
|
|
|
* Call decPtr() on each ptr in the set, and the reset the size of the set
|
|
|
|
* to 0.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
void reset();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void incPtr(void* ptr) {}
|
|
|
|
virtual void decPtr(void* ptr) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Pair {
|
2011-04-29 01:02:43 +00:00
|
|
|
void* fPtr; // never NULL
|
|
|
|
uint32_t fIndex; // 1...N
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
2011-04-29 01:02:43 +00:00
|
|
|
|
|
|
|
// we store the ptrs in sorted-order (using Cmp) so that we can efficiently
|
2011-04-29 01:44:52 +00:00
|
|
|
// detect duplicates when add() is called. Hence we need to store the
|
2011-04-29 01:02:43 +00:00
|
|
|
// ptr and its ID/fIndex explicitly, since the ptr's position in the array
|
|
|
|
// is not related to its "index".
|
2008-12-17 15:59:43 +00:00
|
|
|
SkTDArray<Pair> fList;
|
|
|
|
|
2012-04-10 21:03:23 +00:00
|
|
|
static int Cmp(const Pair* a, const Pair* b);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
typedef SkRefCnt INHERITED;
|
|
|
|
};
|
|
|
|
|
2011-04-29 01:44:52 +00:00
|
|
|
/**
|
|
|
|
* Templated wrapper for SkPtrSet, just meant to automate typecasting
|
|
|
|
* parameters to and from void* (which the base class expects).
|
|
|
|
*/
|
|
|
|
template <typename T> class SkTPtrSet : public SkPtrSet {
|
|
|
|
public:
|
2011-06-21 19:24:00 +00:00
|
|
|
uint32_t find(T ptr) {
|
|
|
|
return this->INHERITED::find((void*)ptr);
|
|
|
|
}
|
2011-04-29 01:44:52 +00:00
|
|
|
uint32_t add(T ptr) {
|
|
|
|
return this->INHERITED::add((void*)ptr);
|
|
|
|
}
|
2011-06-21 19:24:00 +00:00
|
|
|
|
2011-04-29 01:44:52 +00:00
|
|
|
void copyToArray(T* array) const {
|
|
|
|
this->INHERITED::copyToArray((void**)array);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef SkPtrSet INHERITED;
|
|
|
|
};
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#endif
|