Chromium Code Reviews| Index: base/scoped_nsobject.h |
| =================================================================== |
| --- base/scoped_nsobject.h (revision 12217) |
| +++ base/scoped_nsobject.h (working copy) |
| @@ -1,74 +1,75 @@ |
| -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| +// Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#ifndef BASE_SCOPED_CFTYPEREF_H_ |
| -#define BASE_SCOPED_CFTYPEREF_H_ |
| +#ifndef BASE_SCOPED_NSOBJECT_H_ |
| +#define BASE_SCOPED_NSOBJECT_H_ |
| -#include <CoreFoundation/CoreFoundation.h> |
| -#include "base/basictypes.h" |
| +#import <Foundation/Foundation.h> |
|
tommi (sloooow) - chröme
2009/03/23 15:37:03
um... #import?
|
| -// scoped_cftyperef<> is patterned after scoped_ptr<>, but maintains ownership |
| -// of a CoreFoundation object: any object that can be represented as a |
| -// CFTypeRef. Style deviations here are solely for compatibility with |
| -// scoped_ptr<>'s interface, with which everyone is already familiar. |
| -template<typename CFT> |
| -class scoped_cftyperef { |
| +// scoped_nsobject<> is patterned after scoped_ptr<>, but maintains ownership |
| +// of an NSObject subclass object. Style deviations here are solely for |
| +// compatibility scoped_ptr<>'s interface, with which everyone is already |
| +// familiar. |
| +// |
| +// When scoped_nsobject<> takes ownership of an object (in the constructor or |
| +// in reset()), it takes over the caller's existing ownership claim. The |
| +// caller must own the object. scoped_nsobject<> does not call -retain. |
| +template<typename NST> |
| +class scoped_nsobject { |
| public: |
| - typedef CFT element_type; |
| + typedef NST* element_type; |
| - explicit scoped_cftyperef(CFT object = NULL) |
| + explicit scoped_nsobject(NST* object = nil) |
| : object_(object) { |
| } |
| - ~scoped_cftyperef() { |
| - if (object_) |
| - CFRelease(object_); |
| + ~scoped_nsobject() { |
| + [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?
|
| } |
| - void reset(CFT object = NULL) { |
| + 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:
|
| if (object_ != object) { |
| - if (object_) |
| - CFRelease(object_); |
| + [object_ release]; |
| object_ = object; |
| } |
| } |
| - bool operator==(CFT that) const { |
| + bool operator==(NST* that) const { |
| return object_ == that; |
| } |
| - bool operator!=(CFT that) const { |
| + bool operator!=(NST* that) const { |
| return object_ != that; |
| } |
| - operator CFT() const { |
| + operator NST*() const { |
| return object_; |
| } |
| - CFT get() const { |
| + NST* get() const { |
| return object_; |
| } |
| - void swap(scoped_cftyperef& that) { |
| - CFT temp = that.object_; |
| + void swap(scoped_nsobject& that) { |
| + NST* temp = that.object_; |
| that.object_ = object_; |
| object_ = temp; |
| } |
| - // scoped_cftyperef<>::release() is like scoped_ptr<>::release. It is NOT |
| - // a wrapper for CFRelease(). To force a scoped_cftyperef<> object to call |
| - // CFRelease(), use scoped_cftyperef<>::reset(). |
| - CFT release() { |
| - CFT temp = object_; |
| - object_ = NULL; |
| + // scoped_nsobject<>::release() is like scoped_ptr<>::release. It is NOT |
| + // a wrapper for [object_ release]. To force a scoped_nsobject<> object to |
| + // call [object_ release], use scoped_nsobject<>::reset(). |
| + NST* release() { |
| + NST* temp = object_; |
| + object_ = nil; |
| return temp; |
| } |
| private: |
| - CFT object_; |
| + NST* object_; |
| - DISALLOW_COPY_AND_ASSIGN(scoped_cftyperef); |
| + DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); |
| }; |
| -#endif // BASE_SCOPED_CFTYPEREF_H_ |
| +#endif // BASE_SCOPED_NSOBJECT_H_ |