libstdc++
alloc_traits.h
Go to the documentation of this file.
00001 // Allocator traits -*- C++ -*-
00002 
00003 // Copyright (C) 2011-2017 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 /**
00046  * @brief  Uniform interface to C++98 and C++11 allocators.
00047  * @ingroup allocators
00048 */
00049 template<typename _Alloc>
00050   struct __alloc_traits
00051 #if __cplusplus >= 201103L
00052   : std::allocator_traits<_Alloc>
00053 #endif
00054   {
00055     typedef _Alloc allocator_type;
00056 #if __cplusplus >= 201103L
00057     typedef std::allocator_traits<_Alloc>           _Base_type;
00058     typedef typename _Base_type::value_type         value_type;
00059     typedef typename _Base_type::pointer            pointer;
00060     typedef typename _Base_type::const_pointer      const_pointer;
00061     typedef typename _Base_type::size_type          size_type;
00062     typedef typename _Base_type::difference_type    difference_type;
00063     // C++11 allocators do not define reference or const_reference
00064     typedef value_type&                             reference;
00065     typedef const value_type&                       const_reference;
00066     using _Base_type::allocate;
00067     using _Base_type::deallocate;
00068     using _Base_type::construct;
00069     using _Base_type::destroy;
00070     using _Base_type::max_size;
00071 
00072   private:
00073     template<typename _Ptr>
00074       using __is_custom_pointer
00075         = std::__and_<std::is_same<pointer, _Ptr>,
00076                       std::__not_<std::is_pointer<_Ptr>>>;
00077 
00078   public:
00079     // overload construct for non-standard pointer types
00080     template<typename _Ptr, typename... _Args>
00081       static typename std::enable_if<__is_custom_pointer<_Ptr>::value>::type
00082       construct(_Alloc& __a, _Ptr __p, _Args&&... __args)
00083       {
00084         _Base_type::construct(__a, std::addressof(*__p),
00085                               std::forward<_Args>(__args)...);
00086       }
00087 
00088     // overload destroy for non-standard pointer types
00089     template<typename _Ptr>
00090       static typename std::enable_if<__is_custom_pointer<_Ptr>::value>::type
00091       destroy(_Alloc& __a, _Ptr __p)
00092       { _Base_type::destroy(__a, std::addressof(*__p)); }
00093 
00094     static _Alloc _S_select_on_copy(const _Alloc& __a)
00095     { return _Base_type::select_on_container_copy_construction(__a); }
00096 
00097     static void _S_on_swap(_Alloc& __a, _Alloc& __b)
00098     { std::__alloc_on_swap(__a, __b); }
00099 
00100     static constexpr bool _S_propagate_on_copy_assign()
00101     { return _Base_type::propagate_on_container_copy_assignment::value; }
00102 
00103     static constexpr bool _S_propagate_on_move_assign()
00104     { return _Base_type::propagate_on_container_move_assignment::value; }
00105 
00106     static constexpr bool _S_propagate_on_swap()
00107     { return _Base_type::propagate_on_container_swap::value; }
00108 
00109     static constexpr bool _S_always_equal()
00110     { return _Base_type::is_always_equal::value; }
00111 
00112     static constexpr bool _S_nothrow_move()
00113     { return _S_propagate_on_move_assign() || _S_always_equal(); }
00114 
00115     template<typename _Tp>
00116       struct rebind
00117       { typedef typename _Base_type::template rebind_alloc<_Tp> other; };
00118 #else
00119 
00120     typedef typename _Alloc::pointer                pointer;
00121     typedef typename _Alloc::const_pointer          const_pointer;
00122     typedef typename _Alloc::value_type             value_type;
00123     typedef typename _Alloc::reference              reference;
00124     typedef typename _Alloc::const_reference        const_reference;
00125     typedef typename _Alloc::size_type              size_type;
00126     typedef typename _Alloc::difference_type        difference_type;
00127 
00128     static pointer
00129     allocate(_Alloc& __a, size_type __n)
00130     { return __a.allocate(__n); }
00131 
00132     static void deallocate(_Alloc& __a, pointer __p, size_type __n)
00133     { __a.deallocate(__p, __n); }
00134 
00135     template<typename _Tp>
00136       static void construct(_Alloc& __a, pointer __p, const _Tp& __arg)
00137       { __a.construct(__p, __arg); }
00138 
00139     static void destroy(_Alloc& __a, pointer __p)
00140     { __a.destroy(__p); }
00141 
00142     static size_type max_size(const _Alloc& __a)
00143     { return __a.max_size(); }
00144 
00145     static const _Alloc& _S_select_on_copy(const _Alloc& __a) { return __a; }
00146 
00147     static void _S_on_swap(_Alloc& __a, _Alloc& __b)
00148     {
00149       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00150       // 431. Swapping containers with unequal allocators.
00151       std::__alloc_swap<_Alloc>::_S_do_it(__a, __b);
00152     }
00153 
00154     template<typename _Tp>
00155       struct rebind
00156       { typedef typename _Alloc::template rebind<_Tp>::other other; };
00157 #endif
00158   };
00159 
00160 _GLIBCXX_END_NAMESPACE_VERSION
00161 } // namespace __gnu_cxx
00162 
00163 #endif