| OLD | NEW |
| 1 // Copyright (c) 2010 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <CoreFoundation/CoreFoundation.h> | 9 #include <CoreFoundation/CoreFoundation.h> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/memory/scoped_policy.h" |
| 13 | 14 |
| 14 namespace base { | 15 namespace base { |
| 15 namespace mac { | 16 namespace mac { |
| 16 | 17 |
| 17 // ScopedCFTypeRef<> is patterned after scoped_ptr<>, but maintains ownership | 18 // ScopedCFTypeRef<> is patterned after scoped_ptr<>, but maintains ownership |
| 18 // of a CoreFoundation object: any object that can be represented as a | 19 // of a CoreFoundation object: any object that can be represented as a |
| 19 // CFTypeRef. Style deviations here are solely for compatibility with | 20 // CFTypeRef. Style deviations here are solely for compatibility with |
| 20 // scoped_ptr<>'s interface, with which everyone is already familiar. | 21 // scoped_ptr<>'s interface, with which everyone is already familiar. |
| 21 // | 22 // |
| 22 // When ScopedCFTypeRef<> takes ownership of an object (in the constructor or | 23 // By default, ScopedCFTypeRef<> takes ownership of an object (in the |
| 23 // in reset()), it takes over the caller's existing ownership claim. The | 24 // constructor or in reset()) by taking over the caller's existing ownership |
| 24 // caller must own the object it gives to ScopedCFTypeRef<>, and relinquishes | 25 // claim. The caller must own the object it gives to ScopedCFTypeRef<>, and |
| 25 // an ownership claim to that object. ScopedCFTypeRef<> does not call | 26 // relinquishes an ownership claim to that object. ScopedCFTypeRef<> does not |
| 26 // CFRetain(). | 27 // call CFRetain(). This behavior is parameterized by the |OwnershipPolicy| |
| 28 // enum. If the value |RETAIN| is passed (in the constructor or in reset()), |
| 29 // then ScopedCFTypeRef<> will call CFRetain() on the object, and the initial |
| 30 // ownership is not changed. |
| 31 |
| 27 template<typename CFT> | 32 template<typename CFT> |
| 28 class ScopedCFTypeRef { | 33 class ScopedCFTypeRef { |
| 29 public: | 34 public: |
| 30 typedef CFT element_type; | 35 typedef CFT element_type; |
| 31 | 36 |
| 32 explicit ScopedCFTypeRef(CFT object = NULL) | 37 explicit ScopedCFTypeRef( |
| 38 CFT object = NULL, |
| 39 base::scoped_policy::OwnershipPolicy policy = base::scoped_policy::ASSUME) |
| 33 : object_(object) { | 40 : object_(object) { |
| 41 if (object_ && policy == base::scoped_policy::RETAIN) |
| 42 CFRetain(object_); |
| 43 } |
| 44 |
| 45 ScopedCFTypeRef(const ScopedCFTypeRef<CFT>& that) |
| 46 : object_(that.object_) { |
| 47 if (object_) |
| 48 CFRetain(object_); |
| 34 } | 49 } |
| 35 | 50 |
| 36 ~ScopedCFTypeRef() { | 51 ~ScopedCFTypeRef() { |
| 37 if (object_) | 52 if (object_) |
| 38 CFRelease(object_); | 53 CFRelease(object_); |
| 39 } | 54 } |
| 40 | 55 |
| 41 void reset(CFT object = NULL) { | 56 ScopedCFTypeRef& operator=(const ScopedCFTypeRef<CFT>& that) { |
| 57 reset(that.get(), base::scoped_policy::RETAIN); |
| 58 return *this; |
| 59 } |
| 60 |
| 61 void reset(CFT object = NULL, |
| 62 base::scoped_policy::OwnershipPolicy policy = |
| 63 base::scoped_policy::ASSUME) { |
| 64 if (object && policy == base::scoped_policy::RETAIN) |
| 65 CFRetain(object); |
| 42 if (object_) | 66 if (object_) |
| 43 CFRelease(object_); | 67 CFRelease(object_); |
| 44 object_ = object; | 68 object_ = object; |
| 45 } | 69 } |
| 46 | 70 |
| 47 bool operator==(CFT that) const { | 71 bool operator==(CFT that) const { |
| 48 return object_ == that; | 72 return object_ == that; |
| 49 } | 73 } |
| 50 | 74 |
| 51 bool operator!=(CFT that) const { | 75 bool operator!=(CFT that) const { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 70 // a wrapper for CFRelease(). To force a ScopedCFTypeRef<> object to call | 94 // a wrapper for CFRelease(). To force a ScopedCFTypeRef<> object to call |
| 71 // CFRelease(), use ScopedCFTypeRef<>::reset(). | 95 // CFRelease(), use ScopedCFTypeRef<>::reset(). |
| 72 CFT release() WARN_UNUSED_RESULT { | 96 CFT release() WARN_UNUSED_RESULT { |
| 73 CFT temp = object_; | 97 CFT temp = object_; |
| 74 object_ = NULL; | 98 object_ = NULL; |
| 75 return temp; | 99 return temp; |
| 76 } | 100 } |
| 77 | 101 |
| 78 private: | 102 private: |
| 79 CFT object_; | 103 CFT object_; |
| 80 | |
| 81 DISALLOW_COPY_AND_ASSIGN(ScopedCFTypeRef); | |
| 82 }; | 104 }; |
| 83 | 105 |
| 84 } // namespace mac | 106 } // namespace mac |
| 85 } // namespace base | 107 } // namespace base |
| 86 | 108 |
| 87 #endif // BASE_MAC_SCOPED_CFTYPEREF_H_ | 109 #endif // BASE_MAC_SCOPED_CFTYPEREF_H_ |
| OLD | NEW |