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

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

Issue 1840663003: Trim scoped_ptr.h down to just #include <memory>. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Compile fix (only ASAN?) Created 4 years, 8 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 | media/filters/vp9_parser.h » ('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 // scoped_ptr is just a type alias for std::unique_ptr. Mass conversion coming
6 // pointer within a scope, and automatically destroying the pointer at the end 6 // soon (stay tuned for the PSA!), but until then, please continue using
7 // of a scope. There are two main classes you will use, which correspond to the 7 // scoped_ptr.
8 // operators new/delete and new[]/delete[].
9 //
10 // Example usage (scoped_ptr<T>):
11 // {
12 // scoped_ptr<Foo> foo(new Foo("wee"));
13 // } // foo goes out of scope, releasing the pointer with it.
14 //
15 // {
16 // scoped_ptr<Foo> foo; // No pointer managed.
17 // foo.reset(new Foo("wee")); // Now a pointer is managed.
18 // foo.reset(new Foo("wee2")); // Foo("wee") was destroyed.
19 // foo.reset(new Foo("wee3")); // Foo("wee2") was destroyed.
20 // foo->Method(); // Foo::Method() called.
21 // foo.get()->Method(); // Foo::Method() called.
22 // SomeFunc(foo.release()); // SomeFunc takes ownership, foo no longer
23 // // manages a pointer.
24 // foo.reset(new Foo("wee4")); // foo manages a pointer again.
25 // foo.reset(); // Foo("wee4") destroyed, foo no longer
26 // // manages a pointer.
27 // } // foo wasn't managing a pointer, so nothing was destroyed.
28 //
29 // Example usage (scoped_ptr<T[]>):
30 // {
31 // scoped_ptr<Foo[]> foo(new Foo[100]);
32 // foo.get()->Method(); // Foo::Method on the 0th element.
33 // foo[10].Method(); // Foo::Method on the 10th element.
34 // }
35 //
36 // Scopers are testable as booleans:
37 // {
38 // scoped_ptr<Foo> foo;
39 // if (!foo)
40 // foo.reset(new Foo());
41 // if (foo)
42 // LOG(INFO) << "This code is reached."
43 // }
44 //
45 // These scopers also implement part of the functionality of C++11 unique_ptr
46 // in that they are "movable but not copyable." You can use the scopers in
47 // the parameter and return types of functions to signify ownership transfer
48 // in to and out of a function. When calling a function that has a scoper
49 // as the argument type, it must be called with an rvalue of a scoper, which
50 // can be created by using std::move(), or the result of another function that
51 // generates a temporary; passing by copy will NOT work. Here is an example
52 // using scoped_ptr:
53 //
54 // void TakesOwnership(scoped_ptr<Foo> arg) {
55 // // Do something with arg.
56 // }
57 // scoped_ptr<Foo> CreateFoo() {
58 // // No need for calling std::move() for returning a move-only value, or
59 // // when you already have an rvalue as we do here.
60 // return scoped_ptr<Foo>(new Foo("new"));
61 // }
62 // scoped_ptr<Foo> PassThru(scoped_ptr<Foo> arg) {
63 // return arg;
64 // }
65 //
66 // {
67 // scoped_ptr<Foo> ptr(new Foo("yay")); // ptr manages Foo("yay").
68 // TakesOwnership(std::move(ptr)); // ptr no longer owns Foo("yay").
69 // scoped_ptr<Foo> ptr2 = CreateFoo(); // ptr2 owns the return Foo.
70 // scoped_ptr<Foo> ptr3 = // ptr3 now owns what was in ptr2.
71 // PassThru(std::move(ptr2)); // ptr2 is correspondingly nullptr.
72 // }
73 //
74 // Notice that if you do not call std::move() when returning from PassThru(), or
75 // when invoking TakesOwnership(), the code will not compile because scopers
76 // are not copyable; they only implement move semantics which require calling
77 // the std::move() function to signify a destructive transfer of state.
78 // CreateFoo() is different though because we are constructing a temporary on
79 // the return line and thus can avoid needing to call std::move().
80 //
81 // The conversion move-constructor properly handles upcast in initialization,
82 // i.e. you can use a scoped_ptr<Child> to initialize a scoped_ptr<Parent>:
83 //
84 // scoped_ptr<Foo> foo(new Foo());
85 // scoped_ptr<FooParent> parent(std::move(foo));
86 8
87 #ifndef BASE_MEMORY_SCOPED_PTR_H_ 9 #ifndef BASE_MEMORY_SCOPED_PTR_H_
88 #define BASE_MEMORY_SCOPED_PTR_H_ 10 #define BASE_MEMORY_SCOPED_PTR_H_
89 11
90 // This is an implementation designed to match the anticipated future TR2
91 // implementation of the scoped_ptr class.
92
93 // TODO(dcheng): Clean up these headers, but there are likely lots of existing
94 // IWYU violations.
95 #include <stdlib.h>
96
97 #include <memory> 12 #include <memory>
98 13
99 template <typename T, typename D = std::default_delete<T>> 14 template <typename T, typename D = std::default_delete<T>>
100 using scoped_ptr = std::unique_ptr<T, D>; 15 using scoped_ptr = std::unique_ptr<T, D>;
101 16
102 // A function to convert T* into scoped_ptr<T> 17 // A function to convert T* into scoped_ptr<T>
103 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation 18 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation
104 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) 19 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))
105 template <typename T> 20 template <typename T>
106 scoped_ptr<T> make_scoped_ptr(T* ptr) { 21 scoped_ptr<T> make_scoped_ptr(T* ptr) {
107 return scoped_ptr<T>(ptr); 22 return scoped_ptr<T>(ptr);
108 } 23 }
109 24
110 #endif // BASE_MEMORY_SCOPED_PTR_H_ 25 #endif // BASE_MEMORY_SCOPED_PTR_H_
OLDNEW
« no previous file with comments | « no previous file | media/filters/vp9_parser.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698