| 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 #include <cstddef> |
| 9 | 10 |
| 10 #include "base/move.h" | |
| 11 #include "third_party/skia/include/core/SkRefCnt.h" | 11 #include "third_party/skia/include/core/SkRefCnt.h" |
| 12 | 12 |
| 13 namespace skia { | 13 namespace skia { |
| 14 | 14 |
| 15 // When creating/receiving a ref-counted pointer from Skia, wrap that pointer in | 15 // 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 | 16 // this class to avoid dealing with the ref-counting and prevent leaks/crashes |
| 17 // due to ref-counting bugs. | 17 // due to ref-counting bugs. |
| 18 // | 18 // |
| 19 // Example of creating a new SkShader* and setting it on a SkPaint: | 19 // Example of creating a new SkShader* and setting it on a SkPaint: |
| 20 // skia::RefPtr<SkShader> shader = skia::AdoptRef(SkGradientShader::Create()); | 20 // skia::RefPtr<SkShader> shader = skia::AdoptRef(SkGradientShader::Create()); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 45 // | 45 // |
| 46 // skia::RefPtr<SkShader> shader = ...; | 46 // skia::RefPtr<SkShader> shader = ...; |
| 47 // UseThisShader(shader.Pass()); | 47 // UseThisShader(shader.Pass()); |
| 48 // | 48 // |
| 49 // Never call ref() or unref() on the underlying ref-counted pointer. If you | 49 // 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 | 50 // 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 | 51 // with skia::RefPtr instances instead, the ref-counting will be taken care of |
| 52 // for you. | 52 // for you. |
| 53 template<typename T> | 53 template<typename T> |
| 54 class RefPtr { | 54 class RefPtr { |
| 55 TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(RefPtr) | |
| 56 public: | 55 public: |
| 57 RefPtr() : ptr_(nullptr) {} | 56 RefPtr() : ptr_(nullptr) {} |
| 58 | 57 |
| 59 RefPtr(decltype(nullptr)) : ptr_(nullptr) {} | 58 RefPtr(std::nullptr_t) : ptr_(nullptr) {} |
| 60 | 59 |
| 60 // Copy constructor. |
| 61 RefPtr(const RefPtr& other) | 61 RefPtr(const RefPtr& other) |
| 62 : ptr_(other.get()) { | 62 : ptr_(other.get()) { |
| 63 SkSafeRef(ptr_); | 63 SkSafeRef(ptr_); |
| 64 } | 64 } |
| 65 | 65 |
| 66 // Copy conversion constructor. |
| 66 template<typename U> | 67 template<typename U> |
| 67 RefPtr(const RefPtr<U>& other) | 68 RefPtr(const RefPtr<U>& other) |
| 68 : ptr_(other.get()) { | 69 : ptr_(other.get()) { |
| 69 SkSafeRef(ptr_); | 70 SkSafeRef(ptr_); |
| 70 } | 71 } |
| 71 | 72 |
| 73 // Move constructor. This is required in addition to the conversion |
| 74 // constructor below in order for clang to warn about pessimizing moves. |
| 75 RefPtr(RefPtr&& other) : ptr_(other.get()) { other.ptr_ = nullptr; } |
| 76 |
| 77 // Move conversion constructor. |
| 72 template <typename U> | 78 template <typename U> |
| 73 RefPtr(RefPtr<U>&& other) | 79 RefPtr(RefPtr<U>&& other) |
| 74 : ptr_(other.get()) { | 80 : ptr_(other.get()) { |
| 75 other.ptr_ = nullptr; | 81 other.ptr_ = nullptr; |
| 76 } | 82 } |
| 77 | 83 |
| 78 ~RefPtr() { | 84 ~RefPtr() { |
| 79 clear(); | 85 clear(); |
| 80 } | 86 } |
| 81 | 87 |
| 82 RefPtr& operator=(decltype(nullptr)) { | 88 RefPtr& operator=(std::nullptr_t) { |
| 83 clear(); | 89 clear(); |
| 84 return *this; | 90 return *this; |
| 85 } | 91 } |
| 86 | 92 |
| 87 RefPtr& operator=(const RefPtr& other) { | 93 RefPtr& operator=(const RefPtr& other) { |
| 88 SkRefCnt_SafeAssign(ptr_, other.get()); | 94 SkRefCnt_SafeAssign(ptr_, other.get()); |
| 89 return *this; | 95 return *this; |
| 90 } | 96 } |
| 91 | 97 |
| 92 template<typename U> | 98 template<typename U> |
| 93 RefPtr& operator=(const RefPtr<U>& other) { | 99 RefPtr& operator=(const RefPtr<U>& other) { |
| 94 SkRefCnt_SafeAssign(ptr_, other.get()); | 100 SkRefCnt_SafeAssign(ptr_, other.get()); |
| 95 return *this; | 101 return *this; |
| 96 } | 102 } |
| 97 | 103 |
| 98 template <typename U> | 104 template <typename U> |
| 99 RefPtr& operator=(RefPtr<U>&& other) { | 105 RefPtr& operator=(RefPtr<U>&& other) { |
| 100 RefPtr<T> temp(other.Pass()); | 106 RefPtr<T> temp(std::move(other)); |
| 101 std::swap(ptr_, temp.ptr_); | 107 std::swap(ptr_, temp.ptr_); |
| 102 return *this; | 108 return *this; |
| 103 } | 109 } |
| 104 | 110 |
| 105 void clear() { | 111 void clear() { |
| 106 T* to_unref = ptr_; | 112 T* to_unref = ptr_; |
| 107 ptr_ = nullptr; | 113 ptr_ = nullptr; |
| 108 SkSafeUnref(to_unref); | 114 SkSafeUnref(to_unref); |
| 109 } | 115 } |
| 110 | 116 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 RefPtr<T> AdoptRef(T* ptr) { return RefPtr<T>(ptr); } | 148 RefPtr<T> AdoptRef(T* ptr) { return RefPtr<T>(ptr); } |
| 143 | 149 |
| 144 // For objects that are already owned. This doesn't take ownership of existing | 150 // For objects that are already owned. This doesn't take ownership of existing |
| 145 // references and adds a new one. | 151 // references and adds a new one. |
| 146 template<typename T> | 152 template<typename T> |
| 147 RefPtr<T> SharePtr(T* ptr) { return RefPtr<T>(SkSafeRef(ptr)); } | 153 RefPtr<T> SharePtr(T* ptr) { return RefPtr<T>(SkSafeRef(ptr)); } |
| 148 | 154 |
| 149 } // namespace skia | 155 } // namespace skia |
| 150 | 156 |
| 151 #endif // SKIA_EXT_REFPTR_H_ | 157 #endif // SKIA_EXT_REFPTR_H_ |
| OLD | NEW |