Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_MAC_SCOPED_CFTYPEREF_H_ | 5 #ifndef BASE_MAC_SCOPED_CFTYPEREF_H_ |
| 6 #define BASE_MAC_SCOPED_CFTYPEREF_H_ | 6 #define BASE_MAC_SCOPED_CFTYPEREF_H_ |
| 7 | 7 |
| 8 #include <CoreFoundation/CoreFoundation.h> | 8 #include <CoreFoundation/CoreFoundation.h> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/mac/scoped_typeref.h" |
| 11 #include "base/compiler_specific.h" | |
| 12 #include "base/memory/scoped_policy.h" | |
| 13 | 11 |
| 14 namespace base { | 12 namespace base { |
| 15 | 13 |
| 16 // ScopedCFTypeRef<> is patterned after scoped_ptr<>, but maintains ownership | 14 // ScopedCFTypeRef<> is patterned after scoped_ptr<>, but maintains ownership |
| 17 // of a CoreFoundation object: any object that can be represented as a | 15 // of a CoreFoundation object: any object that can be represented as a |
| 18 // CFTypeRef. Style deviations here are solely for compatibility with | 16 // CFTypeRef. Style deviations here are solely for compatibility with |
| 19 // scoped_ptr<>'s interface, with which everyone is already familiar. | 17 // scoped_ptr<>'s interface, with which everyone is already familiar. |
| 20 // | 18 // |
| 21 // By default, ScopedCFTypeRef<> takes ownership of an object (in the | 19 // By default, ScopedCFTypeRef<> takes ownership of an object (in the |
| 22 // constructor or in reset()) by taking over the caller's existing ownership | 20 // constructor or in reset()) by taking over the caller's existing ownership |
| 23 // claim. The caller must own the object it gives to ScopedCFTypeRef<>, and | 21 // claim. The caller must own the object it gives to ScopedCFTypeRef<>, and |
| 24 // relinquishes an ownership claim to that object. ScopedCFTypeRef<> does not | 22 // relinquishes an ownership claim to that object. ScopedCFTypeRef<> does not |
| 25 // call CFRetain(). This behavior is parameterized by the |OwnershipPolicy| | 23 // call CFRetain(). This behavior is parameterized by the |OwnershipPolicy| |
| 26 // enum. If the value |RETAIN| is passed (in the constructor or in reset()), | 24 // enum. If the value |RETAIN| is passed (in the constructor or in reset()), |
| 27 // then ScopedCFTypeRef<> will call CFRetain() on the object, and the initial | 25 // then ScopedCFTypeRef<> will call CFRetain() on the object, and the initial |
| 28 // ownership is not changed. | 26 // ownership is not changed. |
| 29 | 27 |
| 30 template<typename CFT> | 28 template<typename CFT> |
|
Mark Mentovai
2014/02/07 19:53:40
No need to templatize this at all, since you’re sp
ccameron
2014/02/07 20:58:04
Done.
| |
| 31 class ScopedCFTypeRef { | 29 struct ScopedCFTypeRefTraits { |
| 30 static void Retain(CFT object) { | |
| 31 CFRetain(object); | |
| 32 } | |
| 33 static void Release(CFT object) { | |
| 34 CFRelease(object); | |
| 35 } | |
| 36 }; | |
| 37 | |
| 38 template<typename CFT> | |
| 39 class ScopedCFTypeRef | |
| 40 : public ScopedTypeRef<CFT, ScopedCFTypeRefTraits<CFT> > { | |
|
Mark Mentovai
2014/02/07 19:53:40
I think we only ever build with C++11 language sup
Avi (use Gerrit)
2014/02/07 19:59:23
Does the compiler we use for iOS support C++11? Th
ccameron
2014/02/07 20:58:04
Changed (a different instance) to >> -- if the iOS
| |
| 32 public: | 41 public: |
| 33 typedef CFT element_type; | 42 typedef CFT element_type; |
| 34 | 43 |
| 35 explicit ScopedCFTypeRef( | 44 explicit ScopedCFTypeRef( |
| 36 CFT object = NULL, | 45 CFT object = NULL, |
| 37 base::scoped_policy::OwnershipPolicy policy = base::scoped_policy::ASSUME) | 46 base::scoped_policy::OwnershipPolicy policy = base::scoped_policy::ASSUME) |
| 38 : object_(object) { | 47 : ScopedTypeRef<CFT, ScopedCFTypeRefTraits<CFT> >(object, policy) {} |
| 39 if (object_ && policy == base::scoped_policy::RETAIN) | |
| 40 CFRetain(object_); | |
| 41 } | |
| 42 | 48 |
| 43 ScopedCFTypeRef(const ScopedCFTypeRef<CFT>& that) | 49 ScopedCFTypeRef(const ScopedCFTypeRef<CFT>& that) |
| 44 : object_(that.object_) { | 50 : ScopedTypeRef<CFT, ScopedCFTypeRefTraits<CFT> >(that) {} |
| 45 if (object_) | |
| 46 CFRetain(object_); | |
| 47 } | |
| 48 | 51 |
| 49 ~ScopedCFTypeRef() { | 52 virtual ~ScopedCFTypeRef() {} |
|
Mark Mentovai
2014/02/07 19:53:40
Why is this virtual?
ccameron
2014/02/07 20:58:04
Removed (was here for style, not correctness).
| |
| 50 if (object_) | |
| 51 CFRelease(object_); | |
| 52 } | |
| 53 | |
| 54 ScopedCFTypeRef& operator=(const ScopedCFTypeRef<CFT>& that) { | |
| 55 reset(that.get(), base::scoped_policy::RETAIN); | |
| 56 return *this; | |
| 57 } | |
| 58 | |
| 59 void reset(CFT object = NULL, | |
| 60 base::scoped_policy::OwnershipPolicy policy = | |
| 61 base::scoped_policy::ASSUME) { | |
| 62 if (object && policy == base::scoped_policy::RETAIN) | |
| 63 CFRetain(object); | |
| 64 if (object_) | |
| 65 CFRelease(object_); | |
| 66 object_ = object; | |
| 67 } | |
| 68 | |
| 69 bool operator==(CFT that) const { | |
| 70 return object_ == that; | |
| 71 } | |
| 72 | |
| 73 bool operator!=(CFT that) const { | |
| 74 return object_ != that; | |
| 75 } | |
| 76 | |
| 77 operator CFT() const { | |
| 78 return object_; | |
| 79 } | |
| 80 | |
| 81 CFT get() const { | |
| 82 return object_; | |
| 83 } | |
| 84 | |
| 85 void swap(ScopedCFTypeRef& that) { | |
| 86 CFT temp = that.object_; | |
| 87 that.object_ = object_; | |
| 88 object_ = temp; | |
| 89 } | |
| 90 | |
| 91 // ScopedCFTypeRef<>::release() is like scoped_ptr<>::release. It is NOT | |
| 92 // a wrapper for CFRelease(). To force a ScopedCFTypeRef<> object to call | |
| 93 // CFRelease(), use ScopedCFTypeRef<>::reset(). | |
| 94 CFT release() WARN_UNUSED_RESULT { | |
| 95 CFT temp = object_; | |
| 96 object_ = NULL; | |
| 97 return temp; | |
| 98 } | |
| 99 | |
| 100 private: | |
| 101 CFT object_; | |
| 102 }; | 53 }; |
| 103 | 54 |
| 104 } // namespace base | 55 } // namespace base |
| 105 | 56 |
| 106 #endif // BASE_MAC_SCOPED_CFTYPEREF_H_ | 57 #endif // BASE_MAC_SCOPED_CFTYPEREF_H_ |
| OLD | NEW |