libstdc++
allocator.h
Go to the documentation of this file.
00001 // Allocators -*- C++ -*-
00002 
00003 // Copyright (C) 2001-2016 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 /*
00026  * Copyright (c) 1996-1997
00027  * Silicon Graphics Computer Systems, Inc.
00028  *
00029  * Permission to use, copy, modify, distribute and sell this software
00030  * and its documentation for any purpose is hereby granted without fee,
00031  * provided that the above copyright notice appear in all copies and
00032  * that both that copyright notice and this permission notice appear
00033  * in supporting documentation.  Silicon Graphics makes no
00034  * representations about the suitability of this software for any
00035  * purpose.  It is provided "as is" without express or implied warranty.
00036  */
00037 
00038 /** @file bits/allocator.h
00039  *  This is an internal header file, included by other library headers.
00040  *  Do not attempt to use it directly. @headername{memory}
00041  */
00042 
00043 #ifndef _ALLOCATOR_H
00044 #define _ALLOCATOR_H 1
00045 
00046 #include <bits/c++allocator.h> // Define the base class to std::allocator.
00047 #include <bits/memoryfwd.h>
00048 #if __cplusplus >= 201103L
00049 #include <type_traits>
00050 #endif
00051 
00052 #define __cpp_lib_incomplete_container_elements 201505
00053 
00054 namespace std _GLIBCXX_VISIBILITY(default)
00055 {
00056 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00057 
00058   /**
00059    *  @addtogroup allocators
00060    *  @{
00061    */
00062 
00063   /// allocator<void> specialization.
00064   template<>
00065     class allocator<void>
00066     {
00067     public:
00068       typedef size_t      size_type;
00069       typedef ptrdiff_t   difference_type;
00070       typedef void*       pointer;
00071       typedef const void* const_pointer;
00072       typedef void        value_type;
00073 
00074       template<typename _Tp1>
00075         struct rebind
00076         { typedef allocator<_Tp1> other; };
00077 
00078 #if __cplusplus >= 201103L
00079       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00080       // 2103. std::allocator propagate_on_container_move_assignment
00081       typedef true_type propagate_on_container_move_assignment;
00082 
00083 #define __cpp_lib_allocator_is_always_equal 201411
00084       typedef true_type is_always_equal;
00085 #endif
00086     };
00087 
00088   /**
00089    * @brief  The @a standard allocator, as per [20.4].
00090    *
00091    *  See https://gcc.gnu.org/onlinedocs/libstdc++/manual/memory.html#std.util.memory.allocator
00092    *  for further details.
00093    *
00094    *  @tparam  _Tp  Type of allocated object.
00095    */
00096   template<typename _Tp>
00097     class allocator: public __allocator_base<_Tp>
00098     {
00099    public:
00100       typedef size_t     size_type;
00101       typedef ptrdiff_t  difference_type;
00102       typedef _Tp*       pointer;
00103       typedef const _Tp* const_pointer;
00104       typedef _Tp&       reference;
00105       typedef const _Tp& const_reference;
00106       typedef _Tp        value_type;
00107 
00108       template<typename _Tp1>
00109         struct rebind
00110         { typedef allocator<_Tp1> other; };
00111 
00112 #if __cplusplus >= 201103L
00113       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00114       // 2103. std::allocator propagate_on_container_move_assignment
00115       typedef true_type propagate_on_container_move_assignment;
00116 #endif
00117 
00118       allocator() throw() { }
00119 
00120       allocator(const allocator& __a) throw()
00121       : __allocator_base<_Tp>(__a) { }
00122 
00123       template<typename _Tp1>
00124         allocator(const allocator<_Tp1>&) throw() { }
00125 
00126       ~allocator() throw() { }
00127 
00128       // Inherit everything else.
00129     };
00130 
00131   template<typename _T1, typename _T2>
00132     inline bool
00133     operator==(const allocator<_T1>&, const allocator<_T2>&)
00134     _GLIBCXX_USE_NOEXCEPT
00135     { return true; }
00136 
00137   template<typename _Tp>
00138     inline bool
00139     operator==(const allocator<_Tp>&, const allocator<_Tp>&)
00140     _GLIBCXX_USE_NOEXCEPT
00141     { return true; }
00142 
00143   template<typename _T1, typename _T2>
00144     inline bool
00145     operator!=(const allocator<_T1>&, const allocator<_T2>&)
00146     _GLIBCXX_USE_NOEXCEPT
00147     { return false; }
00148 
00149   template<typename _Tp>
00150     inline bool
00151     operator!=(const allocator<_Tp>&, const allocator<_Tp>&)
00152     _GLIBCXX_USE_NOEXCEPT
00153     { return false; }
00154 
00155   /// @} group allocator
00156 
00157   // Inhibit implicit instantiations for required instantiations,
00158   // which are defined via explicit instantiations elsewhere.
00159 #if _GLIBCXX_EXTERN_TEMPLATE
00160   extern template class allocator<char>;
00161   extern template class allocator<wchar_t>;
00162 #endif
00163 
00164   // Undefine.
00165 #undef __allocator_base
00166 
00167   // To implement Option 3 of DR 431.
00168   template<typename _Alloc, bool = __is_empty(_Alloc)>
00169     struct __alloc_swap
00170     { static void _S_do_it(_Alloc&, _Alloc&) _GLIBCXX_NOEXCEPT { } };
00171 
00172   template<typename _Alloc>
00173     struct __alloc_swap<_Alloc, false>
00174     {
00175       static void
00176       _S_do_it(_Alloc& __one, _Alloc& __two) _GLIBCXX_NOEXCEPT
00177       {
00178         // Precondition: swappable allocators.
00179         if (__one != __two)
00180           swap(__one, __two);
00181       }
00182     };
00183 
00184   // Optimize for stateless allocators.
00185   template<typename _Alloc, bool = __is_empty(_Alloc)>
00186     struct __alloc_neq
00187     {
00188       static bool
00189       _S_do_it(const _Alloc&, const _Alloc&)
00190       { return false; }
00191     };
00192 
00193   template<typename _Alloc>
00194     struct __alloc_neq<_Alloc, false>
00195     {
00196       static bool
00197       _S_do_it(const _Alloc& __one, const _Alloc& __two)
00198       { return __one != __two; }
00199     };
00200 
00201 #if __cplusplus >= 201103L
00202   template<typename _Tp, bool
00203     = __or_<is_copy_constructible<typename _Tp::value_type>,
00204             is_nothrow_move_constructible<typename _Tp::value_type>>::value>
00205     struct __shrink_to_fit_aux
00206     { static bool _S_do_it(_Tp&) noexcept { return false; } };
00207 
00208   template<typename _Tp>
00209     struct __shrink_to_fit_aux<_Tp, true>
00210     {
00211       static bool
00212       _S_do_it(_Tp& __c) noexcept
00213       {
00214 #if __cpp_exceptions
00215         try
00216           {
00217             _Tp(__make_move_if_noexcept_iterator(__c.begin()),
00218                 __make_move_if_noexcept_iterator(__c.end()),
00219                 __c.get_allocator()).swap(__c);
00220             return true;
00221           }
00222         catch(...)
00223           { return false; }
00224 #else
00225         return false;
00226 #endif
00227       }
00228     };
00229 #endif
00230 
00231 _GLIBCXX_END_NAMESPACE_VERSION
00232 } // namespace std
00233 
00234 #endif