gfx/skia/trunk/src/core/SkPtrRecorder.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/core/SkPtrRecorder.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,151 @@
     1.4 +
     1.5 +/*
     1.6 + * Copyright 2008 The Android Open Source Project
     1.7 + *
     1.8 + * Use of this source code is governed by a BSD-style license that can be
     1.9 + * found in the LICENSE file.
    1.10 + */
    1.11 +
    1.12 +
    1.13 +#ifndef SkPtrSet_DEFINED
    1.14 +#define SkPtrSet_DEFINED
    1.15 +
    1.16 +#include "SkRefCnt.h"
    1.17 +#include "SkFlattenable.h"
    1.18 +#include "SkTDArray.h"
    1.19 +
    1.20 +/**
    1.21 + *  Maintains a set of ptrs, assigning each a unique ID [1...N]. Duplicate ptrs
    1.22 + *  return the same ID (since its a set). Subclasses can override inPtr()
    1.23 + *  and decPtr(). incPtr() is called each time a unique ptr is added ot the
    1.24 + *  set. decPtr() is called on each ptr when the set is destroyed or reset.
    1.25 + */
    1.26 +class SkPtrSet : public SkRefCnt {
    1.27 +public:
    1.28 +    SK_DECLARE_INST_COUNT(SkPtrSet)
    1.29 +
    1.30 +    /**
    1.31 +     *  Search for the specified ptr in the set. If it is found, return its
    1.32 +     *  32bit ID [1..N], or if not found, return 0. Always returns 0 for NULL.
    1.33 +     */
    1.34 +    uint32_t find(void*) const;
    1.35 +
    1.36 +    /**
    1.37 +     *  Add the specified ptr to the set, returning a unique 32bit ID for it
    1.38 +     *  [1...N]. Duplicate ptrs will return the same ID.
    1.39 +     *
    1.40 +     *  If the ptr is NULL, it is not added, and 0 is returned.
    1.41 +     */
    1.42 +    uint32_t add(void*);
    1.43 +
    1.44 +    /**
    1.45 +     *  Return the number of (non-null) ptrs in the set.
    1.46 +     */
    1.47 +    int count() const { return fList.count(); }
    1.48 +
    1.49 +    /**
    1.50 +     *  Copy the ptrs in the set into the specified array (allocated by the
    1.51 +     *  caller). The ptrs are assgined to the array based on their corresponding
    1.52 +     *  ID. e.g. array[ptr.ID - 1] = ptr.
    1.53 +     *
    1.54 +     *  incPtr() and decPtr() are not called during this operation.
    1.55 +     */
    1.56 +    void copyToArray(void* array[]) const;
    1.57 +
    1.58 +    /**
    1.59 +     *  Call decPtr() on each ptr in the set, and the reset the size of the set
    1.60 +     *  to 0.
    1.61 +     */
    1.62 +    void reset();
    1.63 +
    1.64 +protected:
    1.65 +    virtual void incPtr(void*) {}
    1.66 +    virtual void decPtr(void*) {}
    1.67 +
    1.68 +private:
    1.69 +    struct Pair {
    1.70 +        void*       fPtr;   // never NULL
    1.71 +        uint32_t    fIndex; // 1...N
    1.72 +    };
    1.73 +
    1.74 +    // we store the ptrs in sorted-order (using Cmp) so that we can efficiently
    1.75 +    // detect duplicates when add() is called. Hence we need to store the
    1.76 +    // ptr and its ID/fIndex explicitly, since the ptr's position in the array
    1.77 +    // is not related to its "index".
    1.78 +    SkTDArray<Pair>  fList;
    1.79 +
    1.80 +    static bool Less(const Pair& a, const Pair& b);
    1.81 +
    1.82 +    typedef SkRefCnt INHERITED;
    1.83 +};
    1.84 +
    1.85 +/**
    1.86 + *  Templated wrapper for SkPtrSet, just meant to automate typecasting
    1.87 + *  parameters to and from void* (which the base class expects).
    1.88 + */
    1.89 +template <typename T> class SkTPtrSet : public SkPtrSet {
    1.90 +public:
    1.91 +    uint32_t find(T ptr) {
    1.92 +        return this->INHERITED::find((void*)ptr);
    1.93 +    }
    1.94 +    uint32_t add(T ptr) {
    1.95 +        return this->INHERITED::add((void*)ptr);
    1.96 +    }
    1.97 +
    1.98 +    void copyToArray(T* array) const {
    1.99 +        this->INHERITED::copyToArray((void**)array);
   1.100 +    }
   1.101 +
   1.102 +private:
   1.103 +    typedef SkPtrSet INHERITED;
   1.104 +};
   1.105 +
   1.106 +/**
   1.107 + *  Subclass of SkTPtrSet specialed to call ref() and unref() when the
   1.108 + *  base class's incPtr() and decPtr() are called. This makes it a valid owner
   1.109 + *  of each ptr, which is released when the set is reset or destroyed.
   1.110 + */
   1.111 +class SkRefCntSet : public SkTPtrSet<SkRefCnt*> {
   1.112 +public:
   1.113 +    virtual ~SkRefCntSet();
   1.114 +
   1.115 +protected:
   1.116 +    // overrides
   1.117 +    virtual void incPtr(void*);
   1.118 +    virtual void decPtr(void*);
   1.119 +};
   1.120 +
   1.121 +class SkFactorySet : public SkTPtrSet<SkFlattenable::Factory> {};
   1.122 +
   1.123 +/**
   1.124 + * Similar to SkFactorySet, but only allows Factorys that have registered names.
   1.125 + * Also has a function to return the next added Factory's name.
   1.126 + */
   1.127 +class SkNamedFactorySet : public SkRefCnt {
   1.128 +public:
   1.129 +    SK_DECLARE_INST_COUNT(SkNamedFactorySet)
   1.130 +
   1.131 +    SkNamedFactorySet();
   1.132 +
   1.133 +    /**
   1.134 +     * Find the specified Factory in the set. If it is not already in the set,
   1.135 +     * and has registered its name, add it to the set, and return its index.
   1.136 +     * If the Factory has no registered name, return 0.
   1.137 +     */
   1.138 +    uint32_t find(SkFlattenable::Factory);
   1.139 +
   1.140 +    /**
   1.141 +     * If new Factorys have been added to the set, return the name of the first
   1.142 +     * Factory added after the Factory name returned by the last call to this
   1.143 +     * function.
   1.144 +     */
   1.145 +    const char* getNextAddedFactoryName();
   1.146 +private:
   1.147 +    int                    fNextAddedFactory;
   1.148 +    SkFactorySet           fFactorySet;
   1.149 +    SkTDArray<const char*> fNames;
   1.150 +
   1.151 +    typedef SkRefCnt INHERITED;
   1.152 +};
   1.153 +
   1.154 +#endif

mercurial