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

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

Issue 11028044: Allow scoped_ptr variables to be used in boolean expressions (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 2 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) 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 // 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 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 C& operator*() const { 196 C& operator*() const {
197 assert(ptr_ != NULL); 197 assert(ptr_ != NULL);
198 return *ptr_; 198 return *ptr_;
199 } 199 }
200 C* operator->() const { 200 C* operator->() const {
201 assert(ptr_ != NULL); 201 assert(ptr_ != NULL);
202 return ptr_; 202 return ptr_;
203 } 203 }
204 C* get() const { return ptr_; } 204 C* get() const { return ptr_; }
205 205
206 // Allow scoped_ptr<C> to be used in boolean expressions, but not
207 // implicitly convertable to a real bool (which is dangerous).
willchan no longer on Chromium 2012/10/05 22:19:13 s/convertable/convertible/
enne (OOO) 2012/10/06 02:18:41 Done. *blush*
208 typedef C* scoped_ptr::*testable;
willchan no longer on Chromium 2012/10/05 22:19:13 Since testable is a type, please name it Testable.
enne (OOO) 2012/10/06 02:18:41 Done.
209 operator testable() const { return ptr_ ? &scoped_ptr::ptr_ : 0; }
willchan no longer on Chromium 2012/10/05 22:19:13 We use NULL, a la Google C++ Style Guide.
enne (OOO) 2012/10/06 02:18:41 Done.
210
206 // Comparison operators. 211 // Comparison operators.
207 // These return whether two scoped_ptr refer to the same object, not just to 212 // These return whether two scoped_ptr refer to the same object, not just to
208 // two different but equal objects. 213 // two different but equal objects.
209 bool operator==(C* p) const { return ptr_ == p; } 214 bool operator==(C* p) const { return ptr_ == p; }
210 bool operator!=(C* p) const { return ptr_ != p; } 215 bool operator!=(C* p) const { return ptr_ != p; }
211 216
212 // Swap two scoped pointers. 217 // Swap two scoped pointers.
213 void swap(scoped_ptr& p2) { 218 void swap(scoped_ptr& p2) {
214 C* tmp = ptr_; 219 C* tmp = ptr_;
215 ptr_ = p2.ptr_; 220 ptr_ = p2.ptr_;
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 assert(array_ != NULL); 326 assert(array_ != NULL);
322 return array_[i]; 327 return array_[i];
323 } 328 }
324 329
325 // Get a pointer to the zeroth element of the current object. 330 // Get a pointer to the zeroth element of the current object.
326 // If there is no current object, return NULL. 331 // If there is no current object, return NULL.
327 C* get() const { 332 C* get() const {
328 return array_; 333 return array_;
329 } 334 }
330 335
336 // Allow scoped_array<C> to be used in boolean expressions, but not
337 // implicitly convertable to a real bool (which is dangerous).
338 typedef C* scoped_array::*testable;
339 operator testable() const { return array_ ? &scoped_array::array_ : 0; }
340
331 // Comparison operators. 341 // Comparison operators.
332 // These return whether two scoped_array refer to the same object, not just to 342 // These return whether two scoped_array refer to the same object, not just to
333 // two different but equal objects. 343 // two different but equal objects.
334 bool operator==(C* p) const { return array_ == p; } 344 bool operator==(C* p) const { return array_ == p; }
335 bool operator!=(C* p) const { return array_ != p; } 345 bool operator!=(C* p) const { return array_ != p; }
336 346
337 // Swap two scoped arrays. 347 // Swap two scoped arrays.
338 void swap(scoped_array& p2) { 348 void swap(scoped_array& p2) {
339 C* tmp = array_; 349 C* tmp = array_;
340 array_ = p2.array_; 350 array_ = p2.array_;
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 454
445 C* operator->() const { 455 C* operator->() const {
446 assert(ptr_ != NULL); 456 assert(ptr_ != NULL);
447 return ptr_; 457 return ptr_;
448 } 458 }
449 459
450 C* get() const { 460 C* get() const {
451 return ptr_; 461 return ptr_;
452 } 462 }
453 463
464 // Allow scoped_ptr_malloc<C> to be used in boolean expressions, but not
465 // implicitly convertable to a real bool (which is dangerous).
466 typedef C* scoped_ptr_malloc::*testable;
467 operator testable() const { return ptr_ ? &scoped_ptr_malloc::ptr_ : 0; }
468
454 // Comparison operators. 469 // Comparison operators.
455 // These return whether a scoped_ptr_malloc and a plain pointer refer 470 // These return whether a scoped_ptr_malloc and a plain pointer refer
456 // to the same object, not just to two different but equal objects. 471 // to the same object, not just to two different but equal objects.
457 // For compatibility with the boost-derived implementation, these 472 // For compatibility with the boost-derived implementation, these
458 // take non-const arguments. 473 // take non-const arguments.
459 bool operator==(C* p) const { 474 bool operator==(C* p) const {
460 return ptr_ == p; 475 return ptr_ == p;
461 } 476 }
462 477
463 bool operator!=(C* p) const { 478 bool operator!=(C* p) const {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
509 524
510 // A function to convert T* into scoped_ptr<T> 525 // A function to convert T* into scoped_ptr<T>
511 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation 526 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation
512 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) 527 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))
513 template <typename T> 528 template <typename T>
514 scoped_ptr<T> make_scoped_ptr(T* ptr) { 529 scoped_ptr<T> make_scoped_ptr(T* ptr) {
515 return scoped_ptr<T>(ptr); 530 return scoped_ptr<T>(ptr);
516 } 531 }
517 532
518 #endif // BASE_MEMORY_SCOPED_PTR_H_ 533 #endif // BASE_MEMORY_SCOPED_PTR_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