1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/base/scoped_nsobject.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,76 @@ 1.4 +// Copyright (c) 2009 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#ifndef BASE_SCOPED_NSOBJECT_H_ 1.9 +#define BASE_SCOPED_NSOBJECT_H_ 1.10 + 1.11 +#import <Foundation/Foundation.h> 1.12 +#include "base/basictypes.h" 1.13 + 1.14 +// scoped_nsobject<> is patterned after scoped_ptr<>, but maintains ownership 1.15 +// of an NSObject subclass object. Style deviations here are solely for 1.16 +// compatibility with scoped_ptr<>'s interface, with which everyone is already 1.17 +// familiar. 1.18 +// 1.19 +// When scoped_nsobject<> takes ownership of an object (in the constructor or 1.20 +// in reset()), it takes over the caller's existing ownership claim. The 1.21 +// caller must own the object it gives to scoped_nsobject<>, and relinquishes 1.22 +// an ownership claim to that object. scoped_nsobject<> does not call 1.23 +// -retain. 1.24 +template<typename NST> 1.25 +class scoped_nsobject { 1.26 + public: 1.27 + typedef NST* element_type; 1.28 + 1.29 + explicit scoped_nsobject(NST* object = nil) 1.30 + : object_(object) { 1.31 + } 1.32 + 1.33 + ~scoped_nsobject() { 1.34 + [object_ release]; 1.35 + } 1.36 + 1.37 + void reset(NST* object = nil) { 1.38 + [object_ release]; 1.39 + object_ = object; 1.40 + } 1.41 + 1.42 + bool operator==(NST* that) const { 1.43 + return object_ == that; 1.44 + } 1.45 + 1.46 + bool operator!=(NST* that) const { 1.47 + return object_ != that; 1.48 + } 1.49 + 1.50 + operator NST*() const { 1.51 + return object_; 1.52 + } 1.53 + 1.54 + NST* get() const { 1.55 + return object_; 1.56 + } 1.57 + 1.58 + void swap(scoped_nsobject& that) { 1.59 + NST* temp = that.object_; 1.60 + that.object_ = object_; 1.61 + object_ = temp; 1.62 + } 1.63 + 1.64 + // scoped_nsobject<>::release() is like scoped_ptr<>::release. It is NOT 1.65 + // a wrapper for [object_ release]. To force a scoped_nsobject<> object to 1.66 + // call [object_ release], use scoped_nsobject<>::reset(). 1.67 + NST* release() { 1.68 + NST* temp = object_; 1.69 + object_ = nil; 1.70 + return temp; 1.71 + } 1.72 + 1.73 + private: 1.74 + NST* object_; 1.75 + 1.76 + DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); 1.77 +}; 1.78 + 1.79 +#endif // BASE_SCOPED_NSOBJECT_H_