2 * Copyright (C) 2006 Apple Inc.
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.
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.
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.
28 template <typename T> inline T* getPtr(T* p) { return p; }
30 template <typename T> struct IsSmartPtr {
31 static const bool value = false;
34 template <typename T, bool isSmartPtr>
35 struct GetPtrHelperBase;
38 struct GetPtrHelperBase<T, false /* isSmartPtr */> {
40 static T* getPtr(T& p) { return &p; }
44 struct GetPtrHelperBase<T, true /* isSmartPtr */> {
45 typedef typename T::PtrType PtrType;
46 static PtrType getPtr(const T& p) { return p.get(); }
50 struct GetPtrHelper : GetPtrHelperBase<T, IsSmartPtr<T>::value> {
54 inline typename GetPtrHelper<T>::PtrType getPtr(T& p)
56 return GetPtrHelper<T>::getPtr(p);
60 inline typename GetPtrHelper<T>::PtrType getPtr(const T& p)
62 return GetPtrHelper<T>::getPtr(p);
65 // Explicit specialization for C++ standard library types.
67 template <typename T, typename Deleter> struct IsSmartPtr<std::unique_ptr<T, Deleter>> {
68 static const bool value = true;
71 template <typename T, typename Deleter>
72 struct GetPtrHelper<std::unique_ptr<T, Deleter>> {
74 static T* getPtr(const std::unique_ptr<T, Deleter>& p) { return const_cast<T*>(p.get()); }
79 #endif // WTF_GetPtr_h