libstdc++
ext/alloc_traits.h
Go to the documentation of this file.
00001 // Allocator traits -*- C++ -*-
00002 
00003 // Copyright (C) 2011, 2012 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 ext/alloc_traits.h
00026  *  This file is a GNU extension to the Standard C++ Library.
00027  */
00028 
00029 #ifndef _EXT_ALLOC_TRAITS_H
00030 #define _EXT_ALLOC_TRAITS_H 1
00031 
00032 #pragma GCC system_header
00033 
00034 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00035 # include <bits/alloc_traits.h>
00036 #else
00037 # include <bits/allocator.h>  // for __alloc_swap
00038 #endif
00039 
00040 namespace std _GLIBCXX_VISIBILITY(default)
00041 {
00042 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00043   template<typename> struct allocator;
00044 _GLIBCXX_END_NAMESPACE_VERSION
00045 } // namespace
00046 
00047 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
00048 {
00049 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00050 
00051 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00052   template<typename _Alloc>
00053     struct __allocator_always_compares_equal
00054     { static const bool value = false; };
00055 
00056   template<typename _Alloc>
00057     const bool __allocator_always_compares_equal<_Alloc>::value;
00058 
00059   template<typename _Tp>
00060     struct __allocator_always_compares_equal<std::allocator<_Tp>>
00061     { static const bool value = true; };
00062 
00063   template<typename _Tp>
00064     const bool __allocator_always_compares_equal<std::allocator<_Tp>>::value;
00065 
00066   template<typename, typename> struct array_allocator;
00067 
00068   template<typename _Tp, typename _Array>
00069     struct __allocator_always_compares_equal<array_allocator<_Tp, _Array>>
00070     { static const bool value = true; };
00071 
00072   template<typename _Tp, typename _Array>
00073     const bool
00074     __allocator_always_compares_equal<array_allocator<_Tp, _Array>>::value;
00075 
00076   template<typename> struct mt_allocator;
00077 
00078   template<typename _Tp>
00079     struct __allocator_always_compares_equal<mt_allocator<_Tp>>
00080     { static const bool value = true; };
00081 
00082   template<typename _Tp>
00083     const bool __allocator_always_compares_equal<mt_allocator<_Tp>>::value;
00084 
00085   template<typename> struct new_allocator;
00086 
00087   template<typename _Tp>
00088     struct __allocator_always_compares_equal<new_allocator<_Tp>>
00089     { static const bool value = true; };
00090 
00091   template<typename _Tp>
00092     const bool __allocator_always_compares_equal<new_allocator<_Tp>>::value;
00093 
00094   template<typename> struct pool_allocator;
00095 
00096   template<typename _Tp>
00097     struct __allocator_always_compares_equal<pool_allocator<_Tp>>
00098     { static const bool value = true; };
00099 
00100   template<typename _Tp>
00101     const bool __allocator_always_compares_equal<pool_allocator<_Tp>>::value;
00102 #endif
00103 
00104 /**
00105  * @brief  Uniform interface to C++98 and C++0x allocators.
00106  * @ingroup allocators
00107 */
00108 template<typename _Alloc>
00109   struct __alloc_traits
00110 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00111   : std::allocator_traits<_Alloc>
00112 #endif
00113   {
00114     typedef _Alloc allocator_type;
00115 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00116     typedef std::allocator_traits<_Alloc>           _Base_type;
00117     typedef typename _Base_type::value_type         value_type;
00118     typedef typename _Base_type::pointer            pointer;
00119     typedef typename _Base_type::const_pointer      const_pointer;
00120     typedef typename _Base_type::size_type          size_type;
00121     // C++0x allocators do not define reference or const_reference
00122     typedef value_type&                             reference;
00123     typedef const value_type&                       const_reference;
00124     using _Base_type::allocate;
00125     using _Base_type::deallocate;
00126     using _Base_type::construct;
00127     using _Base_type::destroy;
00128     using _Base_type::max_size;
00129 
00130   private:
00131     template<typename _Ptr>
00132       struct __is_custom_pointer
00133       : std::integral_constant<bool, std::is_same<pointer, _Ptr>::value
00134                                      && !std::is_pointer<_Ptr>::value>
00135       { };
00136 
00137   public:
00138     // overload construct for non-standard pointer types
00139     template<typename _Ptr, typename... _Args>
00140       static typename std::enable_if<__is_custom_pointer<_Ptr>::value>::type
00141       construct(_Alloc& __a, _Ptr __p, _Args&&... __args)
00142       {
00143     _Base_type::construct(__a, std::addressof(*__p),
00144                   std::forward<_Args>(__args)...);
00145       }
00146 
00147     // overload destroy for non-standard pointer types
00148     template<typename _Ptr>
00149       static typename std::enable_if<__is_custom_pointer<_Ptr>::value>::type
00150       destroy(_Alloc& __a, _Ptr __p)
00151       { _Base_type::destroy(__a, std::addressof(*__p)); }
00152 
00153     static _Alloc _S_select_on_copy(const _Alloc& __a)
00154     { return _Base_type::select_on_container_copy_construction(__a); }
00155 
00156     static void _S_on_swap(_Alloc& __a, _Alloc& __b)
00157     { std::__alloc_on_swap(__a, __b); }
00158 
00159     static constexpr bool _S_propagate_on_copy_assign()
00160     { return _Base_type::propagate_on_container_copy_assignment::value; }
00161 
00162     static constexpr bool _S_propagate_on_move_assign()
00163     { return _Base_type::propagate_on_container_move_assignment::value; }
00164 
00165     static constexpr bool _S_propagate_on_swap()
00166     { return _Base_type::propagate_on_container_swap::value; }
00167 
00168     static constexpr bool _S_always_equal()
00169     { return __allocator_always_compares_equal<_Alloc>::value; }
00170 
00171     static constexpr bool _S_nothrow_move()
00172     { return _S_propagate_on_move_assign() || _S_always_equal(); }
00173 
00174     static constexpr bool _S_nothrow_swap()
00175     {
00176       using std::swap;
00177       return !_S_propagate_on_swap()
00178         || noexcept(swap(std::declval<_Alloc&>(), std::declval<_Alloc&>()));
00179     }
00180 
00181     template<typename _Tp>
00182       struct rebind
00183       { typedef typename _Base_type::template rebind_alloc<_Tp> other; };
00184 #else
00185 
00186     typedef typename _Alloc::pointer                pointer;
00187     typedef typename _Alloc::const_pointer          const_pointer;
00188     typedef typename _Alloc::value_type             value_type;
00189     typedef typename _Alloc::reference              reference;
00190     typedef typename _Alloc::const_reference        const_reference;
00191     typedef typename _Alloc::size_type              size_type;
00192 
00193     static pointer
00194     allocate(_Alloc& __a, size_type __n)
00195     { return __a.allocate(__n); }
00196 
00197     static void deallocate(_Alloc& __a, pointer __p, size_type __n)
00198     { __a.deallocate(__p, __n); }
00199 
00200     template<typename _Tp>
00201       static void construct(_Alloc& __a, pointer __p, const _Tp& __arg)
00202       { __a.construct(__p, __arg); }
00203 
00204     static void destroy(_Alloc& __a, pointer __p)
00205     { __a.destroy(__p); }
00206 
00207     static size_type max_size(const _Alloc& __a)
00208     { return __a.max_size(); }
00209 
00210     static const _Alloc& _S_select_on_copy(const _Alloc& __a) { return __a; }
00211 
00212     static void _S_on_swap(_Alloc& __a, _Alloc& __b)
00213     {
00214       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00215       // 431. Swapping containers with unequal allocators.
00216       std::__alloc_swap<_Alloc>::_S_do_it(__a, __b);
00217     }
00218 
00219     template<typename _Tp>
00220       struct rebind
00221       { typedef typename _Alloc::template rebind<_Tp>::other other; };
00222 #endif
00223   };
00224 
00225 _GLIBCXX_END_NAMESPACE_VERSION
00226 } // namespace
00227 
00228 #endif