libstdc++
ptr_traits.h
Go to the documentation of this file.
00001 // Pointer Traits -*- C++ -*-
00002 
00003 // Copyright (C) 2011-2013 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 bool
00077         _S_chk(typename _Ptr2::template rebind<_Up2>*)
00078         { return true; }
00079 
00080       template<typename, typename>
00081         static constexpr bool
00082         _S_chk(...)
00083         { return false; }
00084 
00085     public:
00086       static const bool __value = _S_chk<_Ptr, _Up>(nullptr);
00087     };
00088 
00089   template<typename _Ptr, typename _Up>
00090     const bool __ptrtr_rebind_helper<_Ptr, _Up>::__value;
00091 
00092   template<typename _Tp, typename _Up,
00093            bool = __ptrtr_rebind_helper<_Tp, _Up>::__value>
00094     struct __ptrtr_rebind;
00095 
00096   template<typename _Tp, typename _Up>
00097     struct __ptrtr_rebind<_Tp, _Up, true>
00098     {
00099       typedef typename _Tp::template rebind<_Up> __type;
00100     };
00101 
00102   template<template<typename, typename...> class _SomePtr, typename _Up,
00103             typename _Tp, typename... _Args>
00104     struct __ptrtr_rebind<_SomePtr<_Tp, _Args...>, _Up, false>
00105     {
00106       typedef _SomePtr<_Up, _Args...> __type;
00107     };
00108 
00109   template<typename _Tp, typename = typename remove_cv<_Tp>::type>
00110     struct __ptrtr_not_void
00111     {
00112       typedef _Tp __type;
00113     };
00114 
00115   template<typename _Tp>
00116     struct __ptrtr_not_void<_Tp, void>
00117     {
00118       struct __type { };
00119     };
00120 
00121   template<typename _Ptr>
00122     class __ptrtr_pointer_to
00123     {
00124       typedef typename __ptrtr_elt_type<_Ptr>::__type   __orig_type;
00125       typedef typename __ptrtr_not_void<__orig_type>::__type __element_type;
00126 
00127     public:
00128       static _Ptr pointer_to(__element_type& __e)
00129       { return _Ptr::pointer_to(__e); }
00130     };
00131 
00132   /**
00133    * @brief  Uniform interface to all pointer-like types
00134    * @ingroup pointer_abstractions
00135   */
00136   template<typename _Ptr>
00137     struct pointer_traits : __ptrtr_pointer_to<_Ptr>
00138     {
00139       /// The pointer type
00140       typedef _Ptr                                      pointer;
00141       /// The type pointed to
00142       typedef typename __ptrtr_elt_type<_Ptr>::__type   element_type;
00143       /// Type used to represent the difference between two pointers
00144       typedef typename __ptrtr_diff_type<_Ptr>::__type  difference_type;
00145 
00146       template<typename _Up>
00147         using rebind = typename __ptrtr_rebind<_Ptr, _Up>::__type;
00148     };
00149 
00150   /**
00151    * @brief  Partial specialization for built-in pointers.
00152    * @ingroup pointer_abstractions
00153   */
00154   template<typename _Tp>
00155     struct pointer_traits<_Tp*>
00156     {
00157       /// The pointer type
00158       typedef _Tp* pointer;
00159       /// The type pointed to
00160       typedef _Tp  element_type;
00161       /// Type used to represent the difference between two pointers
00162       typedef ptrdiff_t difference_type;
00163 
00164       template<typename _Up>
00165         using rebind = _Up*;
00166 
00167       /**
00168        *  @brief  Obtain a pointer to an object
00169        *  @param  __r  A reference to an object of type @c element_type
00170        *  @return @c addressof(__r)
00171       */
00172       static pointer
00173       pointer_to(typename __ptrtr_not_void<element_type>::__type& __r) noexcept
00174       { return std::addressof(__r); }
00175     };
00176 
00177 _GLIBCXX_END_NAMESPACE_VERSION
00178 } // namespace std
00179 
00180 #endif
00181 
00182 #endif