OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #ifndef BASE_MEMORY_PTR_UTIL_H_ | 5 #ifndef BASE_MEMORY_PTR_UTIL_H_ |
6 #define BASE_MEMORY_PTR_UTIL_H_ | 6 #define BASE_MEMORY_PTR_UTIL_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
| 10 // A function to convert T* into scoped_ptr<T> |
| 11 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation |
| 12 // for scoped_ptr<FooBarBaz<type>>(new FooBarBaz<type>(arg)) |
| 13 // |
| 14 // Why doesn't this just return a scoped_ptr? |
| 15 // |
| 16 // make_scoped_ptr is currently being migrated out of scoped_ptr.h, so we can |
| 17 // globally rename make_scoped_ptr to WrapUnique without breaking the build. |
| 18 // Doing so without breaking intermediate builds involves several steps: |
| 19 // |
| 20 // 1. Move make_scoped_ptr into ptr_util.h and include ptr_util.h from |
| 21 // scoped_ptr.h. |
| 22 // 2. Add an #include for ptr_util.h to every file that references |
| 23 // make_scoped_ptr. |
| 24 // 3. Remove ptr_util.h include from scoped_ptr.h. |
| 25 // 4. Global rewrite everything. |
| 26 // |
| 27 // Unfortunately, step 1 introduces an awkward cycle of dependencies between |
| 28 // ptr_util.h and scoped_ptr.h To break that cycle, we exploit the fact that |
| 29 // scoped_ptr is really just a type alias for std::unique_ptr. |
| 30 template <typename T> |
| 31 std::unique_ptr<T> make_scoped_ptr(T* ptr) { |
| 32 return std::unique_ptr<T>(ptr); |
| 33 } |
| 34 |
10 namespace base { | 35 namespace base { |
11 | 36 |
12 // Helper to transfer ownership of a raw pointer to a std::unique_ptr<T>. | 37 // Helper to transfer ownership of a raw pointer to a std::unique_ptr<T>. |
13 // Note that std::unique_ptr<T> has very different semantics from | 38 // Note that std::unique_ptr<T> has very different semantics from |
14 // std::unique_ptr<T[]>: do not use this helper for array allocations. | 39 // std::unique_ptr<T[]>: do not use this helper for array allocations. |
15 template <typename T> | 40 template <typename T> |
16 std::unique_ptr<T> WrapUnique(T* ptr) { | 41 std::unique_ptr<T> WrapUnique(T* ptr) { |
17 return std::unique_ptr<T>(ptr); | 42 return std::unique_ptr<T>(ptr); |
18 } | 43 } |
19 | 44 |
20 } // namespace base | 45 } // namespace base |
21 | 46 |
22 #endif // BASE_MEMORY_PTR_UTIL_H_ | 47 #endif // BASE_MEMORY_PTR_UTIL_H_ |
OLD | NEW |