allocator.h

Go to the documentation of this file.
00001 // Allocators -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
00004 // 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 2, 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 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 /*
00032  * Copyright (c) 1996-1997
00033  * Silicon Graphics Computer Systems, Inc.
00034  *
00035  * Permission to use, copy, modify, distribute and sell this software
00036  * and its documentation for any purpose is hereby granted without fee,
00037  * provided that the above copyright notice appear in all copies and
00038  * that both that copyright notice and this permission notice appear
00039  * in supporting documentation.  Silicon Graphics makes no
00040  * representations about the suitability of this software for any
00041  * purpose.  It is provided "as is" without express or implied warranty.
00042  */
00043 
00044 /** @file allocator.h
00045  *  This is an internal header file, included by other library headers.
00046  *  You should not attempt to use it directly.
00047  */
00048 
00049 #ifndef _ALLOCATOR_H
00050 #define _ALLOCATOR_H 1
00051 
00052 // Define the base class to std::allocator.
00053 #include <bits/c++allocator.h>
00054 
00055 _GLIBCXX_BEGIN_NAMESPACE(std)
00056 
00057   template<typename _Tp>
00058     class allocator;
00059 
00060   /// allocator<void> specialization.
00061   template<>
00062     class allocator<void>
00063     {
00064     public:
00065       typedef size_t      size_type;
00066       typedef ptrdiff_t   difference_type;
00067       typedef void*       pointer;
00068       typedef const void* const_pointer;
00069       typedef void        value_type;
00070 
00071       template<typename _Tp1>
00072         struct rebind
00073         { typedef allocator<_Tp1> other; };
00074     };
00075 
00076   /**
00077    * @brief  The "standard" allocator, as per [20.4].
00078    *
00079    *  Further details:
00080    *  http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt04ch11.html
00081    */
00082   template<typename _Tp>
00083     class allocator: public __glibcxx_base_allocator<_Tp>
00084     {
00085    public:
00086       typedef size_t     size_type;
00087       typedef ptrdiff_t  difference_type;
00088       typedef _Tp*       pointer;
00089       typedef const _Tp* const_pointer;
00090       typedef _Tp&       reference;
00091       typedef const _Tp& const_reference;
00092       typedef _Tp        value_type;
00093 
00094       template<typename _Tp1>
00095         struct rebind
00096         { typedef allocator<_Tp1> other; };
00097 
00098       allocator() throw() { }
00099 
00100       allocator(const allocator& __a) throw()
00101       : __glibcxx_base_allocator<_Tp>(__a) { }
00102 
00103       template<typename _Tp1>
00104         allocator(const allocator<_Tp1>&) throw() { }
00105 
00106       ~allocator() throw() { }
00107 
00108       // Inherit everything else.
00109     };
00110 
00111   template<typename _T1, typename _T2>
00112     inline bool
00113     operator==(const allocator<_T1>&, const allocator<_T2>&)
00114     { return true; }
00115 
00116   template<typename _Tp>
00117     inline bool
00118     operator==(const allocator<_Tp>&, const allocator<_Tp>&)
00119     { return true; }
00120 
00121   template<typename _T1, typename _T2>
00122     inline bool
00123     operator!=(const allocator<_T1>&, const allocator<_T2>&)
00124     { return false; }
00125 
00126   template<typename _Tp>
00127     inline bool
00128     operator!=(const allocator<_Tp>&, const allocator<_Tp>&)
00129     { return false; }
00130 
00131   // Inhibit implicit instantiations for required instantiations,
00132   // which are defined via explicit instantiations elsewhere.
00133   // NB: This syntax is a GNU extension.
00134 #if _GLIBCXX_EXTERN_TEMPLATE
00135   extern template class allocator<char>;
00136   extern template class allocator<wchar_t>;
00137 #endif
00138 
00139   // Undefine.
00140 #undef __glibcxx_base_allocator
00141 
00142   // To implement Option 3 of DR 431.
00143   template<typename _Alloc, bool = __is_empty(_Alloc)>
00144     struct __alloc_swap
00145     { static void _S_do_it(_Alloc&, _Alloc&) { } };
00146 
00147   template<typename _Alloc>
00148     struct __alloc_swap<_Alloc, false>
00149     {
00150       static void
00151       _S_do_it(_Alloc& __one, _Alloc& __two)
00152       {
00153     // Precondition: swappable allocators.
00154     if (__one != __two)
00155       swap(__one, __two);
00156       }
00157     };
00158 
00159   // Optimize for stateless allocators.
00160   template<typename _Alloc, bool = __is_empty(_Alloc)>
00161     struct __alloc_neq
00162     {
00163       static bool
00164       _S_do_it(const _Alloc&, const _Alloc&)
00165       { return false; }
00166     };
00167 
00168   template<typename _Alloc>
00169     struct __alloc_neq<_Alloc, false>
00170     {
00171       static bool
00172       _S_do_it(const _Alloc& __one, const _Alloc& __two)
00173       { return __one != __two; }
00174     };
00175 
00176 _GLIBCXX_END_NAMESPACE
00177 
00178 #endif

Generated on Wed Mar 26 00:42:53 2008 for libstdc++ by  doxygen 1.5.1