OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_MEMORY_SCOPED_NSOBJECT_H_ | 5 #ifndef BASE_MEMORY_SCOPED_NSOBJECT_H_ |
6 #define BASE_MEMORY_SCOPED_NSOBJECT_H_ | 6 #define BASE_MEMORY_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 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 // Do not use scoped_nsobject for NSAutoreleasePools, use | 153 // Do not use scoped_nsobject for NSAutoreleasePools, use |
154 // ScopedNSAutoreleasePool instead. This is a compile time check. See details | 154 // ScopedNSAutoreleasePool instead. This is a compile time check. See details |
155 // at top of header. | 155 // at top of header. |
156 template<> | 156 template<> |
157 class scoped_nsobject<NSAutoreleasePool> { | 157 class scoped_nsobject<NSAutoreleasePool> { |
158 private: | 158 private: |
159 explicit scoped_nsobject(NSAutoreleasePool* object = nil); | 159 explicit scoped_nsobject(NSAutoreleasePool* object = nil); |
160 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); | 160 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); |
161 }; | 161 }; |
162 | 162 |
| 163 // Equivalent of scoped_nsobject for a id<protocol>. This is exactly the same |
| 164 // class as scoped_nsobject, except that it doesn't handle any pointer |
| 165 // transformation. |
| 166 template<typename NST> |
| 167 class scoped_nsprotocol { |
| 168 public: |
| 169 explicit scoped_nsprotocol(NST object = nil) : object_(object) { |
| 170 } |
| 171 |
| 172 ~scoped_nsprotocol() { |
| 173 [object_ release]; |
| 174 } |
| 175 |
| 176 void reset(NST object = nil) { |
| 177 [object_ release]; |
| 178 object_ = object; |
| 179 } |
| 180 |
| 181 bool operator==(NST that) const { return object_ == that; } |
| 182 bool operator!=(NST that) const { return object_ != that; } |
| 183 |
| 184 operator NST() const { |
| 185 return object_; |
| 186 } |
| 187 |
| 188 NST get() const { |
| 189 return object_; |
| 190 } |
| 191 |
| 192 void swap(scoped_nsprotocol& that) { |
| 193 NST temp = that.object_; |
| 194 that.object_ = object_; |
| 195 object_ = temp; |
| 196 } |
| 197 |
| 198 NST release() WARN_UNUSED_RESULT { |
| 199 NST temp = object_; |
| 200 object_ = nil; |
| 201 return temp; |
| 202 } |
| 203 |
| 204 private: |
| 205 NST object_; |
| 206 |
| 207 DISALLOW_COPY_AND_ASSIGN(scoped_nsprotocol); |
| 208 }; |
| 209 |
| 210 // Free functions |
| 211 template <class C> |
| 212 void swap(scoped_nsprotocol<C>& p1, scoped_nsprotocol<C>& p2) { |
| 213 p1.swap(p2); |
| 214 } |
| 215 |
| 216 template <class C> |
| 217 bool operator==(C p1, const scoped_nsprotocol<C>& p2) { |
| 218 return p1 == p2.get(); |
| 219 } |
| 220 |
| 221 template <class C> |
| 222 bool operator!=(C p1, const scoped_nsprotocol<C>& p2) { |
| 223 return p1 != p2.get(); |
| 224 } |
| 225 |
163 #endif // BASE_MEMORY_SCOPED_NSOBJECT_H_ | 226 #endif // BASE_MEMORY_SCOPED_NSOBJECT_H_ |
OLD | NEW |