reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 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" |
Ben Wagner | 36fe60d | 2018-07-10 17:38:12 -0400 | [diff] [blame] | 12 | #include "SkTArray.h" |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 13 | |
| 14 | template <typename T> class SkRefSet { |
| 15 | public: |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 16 | T* get(int index) const { |
| 17 | SkASSERT((unsigned)index < (unsigned)fArray.count()); |
Ben Wagner | 36fe60d | 2018-07-10 17:38:12 -0400 | [diff] [blame] | 18 | return fArray[index].get(); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 19 | } |
| 20 | |
Ben Wagner | 36fe60d | 2018-07-10 17:38:12 -0400 | [diff] [blame] | 21 | bool set(int index, sk_sp<T> value) { |
| 22 | if (index < fArray.count()) { |
| 23 | fArray[index] = std::move(value); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 24 | return true; |
| 25 | } |
| 26 | if (fArray.count() == index && value) { |
Ben Wagner | 36fe60d | 2018-07-10 17:38:12 -0400 | [diff] [blame] | 27 | fArray.emplace_back(std::move(value)); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 28 | return true; |
| 29 | } |
| 30 | SkDebugf("SkRefSet: index [%d] out of range %d\n", index, fArray.count()); |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | private: |
Ben Wagner | 36fe60d | 2018-07-10 17:38:12 -0400 | [diff] [blame] | 35 | SkTArray<sk_sp<T>, true> fArray; |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | #endif |