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 // Scopers help you manage ownership of a pointer, helping you easily manage the | 5 // Scopers help you manage ownership of a pointer, helping you easily manage the |
6 // a pointer within a scope, and automatically destroying the pointer at the | 6 // a pointer within a scope, and automatically destroying the pointer at the |
7 // end of a scope. There are two main classes you will use, which correspond | 7 // end of a scope. There are two main classes you will use, which correspond |
8 // to the operators new/delete and new[]/delete[]. | 8 // to the operators new/delete and new[]/delete[]. |
9 // | 9 // |
10 // Example usage (scoped_ptr): | 10 // Example usage (scoped_ptr): |
(...skipping 14 matching lines...) Expand all Loading... | |
25 // foo.reset(); // Foo("wee4") destroyed, foo no longer | 25 // foo.reset(); // Foo("wee4") destroyed, foo no longer |
26 // // manages a pointer. | 26 // // manages a pointer. |
27 // } // foo wasn't managing a pointer, so nothing was destroyed. | 27 // } // foo wasn't managing a pointer, so nothing was destroyed. |
28 // | 28 // |
29 // Example usage (scoped_array): | 29 // Example usage (scoped_array): |
30 // { | 30 // { |
31 // scoped_array<Foo> foo(new Foo[100]); | 31 // scoped_array<Foo> foo(new Foo[100]); |
32 // foo.get()->Method(); // Foo::Method on the 0th element. | 32 // foo.get()->Method(); // Foo::Method on the 0th element. |
33 // foo[10].Method(); // Foo::Method on the 10th element. | 33 // foo[10].Method(); // Foo::Method on the 10th element. |
34 // } | 34 // } |
35 // | |
36 // These scopers also implement part of the functionality of C++11 unique_ptr | |
37 // in that they are "moveable but not copyable." You can use the scopers in | |
38 // the parameter and return types of functions to signify ownership transfer | |
39 // in to and out of a function. When calling a function that has a scoper | |
40 // as the argument type, it must be called with the result of an analogous | |
41 // scoper's Pass() function or another funciton that generates a temporary; | |
willchan no longer on Chromium
2011/12/07 16:24:34
function
awong
2011/12/08 21:04:10
Done.
| |
42 // passing by copy will NOT work. Here is an example using scoped_ptr: | |
43 // | |
44 // void TakesOwnership(scoped_ptr<Foo> arg) { | |
45 // // Do something with arg | |
46 // } | |
47 // scoped_ptr<Foo> CreateFoo() { | |
48 // // No need for calling Pass() because we are constructing a temporary | |
49 // // for the return value. | |
50 // return scoped_ptr<Foo>(new Foo("new")); | |
51 // } | |
52 // scoped_ptr<Foo> PassThru(scoped_ptr<Foo> arg) { | |
53 // return arg.Pass(); | |
54 // } | |
55 // | |
56 // { | |
57 // scoped_ptr<Foo> ptr(new Foo("yay")); // ptr manages Foo("yay)" | |
58 // TakesOwnership(ptr.Pass()); // ptr no longer owns Foo("yay"). | |
59 // scoped_ptr<Foo> ptr2 = CreateFoo(); // ptr2 owns the return Foo. | |
60 // scoped_ptr<Foo> ptr3 = // ptr3 now owns what was in ptr2. | |
61 // PassThru(ptr2.Pass()); // ptr2 is correspondly NULL. | |
62 // } | |
63 // | |
64 // Notice that if you do not call Pass() when returning from PassThru(), or | |
65 // when invoking TakesOwnership(), the code will not compile because scopers | |
66 // are not copyable; they only implement move semantics which require calling | |
67 // the Pass() function to signify a destructive transfer of state. CreateFoo() | |
68 // is different though because we are constructing a temporary on the return | |
69 // line and thus can avoid needing to call Pass(). | |
35 | 70 |
36 #ifndef BASE_MEMORY_SCOPED_PTR_H_ | 71 #ifndef BASE_MEMORY_SCOPED_PTR_H_ |
37 #define BASE_MEMORY_SCOPED_PTR_H_ | 72 #define BASE_MEMORY_SCOPED_PTR_H_ |
38 #pragma once | 73 #pragma once |
39 | 74 |
40 // This is an implementation designed to match the anticipated future TR2 | 75 // This is an implementation designed to match the anticipated future TR2 |
41 // implementation of the scoped_ptr class, and its closely-related brethren, | 76 // implementation of the scoped_ptr class, and its closely-related brethren, |
42 // scoped_array, scoped_ptr_malloc. | 77 // scoped_array, scoped_ptr_malloc. |
43 | 78 |
44 #include <assert.h> | 79 #include <assert.h> |
45 #include <stddef.h> | 80 #include <stddef.h> |
46 #include <stdlib.h> | 81 #include <stdlib.h> |
47 | 82 |
48 #include "base/compiler_specific.h" | 83 #include "base/compiler_specific.h" |
49 | 84 |
85 // Macro with the boilerplate C++03 move emulation for a class. | |
86 // | |
87 // In C++11, this is done via rvalue references. Here, we use C++03 move | |
88 // emulation to fake an rvalue reference. For a more thorough explanation | |
89 // of the technique, see: | |
90 // | |
91 // http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Move_Constructor | |
92 // | |
93 #define CPP_03_MOVE_EMULATION(scoper, field) \ | |
94 public: \ | |
willchan no longer on Chromium
2011/12/07 19:25:24
this line is unnecessary, right?
awong
2011/12/08 21:04:10
Fixed to make it necessary. :)
| |
95 struct RValue { \ | |
96 explicit RValue(scoper& obj) : obj_(obj) {} \ | |
97 scoper& obj_; \ | |
98 }; \ | |
99 operator RValue() { return RValue(*this); } \ | |
100 scoper(RValue proxy) : field(proxy.obj_.release()) { } \ | |
101 scoper& operator=(RValue proxy) { \ | |
102 swap(proxy.obj_); \ | |
103 return *this; \ | |
104 } \ | |
105 scoper Pass() { return scoper(RValue(*this)); } | |
106 | |
50 // A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T> | 107 // A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T> |
51 // automatically deletes the pointer it holds (if any). | 108 // automatically deletes the pointer it holds (if any). |
52 // That is, scoped_ptr<T> owns the T object that it points to. | 109 // That is, scoped_ptr<T> owns the T object that it points to. |
53 // Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object. | 110 // Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object. |
54 // Also like T*, scoped_ptr<T> is thread-compatible, and once you | 111 // Also like T*, scoped_ptr<T> is thread-compatible, and once you |
55 // dereference it, you get the threadsafety guarantees of T. | 112 // dereference it, you get the threadsafety guarantees of T. |
56 // | 113 // |
57 // The size of a scoped_ptr is small: | 114 // The size of a scoped_ptr is small: |
58 // sizeof(scoped_ptr<C>) == sizeof(C*) | 115 // sizeof(scoped_ptr<C>) == sizeof(C*) |
59 template <class C> | 116 template <class C> |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
115 // The return value is the current pointer held by this object. | 172 // The return value is the current pointer held by this object. |
116 // If this object holds a NULL pointer, the return value is NULL. | 173 // If this object holds a NULL pointer, the return value is NULL. |
117 // After this operation, this object will hold a NULL pointer, | 174 // After this operation, this object will hold a NULL pointer, |
118 // and will not own the object any more. | 175 // and will not own the object any more. |
119 C* release() WARN_UNUSED_RESULT { | 176 C* release() WARN_UNUSED_RESULT { |
120 C* retVal = ptr_; | 177 C* retVal = ptr_; |
121 ptr_ = NULL; | 178 ptr_ = NULL; |
122 return retVal; | 179 return retVal; |
123 } | 180 } |
124 | 181 |
182 CPP_03_MOVE_EMULATION(scoped_ptr, ptr_); | |
183 | |
125 private: | 184 private: |
126 C* ptr_; | 185 C* ptr_; |
127 | 186 |
128 // Forbid comparison of scoped_ptr types. If C2 != C, it totally doesn't | 187 // Forbid comparison of scoped_ptr types. If C2 != C, it totally doesn't |
129 // make sense, and if C2 == C, it still doesn't make sense because you should | 188 // make sense, and if C2 == C, it still doesn't make sense because you should |
130 // never have the same object owned by two different scoped_ptrs. | 189 // never have the same object owned by two different scoped_ptrs. |
131 template <class C2> bool operator==(scoped_ptr<C2> const& p2) const; | 190 template <class C2> bool operator==(scoped_ptr<C2> const& p2) const; |
132 template <class C2> bool operator!=(scoped_ptr<C2> const& p2) const; | 191 template <class C2> bool operator!=(scoped_ptr<C2> const& p2) const; |
133 | 192 |
134 // Disallow evil constructors | 193 // Disallow evil constructors. Note that MUST NOT take a const& because we |
135 scoped_ptr(const scoped_ptr&); | 194 // are implmenting move semantics. See the CPP_03_MOVE_EMULATION macro. |
willchan no longer on Chromium
2011/12/07 19:25:24
implementing
awong
2011/12/08 21:04:10
Done.
| |
136 void operator=(const scoped_ptr&); | 195 scoped_ptr(scoped_ptr&); |
196 void operator=(scoped_ptr&); | |
137 }; | 197 }; |
138 | 198 |
139 // Free functions | 199 // Free functions |
140 template <class C> | 200 template <class C> |
141 void swap(scoped_ptr<C>& p1, scoped_ptr<C>& p2) { | 201 void swap(scoped_ptr<C>& p1, scoped_ptr<C>& p2) { |
142 p1.swap(p2); | 202 p1.swap(p2); |
143 } | 203 } |
144 | 204 |
145 template <class C> | 205 template <class C> |
146 bool operator==(C* p1, const scoped_ptr<C>& p2) { | 206 bool operator==(C* p1, const scoped_ptr<C>& p2) { |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
222 // The return value is the current pointer held by this object. | 282 // The return value is the current pointer held by this object. |
223 // If this object holds a NULL pointer, the return value is NULL. | 283 // If this object holds a NULL pointer, the return value is NULL. |
224 // After this operation, this object will hold a NULL pointer, | 284 // After this operation, this object will hold a NULL pointer, |
225 // and will not own the object any more. | 285 // and will not own the object any more. |
226 C* release() WARN_UNUSED_RESULT { | 286 C* release() WARN_UNUSED_RESULT { |
227 C* retVal = array_; | 287 C* retVal = array_; |
228 array_ = NULL; | 288 array_ = NULL; |
229 return retVal; | 289 return retVal; |
230 } | 290 } |
231 | 291 |
292 CPP_03_MOVE_EMULATION(scoped_array, array_); | |
293 | |
232 private: | 294 private: |
233 C* array_; | 295 C* array_; |
234 | 296 |
235 // Forbid comparison of different scoped_array types. | 297 // Forbid comparison of different scoped_array types. |
236 template <class C2> bool operator==(scoped_array<C2> const& p2) const; | 298 template <class C2> bool operator==(scoped_array<C2> const& p2) const; |
237 template <class C2> bool operator!=(scoped_array<C2> const& p2) const; | 299 template <class C2> bool operator!=(scoped_array<C2> const& p2) const; |
238 | 300 |
239 // Disallow evil constructors | 301 // Disallow evil constructors. Note that MUST NOT take a const& because we |
240 scoped_array(const scoped_array&); | 302 // are implmenting move semantics. See the CPP_03_MOVE_EMULATION macro. |
241 void operator=(const scoped_array&); | 303 scoped_array(scoped_array&); |
304 void operator=(scoped_array&); | |
242 }; | 305 }; |
243 | 306 |
244 // Free functions | 307 // Free functions |
245 template <class C> | 308 template <class C> |
246 void swap(scoped_array<C>& p1, scoped_array<C>& p2) { | 309 void swap(scoped_array<C>& p1, scoped_array<C>& p2) { |
247 p1.swap(p2); | 310 p1.swap(p2); |
248 } | 311 } |
249 | 312 |
250 template <class C> | 313 template <class C> |
251 bool operator==(C* p1, const scoped_array<C>& p2) { | 314 bool operator==(C* p1, const scoped_array<C>& p2) { |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
340 // The return value is the current pointer held by this object. | 403 // The return value is the current pointer held by this object. |
341 // If this object holds a NULL pointer, the return value is NULL. | 404 // If this object holds a NULL pointer, the return value is NULL. |
342 // After this operation, this object will hold a NULL pointer, | 405 // After this operation, this object will hold a NULL pointer, |
343 // and will not own the object any more. | 406 // and will not own the object any more. |
344 C* release() WARN_UNUSED_RESULT { | 407 C* release() WARN_UNUSED_RESULT { |
345 C* tmp = ptr_; | 408 C* tmp = ptr_; |
346 ptr_ = NULL; | 409 ptr_ = NULL; |
347 return tmp; | 410 return tmp; |
348 } | 411 } |
349 | 412 |
413 CPP_03_MOVE_EMULATION(scoped_ptr_malloc, ptr_); | |
414 | |
350 private: | 415 private: |
351 C* ptr_; | 416 C* ptr_; |
352 | 417 |
353 // no reason to use these: each scoped_ptr_malloc should have its own object | 418 // no reason to use these: each scoped_ptr_malloc should have its own object |
354 template <class C2, class GP> | 419 template <class C2, class GP> |
355 bool operator==(scoped_ptr_malloc<C2, GP> const& p) const; | 420 bool operator==(scoped_ptr_malloc<C2, GP> const& p) const; |
356 template <class C2, class GP> | 421 template <class C2, class GP> |
357 bool operator!=(scoped_ptr_malloc<C2, GP> const& p) const; | 422 bool operator!=(scoped_ptr_malloc<C2, GP> const& p) const; |
358 | 423 |
359 // Disallow evil constructors | 424 // Disallow evil constructors. Note that MUST NOT take a const& because we |
360 scoped_ptr_malloc(const scoped_ptr_malloc&); | 425 // are implmenting move semantics. See the CPP_03_MOVE_EMULATION macro. |
willchan no longer on Chromium
2011/12/07 19:25:24
implementing
awong
2011/12/08 21:04:10
Done.
| |
361 void operator=(const scoped_ptr_malloc&); | 426 scoped_ptr_malloc(scoped_ptr_malloc&); |
427 void operator=(scoped_ptr_malloc&); | |
362 }; | 428 }; |
363 | 429 |
430 #undef CPP_03_MOVE_EMULATION | |
431 | |
364 template<class C, class FP> inline | 432 template<class C, class FP> inline |
365 void swap(scoped_ptr_malloc<C, FP>& a, scoped_ptr_malloc<C, FP>& b) { | 433 void swap(scoped_ptr_malloc<C, FP>& a, scoped_ptr_malloc<C, FP>& b) { |
366 a.swap(b); | 434 a.swap(b); |
367 } | 435 } |
368 | 436 |
369 template<class C, class FP> inline | 437 template<class C, class FP> inline |
370 bool operator==(C* p, const scoped_ptr_malloc<C, FP>& b) { | 438 bool operator==(C* p, const scoped_ptr_malloc<C, FP>& b) { |
371 return p == b.get(); | 439 return p == b.get(); |
372 } | 440 } |
373 | 441 |
374 template<class C, class FP> inline | 442 template<class C, class FP> inline |
375 bool operator!=(C* p, const scoped_ptr_malloc<C, FP>& b) { | 443 bool operator!=(C* p, const scoped_ptr_malloc<C, FP>& b) { |
376 return p != b.get(); | 444 return p != b.get(); |
377 } | 445 } |
378 | 446 |
379 #endif // BASE_MEMORY_SCOPED_PTR_H_ | 447 #endif // BASE_MEMORY_SCOPED_PTR_H_ |
OLD | NEW |