Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_SCOPED_CFTYPEREF_H_ | 5 #ifndef BASE_SCOPED_CFTYPEREF_H_ |
| 6 #define BASE_SCOPED_CFTYPEREF_H_ | 6 #define BASE_SCOPED_CFTYPEREF_H_ |
| 7 | 7 |
| 8 #include <CoreFoundation/CoreFoundation.h> | 8 #include <CoreFoundation/CoreFoundation.h> |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 | 10 |
| 11 // scoped_cftyperef<> is patterned after scoped_ptr<>, but maintains ownership | 11 // scoped_cftyperef<> is patterned after scoped_ptr<>, but maintains ownership |
| 12 // of a CoreFoundation object: any object that can be represented as a | 12 // of a CoreFoundation object: any object that can be represented as a |
| 13 // CFTypeRef. Style deviations here are solely for compatibility with | 13 // CFTypeRef. Style deviations here are solely for compatibility with |
| 14 // scoped_ptr<>'s interface, with which everyone is already familiar. | 14 // scoped_ptr<>'s interface, with which everyone is already familiar. |
| 15 // | 15 // |
| 16 // When scoped_cftyperef<> takes ownership of an object (in the constructor or | 16 // When scoped_cftyperef<> takes ownership of an object (in the constructor or |
| 17 // in reset()), it takes over the caller's existing ownership claim. The | 17 // in reset()), it takes over the caller's existing ownership claim. The |
| 18 // caller must own the object. scoped_cftyperef<> does not call CFRetain(). | 18 // caller must own the object it gives to scoped_cftyperef<>, and relinquishes |
| 19 // an ownership claim to that object. scoped_cftyperef<> does not call | |
| 20 // CFRetain(). | |
| 19 template<typename CFT> | 21 template<typename CFT> |
| 20 class scoped_cftyperef { | 22 class scoped_cftyperef { |
| 21 public: | 23 public: |
| 22 typedef CFT element_type; | 24 typedef CFT element_type; |
| 23 | 25 |
| 24 explicit scoped_cftyperef(CFT object = NULL) | 26 explicit scoped_cftyperef(CFT object = NULL) |
| 25 : object_(object) { | 27 : object_(object) { |
| 26 } | 28 } |
| 27 | 29 |
| 28 ~scoped_cftyperef() { | 30 ~scoped_cftyperef() { |
| 29 if (object_) | 31 if (object_) |
| 30 CFRelease(object_); | 32 CFRelease(object_); |
| 31 } | 33 } |
| 32 | 34 |
| 33 void reset(CFT object = NULL) { | 35 void reset(CFT object = NULL) { |
| 34 if (object_ != object) { | 36 if (object_) |
|
TVL
2009/03/23 17:30:22
part of me thinks it's better to match the other s
| |
| 35 if (object_) | 37 CFRelease(object_); |
| 36 CFRelease(object_); | 38 object_ = object; |
| 37 object_ = object; | |
| 38 } | |
| 39 } | 39 } |
| 40 | 40 |
| 41 bool operator==(CFT that) const { | 41 bool operator==(CFT that) const { |
| 42 return object_ == that; | 42 return object_ == that; |
| 43 } | 43 } |
| 44 | 44 |
| 45 bool operator!=(CFT that) const { | 45 bool operator!=(CFT that) const { |
| 46 return object_ != that; | 46 return object_ != that; |
| 47 } | 47 } |
| 48 | 48 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 69 return temp; | 69 return temp; |
| 70 } | 70 } |
| 71 | 71 |
| 72 private: | 72 private: |
| 73 CFT object_; | 73 CFT object_; |
| 74 | 74 |
| 75 DISALLOW_COPY_AND_ASSIGN(scoped_cftyperef); | 75 DISALLOW_COPY_AND_ASSIGN(scoped_cftyperef); |
| 76 }; | 76 }; |
| 77 | 77 |
| 78 #endif // BASE_SCOPED_CFTYPEREF_H_ | 78 #endif // BASE_SCOPED_CFTYPEREF_H_ |
| OLD | NEW |