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