Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(255)

Side by Side Diff: base/memory/scoped_nsobject.h

Issue 9290046: Adding a scoped_nsprotocol (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Follow review. Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698