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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
72 object_ = nil; | 72 object_ = nil; |
73 return temp; | 73 return temp; |
74 } | 74 } |
75 | 75 |
76 private: | 76 private: |
77 NST* object_; | 77 NST* object_; |
78 | 78 |
79 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); | 79 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); |
80 }; | 80 }; |
81 | 81 |
82 // Free functions | 82 // Free functions |
Mark Mentovai
2012/01/26 14:21:15
Do these for scoped_nsprotocol too?
qsr
2012/01/26 14:34:50
Done.
| |
83 template <class C> | 83 template <class C> |
84 void swap(scoped_nsobject<C>& p1, scoped_nsobject<C>& p2) { | 84 void swap(scoped_nsobject<C>& p1, scoped_nsobject<C>& p2) { |
85 p1.swap(p2); | 85 p1.swap(p2); |
86 } | 86 } |
87 | 87 |
88 template <class C> | 88 template <class C> |
89 bool operator==(C* p1, const scoped_nsobject<C>& p2) { | 89 bool operator==(C* p1, const scoped_nsobject<C>& p2) { |
90 return p1 == p2.get(); | 90 return p1 == p2.get(); |
91 } | 91 } |
92 | 92 |
(...skipping 60 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>. | |
Mark Mentovai
2012/01/26 14:21:15
This is just scoped_nsobject without the helpful *
qsr
2012/01/26 14:34:50
Done.
| |
164 template<typename NST> | |
165 class scoped_nsprotocol { | |
166 public: | |
167 explicit scoped_nsprotocol(NST object = nil) : object_(object) { | |
168 } | |
169 | |
170 ~scoped_nsprotocol() { | |
171 [object_ release]; | |
172 } | |
173 | |
174 void reset(NST object = nil) { | |
175 [object_ release]; | |
176 object_ = object; | |
177 } | |
178 | |
179 bool operator==(NST that) const { return object_ == that; } | |
180 bool operator!=(NST that) const { return object_ != that; } | |
181 | |
182 operator NST() const { | |
183 return object_; | |
184 } | |
185 | |
186 NST get() const { | |
187 return object_; | |
188 } | |
189 | |
190 void swap(scoped_nsprotocol& that) { | |
191 NST temp = that.object_; | |
192 that.object_ = object_; | |
193 object_ = temp; | |
194 } | |
195 | |
196 NST release() WARN_UNUSED_RESULT { | |
197 NST temp = object_; | |
198 object_ = nil; | |
199 return temp; | |
200 } | |
201 | |
202 private: | |
203 NST object_; | |
204 | |
205 DISALLOW_COPY_AND_ASSIGN(scoped_nsprotocol); | |
206 }; | |
207 | |
163 #endif // BASE_MEMORY_SCOPED_NSOBJECT_H_ | 208 #endif // BASE_MEMORY_SCOPED_NSOBJECT_H_ |
OLD | NEW |