michael@0: // Copyright (c) 2009 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_SCOPED_NSOBJECT_H_ michael@0: #define BASE_SCOPED_NSOBJECT_H_ michael@0: michael@0: #import michael@0: #include "base/basictypes.h" michael@0: michael@0: // scoped_nsobject<> is patterned after scoped_ptr<>, but maintains ownership michael@0: // of an NSObject subclass object. Style deviations here are solely for michael@0: // compatibility with scoped_ptr<>'s interface, with which everyone is already michael@0: // familiar. michael@0: // michael@0: // When scoped_nsobject<> takes ownership of an object (in the constructor or michael@0: // in reset()), it takes over the caller's existing ownership claim. The michael@0: // caller must own the object it gives to scoped_nsobject<>, and relinquishes michael@0: // an ownership claim to that object. scoped_nsobject<> does not call michael@0: // -retain. michael@0: template michael@0: class scoped_nsobject { michael@0: public: michael@0: typedef NST* element_type; michael@0: michael@0: explicit scoped_nsobject(NST* object = nil) michael@0: : object_(object) { michael@0: } michael@0: michael@0: ~scoped_nsobject() { michael@0: [object_ release]; michael@0: } michael@0: michael@0: void reset(NST* object = nil) { michael@0: [object_ release]; michael@0: object_ = object; michael@0: } michael@0: michael@0: bool operator==(NST* that) const { michael@0: return object_ == that; michael@0: } michael@0: michael@0: bool operator!=(NST* that) const { michael@0: return object_ != that; michael@0: } michael@0: michael@0: operator NST*() const { michael@0: return object_; michael@0: } michael@0: michael@0: NST* get() const { michael@0: return object_; michael@0: } michael@0: michael@0: void swap(scoped_nsobject& that) { michael@0: NST* temp = that.object_; michael@0: that.object_ = object_; michael@0: object_ = temp; michael@0: } michael@0: michael@0: // scoped_nsobject<>::release() is like scoped_ptr<>::release. It is NOT michael@0: // a wrapper for [object_ release]. To force a scoped_nsobject<> object to michael@0: // call [object_ release], use scoped_nsobject<>::reset(). michael@0: NST* release() { michael@0: NST* temp = object_; michael@0: object_ = nil; michael@0: return temp; michael@0: } michael@0: michael@0: private: michael@0: NST* object_; michael@0: michael@0: DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); michael@0: }; michael@0: michael@0: #endif // BASE_SCOPED_NSOBJECT_H_