Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2009 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_NSOBJECT_H_ | 5 #ifndef BASE_SCOPED_NSOBJECT_H_ |
| 6 #define BASE_SCOPED_NSOBJECT_H_ | 6 #define BASE_SCOPED_NSOBJECT_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #import <Foundation/Foundation.h> | 9 #import <Foundation/Foundation.h> |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 void reset(NST* object = nil) { | 42 void reset(NST* object = nil) { |
| 43 // We intentionally do not check that object != object_ as the caller must | 43 // We intentionally do not check that object != object_ as the caller must |
| 44 // already have an ownership claim over whatever it gives to scoped_nsobject | 44 // already have an ownership claim over whatever it gives to scoped_nsobject |
| 45 // and scoped_cftyperef, whether it's in the constructor or in a call to | 45 // and scoped_cftyperef, whether it's in the constructor or in a call to |
| 46 // reset(). In either case, it relinquishes that claim and the scoper | 46 // reset(). In either case, it relinquishes that claim and the scoper |
| 47 // assumes it. | 47 // assumes it. |
| 48 [object_ release]; | 48 [object_ release]; |
| 49 object_ = object; | 49 object_ = object; |
| 50 } | 50 } |
| 51 | 51 |
| 52 bool operator==(NST* that) const { | 52 bool operator==(NST* that) const { return object_ == that; } |
| 53 return object_ == that; | 53 bool operator!=(NST* that) const { return object_ != that; } |
| 54 } | |
| 55 | |
| 56 bool operator!=(NST* that) const { | |
| 57 return object_ != that; | |
| 58 } | |
| 59 | 54 |
| 60 operator NST*() const { | 55 operator NST*() const { |
| 61 return object_; | 56 return object_; |
| 62 } | 57 } |
| 63 | 58 |
| 64 NST* get() const { | 59 NST* get() const { |
| 65 return object_; | 60 return object_; |
| 66 } | 61 } |
| 67 | 62 |
| 68 void swap(scoped_nsobject& that) { | 63 void swap(scoped_nsobject& that) { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 79 object_ = nil; | 74 object_ = nil; |
| 80 return temp; | 75 return temp; |
| 81 } | 76 } |
| 82 | 77 |
| 83 private: | 78 private: |
| 84 NST* object_; | 79 NST* object_; |
| 85 | 80 |
| 86 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); | 81 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); |
| 87 }; | 82 }; |
| 88 | 83 |
| 84 // Free functions | |
| 85 template <class C> | |
| 86 void swap(scoped_nsobject<C>& p1, scoped_nsobject<C>& p2) { | |
|
Mark Mentovai
2010/09/21 18:40:01
What uses swap as a free guy?
| |
| 87 p1.swap(p2); | |
| 88 } | |
| 89 | |
| 90 template <class C> | |
| 91 bool operator==(C* p1, const scoped_nsobject<C>& p2) { | |
| 92 return p1 == p2.get(); | |
| 93 } | |
| 94 | |
| 95 template <class C> | |
| 96 bool operator!=(C* p1, const scoped_nsobject<C>& p2) { | |
| 97 return p1 != p2.get(); | |
| 98 } | |
| 99 | |
| 100 | |
| 89 // Specialization to make scoped_nsobject<id> work. | 101 // Specialization to make scoped_nsobject<id> work. |
| 90 template<> | 102 template<> |
| 91 class scoped_nsobject<id> { | 103 class scoped_nsobject<id> { |
| 92 public: | 104 public: |
| 93 typedef id element_type; | 105 typedef id element_type; |
| 94 | 106 |
| 95 explicit scoped_nsobject(id object = nil) | 107 explicit scoped_nsobject(id object = nil) |
| 96 : object_(object) { | 108 : object_(object) { |
| 97 } | 109 } |
| 98 | 110 |
| 99 ~scoped_nsobject() { | 111 ~scoped_nsobject() { |
| 100 [object_ release]; | 112 [object_ release]; |
| 101 } | 113 } |
| 102 | 114 |
| 103 void reset(id object = nil) { | 115 void reset(id object = nil) { |
| 104 // We intentionally do not check that object != object_ as the caller must | 116 // We intentionally do not check that object != object_ as the caller must |
| 105 // already have an ownership claim over whatever it gives to scoped_nsobject | 117 // already have an ownership claim over whatever it gives to scoped_nsobject |
| 106 // and scoped_cftyperef, whether it's in the constructor or in a call to | 118 // and scoped_cftyperef, whether it's in the constructor or in a call to |
| 107 // reset(). In either case, it relinquishes that claim and the scoper | 119 // reset(). In either case, it relinquishes that claim and the scoper |
| 108 // assumes it. | 120 // assumes it. |
| 109 [object_ release]; | 121 [object_ release]; |
| 110 object_ = object; | 122 object_ = object; |
| 111 } | 123 } |
| 112 | 124 |
| 113 bool operator==(id that) const { | 125 bool operator==(id that) const { return object_ == that; } |
| 114 return object_ == that; | 126 bool operator!=(id that) const { return object_ != that; } |
| 115 } | |
| 116 | |
| 117 bool operator!=(id that) const { | |
| 118 return object_ != that; | |
| 119 } | |
| 120 | 127 |
| 121 operator id() const { | 128 operator id() const { |
| 122 return object_; | 129 return object_; |
| 123 } | 130 } |
| 124 | 131 |
| 125 id get() const { | 132 id get() const { |
| 126 return object_; | 133 return object_; |
| 127 } | 134 } |
| 128 | 135 |
| 129 void swap(scoped_nsobject& that) { | 136 void swap(scoped_nsobject& that) { |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 151 // ScopedNSAutoreleasePool instead. This is a compile time check. See details | 158 // ScopedNSAutoreleasePool instead. This is a compile time check. See details |
| 152 // at top of header. | 159 // at top of header. |
| 153 template<> | 160 template<> |
| 154 class scoped_nsobject<NSAutoreleasePool> { | 161 class scoped_nsobject<NSAutoreleasePool> { |
| 155 private: | 162 private: |
| 156 explicit scoped_nsobject(NSAutoreleasePool* object = nil); | 163 explicit scoped_nsobject(NSAutoreleasePool* object = nil); |
| 157 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); | 164 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); |
| 158 }; | 165 }; |
| 159 | 166 |
| 160 #endif // BASE_SCOPED_NSOBJECT_H_ | 167 #endif // BASE_SCOPED_NSOBJECT_H_ |
| OLD | NEW |