| 1 | /* |
| 2 | * Copyright (C) 2006 Apple Inc. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public License |
| 15 | * along with this library; see the file COPYING.LIB. If not, write to |
| 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #pragma once |
| 22 | |
| 23 | #include <memory> |
| 24 | |
| 25 | namespace WTF { |
| 26 | |
| 27 | enum HashTableDeletedValueType { HashTableDeletedValue }; |
| 28 | enum HashTableEmptyValueType { HashTableEmptyValue }; |
| 29 | |
| 30 | template <typename T> inline T* getPtr(T* p) { return p; } |
| 31 | |
| 32 | template <typename T> struct IsSmartPtr { |
| 33 | static const bool value = false; |
| 34 | }; |
| 35 | |
| 36 | template <typename T, bool isSmartPtr> |
| 37 | struct GetPtrHelperBase; |
| 38 | |
| 39 | template <typename T> |
| 40 | struct GetPtrHelperBase<T, false /* isSmartPtr */> { |
| 41 | typedef T* PtrType; |
| 42 | static T* getPtr(T& p) { return std::addressof(p); } |
| 43 | }; |
| 44 | |
| 45 | template <typename T> |
| 46 | struct GetPtrHelperBase<T, true /* isSmartPtr */> { |
| 47 | typedef typename T::PtrType PtrType; |
| 48 | static PtrType getPtr(const T& p) { return p.get(); } |
| 49 | }; |
| 50 | |
| 51 | template <typename T> |
| 52 | struct GetPtrHelper : GetPtrHelperBase<T, IsSmartPtr<T>::value> { |
| 53 | }; |
| 54 | |
| 55 | template <typename T> |
| 56 | inline typename GetPtrHelper<T>::PtrType getPtr(T& p) |
| 57 | { |
| 58 | return GetPtrHelper<T>::getPtr(p); |
| 59 | } |
| 60 | |
| 61 | template <typename T> |
| 62 | inline typename GetPtrHelper<T>::PtrType getPtr(const T& p) |
| 63 | { |
| 64 | return GetPtrHelper<T>::getPtr(p); |
| 65 | } |
| 66 | |
| 67 | // Explicit specialization for C++ standard library types. |
| 68 | |
| 69 | template <typename T, typename Deleter> struct IsSmartPtr<std::unique_ptr<T, Deleter>> { |
| 70 | static const bool value = true; |
| 71 | }; |
| 72 | |
| 73 | template <typename T, typename Deleter> |
| 74 | struct GetPtrHelper<std::unique_ptr<T, Deleter>> { |
| 75 | typedef T* PtrType; |
| 76 | static T* getPtr(const std::unique_ptr<T, Deleter>& p) { return p.get(); } |
| 77 | }; |
| 78 | |
| 79 | } // namespace WTF |
| 80 | |