Line data Source code
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 :
14 0 : template <typename T> class SkRefSet {
15 : public:
16 0 : ~SkRefSet() { fArray.unrefAll(); }
17 :
18 0 : T* get(int index) const {
19 0 : SkASSERT((unsigned)index < (unsigned)fArray.count());
20 0 : return fArray[index];
21 : }
22 :
23 0 : bool set(int index, T* value) {
24 0 : if ((unsigned)index < (unsigned)fArray.count()) {
25 0 : SkRefCnt_SafeAssign(fArray[index], value);
26 0 : return true;
27 : }
28 0 : if (fArray.count() == index && value) {
29 0 : *fArray.append() = SkRef(value);
30 0 : return true;
31 : }
32 0 : SkDebugf("SkRefSet: index [%d] out of range %d\n", index, fArray.count());
33 0 : return false;
34 : }
35 :
36 : private:
37 : SkTDArray<T*> fArray;
38 : };
39 :
40 : #endif
|