Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(256)

Side by Side Diff: skia/ext/refptr.h

Issue 15004024: Only use skia::RefPtr for refcounting (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comment rewording Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « printing/pdf_metafile_skia.cc ('k') | ui/gfx/render_text.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // If a Skia API returns a raw pointer that already has an owner, then use
vandebo (ex-Chrome) 2013/05/17 20:47:49 The object under consideration is reference counte
enne (OOO) 2013/05/17 21:37:02 Incorporated most of that wording.
35 // the ShareRef helper to have a RefPtr share that ref with the other owner.
36 // skia::RefPtr<SkShader> shader = skia::ShareRef(paint.getShader());
37 //
34 // Never call ref() or unref() on the underlying ref-counted pointer. If you 38 // 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 39 // 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 40 // with skia::RefPtr instances instead, the ref-counting will be taken care of
37 // for you. 41 // for you.
38 template<typename T> 42 template<typename T>
39 class RefPtr { 43 class RefPtr {
40 public: 44 public:
41 RefPtr() : ptr_(NULL) {} 45 RefPtr() : ptr_(NULL) {}
42 46
43 RefPtr(const RefPtr& other) 47 RefPtr(const RefPtr& other)
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 T* operator->() const { return ptr_; } 81 T* operator->() const { return ptr_; }
78 82
79 typedef T* RefPtr::*unspecified_bool_type; 83 typedef T* RefPtr::*unspecified_bool_type;
80 operator unspecified_bool_type() const { 84 operator unspecified_bool_type() const {
81 return ptr_ ? &RefPtr::ptr_ : NULL; 85 return ptr_ ? &RefPtr::ptr_ : NULL;
82 } 86 }
83 87
84 private: 88 private:
85 T* ptr_; 89 T* ptr_;
86 90
91 // This function cannot be public because Skia starts its ref-counted
92 // objects at refcnt=1. This makes it impossible to differentiate
93 // between a newly created object (that doesn't need to be ref'd) or an
94 // already existing object with one owner (that does to be ref'd so that
vandebo (ex-Chrome) 2013/05/17 20:47:49 typo: ...that does *need* to be ref'd...
enne (OOO) 2013/05/17 21:37:02 Done.
95 // this RefPtr can also manage its lifetime).
87 explicit RefPtr(T* ptr) : ptr_(ptr) {} 96 explicit RefPtr(T* ptr) : ptr_(ptr) {}
88 97
89 template<typename U> 98 template<typename U>
90 friend RefPtr<U> AdoptRef(U* ptr); 99 friend RefPtr<U> AdoptRef(U* ptr);
100
101 template<typename U>
102 friend RefPtr<U> ShareRef(U* ptr);
91 }; 103 };
92 104
105 // For newly created raw pointers without any owners.
vandebo (ex-Chrome) 2013/05/17 20:47:49 Technically the object doesn't need to be newly cr
enne (OOO) 2013/05/17 21:37:02 Done.
93 template<typename T> 106 template<typename T>
94 RefPtr<T> AdoptRef(T* ptr) { return RefPtr<T>(ptr); } 107 RefPtr<T> AdoptRef(T* ptr) { return RefPtr<T>(ptr); }
95 108
109 // For pointers already owned. Doesn't take ownership of existing references.
vandebo (ex-Chrome) 2013/05/17 20:47:49 Suggestion: For objects that do not have unowned r
enne (OOO) 2013/05/17 21:37:02 Incorporated that wording into this comment.
110 template<typename T>
111 RefPtr<T> ShareRef(T* ptr) { return RefPtr<T>(SkSafeRef(ptr)); }
112
96 } // namespace skia 113 } // namespace skia
97 114
98 #endif // SKIA_EXT_REFPTR_H_ 115 #endif // SKIA_EXT_REFPTR_H_
OLDNEW
« no previous file with comments | « printing/pdf_metafile_skia.cc ('k') | ui/gfx/render_text.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698