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

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

Issue 8968032: Allow construction and assignment of one scoped_ptr from another if the types are convertible. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix copyright Created 8 years, 11 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 | 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) 2011 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):
11 // { 11 // {
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 public: 119 public:
120 120
121 // The element type 121 // The element type
122 typedef C element_type; 122 typedef C element_type;
123 123
124 // Constructor. Defaults to initializing with NULL. 124 // Constructor. Defaults to initializing with NULL.
125 // There is no way to create an uninitialized scoped_ptr. 125 // There is no way to create an uninitialized scoped_ptr.
126 // The input parameter must be allocated with new. 126 // The input parameter must be allocated with new.
127 explicit scoped_ptr(C* p = NULL) : ptr_(p) { } 127 explicit scoped_ptr(C* p = NULL) : ptr_(p) { }
128 128
129 // Constructor. Allows construction from a scoped_ptr rvalue for a
130 // convertible type.
131 template <typename U>
132 scoped_ptr(scoped_ptr<U> other) : ptr_(other.release()) {
133 }
134
129 // Destructor. If there is a C object, delete it. 135 // Destructor. If there is a C object, delete it.
130 // We don't need to test ptr_ == NULL because C++ does that for us. 136 // We don't need to test ptr_ == NULL because C++ does that for us.
131 ~scoped_ptr() { 137 ~scoped_ptr() {
132 enum { type_must_be_complete = sizeof(C) }; 138 enum { type_must_be_complete = sizeof(C) };
133 delete ptr_; 139 delete ptr_;
134 } 140 }
135 141
142 // operator=. Allows assignment from a scoped_ptr rvalue for a convertible
143 // type.
144 template <typename U>
145 scoped_ptr& operator=(scoped_ptr<U> rhs) {
146 reset(rhs.release());
147 return *this;
148 }
149
136 // Reset. Deletes the current owned object, if any. 150 // Reset. Deletes the current owned object, if any.
137 // Then takes ownership of a new object, if given. 151 // Then takes ownership of a new object, if given.
138 // this->reset(this->get()) works. 152 // this->reset(this->get()) works.
139 void reset(C* p = NULL) { 153 void reset(C* p = NULL) {
140 if (p != ptr_) { 154 if (p != ptr_) {
141 enum { type_must_be_complete = sizeof(C) }; 155 enum { type_must_be_complete = sizeof(C) };
142 delete ptr_; 156 delete ptr_;
143 ptr_ = p; 157 ptr_ = p;
144 } 158 }
145 } 159 }
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 bool operator==(C* p, const scoped_ptr_malloc<C, FP>& b) { 453 bool operator==(C* p, const scoped_ptr_malloc<C, FP>& b) {
440 return p == b.get(); 454 return p == b.get();
441 } 455 }
442 456
443 template<class C, class FP> inline 457 template<class C, class FP> inline
444 bool operator!=(C* p, const scoped_ptr_malloc<C, FP>& b) { 458 bool operator!=(C* p, const scoped_ptr_malloc<C, FP>& b) {
445 return p != b.get(); 459 return p != b.get();
446 } 460 }
447 461
448 #endif // BASE_MEMORY_SCOPED_PTR_H_ 462 #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