Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 // This class wraps the CoreFoundation routine CFRelease() in a class | |
| 18 // that can be passed as a template argument to ScopedCFTypeRef below. | |
| 19 class ScopedCFTypeRefRelease { | |
| 20 public: | |
| 21 inline void operator()(CFTypeRef x) const { | |
| 22 CFRelease(x); | |
| 23 } | |
| 24 }; | |
| 25 | |
| 17 // ScopedCFTypeRef<> is patterned after scoped_ptr<>, but maintains ownership | 26 // ScopedCFTypeRef<> is patterned after scoped_ptr<>, but maintains ownership |
| 18 // of a CoreFoundation object: any object that can be represented as a | 27 // of a CoreFoundation object: any object that can be represented as a |
| 19 // CFTypeRef. Style deviations here are solely for compatibility with | 28 // CFTypeRef. Style deviations here are solely for compatibility with |
| 20 // scoped_ptr<>'s interface, with which everyone is already familiar. | 29 // scoped_ptr<>'s interface, with which everyone is already familiar. |
| 21 // | 30 // |
| 22 // When ScopedCFTypeRef<> takes ownership of an object (in the constructor or | 31 // When ScopedCFTypeRef<> takes ownership of an object (in the constructor or |
| 23 // in reset()), it takes over the caller's existing ownership claim. The | 32 // in reset()), it takes over the caller's existing ownership claim. The |
| 24 // caller must own the object it gives to ScopedCFTypeRef<>, and relinquishes | 33 // caller must own the object it gives to ScopedCFTypeRef<>, and relinquishes |
| 25 // an ownership claim to that object. ScopedCFTypeRef<> does not call | 34 // an ownership claim to that object. ScopedCFTypeRef<> does not call |
| 26 // CFRetain(). | 35 // CFRetain(). |
| 27 template<typename CFT> | 36 // |
| 37 // The second template argument can be used to specify a functor used | |
| 38 // to free the object. This allows this class to manage the lifetime | |
| 39 // of objects similar to Core Foundation types, where the pointer-ness | |
| 40 // is embedded in the type name) but with specialized releasing routines. | |
|
Mark Mentovai
2011/10/13 20:20:48
The ) doesn’t close any (.
| |
| 41 template<typename CFT, class FreeProc = ScopedCFTypeRefRelease> | |
| 28 class ScopedCFTypeRef { | 42 class ScopedCFTypeRef { |
| 29 public: | 43 public: |
| 30 typedef CFT element_type; | 44 typedef CFT element_type; |
| 31 | 45 |
| 32 explicit ScopedCFTypeRef(CFT object = NULL) | 46 explicit ScopedCFTypeRef(CFT object = NULL) |
| 33 : object_(object) { | 47 : object_(object) { |
| 34 } | 48 } |
| 35 | 49 |
| 36 ~ScopedCFTypeRef() { | 50 ~ScopedCFTypeRef() { |
| 37 if (object_) | 51 if (object_) { |
| 38 CFRelease(object_); | 52 FreeProc free_proc; |
| 53 free_proc(object_); | |
| 54 } | |
| 39 } | 55 } |
| 40 | 56 |
| 41 void reset(CFT object = NULL) { | 57 void reset(CFT object = NULL) { |
| 42 if (object_) | 58 if (object_) { |
| 43 CFRelease(object_); | 59 FreeProc free_proc; |
| 60 free_proc(object_); | |
| 61 } | |
| 44 object_ = object; | 62 object_ = object; |
| 45 } | 63 } |
| 46 | 64 |
| 47 bool operator==(CFT that) const { | 65 bool operator==(CFT that) const { |
| 48 return object_ == that; | 66 return object_ == that; |
| 49 } | 67 } |
| 50 | 68 |
| 51 bool operator!=(CFT that) const { | 69 bool operator!=(CFT that) const { |
| 52 return object_ != that; | 70 return object_ != that; |
| 53 } | 71 } |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 78 private: | 96 private: |
| 79 CFT object_; | 97 CFT object_; |
| 80 | 98 |
| 81 DISALLOW_COPY_AND_ASSIGN(ScopedCFTypeRef); | 99 DISALLOW_COPY_AND_ASSIGN(ScopedCFTypeRef); |
| 82 }; | 100 }; |
| 83 | 101 |
| 84 } // namespace mac | 102 } // namespace mac |
| 85 } // namespace base | 103 } // namespace base |
| 86 | 104 |
| 87 #endif // BASE_MAC_SCOPED_CFTYPEREF_H_ | 105 #endif // BASE_MAC_SCOPED_CFTYPEREF_H_ |
| OLD | NEW |