zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | // This implementation is borrowed from chromium. |
| 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #ifndef RTC_BASE_PTR_UTIL_H_ |
| 14 | #define RTC_BASE_PTR_UTIL_H_ |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 15 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 16 | #include <memory> |
| 17 | #include <utility> |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 18 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 19 | namespace rtc { |
| 20 | |
| 21 | // Helper to transfer ownership of a raw pointer to a std::unique_ptr<T>. |
| 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 |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 81 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 82 | #endif // RTC_BASE_PTR_UTIL_H_ |