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 #include <utility> | 9 #include <utility> |
10 | 10 |
11 namespace base { | 11 namespace base { |
12 | 12 |
13 // Helper to transfer ownership of a raw pointer to a std::unique_ptr<T>. | 13 // Helper to transfer ownership of a raw pointer to a std::unique_ptr<T>. |
14 // Note that std::unique_ptr<T> has very different semantics from | 14 // Note that std::unique_ptr<T> has very different semantics from |
15 // std::unique_ptr<T[]>: do not use this helper for array allocations. | 15 // std::unique_ptr<T[]>: do not use this helper for array allocations. |
16 template <typename T> | 16 template <typename T, typename D = std::default_delete<T>> |
17 std::unique_ptr<T> WrapUnique(T* ptr) { | 17 std::unique_ptr<T, D> WrapUnique(T* ptr, D deleter = D()) { |
dcheng
2016/07/22 07:29:00
We don't need the custom deleter argument anymore,
tzik
2016/07/22 08:14:53
Ah, right. Removed.
| |
18 return std::unique_ptr<T>(ptr); | 18 return std::unique_ptr<T, D>(ptr, std::move(deleter)); |
19 } | 19 } |
20 | 20 |
21 namespace internal { | 21 namespace internal { |
22 | 22 |
23 template <typename T> | 23 template <typename T> |
24 struct MakeUniqueResult { | 24 struct MakeUniqueResult { |
25 using Scalar = std::unique_ptr<T>; | 25 using Scalar = std::unique_ptr<T>; |
26 }; | 26 }; |
27 | 27 |
28 template <typename T> | 28 template <typename T> |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 } | 65 } |
66 | 66 |
67 // Overload to reject array types of known bound, e.g. T[n]. | 67 // Overload to reject array types of known bound, e.g. T[n]. |
68 template <typename T, typename... Args> | 68 template <typename T, typename... Args> |
69 typename internal::MakeUniqueResult<T>::Invalid MakeUnique(Args&&... args) = | 69 typename internal::MakeUniqueResult<T>::Invalid MakeUnique(Args&&... args) = |
70 delete; | 70 delete; |
71 | 71 |
72 } // namespace base | 72 } // namespace base |
73 | 73 |
74 #endif // BASE_MEMORY_PTR_UTIL_H_ | 74 #endif // BASE_MEMORY_PTR_UTIL_H_ |
OLD | NEW |