Chromium Code Reviews| Index: skia/ext/refptr.h |
| diff --git a/skia/ext/refptr.h b/skia/ext/refptr.h |
| index a3900f61ba4502c0d59c3b8664cf6bb4dd3c4aa1..5d3165988ae6ace57c4fb393ad8cc84f6da933ee 100644 |
| --- a/skia/ext/refptr.h |
| +++ b/skia/ext/refptr.h |
| @@ -5,6 +5,8 @@ |
| #ifndef SKIA_EXT_REFPTR_H_ |
| #define SKIA_EXT_REFPTR_H_ |
| +#include "base/basictypes.h" |
| +#include "base/logging.h" |
| #include "third_party/skia/include/core/SkRefCnt.h" |
| namespace skia { |
| @@ -87,6 +89,41 @@ class RefPtr { |
| return ptr_ ? &RefPtr::ptr_ : NULL; |
| } |
| + class Receiver { |
| + public: |
| + ~Receiver() { |
| + DCHECK(!*refptr_) << "RefPtr must be empty."; |
| + refptr_->ptr_ = ptr_; |
| + } |
| + |
| + operator T**() { return &ptr_; } |
| + |
| + private: |
| + friend class RefPtr; |
| + |
| + explicit Receiver(RefPtr* refptr) : refptr_(refptr), ptr_(NULL) { |
| + DCHECK(!*refptr_) << "RefPtr must be empty."; |
| + } |
| + |
| + RefPtr* refptr_; |
| + T* ptr_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(Receiver); |
| + }; |
| + |
| + // Call this and pass it to a function that takes a T** argument and |
| + // sets it to an object with an unowned reference. |
|
awong
2013/07/17 23:51:02
suggestion:
Call this and pass the returned tempor
|
| + // NOTE: Do not use this function as part of a subexpression, and try to use |
|
awong
2013/07/17 23:51:02
suggestion:
Do not use this function as part of a
danakj
2013/07/17 23:58:29
Thanks, will do.
I think that I can also DCHECK t
|
| + // the RefPtr in the same subexpression, as this operation will not be |
| + // complete and the RefPtr will be empty still. |
| + // |
| + // Bad (refptr will be NULL when it is dereffed): |
| + // Foo(refptr.ReceiveAndAdoptRef()) && refptr->Bar(); |
| + // Good: |
| + // bool ok = Foo(refptr.ReceiveAndAdoptRef()); |
| + // ok && refptr->Bar(); |
| + Receiver ReceiveAndAdoptRef() { return Receiver(this); } |
| + |
| private: |
| T* ptr_; |