1/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkRefSet_DEFINED
9#define SkRefSet_DEFINED
10
11#include "SkRefCnt.h"
12#include "SkTDArray.h"
13
14template <typename T> class SkRefSet {
15public:
16    ~SkRefSet() { fArray.unrefAll(); }
17
18    T* get(int index) const {
19        SkASSERT((unsigned)index < (unsigned)fArray.count());
20        return fArray[index];
21    }
22
23    bool set(int index, T* value) {
24        if ((unsigned)index < (unsigned)fArray.count()) {
25            SkRefCnt_SafeAssign(fArray[index], value);
26            return true;
27        }
28        if (fArray.count() == index && value) {
29            *fArray.append() = SkRef(value);
30            return true;
31        }
32        SkDebugf("SkRefSet: index [%d] out of range %d\n", index, fArray.count());
33        return false;
34    }
35
36private:
37    SkTDArray<T*> fArray;
38};
39
40#endif
41