blob: 156df841b5e4020e616a71c3b59c21865931aafa [file] [log] [blame]
zsteinf42cc9d2017-03-27 16:17:19 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020013#ifndef RTC_BASE_PTR_UTIL_H_
14#define RTC_BASE_PTR_UTIL_H_
zsteinf42cc9d2017-03-27 16:17:19 -070015
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016#include <memory>
17#include <utility>
zsteinf42cc9d2017-03-27 16:17:19 -070018
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020019namespace 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.
24template <typename T>
25std::unique_ptr<T> WrapUnique(T* ptr) {
26 return std::unique_ptr<T>(ptr);
27}
28
29namespace internal {
30
31template <typename T>
32struct MakeUniqueResult {
33 using Scalar = std::unique_ptr<T>;
34};
35
36template <typename T>
37struct MakeUniqueResult<T[]> {
38 using Array = std::unique_ptr<T[]>;
39};
40
41template <typename T, size_t N>
42struct 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.
62template <typename T, typename... Args>
63typename 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.
70template <typename T>
71typename 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].
76template <typename T, typename... Args>
77typename internal::MakeUniqueResult<T>::Invalid MakeUnique(Args&&... args) =
78 delete;
79
80} // namespace rtc
zsteinf42cc9d2017-03-27 16:17:19 -070081
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020082#endif // RTC_BASE_PTR_UTIL_H_