libstdc++
ptr_traits.h
Go to the documentation of this file.
00001 // Pointer Traits -*- C++ -*-
00002 
00003 // Copyright (C) 2011-2015 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file bits/ptr_traits.h
00026  *  This is an internal header file, included by other library headers.
00027  *  Do not attempt to use it directly. @headername{memory}
00028  */
00029 
00030 #ifndef _PTR_TRAITS_H
00031 #define _PTR_TRAITS_H 1
00032 
00033 #if __cplusplus >= 201103L
00034 
00035 #include <type_traits> // For _GLIBCXX_HAS_NESTED_TYPE
00036 
00037 namespace std _GLIBCXX_VISIBILITY(default)
00038 {
00039 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00040 
00041 _GLIBCXX_HAS_NESTED_TYPE(element_type)
00042 _GLIBCXX_HAS_NESTED_TYPE(difference_type)
00043 
00044   template<typename _Tp, bool = __has_element_type<_Tp>::value>
00045     struct __ptrtr_elt_type;
00046 
00047   template<typename _Tp>
00048     struct __ptrtr_elt_type<_Tp, true>
00049     {
00050       typedef typename _Tp::element_type __type;
00051     };
00052 
00053   template<template<typename, typename...> class _SomePtr, typename _Tp,
00054             typename... _Args>
00055     struct __ptrtr_elt_type<_SomePtr<_Tp, _Args...>, false>
00056     {
00057       typedef _Tp __type;
00058     };
00059 
00060   template<typename _Tp, bool = __has_difference_type<_Tp>::value>
00061     struct __ptrtr_diff_type
00062     {
00063       typedef typename _Tp::difference_type __type;
00064     };
00065 
00066   template<typename _Tp>
00067     struct __ptrtr_diff_type<_Tp, false>
00068     {
00069       typedef ptrdiff_t __type;
00070     };
00071 
00072   template<typename _Ptr, typename _Up>
00073     class __ptrtr_rebind_helper
00074     {
00075       template<typename _Ptr2, typename _Up2>
00076         static constexpr true_type
00077         _S_chk(typename _Ptr2::template rebind<_Up2>*);
00078 
00079       template<typename, typename>
00080         static constexpr false_type
00081         _S_chk(...);
00082 
00083     public:
00084       using __type = decltype(_S_chk<_Ptr, _Up>(nullptr));
00085     };
00086 
00087   template<typename _Tp, typename _Up,
00088            bool = __ptrtr_rebind_helper<_Tp, _Up>::__type::value>
00089     struct __ptrtr_rebind;
00090 
00091   template<typename _Tp, typename _Up>
00092     struct __ptrtr_rebind<_Tp, _Up, true>
00093     {
00094       typedef typename _Tp::template rebind<_Up> __type;
00095     };
00096 
00097   template<template<typename, typename...> class _SomePtr, typename _Up,
00098             typename _Tp, typename... _Args>
00099     struct __ptrtr_rebind<_SomePtr<_Tp, _Args...>, _Up, false>
00100     {
00101       typedef _SomePtr<_Up, _Args...> __type;
00102     };
00103 
00104   template<typename _Tp, typename = typename remove_cv<_Tp>::type>
00105     struct __ptrtr_not_void
00106     {
00107       typedef _Tp __type;
00108     };
00109 
00110   template<typename _Tp>
00111     struct __ptrtr_not_void<_Tp, void>
00112     {
00113       struct __type { };
00114     };
00115 
00116   template<typename _Ptr>
00117     class __ptrtr_pointer_to
00118     {
00119       typedef typename __ptrtr_elt_type<_Ptr>::__type   __orig_type;
00120       typedef typename __ptrtr_not_void<__orig_type>::__type __element_type;
00121 
00122     public:
00123       static _Ptr pointer_to(__element_type& __e)
00124       { return _Ptr::pointer_to(__e); }
00125     };
00126 
00127   /**
00128    * @brief  Uniform interface to all pointer-like types
00129    * @ingroup pointer_abstractions
00130   */
00131   template<typename _Ptr>
00132     struct pointer_traits : __ptrtr_pointer_to<_Ptr>
00133     {
00134       /// The pointer type
00135       typedef _Ptr                                      pointer;
00136       /// The type pointed to
00137       typedef typename __ptrtr_elt_type<_Ptr>::__type   element_type;
00138       /// Type used to represent the difference between two pointers
00139       typedef typename __ptrtr_diff_type<_Ptr>::__type  difference_type;
00140 
00141       template<typename _Up>
00142         using rebind = typename __ptrtr_rebind<_Ptr, _Up>::__type;
00143     };
00144 
00145   /**
00146    * @brief  Partial specialization for built-in pointers.
00147    * @ingroup pointer_abstractions
00148   */
00149   template<typename _Tp>
00150     struct pointer_traits<_Tp*>
00151     {
00152       /// The pointer type
00153       typedef _Tp* pointer;
00154       /// The type pointed to
00155       typedef _Tp  element_type;
00156       /// Type used to represent the difference between two pointers
00157       typedef ptrdiff_t difference_type;
00158 
00159       template<typename _Up>
00160         using rebind = _Up*;
00161 
00162       /**
00163        *  @brief  Obtain a pointer to an object
00164        *  @param  __r  A reference to an object of type @c element_type
00165        *  @return @c addressof(__r)
00166       */
00167       static pointer
00168       pointer_to(typename __ptrtr_not_void<element_type>::__type& __r) noexcept
00169       { return std::addressof(__r); }
00170     };
00171 
00172 _GLIBCXX_END_NAMESPACE_VERSION
00173 } // namespace std
00174 
00175 #endif
00176 
00177 #endif