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 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 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00051 #include <type_traits> // For _GLIBCXX_HAS_NESTED_TYPE
00052 #endif
00053 
00054 namespace std _GLIBCXX_VISIBILITY(default)
00055 {
00056 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00057 
00058   /**
00059    * @defgroup allocators Allocators
00060    * @ingroup memory
00061    *
00062    * Classes encapsulating memory operations.
00063    */
00064 
00065   template<typename _Tp>
00066     class allocator;
00067 
00068   /// allocator<void> specialization.
00069   template<>
00070     class allocator<void>
00071     {
00072     public:
00073       typedef size_t      size_type;
00074       typedef ptrdiff_t   difference_type;
00075       typedef void*       pointer;
00076       typedef const void* const_pointer;
00077       typedef void        value_type;
00078 
00079       template<typename _Tp1>
00080         struct rebind
00081         { typedef allocator<_Tp1> other; };
00082     };
00083 
00084   /**
00085    * @brief  The @a standard allocator, as per [20.4].
00086    * @ingroup allocators
00087    *
00088    *  Further details:
00089    *  http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt04ch11.html
00090    */
00091   template<typename _Tp>
00092     class allocator: public __glibcxx_base_allocator<_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       allocator() throw() { }
00108 
00109       allocator(const allocator& __a) throw()
00110       : __glibcxx_base_allocator<_Tp>(__a) { }
00111 
00112       template<typename _Tp1>
00113         allocator(const allocator<_Tp1>&) throw() { }
00114 
00115       ~allocator() throw() { }
00116 
00117       // Inherit everything else.
00118     };
00119 
00120   template<typename _T1, typename _T2>
00121     inline bool
00122     operator==(const allocator<_T1>&, const allocator<_T2>&)
00123     { return true; }
00124 
00125   template<typename _Tp>
00126     inline bool
00127     operator==(const allocator<_Tp>&, const allocator<_Tp>&)
00128     { return true; }
00129 
00130   template<typename _T1, typename _T2>
00131     inline bool
00132     operator!=(const allocator<_T1>&, const allocator<_T2>&)
00133     { return false; }
00134 
00135   template<typename _Tp>
00136     inline bool
00137     operator!=(const allocator<_Tp>&, const allocator<_Tp>&)
00138     { return false; }
00139 
00140   // Inhibit implicit instantiations for required instantiations,
00141   // which are defined via explicit instantiations elsewhere.
00142 #if _GLIBCXX_EXTERN_TEMPLATE
00143   extern template class allocator<char>;
00144   extern template class allocator<wchar_t>;
00145 #endif
00146 
00147   // Undefine.
00148 #undef __glibcxx_base_allocator
00149 
00150   // To implement Option 3 of DR 431.
00151   template<typename _Alloc, bool = __is_empty(_Alloc)>
00152     struct __alloc_swap
00153     { static void _S_do_it(_Alloc&, _Alloc&) { } };
00154 
00155   template<typename _Alloc>
00156     struct __alloc_swap<_Alloc, false>
00157     {
00158       static void
00159       _S_do_it(_Alloc& __one, _Alloc& __two)
00160       {
00161     // Precondition: swappable allocators.
00162     if (__one != __two)
00163       swap(__one, __two);
00164       }
00165     };
00166 
00167   // Optimize for stateless allocators.
00168   template<typename _Alloc, bool = __is_empty(_Alloc)>
00169     struct __alloc_neq
00170     {
00171       static bool
00172       _S_do_it(const _Alloc&, const _Alloc&)
00173       { return false; }
00174     };
00175 
00176   template<typename _Alloc>
00177     struct __alloc_neq<_Alloc, false>
00178     {
00179       static bool
00180       _S_do_it(const _Alloc& __one, const _Alloc& __two)
00181       { return __one != __two; }
00182     };
00183 
00184 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00185   // A very basic implementation for now.  In general we have to wait for
00186   // the availability of the infrastructure described in N2983:  we should
00187   // try when either T has a move constructor which cannot throw or T is
00188   // CopyContructible.
00189   // NB: This code doesn't properly belong here, we should find a more
00190   // suited place common to std::vector and std::deque.
00191   template<typename _Tp,
00192        bool = __has_trivial_copy(typename _Tp::value_type)>
00193     struct __shrink_to_fit
00194     { static void _S_do_it(_Tp&) { } };
00195 
00196   template<typename _Tp>
00197     struct __shrink_to_fit<_Tp, true>
00198     {
00199       static void
00200       _S_do_it(_Tp& __v)
00201       {
00202     __try
00203       { _Tp(__v).swap(__v); }
00204     __catch(...) { }
00205       }
00206     };
00207 
00208 
00209   /// [allocator.tag]
00210   struct allocator_arg_t { };
00211 
00212   constexpr allocator_arg_t allocator_arg = allocator_arg_t();
00213 
00214 _GLIBCXX_HAS_NESTED_TYPE(allocator_type)
00215 
00216   template<typename _Tp, typename _Alloc,
00217        bool = __has_allocator_type<_Tp>::value>
00218     struct __uses_allocator_helper
00219     : public false_type { };
00220 
00221   template<typename _Tp, typename _Alloc>
00222     struct __uses_allocator_helper<_Tp, _Alloc, true>
00223     : public integral_constant<bool, is_convertible<_Alloc,
00224                      typename _Tp::allocator_type>::value>
00225     { };
00226 
00227   /// [allocator.uses.trait]
00228   template<typename _Tp, typename _Alloc>
00229     struct uses_allocator
00230     : public integral_constant<bool,
00231                    __uses_allocator_helper<_Tp, _Alloc>::value>
00232     { };
00233 
00234 #endif
00235 
00236 _GLIBCXX_END_NAMESPACE_VERSION
00237 } // namespace std
00238 
00239 #endif