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