180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2008 The Android Open Source Project
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifndef SkPtrSet_DEFINED
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SkPtrSet_DEFINED
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkRefCnt.h"
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkFlattenable.h"
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkTDArray.h"
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/**
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *  Maintains a set of ptrs, assigning each a unique ID [1...N]. Duplicate ptrs
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *  return the same ID (since its a set). Subclasses can override inPtr()
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *  and decPtr(). incPtr() is called each time a unique ptr is added ot the
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *  set. decPtr() is called on each ptr when the set is destroyed or reset.
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass SkPtrSet : public SkRefCnt {
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic:
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SK_DECLARE_INST_COUNT(SkPtrSet)
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  Search for the specified ptr in the set. If it is found, return its
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  32bit ID [1..N], or if not found, return 0. Always returns 0 for NULL.
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    uint32_t find(void*) const;
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  Add the specified ptr to the set, returning a unique 32bit ID for it
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  [1...N]. Duplicate ptrs will return the same ID.
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  If the ptr is NULL, it is not added, and 0 is returned.
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    uint32_t add(void*);
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  Return the number of (non-null) ptrs in the set.
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    int count() const { return fList.count(); }
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  Copy the ptrs in the set into the specified array (allocated by the
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  caller). The ptrs are assgined to the array based on their corresponding
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  ID. e.g. array[ptr.ID - 1] = ptr.
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  incPtr() and decPtr() are not called during this operation.
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void copyToArray(void* array[]) const;
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  Call decPtr() on each ptr in the set, and the reset the size of the set
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  to 0.
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void reset();
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprotected:
62096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger    virtual void incPtr(void*) {}
63096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger    virtual void decPtr(void*) {}
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprivate:
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    struct Pair {
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        void*       fPtr;   // never NULL
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        uint32_t    fIndex; // 1...N
6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    };
7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // we store the ptrs in sorted-order (using Cmp) so that we can efficiently
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // detect duplicates when add() is called. Hence we need to store the
7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // ptr and its ID/fIndex explicitly, since the ptr's position in the array
7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // is not related to its "index".
7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkTDArray<Pair>  fList;
7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
777839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger    static bool Less(const Pair& a, const Pair& b);
7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    typedef SkRefCnt INHERITED;
8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/**
8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *  Templated wrapper for SkPtrSet, just meant to automate typecasting
8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *  parameters to and from void* (which the base class expects).
8580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
8680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querutemplate <typename T> class SkTPtrSet : public SkPtrSet {
8780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic:
8880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    uint32_t find(T ptr) {
8980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return this->INHERITED::find((void*)ptr);
9080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
9180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    uint32_t add(T ptr) {
9280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return this->INHERITED::add((void*)ptr);
9380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
9480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void copyToArray(T* array) const {
9680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        this->INHERITED::copyToArray((void**)array);
9780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
9880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprivate:
10080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    typedef SkPtrSet INHERITED;
10180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
10280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/**
10480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *  Subclass of SkTPtrSet specialed to call ref() and unref() when the
10580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *  base class's incPtr() and decPtr() are called. This makes it a valid owner
10680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *  of each ptr, which is released when the set is reset or destroyed.
10780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
10880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass SkRefCntSet : public SkTPtrSet<SkRefCnt*> {
10980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic:
11080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual ~SkRefCntSet();
11180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
11280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprotected:
11380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // overrides
11480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual void incPtr(void*);
11580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual void decPtr(void*);
11680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
11780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
11880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass SkFactorySet : public SkTPtrSet<SkFlattenable::Factory> {};
11980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
12080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/**
12180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Similar to SkFactorySet, but only allows Factorys that have registered names.
12280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Also has a function to return the next added Factory's name.
12380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
12480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass SkNamedFactorySet : public SkRefCnt {
12580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic:
12680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SK_DECLARE_INST_COUNT(SkNamedFactorySet)
12780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
12880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkNamedFactorySet();
12980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
13080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
13180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     * Find the specified Factory in the set. If it is not already in the set,
13280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     * and has registered its name, add it to the set, and return its index.
13380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     * If the Factory has no registered name, return 0.
13480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
13580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    uint32_t find(SkFlattenable::Factory);
13680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
13780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
13880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     * If new Factorys have been added to the set, return the name of the first
13980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     * Factory added after the Factory name returned by the last call to this
14080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     * function.
14180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
14280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    const char* getNextAddedFactoryName();
14380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprivate:
14480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    int                    fNextAddedFactory;
14580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkFactorySet           fFactorySet;
14680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkTDArray<const char*> fNames;
14780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
14880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    typedef SkRefCnt INHERITED;
14980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
15080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
15180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
152