Chromium Code Reviews| Index: base/mac/scoped_refptr_cftype.h |
| diff --git a/base/mac/scoped_refptr_cftype.h b/base/mac/scoped_refptr_cftype.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..eac397d1b3f72e61d81a6eb0d5b8b1977de652fc |
| --- /dev/null |
| +++ b/base/mac/scoped_refptr_cftype.h |
| @@ -0,0 +1,98 @@ |
| +// Copyright (c) 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. |
|
Avi (use Gerrit)
2012/05/15 18:04:19
scoped_nsobject recently received an extension to
sail
2012/05/15 23:53:31
Done.
Added OwnershipPolicy to ScopedCFTypeRef and
|
| + |
| +#ifndef BASE_MAC_SCOPED_REFPTR_CFTYPE_H_ |
| +#define BASE_MAC_SCOPED_REFPTR_CFTYPE_H_ |
| +#pragma once |
| + |
| +#include <CoreFoundation/CoreFoundation.h> |
| + |
| +#include "base/basictypes.h" |
| +#include "base/compiler_specific.h" |
| + |
| +namespace base { |
| +namespace mac { |
| + |
| +// scoped_refptr_cftype<> is patterned after scoped_ref_ptr<> but maintains |
| +// ownership of a CoreFoundation object: any object that can be represented as a |
| +// CFTypeRef. |
| +template <class T> |
| +class scoped_refptr_cftype { |
|
Robert Sesek
2012/05/15 16:04:49
Needs a test.
Robert Sesek
2012/05/15 16:04:49
I wonder if this should be named ScopedRefptrCFTyp
Ami GONE FROM CHROMIUM
2012/05/15 17:55:22
Could you avoid the need for this class (and tests
sail
2012/05/15 23:53:31
I followed Avi's suggestion and used ScopedCFTypeR
sail
2012/05/15 23:53:31
Removed this class.
|
| + public: |
| + scoped_refptr_cftype() : ptr_(NULL) { |
| + } |
| + |
| + scoped_refptr_cftype(T p) : ptr_(p) { |
| + if (ptr_) |
| + CFRetain(ptr_); |
| + } |
| + |
| + scoped_refptr_cftype(const scoped_refptr_cftype<T>& r) : ptr_(r.ptr_) { |
| + if (ptr_) |
| + CFRetain(ptr_); |
| + } |
| + |
| + template <typename U> |
| + scoped_refptr_cftype(const scoped_refptr_cftype<U>& r) : ptr_(r.get()) { |
| + if (ptr_) |
| + CFRetain(ptr_); |
| + } |
| + |
| + ~scoped_refptr_cftype() { |
| + if (ptr_) |
| + CFRelease(ptr_); |
| + } |
| + |
| + T get() const { return ptr_; } |
| + operator T() const { return ptr_; } |
| + |
| + // Release a pointer. |
| + // The return value is the current pointer held by this object. |
| + // If this object holds a NULL pointer, the return value is NULL. |
| + // After this operation, this object will hold a NULL pointer, |
| + // and will not own the object any more. |
| + T release() { |
| + T retVal = ptr_; |
| + ptr_ = NULL; |
| + return retVal; |
| + } |
| + |
| + scoped_refptr_cftype<T>& operator=(T p) { |
| + // Retain first so that self assignment should work |
|
Robert Sesek
2012/05/15 16:04:49
nit: full-stop
|
| + if (p) |
| + CFRetain(p); |
| + T old_ptr = ptr_; |
| + ptr_ = p; |
| + if (old_ptr) |
| + CFRelease(old_ptr); |
| + return *this; |
| + } |
| + |
| + scoped_refptr_cftype<T>& operator=(const scoped_refptr_cftype<T>& r) { |
| + return *this = r.ptr_; |
| + } |
| + |
| + template <typename U> |
| + scoped_refptr_cftype<T>& operator=(const scoped_refptr_cftype<U>& r) { |
| + return *this = r.get(); |
| + } |
| + |
| + void swap(T* pp) { |
| + T p = ptr_; |
| + ptr_ = *pp; |
| + *pp = p; |
| + } |
| + |
| + void swap(scoped_refptr_cftype<T>& r) { |
| + swap(&r.ptr_); |
| + } |
| + |
| + protected: |
|
Robert Sesek
2012/05/15 16:04:49
Why not private?
|
| + T ptr_; |
| +}; |
| + |
| +} // namespace mac |
| +} // namespace base |
| + |
| +#endif // BASE_MAC_SCOPED_REFPTR_CFTYPE_H_ |