Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #ifndef BASE_SCOPED_NSOBJECT_H_ |
michael@0 | 6 | #define BASE_SCOPED_NSOBJECT_H_ |
michael@0 | 7 | |
michael@0 | 8 | #import <Foundation/Foundation.h> |
michael@0 | 9 | #include "base/basictypes.h" |
michael@0 | 10 | |
michael@0 | 11 | // scoped_nsobject<> is patterned after scoped_ptr<>, but maintains ownership |
michael@0 | 12 | // of an NSObject subclass object. Style deviations here are solely for |
michael@0 | 13 | // compatibility with scoped_ptr<>'s interface, with which everyone is already |
michael@0 | 14 | // familiar. |
michael@0 | 15 | // |
michael@0 | 16 | // When scoped_nsobject<> takes ownership of an object (in the constructor or |
michael@0 | 17 | // in reset()), it takes over the caller's existing ownership claim. The |
michael@0 | 18 | // caller must own the object it gives to scoped_nsobject<>, and relinquishes |
michael@0 | 19 | // an ownership claim to that object. scoped_nsobject<> does not call |
michael@0 | 20 | // -retain. |
michael@0 | 21 | template<typename NST> |
michael@0 | 22 | class scoped_nsobject { |
michael@0 | 23 | public: |
michael@0 | 24 | typedef NST* element_type; |
michael@0 | 25 | |
michael@0 | 26 | explicit scoped_nsobject(NST* object = nil) |
michael@0 | 27 | : object_(object) { |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | ~scoped_nsobject() { |
michael@0 | 31 | [object_ release]; |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | void reset(NST* object = nil) { |
michael@0 | 35 | [object_ release]; |
michael@0 | 36 | object_ = object; |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | bool operator==(NST* that) const { |
michael@0 | 40 | return object_ == that; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | bool operator!=(NST* that) const { |
michael@0 | 44 | return object_ != that; |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | operator NST*() const { |
michael@0 | 48 | return object_; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | NST* get() const { |
michael@0 | 52 | return object_; |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | void swap(scoped_nsobject& that) { |
michael@0 | 56 | NST* temp = that.object_; |
michael@0 | 57 | that.object_ = object_; |
michael@0 | 58 | object_ = temp; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | // scoped_nsobject<>::release() is like scoped_ptr<>::release. It is NOT |
michael@0 | 62 | // a wrapper for [object_ release]. To force a scoped_nsobject<> object to |
michael@0 | 63 | // call [object_ release], use scoped_nsobject<>::reset(). |
michael@0 | 64 | NST* release() { |
michael@0 | 65 | NST* temp = object_; |
michael@0 | 66 | object_ = nil; |
michael@0 | 67 | return temp; |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | private: |
michael@0 | 71 | NST* object_; |
michael@0 | 72 | |
michael@0 | 73 | DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); |
michael@0 | 74 | }; |
michael@0 | 75 | |
michael@0 | 76 | #endif // BASE_SCOPED_NSOBJECT_H_ |