Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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_NSOBJECT_H_ |
| 6 #define BASE_SCOPED_CFTYPEREF_H_ | 6 #define BASE_SCOPED_NSOBJECT_H_ |
| 7 | 7 |
| 8 #include <CoreFoundation/CoreFoundation.h> | 8 #import <Foundation/Foundation.h> |
|
tommi (sloooow) - chröme
2009/03/23 15:37:03
um... #import?
| |
| 9 #include "base/basictypes.h" | |
| 10 | 9 |
| 11 // scoped_cftyperef<> is patterned after scoped_ptr<>, but maintains ownership | 10 // scoped_nsobject<> is patterned after scoped_ptr<>, but maintains ownership |
| 12 // of a CoreFoundation object: any object that can be represented as a | 11 // of an NSObject subclass object. Style deviations here are solely for |
| 13 // CFTypeRef. Style deviations here are solely for compatibility with | 12 // compatibility scoped_ptr<>'s interface, with which everyone is already |
| 14 // scoped_ptr<>'s interface, with which everyone is already familiar. | 13 // familiar. |
| 15 template<typename CFT> | 14 // |
| 16 class scoped_cftyperef { | 15 // When scoped_nsobject<> takes ownership of an object (in the constructor or |
| 16 // in reset()), it takes over the caller's existing ownership claim. The | |
| 17 // caller must own the object. scoped_nsobject<> does not call -retain. | |
| 18 template<typename NST> | |
| 19 class scoped_nsobject { | |
| 17 public: | 20 public: |
| 18 typedef CFT element_type; | 21 typedef NST* element_type; |
| 19 | 22 |
| 20 explicit scoped_cftyperef(CFT object = NULL) | 23 explicit scoped_nsobject(NST* object = nil) |
| 21 : object_(object) { | 24 : object_(object) { |
| 22 } | 25 } |
| 23 | 26 |
| 24 ~scoped_cftyperef() { | 27 ~scoped_nsobject() { |
| 25 if (object_) | 28 [object_ release]; |
|
tommi (sloooow) - chröme
2009/03/23 15:37:03
what's going on here?
Avi (use Gerrit)
2009/03/23 15:38:58
This is Objective C++ code, not plain C++. Please
tommi (sloooow) - chröme
2009/03/23 17:54:59
seriously?
| |
| 26 CFRelease(object_); | |
| 27 } | 29 } |
| 28 | 30 |
| 29 void reset(CFT object = NULL) { | 31 void reset(NST* object = nil) { |
|
Scott Hess - ex-Googler
2009/03/23 16:54:17
I think you always need to -release, due to the ru
Mark Mentovai
2009/03/23 17:03:48
shess wrote:
| |
| 30 if (object_ != object) { | 32 if (object_ != object) { |
| 31 if (object_) | 33 [object_ release]; |
| 32 CFRelease(object_); | |
| 33 object_ = object; | 34 object_ = object; |
| 34 } | 35 } |
| 35 } | 36 } |
| 36 | 37 |
| 37 bool operator==(CFT that) const { | 38 bool operator==(NST* that) const { |
| 38 return object_ == that; | 39 return object_ == that; |
| 39 } | 40 } |
| 40 | 41 |
| 41 bool operator!=(CFT that) const { | 42 bool operator!=(NST* that) const { |
| 42 return object_ != that; | 43 return object_ != that; |
| 43 } | 44 } |
| 44 | 45 |
| 45 operator CFT() const { | 46 operator NST*() const { |
| 46 return object_; | 47 return object_; |
| 47 } | 48 } |
| 48 | 49 |
| 49 CFT get() const { | 50 NST* get() const { |
| 50 return object_; | 51 return object_; |
| 51 } | 52 } |
| 52 | 53 |
| 53 void swap(scoped_cftyperef& that) { | 54 void swap(scoped_nsobject& that) { |
| 54 CFT temp = that.object_; | 55 NST* temp = that.object_; |
| 55 that.object_ = object_; | 56 that.object_ = object_; |
| 56 object_ = temp; | 57 object_ = temp; |
| 57 } | 58 } |
| 58 | 59 |
| 59 // scoped_cftyperef<>::release() is like scoped_ptr<>::release. It is NOT | 60 // scoped_nsobject<>::release() is like scoped_ptr<>::release. It is NOT |
| 60 // a wrapper for CFRelease(). To force a scoped_cftyperef<> object to call | 61 // a wrapper for [object_ release]. To force a scoped_nsobject<> object to |
| 61 // CFRelease(), use scoped_cftyperef<>::reset(). | 62 // call [object_ release], use scoped_nsobject<>::reset(). |
| 62 CFT release() { | 63 NST* release() { |
| 63 CFT temp = object_; | 64 NST* temp = object_; |
| 64 object_ = NULL; | 65 object_ = nil; |
| 65 return temp; | 66 return temp; |
| 66 } | 67 } |
| 67 | 68 |
| 68 private: | 69 private: |
| 69 CFT object_; | 70 NST* object_; |
| 70 | 71 |
| 71 DISALLOW_COPY_AND_ASSIGN(scoped_cftyperef); | 72 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); |
| 72 }; | 73 }; |
| 73 | 74 |
| 74 #endif // BASE_SCOPED_CFTYPEREF_H_ | 75 #endif // BASE_SCOPED_NSOBJECT_H_ |
| OLD | NEW |