libstdc++
allocator.h
Go to the documentation of this file.
00001 // Allocators -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
00004 // 2011, 2012 Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 3, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // Under Section 7 of GPL version 3, you are granted additional
00018 // permissions described in the GCC Runtime Library Exception, version
00019 // 3.1, as published by the Free Software Foundation.
00020 
00021 // You should have received a copy of the GNU General Public License and
00022 // a copy of the GCC Runtime Library Exception along with this program;
00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00024 // <http://www.gnu.org/licenses/>.
00025 
00026 /*
00027  * Copyright (c) 1996-1997
00028  * Silicon Graphics Computer Systems, Inc.
00029  *
00030  * Permission to use, copy, modify, distribute and sell this software
00031  * and its documentation for any purpose is hereby granted without fee,
00032  * provided that the above copyright notice appear in all copies and
00033  * that both that copyright notice and this permission notice appear
00034  * in supporting documentation.  Silicon Graphics makes no
00035  * representations about the suitability of this software for any
00036  * purpose.  It is provided "as is" without express or implied warranty.
00037  */
00038 
00039 /** @file bits/allocator.h
00040  *  This is an internal header file, included by other library headers.
00041  *  Do not attempt to use it directly. @headername{memory}
00042  */
00043 
00044 #ifndef _ALLOCATOR_H
00045 #define _ALLOCATOR_H 1
00046 
00047 // Define the base class to std::allocator.
00048 #include <bits/c++allocator.h>
00049 
00050 namespace std _GLIBCXX_VISIBILITY(default)
00051 {
00052 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00053 
00054   /**
00055    * @defgroup allocators Allocators
00056    * @ingroup memory
00057    *
00058    * Classes encapsulating memory operations.
00059    *
00060    * @{
00061    */
00062 
00063   template<typename _Tp>
00064     class allocator;
00065 
00066   /// allocator<void> specialization.
00067   template<>
00068     class allocator<void>
00069     {
00070     public:
00071       typedef size_t      size_type;
00072       typedef ptrdiff_t   difference_type;
00073       typedef void*       pointer;
00074       typedef const void* const_pointer;
00075       typedef void        value_type;
00076 
00077       template<typename _Tp1>
00078         struct rebind
00079         { typedef allocator<_Tp1> other; };
00080     };
00081 
00082   /**
00083    * @brief  The @a standard allocator, as per [20.4].
00084    *
00085    *  See http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt04ch11.html
00086    *  for further details.
00087    *
00088    *  @tparam  _Tp  Type of allocated object.
00089    */
00090   template<typename _Tp>
00091     class allocator: public __allocator_base<_Tp>
00092     {
00093    public:
00094       typedef size_t     size_type;
00095       typedef ptrdiff_t  difference_type;
00096       typedef _Tp*       pointer;
00097       typedef const _Tp* const_pointer;
00098       typedef _Tp&       reference;
00099       typedef const _Tp& const_reference;
00100       typedef _Tp        value_type;
00101 
00102       template<typename _Tp1>
00103         struct rebind
00104         { typedef allocator<_Tp1> other; };
00105 
00106       allocator() throw() { }
00107 
00108       allocator(const allocator& __a) throw()
00109       : __allocator_base<_Tp>(__a) { }
00110 
00111       template<typename _Tp1>
00112         allocator(const allocator<_Tp1>&) throw() { }
00113 
00114       ~allocator() throw() { }
00115 
00116       // Inherit everything else.
00117     };
00118 
00119   template<typename _T1, typename _T2>
00120     inline bool
00121     operator==(const allocator<_T1>&, const allocator<_T2>&)
00122     { return true; }
00123 
00124   template<typename _Tp>
00125     inline bool
00126     operator==(const allocator<_Tp>&, const allocator<_Tp>&)
00127     { return true; }
00128 
00129   template<typename _T1, typename _T2>
00130     inline bool
00131     operator!=(const allocator<_T1>&, const allocator<_T2>&)
00132     { return false; }
00133 
00134   template<typename _Tp>
00135     inline bool
00136     operator!=(const allocator<_Tp>&, const allocator<_Tp>&)
00137     { return false; }
00138 
00139   /// Declare uses_allocator so it can be specialized in <queue> etc.
00140   template<typename, typename>
00141     struct uses_allocator;
00142 
00143   /**
00144    * @}
00145    */
00146 
00147   // Inhibit implicit instantiations for required instantiations,
00148   // which are defined via explicit instantiations elsewhere.
00149 #if _GLIBCXX_EXTERN_TEMPLATE
00150   extern template class allocator<char>;
00151   extern template class allocator<wchar_t>;
00152 #endif
00153 
00154   // Undefine.
00155 #undef __allocator_base
00156 
00157   // To implement Option 3 of DR 431.
00158   template<typename _Alloc, bool = __is_empty(_Alloc)>
00159     struct __alloc_swap
00160     { static void _S_do_it(_Alloc&, _Alloc&) { } };
00161 
00162   template<typename _Alloc>
00163     struct __alloc_swap<_Alloc, false>
00164     {
00165       static void
00166       _S_do_it(_Alloc& __one, _Alloc& __two)
00167       {
00168     // Precondition: swappable allocators.
00169     if (__one != __two)
00170       swap(__one, __two);
00171       }
00172     };
00173 
00174   // Optimize for stateless allocators.
00175   template<typename _Alloc, bool = __is_empty(_Alloc)>
00176     struct __alloc_neq
00177     {
00178       static bool
00179       _S_do_it(const _Alloc&, const _Alloc&)
00180       { return false; }
00181     };
00182 
00183   template<typename _Alloc>
00184     struct __alloc_neq<_Alloc, false>
00185     {
00186       static bool
00187       _S_do_it(const _Alloc& __one, const _Alloc& __two)
00188       { return __one != __two; }
00189     };
00190 
00191 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00192   template<typename _Tp, bool
00193     = __or_<is_copy_constructible<typename _Tp::value_type>,
00194             is_nothrow_move_constructible<typename _Tp::value_type>>::value>
00195     struct __shrink_to_fit_aux
00196     { static bool _S_do_it(_Tp&) { return false; } };
00197 
00198   template<typename _Tp>
00199     struct __shrink_to_fit_aux<_Tp, true>
00200     {
00201       static bool
00202       _S_do_it(_Tp& __c)
00203       {
00204     __try
00205       {
00206         _Tp(__make_move_if_noexcept_iterator(__c.begin()),
00207         __make_move_if_noexcept_iterator(__c.end()),
00208         __c.get_allocator()).swap(__c);
00209         return true;
00210       }
00211     __catch(...)
00212       { return false; }
00213       }
00214     };
00215 #endif
00216 
00217 _GLIBCXX_END_NAMESPACE_VERSION
00218 } // namespace std
00219 
00220 #endif