aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/pipe/SkRefSet.h
blob: 65fe7af1eb28b38becef95611f00eb4acc30ed30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SkRefSet_DEFINED
#define SkRefSet_DEFINED

#include "SkRefCnt.h"
#include "SkTArray.h"

template <typename T> class SkRefSet {
public:
    T* get(int index) const {
        SkASSERT((unsigned)index < (unsigned)fArray.count());
        return fArray[index].get();
    }

    bool set(int index, sk_sp<T> value) {
        if (index < fArray.count()) {
            fArray[index] = std::move(value);
            return true;
        }
        if (fArray.count() == index && value) {
            fArray.emplace_back(std::move(value));
            return true;
        }
        SkDebugf("SkRefSet: index [%d] out of range %d\n", index, fArray.count());
        return false;
    }

private:
    SkTArray<sk_sp<T>, true> fArray;
};

#endif