Chromium Code Reviews| OLD | NEW |
|---|---|
| (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_ | |
| OLD | NEW |