diff options
Diffstat (limited to 'Source/WTF/wtf/GetPtr.h')
-rw-r--r-- | Source/WTF/wtf/GetPtr.h | 59 |
1 files changed, 54 insertions, 5 deletions
diff --git a/Source/WTF/wtf/GetPtr.h b/Source/WTF/wtf/GetPtr.h index 25a0e6d9b..78107cbe5 100644 --- a/Source/WTF/wtf/GetPtr.h +++ b/Source/WTF/wtf/GetPtr.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006 Apple Computer, Inc. + * Copyright (C) 2006 Apple Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -21,12 +21,61 @@ #ifndef WTF_GetPtr_h #define WTF_GetPtr_h +#include <memory> + namespace WTF { - template <typename T> inline T* getPtr(T* p) - { - return p; - } +enum HashTableDeletedValueType { HashTableDeletedValue }; +enum HashTableEmptyValueType { HashTableEmptyValue }; + +template <typename T> inline T* getPtr(T* p) { return p; } + +template <typename T> struct IsSmartPtr { + static const bool value = false; +}; + +template <typename T, bool isSmartPtr> +struct GetPtrHelperBase; + +template <typename T> +struct GetPtrHelperBase<T, false /* isSmartPtr */> { + typedef T* PtrType; + static T* getPtr(T& p) { return std::addressof(p); } +}; + +template <typename T> +struct GetPtrHelperBase<T, true /* isSmartPtr */> { + typedef typename T::PtrType PtrType; + static PtrType getPtr(const T& p) { return p.get(); } +}; + +template <typename T> +struct GetPtrHelper : GetPtrHelperBase<T, IsSmartPtr<T>::value> { +}; + +template <typename T> +inline typename GetPtrHelper<T>::PtrType getPtr(T& p) +{ + return GetPtrHelper<T>::getPtr(p); +} + +template <typename T> +inline typename GetPtrHelper<T>::PtrType getPtr(const T& p) +{ + return GetPtrHelper<T>::getPtr(p); +} + +// Explicit specialization for C++ standard library types. + +template <typename T, typename Deleter> struct IsSmartPtr<std::unique_ptr<T, Deleter>> { + static const bool value = true; +}; + +template <typename T, typename Deleter> +struct GetPtrHelper<std::unique_ptr<T, Deleter>> { + typedef T* PtrType; + static T* getPtr(const std::unique_ptr<T, Deleter>& p) { return p.get(); } +}; } // namespace WTF |