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 "third_party/skia/include/core/SkRefCnt.h" | 8 #include "third_party/skia/include/core/SkRefCnt.h" |
9 | 9 |
10 namespace skia { | 10 namespace skia { |
11 | 11 |
12 // When creating/receiving a ref-counted pointer from Skia, wrap that pointer in | 12 // When creating/receiving a ref-counted pointer from Skia, wrap that pointer in |
13 // this class to avoid dealing with the ref-counting and prevent leaks/crashes | 13 // this class to avoid dealing with the ref-counting and prevent leaks/crashes |
14 // due to ref-counting bugs. | 14 // due to ref-counting bugs. |
15 // | 15 // |
16 // Example of Creating an SkShader* and setting it on a SkPaint: | 16 // Example of creating a new SkShader* and setting it on a SkPaint: |
17 // skia::RefPtr<SkShader> shader = skia::AdoptRef(SkGradientShader::Create()); | 17 // skia::RefPtr<SkShader> shader = skia::AdoptRef(SkGradientShader::Create()); |
18 // paint.setShader(shader.get()); | 18 // paint.setShader(shader.get()); |
19 // | 19 // |
20 // When passing around a ref-counted pointer to methods outside of Skia, always | 20 // When passing around a ref-counted pointer to methods outside of Skia, always |
21 // pass around the skia::RefPtr instead of the raw pointer. An example method | 21 // pass around the skia::RefPtr instead of the raw pointer. An example method |
22 // that takes a SkShader* parameter and saves the SkShader* in the class. | 22 // that takes a SkShader* parameter and saves the SkShader* in the class. |
23 // void AMethodThatSavesAShader(const skia::RefPtr<SkShader>& shader) { | 23 // void AMethodThatSavesAShader(const skia::RefPtr<SkShader>& shader) { |
24 // member_refptr_ = shader; | 24 // member_refptr_ = shader; |
25 // } | 25 // } |
26 // skia::RefPtr<SkShader> member_refptr_; | 26 // skia::RefPtr<SkShader> member_refptr_; |
27 // | 27 // |
28 // When returning a ref-counted ponter, also return the skia::RefPtr instead. An | 28 // When returning a ref-counted pointer, also return the skia::RefPtr instead. |
29 // example method that creates an SkShader* and returns it: | 29 // An example method that creates an SkShader* and returns it: |
30 // skia::RefPtr<SkShader> MakeAShader() { | 30 // skia::RefPtr<SkShader> MakeAShader() { |
31 // return skia::AdoptRef(SkGradientShader::Create()); | 31 // return skia::AdoptRef(SkGradientShader::Create()); |
32 // } | 32 // } |
33 // | 33 // |
| 34 // To take a scoped reference to an object whose references are all owned |
| 35 // by other objects (i.e. does not have one that needs to be adopted) use the |
| 36 // skia::SharePtr helper: |
| 37 // |
| 38 // skia::RefPtr<SkShader> shader = skia::SharePtr(paint.getShader()); |
| 39 // |
34 // Never call ref() or unref() on the underlying ref-counted pointer. If you | 40 // Never call ref() or unref() on the underlying ref-counted pointer. If you |
35 // AdoptRef() the raw pointer immediately into a skia::RefPtr and always work | 41 // AdoptRef() the raw pointer immediately into a skia::RefPtr and always work |
36 // with skia::RefPtr instances instead, the ref-counting will be taken care of | 42 // with skia::RefPtr instances instead, the ref-counting will be taken care of |
37 // for you. | 43 // for you. |
38 template<typename T> | 44 template<typename T> |
39 class RefPtr { | 45 class RefPtr { |
40 public: | 46 public: |
41 RefPtr() : ptr_(NULL) {} | 47 RefPtr() : ptr_(NULL) {} |
42 | 48 |
43 RefPtr(const RefPtr& other) | 49 RefPtr(const RefPtr& other) |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 T* operator->() const { return ptr_; } | 83 T* operator->() const { return ptr_; } |
78 | 84 |
79 typedef T* RefPtr::*unspecified_bool_type; | 85 typedef T* RefPtr::*unspecified_bool_type; |
80 operator unspecified_bool_type() const { | 86 operator unspecified_bool_type() const { |
81 return ptr_ ? &RefPtr::ptr_ : NULL; | 87 return ptr_ ? &RefPtr::ptr_ : NULL; |
82 } | 88 } |
83 | 89 |
84 private: | 90 private: |
85 T* ptr_; | 91 T* ptr_; |
86 | 92 |
| 93 // This function cannot be public because Skia starts its ref-counted |
| 94 // objects at refcnt=1. This makes it impossible to differentiate |
| 95 // between a newly created object (that doesn't need to be ref'd) or an |
| 96 // already existing object with one owner (that does need to be ref'd so that |
| 97 // this RefPtr can also manage its lifetime). |
87 explicit RefPtr(T* ptr) : ptr_(ptr) {} | 98 explicit RefPtr(T* ptr) : ptr_(ptr) {} |
88 | 99 |
89 template<typename U> | 100 template<typename U> |
90 friend RefPtr<U> AdoptRef(U* ptr); | 101 friend RefPtr<U> AdoptRef(U* ptr); |
| 102 |
| 103 template<typename U> |
| 104 friend RefPtr<U> SharePtr(U* ptr); |
91 }; | 105 }; |
92 | 106 |
| 107 // For objects that have an unowned reference (such as newly created objects). |
93 template<typename T> | 108 template<typename T> |
94 RefPtr<T> AdoptRef(T* ptr) { return RefPtr<T>(ptr); } | 109 RefPtr<T> AdoptRef(T* ptr) { return RefPtr<T>(ptr); } |
95 | 110 |
| 111 // For objects that are already owned. This doesn't take ownership of existing |
| 112 // references and adds a new one. |
| 113 template<typename T> |
| 114 RefPtr<T> SharePtr(T* ptr) { return RefPtr<T>(SkSafeRef(ptr)); } |
| 115 |
96 } // namespace skia | 116 } // namespace skia |
97 | 117 |
98 #endif // SKIA_EXT_REFPTR_H_ | 118 #endif // SKIA_EXT_REFPTR_H_ |
OLD | NEW |