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

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

Issue 9349016: Update scoped_nsobject to be copyable. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Correct documentation. Created 8 years, 10 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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"
11 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
12 12
13 // scoped_nsobject<> is patterned after scoped_ptr<>, but maintains ownership 13 // scoped_nsobject<> is patterned after scoped_ptr<>, but maintains ownership
14 // of an NSObject subclass object. Style deviations here are solely for 14 // of an NSObject subclass object. Style deviations here are solely for
15 // compatibility with scoped_ptr<>'s interface, with which everyone is already 15 // compatibility with scoped_ptr<>'s interface, with which everyone is already
16 // familiar. 16 // familiar.
17 // 17 //
18 // When scoped_nsobject<> takes ownership of an object (in the constructor or 18 // By default, scoped_nsobject<> takes ownership of an object (in the
19 // in reset()), it takes over the caller's existing ownership claim. The 19 // constructor or in reset()), it takes over the caller's existing ownership
Mark Mentovai 2012/02/07 17:13:17 Awkward phrasing. How ’bout By default, scoped_ns
qsr 2012/02/08 14:59:41 Done.
20 // caller must own the object it gives to scoped_nsobject<>, and relinquishes 20 // claim. The caller must own the object it gives to scoped_nsobject<>, and
21 // an ownership claim to that object. scoped_nsobject<> does not call 21 // relinquishes an ownership claim to that object. scoped_nsobject<> does not
22 // -retain. 22 // call -retain. This behavior is parametrized by the |OwnershipPolicy| enum.
23 // If the value |RETAIN| is passed (in the constructor or in reset()), then
24 // scoped_nsobject<> will call |retain| on the object, and the initial
Mark Mentovai 2012/02/07 17:13:17 -retain instead of |retain|
qsr 2012/02/08 14:59:41 Done.
25 // ownsershop is not changed.
Mark Mentovai 2012/02/07 17:13:17 ownership, not ownsershop.
qsr 2012/02/08 14:59:41 Done.
26 //
27 // scoped_nsprotocol<> has the same behavior than scoped_nsobject, but can be
28 // used with protocols.
23 // 29 //
24 // scoped_nsobject<> is not to be used for NSAutoreleasePools. For 30 // scoped_nsobject<> is not to be used for NSAutoreleasePools. For
25 // NSAutoreleasePools use ScopedNSAutoreleasePool from 31 // NSAutoreleasePools use ScopedNSAutoreleasePool from
26 // scoped_nsautorelease_pool.h instead. 32 // scoped_nsautorelease_pool.h instead.
27 // We check for bad uses of scoped_nsobject and NSAutoreleasePool at compile 33 // We check for bad uses of scoped_nsobject and NSAutoreleasePool at compile
28 // time with a template specialization (see below). 34 // time with a template specialization (see below).
35
36 enum OwnershipPolicy {
37 ACQUIRE,
Mark Mentovai 2012/02/07 17:13:17 Can we stuff this into a namespace? Having ACQUIRE
qsr 2012/02/08 14:59:41 Done.
38 RETAIN
39 };
40
29 template<typename NST> 41 template<typename NST>
30 class scoped_nsobject { 42 class scoped_nsprotocol {
31 public: 43 public:
32 explicit scoped_nsobject(NST* object = nil) 44 scoped_nsprotocol(NST object = nil, OwnershipPolicy policy = ACQUIRE)
33 : object_(object) { 45 : object_(policy == ACQUIRE ? object : [object retain]) {
Mark Mentovai 2012/02/07 17:13:17 Here and on line 62: I can appreciate the tersene
qsr 2012/02/08 14:59:41 Done.
34 } 46 }
35 47
36 ~scoped_nsobject() { 48 explicit scoped_nsprotocol(const scoped_nsprotocol<NST>& other)
Mark Mentovai 2012/02/07 17:13:17 I don’t think this will work in STL containers wit
Mark Mentovai 2012/02/07 17:13:17 Here and on line 56: The other methods in this cl
qsr 2012/02/08 14:59:41 Done.
qsr 2012/02/08 14:59:41 You are right. The funny thing is this compiles pe
49 : object_([other.object_ retain]) {
50 }
51
52 ~scoped_nsprotocol() {
37 [object_ release]; 53 [object_ release];
38 } 54 }
39 55
40 void reset(NST* object = nil) { 56 scoped_nsprotocol& operator= (const scoped_nsprotocol<NST>& other) {
Mark Mentovai 2012/02/07 17:13:17 No space between operator= and the (.
qsr 2012/02/08 14:59:41 Done.
41 // We intentionally do not check that object != object_ as the caller must 57 reset(other.get(), RETAIN);
42 // already have an ownership claim over whatever it gives to 58 return *this;
43 // scoped_nsobject and ScopedCFTypeRef, whether it's in the constructor or
44 // in a call to reset(). In either case, it relinquishes that claim and
45 // the scoper assumes it.
46 [object_ release];
47 object_ = object;
48 } 59 }
49 60
50 bool operator==(NST* that) const { return object_ == that; } 61 void reset(NST object = nil, OwnershipPolicy policy = ACQUIRE) {
51 bool operator!=(NST* that) const { return object_ != that; } 62 NST temp = policy == ACQUIRE ? object : [object retain];
63 [object_ release];
64 object_ = temp;
65 }
52 66
53 operator NST*() const { 67 bool operator==(NST that) const { return object_ == that; }
68 bool operator!=(NST that) const { return object_ != that; }
69
70 operator NST() const {
54 return object_; 71 return object_;
55 } 72 }
56 73
57 NST* get() const { 74 NST get() const {
58 return object_; 75 return object_;
59 } 76 }
60 77
61 void swap(scoped_nsobject& that) { 78 void swap(scoped_nsprotocol& that) {
62 NST* temp = that.object_; 79 NST temp = that.object_;
63 that.object_ = object_; 80 that.object_ = object_;
64 object_ = temp; 81 object_ = temp;
65 } 82 }
66 83
67 // scoped_nsobject<>::release() is like scoped_ptr<>::release. It is NOT 84 // scoped_nsprotocol<>::release() is like scoped_ptr<>::release. It is NOT a
68 // a wrapper for [object_ release]. To force a scoped_nsobject<> object to 85 // wrapper for [object_ release]. To force a scoped_nsprotocol<> call
69 // call [object_ release], use scoped_nsobject<>::reset(). 86 // [object_ release], use scoped_nsprotocol<>::reset().
70 NST* release() WARN_UNUSED_RESULT { 87 NST release() WARN_UNUSED_RESULT {
71 NST* temp = object_; 88 NST temp = object_;
72 object_ = nil; 89 object_ = nil;
73 return temp; 90 return temp;
74 } 91 }
75 92
76 private: 93 private:
77 NST* object_; 94 NST object_;
78
79 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject);
80 }; 95 };
81 96
82 // Free functions 97 // Free functions
83 template <class C> 98 template <class C>
84 void swap(scoped_nsobject<C>& p1, scoped_nsobject<C>& p2) { 99 void swap(scoped_nsprotocol<C>& p1, scoped_nsprotocol<C>& p2) {
85 p1.swap(p2); 100 p1.swap(p2);
86 } 101 }
87 102
88 template <class C> 103 template <class C>
89 bool operator==(C* p1, const scoped_nsobject<C>& p2) { 104 bool operator==(C p1, const scoped_nsprotocol<C>& p2) {
90 return p1 == p2.get(); 105 return p1 == p2.get();
91 } 106 }
92 107
93 template <class C> 108 template <class C>
94 bool operator!=(C* p1, const scoped_nsobject<C>& p2) { 109 bool operator!=(C p1, const scoped_nsprotocol<C>& p2) {
95 return p1 != p2.get(); 110 return p1 != p2.get();
96 } 111 }
97 112
113 template<typename NST>
114 class scoped_nsobject : public scoped_nsprotocol<NST*> {
115 public:
116 scoped_nsobject(NST* object = nil, OwnershipPolicy policy = ACQUIRE)
117 : scoped_nsprotocol<NST*>(object, policy) {
118 }
119
120 explicit scoped_nsobject(const scoped_nsobject<NST>& other)
Mark Mentovai 2012/02/07 17:13:17 The same comments as on line 48 applies here and o
qsr 2012/02/08 14:59:41 Done.
121 : scoped_nsprotocol<NST*>(other) {
122 }
123
124 scoped_nsobject& operator= (const scoped_nsobject<NST>& other) {
Mark Mentovai 2012/02/07 17:13:17 The same comment as on line 56 applies here and on
qsr 2012/02/08 14:59:41 Done.
125 scoped_nsprotocol<NST*>::operator=(other);
126 return *this;
127 }
128 };
Mark Mentovai 2012/02/07 17:13:17 Do we need the “free functions” (lines 97-111) for
qsr 2012/02/08 14:59:41 I might be missing something, but why would we nee
98 129
99 // Specialization to make scoped_nsobject<id> work. 130 // Specialization to make scoped_nsobject<id> work.
100 template<> 131 template<>
101 class scoped_nsobject<id> { 132 class scoped_nsobject<id> : public scoped_nsprotocol<id> {
102 public: 133 public:
103 explicit scoped_nsobject(id object = nil) 134 scoped_nsobject(id object = nil, OwnershipPolicy policy = ACQUIRE)
104 : object_(object) { 135 : scoped_nsprotocol<id>(object, policy) {
105 } 136 }
106 137
107 ~scoped_nsobject() { 138 explicit scoped_nsobject(const scoped_nsobject<id>& other)
108 [object_ release]; 139 : scoped_nsprotocol<id>(other) {
109 } 140 }
110 141
111 void reset(id object = nil) { 142 scoped_nsobject& operator= (const scoped_nsobject<id>& other) {
112 // We intentionally do not check that object != object_ as the caller must 143 scoped_nsprotocol<id>::operator=(other);
113 // already have an ownership claim over whatever it gives to 144 return *this;
114 // scoped_nsobject and ScopedCFTypeRef, whether it's in the constructor or
115 // in a call to reset(). In either case, it relinquishes that claim and
116 // the scoper assumes it.
117 [object_ release];
118 object_ = object;
119 } 145 }
120
121 bool operator==(id that) const { return object_ == that; }
122 bool operator!=(id that) const { return object_ != that; }
123
124 operator id() const {
125 return object_;
126 }
127
128 id get() const {
129 return object_;
130 }
131
132 void swap(scoped_nsobject& that) {
133 id temp = that.object_;
134 that.object_ = object_;
135 object_ = temp;
136 }
137
138 // scoped_nsobject<>::release() is like scoped_ptr<>::release. It is NOT
139 // a wrapper for [object_ release]. To force a scoped_nsobject<> object to
140 // call [object_ release], use scoped_nsobject<>::reset().
141 id release() WARN_UNUSED_RESULT {
142 id temp = object_;
143 object_ = nil;
144 return temp;
145 }
146
147 private:
148 id object_;
149
150 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject);
151 }; 146 };
152 147
153 // Do not use scoped_nsobject for NSAutoreleasePools, use 148 // Do not use scoped_nsobject for NSAutoreleasePools, use
154 // ScopedNSAutoreleasePool instead. This is a compile time check. See details 149 // ScopedNSAutoreleasePool instead. This is a compile time check. See details
155 // at top of header. 150 // at top of header.
156 template<> 151 template<>
157 class scoped_nsobject<NSAutoreleasePool> { 152 class scoped_nsobject<NSAutoreleasePool> {
158 private: 153 private:
159 explicit scoped_nsobject(NSAutoreleasePool* object = nil); 154 scoped_nsobject(NSAutoreleasePool* object = nil,
155 OwnershipPolicy policy = ACQUIRE);
160 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); 156 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject);
161 }; 157 };
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
226 #endif // BASE_MEMORY_SCOPED_NSOBJECT_H_ 158 #endif // BASE_MEMORY_SCOPED_NSOBJECT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698