Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 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. | |
|
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
| |
| 4 | |
| 5 #ifndef BASE_MAC_SCOPED_REFPTR_CFTYPE_H_ | |
| 6 #define BASE_MAC_SCOPED_REFPTR_CFTYPE_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <CoreFoundation/CoreFoundation.h> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/compiler_specific.h" | |
| 13 | |
| 14 namespace base { | |
| 15 namespace mac { | |
| 16 | |
| 17 // scoped_refptr_cftype<> is patterned after scoped_ref_ptr<> but maintains | |
| 18 // ownership of a CoreFoundation object: any object that can be represented as a | |
| 19 // CFTypeRef. | |
| 20 template <class T> | |
| 21 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.
| |
| 22 public: | |
| 23 scoped_refptr_cftype() : ptr_(NULL) { | |
| 24 } | |
| 25 | |
| 26 scoped_refptr_cftype(T p) : ptr_(p) { | |
| 27 if (ptr_) | |
| 28 CFRetain(ptr_); | |
| 29 } | |
| 30 | |
| 31 scoped_refptr_cftype(const scoped_refptr_cftype<T>& r) : ptr_(r.ptr_) { | |
| 32 if (ptr_) | |
| 33 CFRetain(ptr_); | |
| 34 } | |
| 35 | |
| 36 template <typename U> | |
| 37 scoped_refptr_cftype(const scoped_refptr_cftype<U>& r) : ptr_(r.get()) { | |
| 38 if (ptr_) | |
| 39 CFRetain(ptr_); | |
| 40 } | |
| 41 | |
| 42 ~scoped_refptr_cftype() { | |
| 43 if (ptr_) | |
| 44 CFRelease(ptr_); | |
| 45 } | |
| 46 | |
| 47 T get() const { return ptr_; } | |
| 48 operator T() const { return ptr_; } | |
| 49 | |
| 50 // Release a pointer. | |
| 51 // The return value is the current pointer held by this object. | |
| 52 // If this object holds a NULL pointer, the return value is NULL. | |
| 53 // After this operation, this object will hold a NULL pointer, | |
| 54 // and will not own the object any more. | |
| 55 T release() { | |
| 56 T retVal = ptr_; | |
| 57 ptr_ = NULL; | |
| 58 return retVal; | |
| 59 } | |
| 60 | |
| 61 scoped_refptr_cftype<T>& operator=(T p) { | |
| 62 // Retain first so that self assignment should work | |
|
Robert Sesek
2012/05/15 16:04:49
nit: full-stop
| |
| 63 if (p) | |
| 64 CFRetain(p); | |
| 65 T old_ptr = ptr_; | |
| 66 ptr_ = p; | |
| 67 if (old_ptr) | |
| 68 CFRelease(old_ptr); | |
| 69 return *this; | |
| 70 } | |
| 71 | |
| 72 scoped_refptr_cftype<T>& operator=(const scoped_refptr_cftype<T>& r) { | |
| 73 return *this = r.ptr_; | |
| 74 } | |
| 75 | |
| 76 template <typename U> | |
| 77 scoped_refptr_cftype<T>& operator=(const scoped_refptr_cftype<U>& r) { | |
| 78 return *this = r.get(); | |
| 79 } | |
| 80 | |
| 81 void swap(T* pp) { | |
| 82 T p = ptr_; | |
| 83 ptr_ = *pp; | |
| 84 *pp = p; | |
| 85 } | |
| 86 | |
| 87 void swap(scoped_refptr_cftype<T>& r) { | |
| 88 swap(&r.ptr_); | |
| 89 } | |
| 90 | |
| 91 protected: | |
|
Robert Sesek
2012/05/15 16:04:49
Why not private?
| |
| 92 T ptr_; | |
| 93 }; | |
| 94 | |
| 95 } // namespace mac | |
| 96 } // namespace base | |
| 97 | |
| 98 #endif // BASE_MAC_SCOPED_REFPTR_CFTYPE_H_ | |
| OLD | NEW |