libstdc++
alloc_traits.h
Go to the documentation of this file.
00001 // Allocator traits -*- C++ -*-
00002 
00003 // Copyright (C) 2011-2019 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, typename = typename _Alloc::value_type>
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       noexcept(noexcept(_Base_type::construct(__a, std::__to_address(__p),
00084                                               std::forward<_Args>(__args)...)))
00085       {
00086         _Base_type::construct(__a, std::__to_address(__p),
00087                               std::forward<_Args>(__args)...);
00088       }
00089 
00090     // overload destroy for non-standard pointer types
00091     template<typename _Ptr>
00092       static typename std::enable_if<__is_custom_pointer<_Ptr>::value>::type
00093       destroy(_Alloc& __a, _Ptr __p)
00094       noexcept(noexcept(_Base_type::destroy(__a, std::__to_address(__p))))
00095       { _Base_type::destroy(__a, std::__to_address(__p)); }
00096 
00097     static _Alloc _S_select_on_copy(const _Alloc& __a)
00098     { return _Base_type::select_on_container_copy_construction(__a); }
00099 
00100     static void _S_on_swap(_Alloc& __a, _Alloc& __b)
00101     { std::__alloc_on_swap(__a, __b); }
00102 
00103     static constexpr bool _S_propagate_on_copy_assign()
00104     { return _Base_type::propagate_on_container_copy_assignment::value; }
00105 
00106     static constexpr bool _S_propagate_on_move_assign()
00107     { return _Base_type::propagate_on_container_move_assignment::value; }
00108 
00109     static constexpr bool _S_propagate_on_swap()
00110     { return _Base_type::propagate_on_container_swap::value; }
00111 
00112     static constexpr bool _S_always_equal()
00113     { return _Base_type::is_always_equal::value; }
00114 
00115     static constexpr bool _S_nothrow_move()
00116     { return _S_propagate_on_move_assign() || _S_always_equal(); }
00117 
00118     template<typename _Tp>
00119       struct rebind
00120       { typedef typename _Base_type::template rebind_alloc<_Tp> other; };
00121 #else
00122 
00123     typedef typename _Alloc::pointer                pointer;
00124     typedef typename _Alloc::const_pointer          const_pointer;
00125     typedef typename _Alloc::value_type             value_type;
00126     typedef typename _Alloc::reference              reference;
00127     typedef typename _Alloc::const_reference        const_reference;
00128     typedef typename _Alloc::size_type              size_type;
00129     typedef typename _Alloc::difference_type        difference_type;
00130 
00131     _GLIBCXX_NODISCARD static pointer
00132     allocate(_Alloc& __a, size_type __n)
00133     { return __a.allocate(__n); }
00134 
00135     static void deallocate(_Alloc& __a, pointer __p, size_type __n)
00136     { __a.deallocate(__p, __n); }
00137 
00138     template<typename _Tp>
00139       static void construct(_Alloc& __a, pointer __p, const _Tp& __arg)
00140       { __a.construct(__p, __arg); }
00141 
00142     static void destroy(_Alloc& __a, pointer __p)
00143     { __a.destroy(__p); }
00144 
00145     static size_type max_size(const _Alloc& __a)
00146     { return __a.max_size(); }
00147 
00148     static const _Alloc& _S_select_on_copy(const _Alloc& __a) { return __a; }
00149 
00150     static void _S_on_swap(_Alloc& __a, _Alloc& __b)
00151     {
00152       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00153       // 431. Swapping containers with unequal allocators.
00154       std::__alloc_swap<_Alloc>::_S_do_it(__a, __b);
00155     }
00156 
00157     template<typename _Tp>
00158       struct rebind
00159       { typedef typename _Alloc::template rebind<_Tp>::other other; };
00160 #endif
00161   };
00162 
00163 _GLIBCXX_END_NAMESPACE_VERSION
00164 } // namespace __gnu_cxx
00165 
00166 #endif