Index: skia/ext/refptr.h |
diff --git a/skia/ext/refptr.h b/skia/ext/refptr.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..598f6d783caf526399def95476d8af63ba8f7559 |
--- /dev/null |
+++ b/skia/ext/refptr.h |
@@ -0,0 +1,75 @@ |
+// Copyright 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef SKIA_EXT_REFPTR_H_ |
+#define SKIA_EXT_REFPTR_H_ |
+ |
+#include "third_party/skia/include/core/SkRefCnt.h" |
+ |
+namespace skia { |
+ |
+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.
|
+class RefPtr { |
+ public: |
+ RefPtr() |
+ : ptr_(NULL) { |
+ } |
+ |
+ RefPtr(const RefPtr& other) |
+ : ptr_(other.ptr_) { |
+ SkSafeRef(ptr_); |
+ } |
+ |
+ // Skia objects come with a reference of 1, steal that reference here to |
+ // take ownership. |
+ 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
|
+ |
+ ~RefPtr() { |
+ clear(); |
+ } |
+ |
+ // Skia objects come with a reference of 1, steal that reference here to |
+ // 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 :)
|
+ RefPtr& operator=(T* ptr) { |
+ clear(); |
+ ptr_ = ptr; |
+ return *this; |
+ } |
+ |
+ RefPtr& operator=(const RefPtr& other) { |
+ SkRefCnt_SafeAssign(ptr_, other.ptr_); |
+ return *this; |
+ } |
+ |
+ void clear() { |
+ T* to_unref = ptr_; |
+ ptr_ = NULL; |
+ SkSafeUnref(to_unref); |
+ } |
+ |
+ // 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!
|
+ template<typename P> |
+ RefPtr<P> PassAs() { |
+ // Moves ownership to the returned RefPtr. |
+ RefPtr<P> as = ptr_; |
+ ptr_ = NULL; |
+ return as; |
+ } |
+ |
+ T* get() const { return ptr_; } |
+ T& operator*() const { return static_cast<T&>(*ptr_); } |
+ T* operator->() const { return static_cast<T*>(ptr_); } |
+ |
+ typedef T* RefPtr::*unspecified_bool_type; |
+ operator unspecified_bool_type() const { |
+ return ptr_ ? &RefPtr::ptr_ : NULL; |
+ } |
+ |
+ private: |
+ T* ptr_; |
+}; |
+ |
+} // namespace skia |
+ |
+#endif // SKIA_EXT_REFPTR_H_ |