| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2017 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2017 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 |
| 11 // This implementation is borrowed from chromium. | 11 // This implementation is borrowed from chromium. |
| 12 | 12 |
| 13 #ifndef WEBRTC_BASE_PTR_UTIL_H_ | 13 #ifndef WEBRTC_BASE_PTR_UTIL_H_ |
| 14 #define WEBRTC_BASE_PTR_UTIL_H_ | 14 #define WEBRTC_BASE_PTR_UTIL_H_ |
| 15 | 15 |
| 16 #include <memory> | |
| 17 #include <utility> | |
| 18 | 16 |
| 19 namespace rtc { | 17 // This header is deprecated and is just left here temporarily during |
| 20 | 18 // refactoring. See https://bugs.webrtc.org/7634 for more details. |
| 21 // Helper to transfer ownership of a raw pointer to a std::unique_ptr<T>. | 19 #include "webrtc/rtc_base/ptr_util.h" |
| 22 // Note that std::unique_ptr<T> has very different semantics from | |
| 23 // std::unique_ptr<T[]>: do not use this helper for array allocations. | |
| 24 template <typename T> | |
| 25 std::unique_ptr<T> WrapUnique(T* ptr) { | |
| 26 return std::unique_ptr<T>(ptr); | |
| 27 } | |
| 28 | |
| 29 namespace internal { | |
| 30 | |
| 31 template <typename T> | |
| 32 struct MakeUniqueResult { | |
| 33 using Scalar = std::unique_ptr<T>; | |
| 34 }; | |
| 35 | |
| 36 template <typename T> | |
| 37 struct MakeUniqueResult<T[]> { | |
| 38 using Array = std::unique_ptr<T[]>; | |
| 39 }; | |
| 40 | |
| 41 template <typename T, size_t N> | |
| 42 struct MakeUniqueResult<T[N]> { | |
| 43 using Invalid = void; | |
| 44 }; | |
| 45 | |
| 46 } // namespace internal | |
| 47 | |
| 48 // Helper to construct an object wrapped in a std::unique_ptr. This is an | |
| 49 // implementation of C++14's std::make_unique that can be used in Chrome. | |
| 50 // | |
| 51 // MakeUnique<T>(args) should be preferred over WrapUnique(new T(args)): bare | |
| 52 // calls to `new` should be treated with scrutiny. | |
| 53 // | |
| 54 // Usage: | |
| 55 // // ptr is a std::unique_ptr<std::string> | |
| 56 // auto ptr = MakeUnique<std::string>("hello world!"); | |
| 57 // | |
| 58 // // arr is a std::unique_ptr<int[]> | |
| 59 // auto arr = MakeUnique<int[]>(5); | |
| 60 | |
| 61 // Overload for non-array types. Arguments are forwarded to T's constructor. | |
| 62 template <typename T, typename... Args> | |
| 63 typename internal::MakeUniqueResult<T>::Scalar MakeUnique(Args&&... args) { | |
| 64 return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); | |
| 65 } | |
| 66 | |
| 67 // Overload for array types of unknown bound, e.g. T[]. The array is allocated | |
| 68 // with `new T[n]()` and value-initialized: note that this is distinct from | |
| 69 // `new T[n]`, which default-initializes. | |
| 70 template <typename T> | |
| 71 typename internal::MakeUniqueResult<T>::Array MakeUnique(size_t size) { | |
| 72 return std::unique_ptr<T>(new typename std::remove_extent<T>::type[size]()); | |
| 73 } | |
| 74 | |
| 75 // Overload to reject array types of known bound, e.g. T[n]. | |
| 76 template <typename T, typename... Args> | |
| 77 typename internal::MakeUniqueResult<T>::Invalid MakeUnique(Args&&... args) = | |
| 78 delete; | |
| 79 | |
| 80 } // namespace rtc | |
| 81 | 20 |
| 82 #endif // WEBRTC_BASE_PTR_UTIL_H_ | 21 #endif // WEBRTC_BASE_PTR_UTIL_H_ |
| OLD | NEW |