OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2012 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 24 matching lines...) Expand all Loading... |
35 // } // foo wasn't managing a pointer, so nothing was destroyed. | 35 // } // foo wasn't managing a pointer, so nothing was destroyed. |
36 // | 36 // |
37 // Example usage (scoped_ptr<T[]>): | 37 // Example usage (scoped_ptr<T[]>): |
38 // { | 38 // { |
39 // scoped_ptr<Foo[]> foo(new Foo[100]); | 39 // scoped_ptr<Foo[]> foo(new Foo[100]); |
40 // foo.get()->Method(); // Foo::Method on the 0th element. | 40 // foo.get()->Method(); // Foo::Method on the 0th element. |
41 // foo[10].Method(); // Foo::Method on the 10th element. | 41 // foo[10].Method(); // Foo::Method on the 10th element. |
42 // } | 42 // } |
43 // | 43 // |
44 // These scopers also implement part of the functionality of C++11 unique_ptr | 44 // These scopers also implement part of the functionality of C++11 unique_ptr |
45 // in that they are "movable but not copyable." You can use the scopers in | 45 // in that they are "movable but not copyable." You can use the scopers in the |
46 // the parameter and return types of functions to signify ownership transfer | 46 // parameter and return types of functions to signify ownership transfer in to |
47 // in to and out of a function. When calling a function that has a scoper | 47 // and out of a function. When calling a function that has a scoper as the |
48 // as the argument type, it must be called with the result of an analogous | 48 // argument type, it must be called with the result of calling std::move on an |
49 // scoper's Pass() function or another function that generates a temporary; | 49 // analogous scoper, or another function that generates a temporary; passing by |
50 // passing by copy will NOT work. Here is an example using scoped_ptr: | 50 // copy will NOT work. Here is an example using scoped_ptr: |
51 // | 51 // |
52 // void TakesOwnership(scoped_ptr<Foo> arg) { | 52 // void TakesOwnership(scoped_ptr<Foo> arg) { |
53 // // Do something with arg | 53 // // Do something with arg |
54 // } | 54 // } |
55 // scoped_ptr<Foo> CreateFoo() { | 55 // scoped_ptr<Foo> CreateFoo() { |
56 // // No need for calling Pass() because we are constructing a temporary | 56 // // No need for calling std::move because we are constructing a temporary |
57 // // for the return value. | 57 // // for the return value. |
58 // return scoped_ptr<Foo>(new Foo("new")); | 58 // return scoped_ptr<Foo>(new Foo("new")); |
59 // } | 59 // } |
60 // scoped_ptr<Foo> PassThru(scoped_ptr<Foo> arg) { | 60 // scoped_ptr<Foo> PassThru(scoped_ptr<Foo> arg) { |
61 // return arg.Pass(); | 61 // return std::move(arg); |
62 // } | 62 // } |
63 // | 63 // |
64 // { | 64 // { |
65 // scoped_ptr<Foo> ptr(new Foo("yay")); // ptr manages Foo("yay"). | 65 // scoped_ptr<Foo> ptr(new Foo("yay")); // ptr manages Foo("yay"). |
66 // TakesOwnership(ptr.Pass()); // ptr no longer owns Foo("yay"). | 66 // TakesOwnership(std::move(ptr)); // ptr no longer owns Foo("yay"). |
67 // scoped_ptr<Foo> ptr2 = CreateFoo(); // ptr2 owns the return Foo. | 67 // scoped_ptr<Foo> ptr2 = CreateFoo(); // ptr2 owns the return Foo. |
68 // scoped_ptr<Foo> ptr3 = // ptr3 now owns what was in ptr2. | 68 // scoped_ptr<Foo> ptr3 = // ptr3 now owns what was in ptr2. |
69 // PassThru(ptr2.Pass()); // ptr2 is correspondingly nullptr. | 69 // PassThru(std::move(ptr2)); // ptr2 is correspondingly nullptr. |
70 // } | 70 // } |
71 // | 71 // |
72 // Notice that if you do not call Pass() when returning from PassThru(), or | 72 // Notice that if you do not call std::move when returning from PassThru(), or |
73 // when invoking TakesOwnership(), the code will not compile because scopers | 73 // when invoking TakesOwnership(), the code will not compile because scopers |
74 // are not copyable; they only implement move semantics which require calling | 74 // are not copyable; they only implement move semantics which require calling |
75 // the Pass() function to signify a destructive transfer of state. CreateFoo() | 75 // std::move to signify a destructive transfer of state. CreateFoo() is |
76 // is different though because we are constructing a temporary on the return | 76 // different though because we are constructing a temporary on the return line |
77 // line and thus can avoid needing to call Pass(). | 77 // and thus can avoid needing to call std::move. |
78 // | |
79 // Pass() properly handles upcast in initialization, i.e. you can use a | |
80 // scoped_ptr<Child> to initialize a scoped_ptr<Parent>: | |
81 // | |
82 // scoped_ptr<Foo> foo(new Foo()); | |
83 // scoped_ptr<FooParent> parent(foo.Pass()); | |
84 // | |
85 // PassAs<>() should be used to upcast return value in return statement: | |
86 // | |
87 // scoped_ptr<Foo> CreateFoo() { | |
88 // scoped_ptr<FooChild> result(new FooChild()); | |
89 // return result.PassAs<Foo>(); | |
90 // } | |
91 // | |
92 // Note that PassAs<>() is implemented only for scoped_ptr<T>, but not for | |
93 // scoped_ptr<T[]>. This is because casting array pointers may not be safe. | |
94 | 78 |
95 #ifndef WEBRTC_BASE_SCOPED_PTR_H__ | 79 #ifndef WEBRTC_BASE_SCOPED_PTR_H__ |
96 #define WEBRTC_BASE_SCOPED_PTR_H__ | 80 #define WEBRTC_BASE_SCOPED_PTR_H__ |
97 | 81 |
98 // This is an implementation designed to match the anticipated future TR2 | 82 // This is an implementation designed to match the anticipated future TR2 |
99 // implementation of the scoped_ptr class. | 83 // implementation of the scoped_ptr class. |
100 | 84 |
101 #include <assert.h> | 85 #include <assert.h> |
102 #include <stddef.h> | 86 #include <stddef.h> |
103 #include <stdlib.h> | 87 #include <stdlib.h> |
(...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
627 | 611 |
628 // A function to convert T* into scoped_ptr<T> | 612 // A function to convert T* into scoped_ptr<T> |
629 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation | 613 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation |
630 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) | 614 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) |
631 template <typename T> | 615 template <typename T> |
632 rtc::scoped_ptr<T> rtc_make_scoped_ptr(T* ptr) { | 616 rtc::scoped_ptr<T> rtc_make_scoped_ptr(T* ptr) { |
633 return rtc::scoped_ptr<T>(ptr); | 617 return rtc::scoped_ptr<T>(ptr); |
634 } | 618 } |
635 | 619 |
636 #endif // #ifndef WEBRTC_BASE_SCOPED_PTR_H__ | 620 #endif // #ifndef WEBRTC_BASE_SCOPED_PTR_H__ |
OLD | NEW |