Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef ptr_util_h | |
|
ikilpatrick
2016/10/31 21:36:08
would prefer to wait until we have this in wtf ins
Gleb Lanbin
2016/10/31 23:08:18
deleted
| |
| 6 #define ptr_util_h | |
| 7 | |
| 8 #include <memory> | |
| 9 #include <utility> | |
| 10 | |
| 11 // This is a partial copy of base/memory/ptr_util.h that sadly cannot be used in | |
| 12 // Blink because of "-base" from third_party's include_rules. | |
| 13 // TODO(glebl): move to WebKit/base? similar to | |
| 14 // third_party/pdfium/third_party/base/ptr_util.h | |
| 15 | |
| 16 namespace blink { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 template <typename T> | |
| 21 struct MakeUniqueResult { | |
| 22 using Scalar = std::unique_ptr<T>; | |
| 23 }; | |
| 24 | |
| 25 template <typename T> | |
| 26 struct MakeUniqueResult<T[]> { | |
| 27 using Array = std::unique_ptr<T[]>; | |
| 28 }; | |
| 29 | |
| 30 template <typename T, size_t N> | |
| 31 struct MakeUniqueResult<T[N]> { | |
| 32 using Invalid = void; | |
| 33 }; | |
| 34 | |
| 35 } // namespace | |
| 36 | |
| 37 // Helper to construct an object wrapped in a std::unique_ptr. This is an | |
| 38 // implementation of C++14's std::MakeUnique that can be used in Chrome. | |
| 39 // | |
| 40 // MakeUnique<T>(args) should be preferred over WrapUnique(new T(args)): bare | |
| 41 // calls to `new` should be treated with scrutiny. | |
| 42 // | |
| 43 // Usage: | |
| 44 // // ptr is a std::unique_ptr<std::string> | |
| 45 // auto ptr = MakeUnique<std::string>("hello world!"); | |
| 46 // | |
| 47 // // arr is a std::unique_ptr<int[]> | |
| 48 // auto arr = MakeUnique<int[]>(5); | |
| 49 | |
| 50 // Overload for non-array types. Arguments are forwarded to T's constructor. | |
| 51 template <typename T, typename... Args> | |
| 52 typename MakeUniqueResult<T>::Scalar MakeUnique(Args&&... args) { | |
| 53 return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); | |
| 54 } | |
| 55 | |
| 56 } // namespace blink | |
| 57 #endif // ptr_util_h | |
| OLD | NEW |