OLD | NEW |
---|---|
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()) by taking over the caller's existing ownership |
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 | |
25 // ownsership is not changed. | |
Avi (use Gerrit)
2012/02/08 15:56:16
spelling: ownership
qsr
2012/02/08 16:37:26
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 namespace scoped_policy { | |
37 enum OwnershipPolicy { | |
38 ACQUIRE, | |
39 RETAIN | |
40 }; | |
41 } // namespace scoped_policy | |
42 | |
29 template<typename NST> | 43 template<typename NST> |
30 class scoped_nsobject { | 44 class scoped_nsprotocol { |
31 public: | 45 public: |
32 explicit scoped_nsobject(NST* object = nil) | 46 explicit scoped_nsprotocol( |
47 NST object = nil, | |
48 scoped_policy::OwnershipPolicy policy = scoped_policy::ACQUIRE) | |
33 : object_(object) { | 49 : object_(object) { |
50 if (policy == scoped_policy::RETAIN) | |
51 [object retain]; | |
34 } | 52 } |
35 | 53 |
36 ~scoped_nsobject() { | 54 scoped_nsprotocol(const scoped_nsprotocol<NST>& that) |
55 : object_([that.object_ retain]) { | |
56 } | |
57 | |
58 virtual ~scoped_nsprotocol() { | |
37 [object_ release]; | 59 [object_ release]; |
38 } | 60 } |
39 | 61 |
40 void reset(NST* object = nil) { | 62 scoped_nsprotocol& operator=(const scoped_nsprotocol<NST>& that) { |
63 reset(that.get(), scoped_policy::RETAIN); | |
64 return *this; | |
65 } | |
66 | |
67 void reset(NST object = nil, | |
68 scoped_policy::OwnershipPolicy policy = scoped_policy::ACQUIRE) { | |
69 if (policy == scoped_policy::RETAIN) | |
70 [object retain]; | |
41 // We intentionally do not check that object != object_ as the caller must | 71 // We intentionally do not check that object != object_ as the caller must |
42 // already have an ownership claim over whatever it gives to | 72 // either already have an ownership claim over whatever it passes to this |
43 // scoped_nsobject and ScopedCFTypeRef, whether it's in the constructor or | 73 // method, or call it with the |RETAIN| policy which will have ensured that |
44 // in a call to reset(). In either case, it relinquishes that claim and | 74 // the object is retained once more when reaching this point. |
45 // the scoper assumes it. | |
46 [object_ release]; | 75 [object_ release]; |
47 object_ = object; | 76 object_ = object; |
48 } | 77 } |
49 | 78 |
50 bool operator==(NST* that) const { return object_ == that; } | 79 bool operator==(NST that) const { return object_ == that; } |
51 bool operator!=(NST* that) const { return object_ != that; } | 80 bool operator!=(NST that) const { return object_ != that; } |
52 | 81 |
53 operator NST*() const { | 82 operator NST() const { |
54 return object_; | 83 return object_; |
55 } | 84 } |
56 | 85 |
57 NST* get() const { | 86 NST get() const { |
58 return object_; | 87 return object_; |
59 } | 88 } |
60 | 89 |
61 void swap(scoped_nsobject& that) { | 90 void swap(scoped_nsprotocol& that) { |
62 NST* temp = that.object_; | 91 NST temp = that.object_; |
63 that.object_ = object_; | 92 that.object_ = object_; |
64 object_ = temp; | 93 object_ = temp; |
65 } | 94 } |
66 | 95 |
67 // scoped_nsobject<>::release() is like scoped_ptr<>::release. It is NOT | 96 // 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 | 97 // wrapper for [object_ release]. To force a scoped_nsprotocol<> call |
69 // call [object_ release], use scoped_nsobject<>::reset(). | 98 // [object_ release], use scoped_nsprotocol<>::reset(). |
70 NST* release() WARN_UNUSED_RESULT { | 99 NST release() WARN_UNUSED_RESULT { |
71 NST* temp = object_; | 100 NST temp = object_; |
72 object_ = nil; | 101 object_ = nil; |
73 return temp; | 102 return temp; |
74 } | 103 } |
75 | 104 |
76 private: | 105 private: |
77 NST* object_; | 106 NST object_; |
78 | |
79 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); | |
80 }; | 107 }; |
81 | 108 |
82 // Free functions | 109 // Free functions |
83 template <class C> | 110 template <class C> |
84 void swap(scoped_nsobject<C>& p1, scoped_nsobject<C>& p2) { | 111 void swap(scoped_nsprotocol<C>& p1, scoped_nsprotocol<C>& p2) { |
85 p1.swap(p2); | 112 p1.swap(p2); |
86 } | 113 } |
87 | 114 |
88 template <class C> | 115 template <class C> |
89 bool operator==(C* p1, const scoped_nsobject<C>& p2) { | 116 bool operator==(C p1, const scoped_nsprotocol<C>& p2) { |
90 return p1 == p2.get(); | 117 return p1 == p2.get(); |
91 } | 118 } |
92 | 119 |
93 template <class C> | 120 template <class C> |
94 bool operator!=(C* p1, const scoped_nsobject<C>& p2) { | 121 bool operator!=(C p1, const scoped_nsprotocol<C>& p2) { |
95 return p1 != p2.get(); | 122 return p1 != p2.get(); |
96 } | 123 } |
97 | 124 |
125 template<typename NST> | |
126 class scoped_nsobject : public scoped_nsprotocol<NST*> { | |
127 public: | |
128 explicit scoped_nsobject( | |
129 NST* object = nil, | |
130 scoped_policy::OwnershipPolicy policy = scoped_policy::ACQUIRE) | |
131 : scoped_nsprotocol<NST*>(object, policy) { | |
132 } | |
133 | |
134 scoped_nsobject(const scoped_nsobject<NST>& that) | |
135 : scoped_nsprotocol<NST*>(that) { | |
136 } | |
137 | |
138 scoped_nsobject& operator=(const scoped_nsobject<NST>& that) { | |
139 scoped_nsprotocol<NST*>::operator=(that); | |
140 return *this; | |
141 } | |
142 }; | |
98 | 143 |
99 // Specialization to make scoped_nsobject<id> work. | 144 // Specialization to make scoped_nsobject<id> work. |
100 template<> | 145 template<> |
101 class scoped_nsobject<id> { | 146 class scoped_nsobject<id> : public scoped_nsprotocol<id> { |
102 public: | 147 public: |
103 explicit scoped_nsobject(id object = nil) | 148 explicit scoped_nsobject( |
104 : object_(object) { | 149 id object = nil, |
150 scoped_policy::OwnershipPolicy policy = scoped_policy::ACQUIRE) | |
151 : scoped_nsprotocol<id>(object, policy) { | |
105 } | 152 } |
106 | 153 |
107 ~scoped_nsobject() { | 154 scoped_nsobject(const scoped_nsobject<id>& that) |
108 [object_ release]; | 155 : scoped_nsprotocol<id>(that) { |
109 } | 156 } |
110 | 157 |
111 void reset(id object = nil) { | 158 scoped_nsobject& operator=(const scoped_nsobject<id>& that) { |
112 // We intentionally do not check that object != object_ as the caller must | 159 scoped_nsprotocol<id>::operator=(that); |
113 // already have an ownership claim over whatever it gives to | 160 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 } | 161 } |
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 }; | 162 }; |
152 | 163 |
153 // Do not use scoped_nsobject for NSAutoreleasePools, use | 164 // Do not use scoped_nsobject for NSAutoreleasePools, use |
154 // ScopedNSAutoreleasePool instead. This is a compile time check. See details | 165 // ScopedNSAutoreleasePool instead. This is a compile time check. See details |
155 // at top of header. | 166 // at top of header. |
156 template<> | 167 template<> |
157 class scoped_nsobject<NSAutoreleasePool> { | 168 class scoped_nsobject<NSAutoreleasePool> { |
158 private: | 169 private: |
159 explicit scoped_nsobject(NSAutoreleasePool* object = nil); | 170 explicit scoped_nsobject( |
171 NSAutoreleasePool* object = nil, | |
172 scoped_policy::OwnershipPolicy policy = scoped_policy::ACQUIRE); | |
160 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); | 173 DISALLOW_COPY_AND_ASSIGN(scoped_nsobject); |
161 }; | 174 }; |
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_ | 175 #endif // BASE_MEMORY_SCOPED_NSOBJECT_H_ |
OLD | NEW |