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

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

Issue 1305973011: Change assert() to DCHECK() in scoped_ptr.h (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add //base dependencies as experiment Created 5 years, 3 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
« no previous file with comments | « no previous file | base/memory/scoped_ptr_unittest.cc » ('j') | 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 a 5 // Scopers help you manage ownership of a pointer, helping you easily manage a
6 // pointer within a scope, and automatically destroying the pointer at the end 6 // pointer within a scope, and automatically destroying the pointer at the end
7 // of a scope. There are two main classes you will use, which correspond to the 7 // of a scope. There are two main classes you will use, which correspond to the
8 // operators new/delete and new[]/delete[]. 8 // operators new/delete and new[]/delete[].
9 // 9 //
10 // Example usage (scoped_ptr<T>): 10 // Example usage (scoped_ptr<T>):
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 // 73 //
74 // scoped_ptr<Foo> foo(new Foo()); 74 // scoped_ptr<Foo> foo(new Foo());
75 // scoped_ptr<FooParent> parent(foo.Pass()); 75 // scoped_ptr<FooParent> parent(foo.Pass());
76 76
77 #ifndef BASE_MEMORY_SCOPED_PTR_H_ 77 #ifndef BASE_MEMORY_SCOPED_PTR_H_
78 #define BASE_MEMORY_SCOPED_PTR_H_ 78 #define BASE_MEMORY_SCOPED_PTR_H_
79 79
80 // This is an implementation designed to match the anticipated future TR2 80 // This is an implementation designed to match the anticipated future TR2
81 // implementation of the scoped_ptr class. 81 // implementation of the scoped_ptr class.
82 82
83 #include <assert.h>
84 #include <stddef.h> 83 #include <stddef.h>
85 #include <stdlib.h> 84 #include <stdlib.h>
86 85
87 #include <algorithm> // For std::swap(). 86 #include <algorithm> // For std::swap().
88 #include <iosfwd> 87 #include <iosfwd>
89 88
90 #include "base/basictypes.h" 89 #include "base/basictypes.h"
91 #include "base/compiler_specific.h" 90 #include "base/compiler_specific.h"
91 #include "base/logging.h"
92 #include "base/move.h" 92 #include "base/move.h"
93 #include "base/template_util.h" 93 #include "base/template_util.h"
94 94
95 namespace base { 95 namespace base {
96 96
97 namespace subtle { 97 namespace subtle {
98 class RefCountedBase; 98 class RefCountedBase;
99 class RefCountedThreadSafeBase; 99 class RefCountedThreadSafeBase;
100 } // namespace subtle 100 } // namespace subtle
101 101
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 if (data_.ptr != nullptr) { 219 if (data_.ptr != nullptr) {
220 // Not using get_deleter() saves one function call in non-optimized 220 // Not using get_deleter() saves one function call in non-optimized
221 // builds. 221 // builds.
222 static_cast<D&>(data_)(data_.ptr); 222 static_cast<D&>(data_)(data_.ptr);
223 } 223 }
224 } 224 }
225 225
226 void reset(T* p) { 226 void reset(T* p) {
227 // This is a self-reset, which is no longer allowed for default deleters: 227 // This is a self-reset, which is no longer allowed for default deleters:
228 // https://crbug.com/162971 228 // https://crbug.com/162971
229 assert(!ShouldAbortOnSelfReset<D>::value || p == nullptr || p != data_.ptr); 229 DCHECK(!ShouldAbortOnSelfReset<D>::value || p == nullptr || p != data_.ptr);
230 230
231 // Note that running data_.ptr = p can lead to undefined behavior if 231 // Note that running data_.ptr = p can lead to undefined behavior if
232 // get_deleter()(get()) deletes this. In order to prevent this, reset() 232 // get_deleter()(get()) deletes this. In order to prevent this, reset()
233 // should update the stored pointer before deleting its old value. 233 // should update the stored pointer before deleting its old value.
234 // 234 //
235 // However, changing reset() to use that behavior may cause current code to 235 // However, changing reset() to use that behavior may cause current code to
236 // break in unexpected ways. If the destruction of the owned object 236 // break in unexpected ways. If the destruction of the owned object
237 // dereferences the scoped_ptr when it is destroyed by a call to reset(), 237 // dereferences the scoped_ptr when it is destroyed by a call to reset(),
238 // then it will incorrectly dispatch calls to |p| rather than the original 238 // then it will incorrectly dispatch calls to |p| rather than the original
239 // value of |data_.ptr|. 239 // value of |data_.ptr|.
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 scoped_ptr& operator=(decltype(nullptr)) { 370 scoped_ptr& operator=(decltype(nullptr)) {
371 reset(); 371 reset();
372 return *this; 372 return *this;
373 } 373 }
374 374
375 // Reset. Deletes the currently owned object, if any. 375 // Reset. Deletes the currently owned object, if any.
376 // Then takes ownership of a new object, if given. 376 // Then takes ownership of a new object, if given.
377 void reset(element_type* p = nullptr) { impl_.reset(p); } 377 void reset(element_type* p = nullptr) { impl_.reset(p); }
378 378
379 // Accessors to get the owned object. 379 // Accessors to get the owned object.
380 // operator* and operator-> will assert() if there is no current object. 380 // operator* and operator-> will DCHECK() if there is no current object.
381 element_type& operator*() const { 381 element_type& operator*() const {
382 assert(impl_.get() != nullptr); 382 DCHECK(impl_.get() != nullptr);
383 return *impl_.get(); 383 return *impl_.get();
384 } 384 }
385 element_type* operator->() const { 385 element_type* operator->() const {
386 assert(impl_.get() != nullptr); 386 DCHECK(impl_.get() != nullptr);
387 return impl_.get(); 387 return impl_.get();
388 } 388 }
389 element_type* get() const { return impl_.get(); } 389 element_type* get() const { return impl_.get(); }
390 390
391 // Access to the deleter. 391 // Access to the deleter.
392 deleter_type& get_deleter() { return impl_.get_deleter(); } 392 deleter_type& get_deleter() { return impl_.get_deleter(); }
393 const deleter_type& get_deleter() const { return impl_.get_deleter(); } 393 const deleter_type& get_deleter() const { return impl_.get_deleter(); }
394 394
395 // Allow scoped_ptr<element_type> to be used in boolean expressions, but not 395 // Allow scoped_ptr<element_type> to be used in boolean expressions, but not
396 // implicitly convertible to a real bool (which is dangerous). 396 // implicitly convertible to a real bool (which is dangerous).
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
488 reset(); 488 reset();
489 return *this; 489 return *this;
490 } 490 }
491 491
492 // Reset. Deletes the currently owned array, if any. 492 // Reset. Deletes the currently owned array, if any.
493 // Then takes ownership of a new object, if given. 493 // Then takes ownership of a new object, if given.
494 void reset(element_type* array = nullptr) { impl_.reset(array); } 494 void reset(element_type* array = nullptr) { impl_.reset(array); }
495 495
496 // Accessors to get the owned array. 496 // Accessors to get the owned array.
497 element_type& operator[](size_t i) const { 497 element_type& operator[](size_t i) const {
498 assert(impl_.get() != nullptr); 498 DCHECK(impl_.get() != nullptr);
499 return impl_.get()[i]; 499 return impl_.get()[i];
500 } 500 }
501 element_type* get() const { return impl_.get(); } 501 element_type* get() const { return impl_.get(); }
502 502
503 // Access to the deleter. 503 // Access to the deleter.
504 deleter_type& get_deleter() { return impl_.get_deleter(); } 504 deleter_type& get_deleter() { return impl_.get_deleter(); }
505 const deleter_type& get_deleter() const { return impl_.get_deleter(); } 505 const deleter_type& get_deleter() const { return impl_.get_deleter(); }
506 506
507 // Allow scoped_ptr<element_type> to be used in boolean expressions, but not 507 // Allow scoped_ptr<element_type> to be used in boolean expressions, but not
508 // implicitly convertible to a real bool (which is dangerous). 508 // implicitly convertible to a real bool (which is dangerous).
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
585 scoped_ptr<T> make_scoped_ptr(T* ptr) { 585 scoped_ptr<T> make_scoped_ptr(T* ptr) {
586 return scoped_ptr<T>(ptr); 586 return scoped_ptr<T>(ptr);
587 } 587 }
588 588
589 template <typename T> 589 template <typename T>
590 std::ostream& operator<<(std::ostream& out, const scoped_ptr<T>& p) { 590 std::ostream& operator<<(std::ostream& out, const scoped_ptr<T>& p) {
591 return out << p.get(); 591 return out << p.get();
592 } 592 }
593 593
594 #endif // BASE_MEMORY_SCOPED_PTR_H_ 594 #endif // BASE_MEMORY_SCOPED_PTR_H_
OLDNEW
« no previous file with comments | « no previous file | base/memory/scoped_ptr_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698