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

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

Issue 9283028: Add scoped_ptr<>::PassAs<>(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: - 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 | « base/base.gyp ('k') | 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 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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 // scoped_ptr<Foo> ptr3 = // ptr3 now owns what was in ptr2. 60 // scoped_ptr<Foo> ptr3 = // ptr3 now owns what was in ptr2.
61 // PassThru(ptr2.Pass()); // ptr2 is correspondingly NULL. 61 // PassThru(ptr2.Pass()); // ptr2 is correspondingly NULL.
62 // } 62 // }
63 // 63 //
64 // Notice that if you do not call Pass() when returning from PassThru(), or 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 65 // when invoking TakesOwnership(), the code will not compile because scopers
66 // are not copyable; they only implement move semantics which require calling 66 // are not copyable; they only implement move semantics which require calling
67 // the Pass() function to signify a destructive transfer of state. CreateFoo() 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 68 // is different though because we are constructing a temporary on the return
69 // line and thus can avoid needing to call Pass(). 69 // line and thus can avoid needing to call Pass().
70 //
71 // Pass() properly handles upcast in assignment, i.e. you can assign
72 // scoped_ptr<Child> to scoped_ptr<Parent>:
73 //
74 // scoped_ptr<Foo> foo(new Foo());
75 // scoped_ptr<FooParent> parent = foo.Pass();
76 //
77 // PassAs<>() should be used to upcast return value in return statement:
78 //
79 // scoped_ptr<Foo> CreateFoo() {
80 // scoped_ptr<FooChild> result(new FooChild());
81 // return result.PassAs<Foo>();
82 // }
83 //
84 // Note that PassAs<>() is implemented only for scoped_ptr, but not for
85 // scoped_array. This is because casting array pointers may not be safe.
70 86
71 #ifndef BASE_MEMORY_SCOPED_PTR_H_ 87 #ifndef BASE_MEMORY_SCOPED_PTR_H_
72 #define BASE_MEMORY_SCOPED_PTR_H_ 88 #define BASE_MEMORY_SCOPED_PTR_H_
73 #pragma once 89 #pragma once
74 90
75 // This is an implementation designed to match the anticipated future TR2 91 // This is an implementation designed to match the anticipated future TR2
76 // implementation of the scoped_ptr class, and its closely-related brethren, 92 // implementation of the scoped_ptr class, and its closely-related brethren,
77 // scoped_array, scoped_ptr_malloc. 93 // scoped_array, scoped_ptr_malloc.
78 94
79 #include <assert.h> 95 #include <assert.h>
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 // The return value is the current pointer held by this object. 191 // The return value is the current pointer held by this object.
176 // If this object holds a NULL pointer, the return value is NULL. 192 // If this object holds a NULL pointer, the return value is NULL.
177 // After this operation, this object will hold a NULL pointer, 193 // After this operation, this object will hold a NULL pointer,
178 // and will not own the object any more. 194 // and will not own the object any more.
179 C* release() WARN_UNUSED_RESULT { 195 C* release() WARN_UNUSED_RESULT {
180 C* retVal = ptr_; 196 C* retVal = ptr_;
181 ptr_ = NULL; 197 ptr_ = NULL;
182 return retVal; 198 return retVal;
183 } 199 }
184 200
201 template <typename PassAsType>
202 scoped_ptr<PassAsType> PassAs() {
darin (slow to review) 2012/01/24 21:17:53 Had you considered putting this in base/move.h alo
Sergey Ulanov 2012/01/24 21:55:18 Yes, I considered it, but decided against it becau
203 return scoped_ptr<PassAsType>(release());
204 }
205
185 private: 206 private:
186 C* ptr_; 207 C* ptr_;
187 208
188 // Forbid comparison of scoped_ptr types. If C2 != C, it totally doesn't 209 // Forbid comparison of scoped_ptr types. If C2 != C, it totally doesn't
189 // make sense, and if C2 == C, it still doesn't make sense because you should 210 // make sense, and if C2 == C, it still doesn't make sense because you should
190 // never have the same object owned by two different scoped_ptrs. 211 // never have the same object owned by two different scoped_ptrs.
191 template <class C2> bool operator==(scoped_ptr<C2> const& p2) const; 212 template <class C2> bool operator==(scoped_ptr<C2> const& p2) const;
192 template <class C2> bool operator!=(scoped_ptr<C2> const& p2) const; 213 template <class C2> bool operator!=(scoped_ptr<C2> const& p2) const;
193 214
194 }; 215 };
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
441 bool operator==(C* p, const scoped_ptr_malloc<C, FP>& b) { 462 bool operator==(C* p, const scoped_ptr_malloc<C, FP>& b) {
442 return p == b.get(); 463 return p == b.get();
443 } 464 }
444 465
445 template<class C, class FP> inline 466 template<class C, class FP> inline
446 bool operator!=(C* p, const scoped_ptr_malloc<C, FP>& b) { 467 bool operator!=(C* p, const scoped_ptr_malloc<C, FP>& b) {
447 return p != b.get(); 468 return p != b.get();
448 } 469 }
449 470
450 #endif // BASE_MEMORY_SCOPED_PTR_H_ 471 #endif // BASE_MEMORY_SCOPED_PTR_H_
OLDNEW
« no previous file with comments | « base/base.gyp ('k') | base/memory/scoped_ptr_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698