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
25namespace WTF {
26
27enum HashTableDeletedValueType { HashTableDeletedValue };
28enum HashTableEmptyValueType { HashTableEmptyValue };
29
30template <typename T> inline T* getPtr(T* p) { return p; }
31
32template <typename T> struct IsSmartPtr {
33 static const bool value = false;
34};
35
36template <typename T, bool isSmartPtr>
37struct GetPtrHelperBase;
38
39template <typename T>
40struct GetPtrHelperBase<T, false /* isSmartPtr */> {
41 typedef T* PtrType;
42 static T* getPtr(T& p) { return std::addressof(p); }
43};
44
45template <typename T>
46struct GetPtrHelperBase<T, true /* isSmartPtr */> {
47 typedef typename T::PtrType PtrType;
48 static PtrType getPtr(const T& p) { return p.get(); }
49};
50
51template <typename T>
52struct GetPtrHelper : GetPtrHelperBase<T, IsSmartPtr<T>::value> {
53};
54
55template <typename T>
56inline typename GetPtrHelper<T>::PtrType getPtr(T& p)
57{
58 return GetPtrHelper<T>::getPtr(p);
59}
60
61template <typename T>
62inline 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
69template <typename T, typename Deleter> struct IsSmartPtr<std::unique_ptr<T, Deleter>> {
70 static const bool value = true;
71};
72
73template <typename T, typename Deleter>
74struct 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