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