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/basictypes.h" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/memory/scoped_policy.h" | 12 #include "base/memory/scoped_policy.h" |
| 13 | 13 |
| 14 namespace base { | 14 namespace base { |
| 15 namespace mac { | |
| 16 | 15 |
| 17 // ScopedCFTypeRef<> is patterned after scoped_ptr<>, but maintains ownership | 16 // ScopedCFTypeRef<> is patterned after scoped_ptr<>, but maintains ownership |
| 18 // of a CoreFoundation object: any object that can be represented as a | 17 // of a CoreFoundation object: any object that can be represented as a |
| 19 // CFTypeRef. Style deviations here are solely for compatibility with | 18 // CFTypeRef. Style deviations here are solely for compatibility with |
| 20 // scoped_ptr<>'s interface, with which everyone is already familiar. | 19 // scoped_ptr<>'s interface, with which everyone is already familiar. |
| 21 // | 20 // |
| 22 // By default, ScopedCFTypeRef<> takes ownership of an object (in the | 21 // By default, ScopedCFTypeRef<> takes ownership of an object (in the |
| 23 // constructor or in reset()) by taking over the caller's existing ownership | 22 // constructor or in reset()) by taking over the caller's existing ownership |
| 24 // claim. The caller must own the object it gives to ScopedCFTypeRef<>, and | 23 // claim. The caller must own the object it gives to ScopedCFTypeRef<>, and |
| 25 // relinquishes an ownership claim to that object. ScopedCFTypeRef<> does not | 24 // relinquishes an ownership claim to that object. ScopedCFTypeRef<> does not |
| 26 // call CFRetain(). This behavior is parameterized by the |OwnershipPolicy| | 25 // call CFRetain(). This behavior is parameterized by the |OwnershipPolicy| |
| 27 // enum. If the value |RETAIN| is passed (in the constructor or in reset()), | 26 // enum. If the value |RETAIN| is passed (in the constructor or in reset()), |
| 28 // then ScopedCFTypeRef<> will call CFRetain() on the object, and the initial | 27 // then ScopedCFTypeRef<> will call CFRetain() on the object, and the initial |
| 29 // ownership is not changed. | 28 // ownership is not changed. |
| 30 | 29 |
| 31 template<typename CFT> | 30 template<typename CFT> |
| 32 class ScopedCFTypeRef { | 31 class ScopedCFTypeRef { |
|
Mark Mentovai
2013/06/20 18:53:22
The CL description says scoped_cftyperef but (for
Nico
2013/06/20 18:56:00
Done.
| |
| 33 public: | 32 public: |
| 34 typedef CFT element_type; | 33 typedef CFT element_type; |
| 35 | 34 |
| 36 explicit ScopedCFTypeRef( | 35 explicit ScopedCFTypeRef( |
| 37 CFT object = NULL, | 36 CFT object = NULL, |
| 38 base::scoped_policy::OwnershipPolicy policy = base::scoped_policy::ASSUME) | 37 base::scoped_policy::OwnershipPolicy policy = base::scoped_policy::ASSUME) |
| 39 : object_(object) { | 38 : object_(object) { |
| 40 if (object_ && policy == base::scoped_policy::RETAIN) | 39 if (object_ && policy == base::scoped_policy::RETAIN) |
| 41 CFRetain(object_); | 40 CFRetain(object_); |
| 42 } | 41 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 95 CFT release() WARN_UNUSED_RESULT { | 94 CFT release() WARN_UNUSED_RESULT { |
| 96 CFT temp = object_; | 95 CFT temp = object_; |
| 97 object_ = NULL; | 96 object_ = NULL; |
| 98 return temp; | 97 return temp; |
| 99 } | 98 } |
| 100 | 99 |
| 101 private: | 100 private: |
| 102 CFT object_; | 101 CFT object_; |
| 103 }; | 102 }; |
| 104 | 103 |
| 104 // TODO(thakis): Remove this once all clients use base::ScopedCFTypeRef | |
| 105 // directly. | |
| 106 namespace mac { | |
| 107 using base::ScopedCFTypeRef; | |
| 105 } // namespace mac | 108 } // namespace mac |
| 109 | |
| 106 } // namespace base | 110 } // namespace base |
| 107 | 111 |
| 108 #endif // BASE_MAC_SCOPED_CFTYPEREF_H_ | 112 #endif // BASE_MAC_SCOPED_CFTYPEREF_H_ |
| OLD | NEW |