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

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

Issue 11418217: Add skia::RefPtr class to wrap ref counted classes from Skia. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Drop TNoRef Created 8 years 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef SKIA_EXT_REFPTR_H_
6 #define SKIA_EXT_REFPTR_H_
7
8 #include "third_party/skia/include/core/SkRefCnt.h"
9
10 namespace skia {
11
12 template<typename T>
jamesr 2012/11/29 07:34:06 could you add some class-level comments indicating
danakj 2012/11/29 21:37:11 Done.
13 class RefPtr {
14 public:
15 RefPtr()
16 : ptr_(NULL) {
17 }
18
19 RefPtr(const RefPtr& other)
20 : ptr_(other.ptr_) {
21 SkSafeRef(ptr_);
22 }
23
24 // Skia objects come with a reference of 1, steal that reference here to
25 // take ownership.
26 RefPtr(T* ptr) : ptr_(ptr) {}
Stephen White 2012/11/29 18:42:04 Out of curiosity, why did you decide on a public c
danakj 2012/11/29 21:37:11 I was having difficulty making an Adopt() method t
27
28 ~RefPtr() {
29 clear();
30 }
31
32 // Skia objects come with a reference of 1, steal that reference here to
33 // take ownership.
jamesr 2012/11/29 07:34:06 this comment's repeated and i can't really tell w
danakj 2012/11/29 21:37:11 This, and the constructor, take a raw skia pointer
danakj 2012/11/29 21:38:02 Actually with AdoptRef() this problem goes away :)
34 RefPtr& operator=(T* ptr) {
35 clear();
36 ptr_ = ptr;
37 return *this;
38 }
39
40 RefPtr& operator=(const RefPtr& other) {
41 SkRefCnt_SafeAssign(ptr_, other.ptr_);
42 return *this;
43 }
44
45 void clear() {
46 T* to_unref = ptr_;
47 ptr_ = NULL;
48 SkSafeUnref(to_unref);
49 }
50
51 // Convert the RefPtr<T> into a RefPtr<P>.
jamesr 2012/11/29 07:34:06 could you just make operator= accept RefPtr<U>s an
danakj 2012/11/29 21:37:11 Yaaaaa!! thanks!
52 template<typename P>
53 RefPtr<P> PassAs() {
54 // Moves ownership to the returned RefPtr.
55 RefPtr<P> as = ptr_;
56 ptr_ = NULL;
57 return as;
58 }
59
60 T* get() const { return ptr_; }
61 T& operator*() const { return static_cast<T&>(*ptr_); }
62 T* operator->() const { return static_cast<T*>(ptr_); }
63
64 typedef T* RefPtr::*unspecified_bool_type;
65 operator unspecified_bool_type() const {
66 return ptr_ ? &RefPtr::ptr_ : NULL;
67 }
68
69 private:
70 T* ptr_;
71 };
72
73 } // namespace skia
74
75 #endif // SKIA_EXT_REFPTR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698