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