libstdc++
allocator.h
Go to the documentation of this file.
00001 // Allocators -*- C++ -*-
00002 
00003 // Copyright (C) 2001-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 /*
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 namespace std _GLIBCXX_VISIBILITY(default)
00053 {
00054 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00055 
00056   /**
00057    *  @addtogroup allocators
00058    *  @{
00059    */
00060 
00061   /// allocator<void> specialization.
00062   template<>
00063     class allocator<void>
00064     {
00065     public:
00066       typedef size_t      size_type;
00067       typedef ptrdiff_t   difference_type;
00068       typedef void*       pointer;
00069       typedef const void* const_pointer;
00070       typedef void        value_type;
00071 
00072       template<typename _Tp1>
00073         struct rebind
00074         { typedef allocator<_Tp1> other; };
00075 
00076 #if __cplusplus >= 201103L
00077       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00078       // 2103. std::allocator propagate_on_container_move_assignment
00079       typedef true_type propagate_on_container_move_assignment;
00080 #endif
00081     };
00082 
00083   /**
00084    * @brief  The @a standard allocator, as per [20.4].
00085    *
00086    *  See http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt04ch11.html
00087    *  for further details.
00088    *
00089    *  @tparam  _Tp  Type of allocated object.
00090    */
00091   template<typename _Tp>
00092     class allocator: public __allocator_base<_Tp>
00093     {
00094    public:
00095       typedef size_t     size_type;
00096       typedef ptrdiff_t  difference_type;
00097       typedef _Tp*       pointer;
00098       typedef const _Tp* const_pointer;
00099       typedef _Tp&       reference;
00100       typedef const _Tp& const_reference;
00101       typedef _Tp        value_type;
00102 
00103       template<typename _Tp1>
00104         struct rebind
00105         { typedef allocator<_Tp1> other; };
00106 
00107 #if __cplusplus >= 201103L
00108       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00109       // 2103. std::allocator propagate_on_container_move_assignment
00110       typedef true_type propagate_on_container_move_assignment;
00111 #endif
00112 
00113       allocator() throw() { }
00114 
00115       allocator(const allocator& __a) throw()
00116       : __allocator_base<_Tp>(__a) { }
00117 
00118       template<typename _Tp1>
00119         allocator(const allocator<_Tp1>&) throw() { }
00120 
00121       ~allocator() throw() { }
00122 
00123       // Inherit everything else.
00124     };
00125 
00126   template<typename _T1, typename _T2>
00127     inline bool
00128     operator==(const allocator<_T1>&, const allocator<_T2>&)
00129     { return true; }
00130 
00131   template<typename _Tp>
00132     inline bool
00133     operator==(const allocator<_Tp>&, const allocator<_Tp>&)
00134     { return true; }
00135 
00136   template<typename _T1, typename _T2>
00137     inline bool
00138     operator!=(const allocator<_T1>&, const allocator<_T2>&)
00139     { return false; }
00140 
00141   template<typename _Tp>
00142     inline bool
00143     operator!=(const allocator<_Tp>&, const allocator<_Tp>&)
00144     { return false; }
00145 
00146   /// @} group allocator
00147 
00148   // Inhibit implicit instantiations for required instantiations,
00149   // which are defined via explicit instantiations elsewhere.
00150 #if _GLIBCXX_EXTERN_TEMPLATE
00151   extern template class allocator<char>;
00152   extern template class allocator<wchar_t>;
00153 #endif
00154 
00155   // Undefine.
00156 #undef __allocator_base
00157 
00158   // To implement Option 3 of DR 431.
00159   template<typename _Alloc, bool = __is_empty(_Alloc)>
00160     struct __alloc_swap
00161     { static void _S_do_it(_Alloc&, _Alloc&) { } };
00162 
00163   template<typename _Alloc>
00164     struct __alloc_swap<_Alloc, false>
00165     {
00166       static void
00167       _S_do_it(_Alloc& __one, _Alloc& __two)
00168       {
00169     // Precondition: swappable allocators.
00170     if (__one != __two)
00171       swap(__one, __two);
00172       }
00173     };
00174 
00175   // Optimize for stateless allocators.
00176   template<typename _Alloc, bool = __is_empty(_Alloc)>
00177     struct __alloc_neq
00178     {
00179       static bool
00180       _S_do_it(const _Alloc&, const _Alloc&)
00181       { return false; }
00182     };
00183 
00184   template<typename _Alloc>
00185     struct __alloc_neq<_Alloc, false>
00186     {
00187       static bool
00188       _S_do_it(const _Alloc& __one, const _Alloc& __two)
00189       { return __one != __two; }
00190     };
00191 
00192 #if __cplusplus >= 201103L
00193   template<typename _Tp, bool
00194     = __or_<is_copy_constructible<typename _Tp::value_type>,
00195             is_nothrow_move_constructible<typename _Tp::value_type>>::value>
00196     struct __shrink_to_fit_aux
00197     { static bool _S_do_it(_Tp&) { return false; } };
00198 
00199   template<typename _Tp>
00200     struct __shrink_to_fit_aux<_Tp, true>
00201     {
00202       static bool
00203       _S_do_it(_Tp& __c)
00204       {
00205     __try
00206       {
00207         _Tp(__make_move_if_noexcept_iterator(__c.begin()),
00208         __make_move_if_noexcept_iterator(__c.end()),
00209         __c.get_allocator()).swap(__c);
00210         return true;
00211       }
00212     __catch(...)
00213       { return false; }
00214       }
00215     };
00216 #endif
00217 
00218 _GLIBCXX_END_NAMESPACE_VERSION
00219 } // namespace std
00220 
00221 #endif