OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef SKIA_EXT_REFPTR_H_ | 5 #ifndef SKIA_EXT_REFPTR_H_ |
6 #define SKIA_EXT_REFPTR_H_ | 6 #define SKIA_EXT_REFPTR_H_ |
7 | 7 |
8 #include <algorithm> | 8 #include <algorithm> |
9 | 9 |
10 #include "base/move.h" | |
11 #include "third_party/skia/include/core/SkRefCnt.h" | 10 #include "third_party/skia/include/core/SkRefCnt.h" |
12 | 11 |
13 namespace skia { | 12 namespace skia { |
14 | 13 |
15 // When creating/receiving a ref-counted pointer from Skia, wrap that pointer in | 14 // When creating/receiving a ref-counted pointer from Skia, wrap that pointer in |
16 // this class to avoid dealing with the ref-counting and prevent leaks/crashes | 15 // this class to avoid dealing with the ref-counting and prevent leaks/crashes |
17 // due to ref-counting bugs. | 16 // due to ref-counting bugs. |
18 // | 17 // |
19 // Example of creating a new SkShader* and setting it on a SkPaint: | 18 // Example of creating a new SkShader* and setting it on a SkPaint: |
20 // skia::RefPtr<SkShader> shader = skia::AdoptRef(SkGradientShader::Create()); | 19 // skia::RefPtr<SkShader> shader = skia::AdoptRef(SkGradientShader::Create()); |
(...skipping 24 matching lines...) Expand all Loading... | |
45 // | 44 // |
46 // skia::RefPtr<SkShader> shader = ...; | 45 // skia::RefPtr<SkShader> shader = ...; |
47 // UseThisShader(shader.Pass()); | 46 // UseThisShader(shader.Pass()); |
48 // | 47 // |
49 // Never call ref() or unref() on the underlying ref-counted pointer. If you | 48 // Never call ref() or unref() on the underlying ref-counted pointer. If you |
50 // AdoptRef() the raw pointer immediately into a skia::RefPtr and always work | 49 // AdoptRef() the raw pointer immediately into a skia::RefPtr and always work |
51 // with skia::RefPtr instances instead, the ref-counting will be taken care of | 50 // with skia::RefPtr instances instead, the ref-counting will be taken care of |
52 // for you. | 51 // for you. |
53 template<typename T> | 52 template<typename T> |
54 class RefPtr { | 53 class RefPtr { |
55 TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(RefPtr) | |
56 public: | 54 public: |
57 RefPtr() : ptr_(nullptr) {} | 55 RefPtr() : ptr_(nullptr) {} |
58 | 56 |
59 RefPtr(decltype(nullptr)) : ptr_(nullptr) {} | 57 RefPtr(decltype(nullptr)) : ptr_(nullptr) {} |
60 | 58 |
61 RefPtr(const RefPtr& other) | 59 RefPtr(const RefPtr& other) |
62 : ptr_(other.get()) { | 60 : ptr_(other.get()) { |
63 SkSafeRef(ptr_); | 61 SkSafeRef(ptr_); |
64 } | 62 } |
65 | 63 |
66 template<typename U> | 64 template<typename U> |
67 RefPtr(const RefPtr<U>& other) | 65 RefPtr(const RefPtr<U>& other) |
68 : ptr_(other.get()) { | 66 : ptr_(other.get()) { |
69 SkSafeRef(ptr_); | 67 SkSafeRef(ptr_); |
70 } | 68 } |
71 | 69 |
72 template <typename U> | 70 template <typename U> |
73 RefPtr(RefPtr<U>&& other) | 71 RefPtr(RefPtr<U>&& other) |
Nico
2015/11/25 14:53:45
I suppose dcheng's comment applies here too
| |
74 : ptr_(other.get()) { | 72 : ptr_(other.get()) { |
75 other.ptr_ = nullptr; | 73 other.ptr_ = nullptr; |
76 } | 74 } |
77 | 75 |
78 ~RefPtr() { | 76 ~RefPtr() { |
79 clear(); | 77 clear(); |
80 } | 78 } |
81 | 79 |
82 RefPtr& operator=(decltype(nullptr)) { | 80 RefPtr& operator=(decltype(nullptr)) { |
83 clear(); | 81 clear(); |
84 return *this; | 82 return *this; |
85 } | 83 } |
86 | 84 |
87 RefPtr& operator=(const RefPtr& other) { | 85 RefPtr& operator=(const RefPtr& other) { |
88 SkRefCnt_SafeAssign(ptr_, other.get()); | 86 SkRefCnt_SafeAssign(ptr_, other.get()); |
89 return *this; | 87 return *this; |
90 } | 88 } |
91 | 89 |
92 template<typename U> | 90 template<typename U> |
93 RefPtr& operator=(const RefPtr<U>& other) { | 91 RefPtr& operator=(const RefPtr<U>& other) { |
94 SkRefCnt_SafeAssign(ptr_, other.get()); | 92 SkRefCnt_SafeAssign(ptr_, other.get()); |
95 return *this; | 93 return *this; |
96 } | 94 } |
97 | 95 |
98 template <typename U> | 96 template <typename U> |
99 RefPtr& operator=(RefPtr<U>&& other) { | 97 RefPtr& operator=(RefPtr<U>&& other) { |
100 RefPtr<T> temp(other.Pass()); | 98 RefPtr<T> temp(std::move(other)); |
101 std::swap(ptr_, temp.ptr_); | 99 std::swap(ptr_, temp.ptr_); |
102 return *this; | 100 return *this; |
103 } | 101 } |
104 | 102 |
105 void clear() { | 103 void clear() { |
106 T* to_unref = ptr_; | 104 T* to_unref = ptr_; |
107 ptr_ = nullptr; | 105 ptr_ = nullptr; |
108 SkSafeUnref(to_unref); | 106 SkSafeUnref(to_unref); |
109 } | 107 } |
110 | 108 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
142 RefPtr<T> AdoptRef(T* ptr) { return RefPtr<T>(ptr); } | 140 RefPtr<T> AdoptRef(T* ptr) { return RefPtr<T>(ptr); } |
143 | 141 |
144 // For objects that are already owned. This doesn't take ownership of existing | 142 // For objects that are already owned. This doesn't take ownership of existing |
145 // references and adds a new one. | 143 // references and adds a new one. |
146 template<typename T> | 144 template<typename T> |
147 RefPtr<T> SharePtr(T* ptr) { return RefPtr<T>(SkSafeRef(ptr)); } | 145 RefPtr<T> SharePtr(T* ptr) { return RefPtr<T>(SkSafeRef(ptr)); } |
148 | 146 |
149 } // namespace skia | 147 } // namespace skia |
150 | 148 |
151 #endif // SKIA_EXT_REFPTR_H_ | 149 #endif // SKIA_EXT_REFPTR_H_ |
OLD | NEW |