| OLD | NEW |
| 1 # std::unique_ptr Transition Guide | 1 # std::unique_ptr Transition Guide |
| 2 | 2 |
| 3 Now `std::unique_ptr<T>` is available anywhere in Blink. This document goes thro
ugh various use cases of `OwnPtr<T>` | 3 Now `std::unique_ptr<T>` is available anywhere in Blink. This document goes thro
ugh various use cases of `OwnPtr<T>` |
| 4 and how they get converted to `std::unique_ptr<T>` so you can smoothly switch yo
ur mind. | 4 and how they get converted to `std::unique_ptr<T>` so you can smoothly switch yo
ur mind. |
| 5 | 5 |
| 6 If you have any uncertainties on using `std::unique_ptr<T>`, ask yutak@chromium.
org (or other C++ gurus around you) | 6 If you have any uncertainties on using `std::unique_ptr<T>`, ask yutak@chromium.
org (or other C++ gurus around you) |
| 7 for help. | 7 for help. |
| 8 | 8 |
| 9 ## Creation and Use | 9 ## Creation and Use |
| 10 | 10 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 T* leakedPointer = owned.leakPtr(); | 23 T* leakedPointer = owned.leakPtr(); |
| 24 } | 24 } |
| 25 ``` | 25 ``` |
| 26 | 26 |
| 27 #### std::unique_ptr | 27 #### std::unique_ptr |
| 28 | 28 |
| 29 ```c++ | 29 ```c++ |
| 30 void f() | 30 void f() |
| 31 { | 31 { |
| 32 std::unique_ptr<T> owned(new T(argument)); | 32 std::unique_ptr<T> owned(new T(argument)); |
| 33 // Or: auto owned = wrapUnique(new T(argument)); †1 | 33 // Or: auto owned = WTF::wrapUnique(new T(argument)); †1 |
| 34 owned->useThis(); | 34 owned->useThis(); |
| 35 T& reference = *owned; | 35 T& reference = *owned; |
| 36 T* pointer = owned.get(); | 36 T* pointer = owned.get(); |
| 37 owned.reset(); // Or: owned = nullptr | 37 owned.reset(); // Or: owned = nullptr |
| 38 owned.reset(new T); // Or: owned = wrapUnique(new T) | 38 owned.reset(new T); // Or: owned = WTF::wrapUnique(new T) |
| 39 T* leakedPointer = owned.release(); | 39 T* leakedPointer = owned.release(); |
| 40 } | 40 } |
| 41 ``` | 41 ``` |
| 42 |||---||| | 42 |||---||| |
| 43 | 43 |
| 44 †1 `wrapUnique()` is particularly useful when you pass a `unique_ptr` to somebod
y else: | 44 †1 `WTF::wrapUnique()` is particularly useful when you pass a `unique_ptr` to so
mebody else: |
| 45 | 45 |
| 46 ```c++ | 46 ```c++ |
| 47 std::unique_ptr<T> g() | 47 std::unique_ptr<T> g() |
| 48 { | 48 { |
| 49 h(wrapUnique(new T(argument))); // Pass to a function. | 49 h(WTF::wrapUnique(new T(argument))); // Pass to a function. |
| 50 return wrapUnique(new T(argument)); // Return from a function. | 50 return WTF::wrapUnique(new T(argument)); // Return from a function. |
| 51 } | 51 } |
| 52 | 52 |
| 53 ``` | 53 ``` |
| 54 | 54 |
| 55 You need to `#include "wtf/PtrUtil.h"` for `wrapUnique()`. | 55 You need to `#include "wtf/PtrUtil.h"` for `WTF::wrapUnique()`. |
| 56 | 56 |
| 57 ## Passing and Receiving | 57 ## Passing and Receiving |
| 58 | 58 |
| 59 |||---||| | 59 |||---||| |
| 60 #### OwnPtr | 60 #### OwnPtr |
| 61 | 61 |
| 62 ```c++ | 62 ```c++ |
| 63 void receive(PassOwnPtr<T> object) | 63 void receive(PassOwnPtr<T> object) |
| 64 { | 64 { |
| 65 OwnPtr<T> localObject = object; | 65 OwnPtr<T> localObject = object; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 an lvalue. This is possible because `return` makes all the local variables go aw
ay by going back to the caller. | 147 an lvalue. This is possible because `return` makes all the local variables go aw
ay by going back to the caller. |
| 148 | 148 |
| 149 *** aside | 149 *** aside |
| 150 *Note:* Well, yes, I know, the definitions of lvalues and rvalues here are not v
ery precise. However, the above | 150 *Note:* Well, yes, I know, the definitions of lvalues and rvalues here are not v
ery precise. However, the above |
| 151 understandings are sufficient in practice. If you want to know further, see | 151 understandings are sufficient in practice. If you want to know further, see |
| 152 [the wiki about value categories at cppreference.com](http://en.cppreference.com
/w/cpp/language/value_category). | 152 [the wiki about value categories at cppreference.com](http://en.cppreference.com
/w/cpp/language/value_category). |
| 153 | 153 |
| 154 See also [Dana's guide for rvalue references](https://sites.google.com/a/chromiu
m.org/dev/rvalue-references) | 154 See also [Dana's guide for rvalue references](https://sites.google.com/a/chromiu
m.org/dev/rvalue-references) |
| 155 if you want to learn about move semantics. | 155 if you want to learn about move semantics. |
| 156 *** | 156 *** |
| OLD | NEW |