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#ifndef WTF_GetPtr_h
22#define WTF_GetPtr_h
23
24#include <memory>
25
26namespace WTF {
27
28template <typename T> inline T* getPtr(T* p) { return p; }
29
30template <typename T> struct IsSmartPtr {
31 static const bool value = false;
32};
33
34template <typename T, bool isSmartPtr>
35struct GetPtrHelperBase;
36
37template <typename T>
38struct GetPtrHelperBase<T, false /* isSmartPtr */> {
39 typedef T* PtrType;
40 static T* getPtr(T& p) { return std::addressof(p); }
41};
42
43template <typename T>
44struct GetPtrHelperBase<T, true /* isSmartPtr */> {
45 typedef typename T::PtrType PtrType;
46 static PtrType getPtr(const T& p) { return p.get(); }
47};
48
49template <typename T>
50struct GetPtrHelper : GetPtrHelperBase<T, IsSmartPtr<T>::value> {
51};
52
53template <typename T>
54inline typename GetPtrHelper<T>::PtrType getPtr(T& p)
55{
56 return GetPtrHelper<T>::getPtr(p);
57}
58
59template <typename T>
60inline typename GetPtrHelper<T>::PtrType getPtr(const T& p)
61{
62 return GetPtrHelper<T>::getPtr(p);
63}
64
65// Explicit specialization for C++ standard library types.
66
67template <typename T, typename Deleter> struct IsSmartPtr<std::unique_ptr<T, Deleter>> {
68 static const bool value = true;
69};
70
71template <typename T, typename Deleter>
72struct GetPtrHelper<std::unique_ptr<T, Deleter>> {
73 typedef T* PtrType;
74 static T* getPtr(const std::unique_ptr<T, Deleter>& p) { return p.get(); }
75};
76
77} // namespace WTF
78
79#endif // WTF_GetPtr_h
80