| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 PtrUtil_h | 5 #ifndef PtrUtil_h |
| 6 #define PtrUtil_h | 6 #define PtrUtil_h |
| 7 | 7 |
| 8 #include "base/memory/ptr_util.h" |
| 8 #include "wtf/TypeTraits.h" | 9 #include "wtf/TypeTraits.h" |
| 9 | 10 |
| 10 #include <memory> | 11 #include <memory> |
| 11 | 12 |
| 12 namespace WTF { | 13 namespace WTF { |
| 13 | 14 |
| 14 template <typename T> | 15 template <typename T> |
| 15 std::unique_ptr<T> wrapUnique(T* ptr) { | 16 std::unique_ptr<T> wrapUnique(T* ptr) { |
| 16 static_assert( | 17 static_assert( |
| 17 !WTF::IsGarbageCollectedType<T>::value, | 18 !WTF::IsGarbageCollectedType<T>::value, |
| 18 "Garbage collected types should not be stored in std::unique_ptr!"); | 19 "Garbage collected types should not be stored in std::unique_ptr!"); |
| 19 return std::unique_ptr<T>(ptr); | 20 return std::unique_ptr<T>(ptr); |
| 20 } | 21 } |
| 21 | 22 |
| 22 template <typename T> | 23 template <typename T> |
| 23 std::unique_ptr<T[]> wrapArrayUnique(T* ptr) { | 24 std::unique_ptr<T[]> wrapArrayUnique(T* ptr) { |
| 24 static_assert( | 25 static_assert( |
| 25 !WTF::IsGarbageCollectedType<T>::value, | 26 !WTF::IsGarbageCollectedType<T>::value, |
| 26 "Garbage collected types should not be stored in std::unique_ptr!"); | 27 "Garbage collected types should not be stored in std::unique_ptr!"); |
| 27 return std::unique_ptr<T[]>(ptr); | 28 return std::unique_ptr<T[]>(ptr); |
| 28 } | 29 } |
| 29 | 30 |
| 31 // WTF::makeUnique is base::MakeUnique. See base/ptr_util.h for documentation. |
| 32 template <typename T, typename... Args> |
| 33 auto makeUnique(Args&&... args) |
| 34 -> decltype(base::MakeUnique<T>(std::forward<Args>(args)...)) { |
| 35 static_assert( |
| 36 !WTF::IsGarbageCollectedType<T>::value, |
| 37 "Garbage collected types should not be stored in std::unique_ptr!"); |
| 38 return base::MakeUnique<T>(std::forward<Args>(args)...); |
| 39 } |
| 40 |
| 41 template <typename T> |
| 42 auto makeUnique(size_t size) -> decltype(base::MakeUnique<T>(size)) { |
| 43 static_assert( |
| 44 !WTF::IsGarbageCollectedType<T>::value, |
| 45 "Garbage collected types should not be stored in std::unique_ptr!"); |
| 46 return base::MakeUnique<T>(size); |
| 47 } |
| 48 |
| 30 } // namespace WTF | 49 } // namespace WTF |
| 31 | 50 |
| 51 using WTF::makeUnique; |
| 32 using WTF::wrapUnique; | 52 using WTF::wrapUnique; |
| 33 using WTF::wrapArrayUnique; | 53 using WTF::wrapArrayUnique; |
| 34 | 54 |
| 35 #endif // PtrUtil_h | 55 #endif // PtrUtil_h |
| OLD | NEW |