# allocator.h

Go to the documentation of this file.

    00001 // Allocators -*- C++ -*-
    00002
    00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
    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 #include <bits/cpp_type_traits.h> // for __is_empty
    00056
    00057 _GLIBCXX_BEGIN_NAMESPACE(std)
    00058
    00059   template<typename _Tp>
    00060     class allocator;
    00061
    00062   /// allocator<void> specialization.
    00063   template<>
    00064     class allocator<void>
    00065     {
    00066     public:
    00067       typedef size_t      size_type;
    00068       typedef ptrdiff_t   difference_type;
    00069       typedef void*       pointer;
    00070       typedef const void* const_pointer;
    00071       typedef void        value_type;
    00072
    00073       template<typename _Tp1>
    00074         struct rebind
    00075         { typedef allocator<_Tp1> other; };
    00076     };
    00077
    00078   /**
    00079    * @brief  The "standard" allocator, as per [20.4].
    00080    *
    00081    *  Further details:
    00082    *  http://gcc.gnu.org/onlinedocs/libstdc++/20_util/allocator.html
    00083    */
    00084   template<typename _Tp>
    00085     class allocator: public __glibcxx_base_allocator<_Tp>
    00086     {
    00087    public:
    00088       typedef size_t     size_type;
    00089       typedef ptrdiff_t  difference_type;
    00090       typedef _Tp*       pointer;
    00091       typedef const _Tp* const_pointer;
    00092       typedef _Tp&       reference;
    00093       typedef const _Tp& const_reference;
    00094       typedef _Tp        value_type;
    00095
    00096       template<typename _Tp1>
    00097         struct rebind
    00098         { typedef allocator<_Tp1> other; };
    00099
    00100       allocator() throw() { }
    00101
    00102       allocator(const allocator& __a) throw()
    00103       : __glibcxx_base_allocator<_Tp>(__a) { }
    00104
    00105       template<typename _Tp1>
    00106         allocator(const allocator<_Tp1>&) throw() { }
    00107
    00108       ~allocator() throw() { }
    00109
    00110       // Inherit everything else.
    00111     };
    00112
    00113   template<typename _T1, typename _T2>
    00114     inline bool
    00115     operator==(const allocator<_T1>&, const allocator<_T2>&)
    00116     { return true; }
    00117
    00118   template<typename _Tp>
    00119     inline bool
    00120     operator==(const allocator<_Tp>&, const allocator<_Tp>&)
    00121     { return true; }
    00122
    00123   template<typename _T1, typename _T2>
    00124     inline bool
    00125     operator!=(const allocator<_T1>&, const allocator<_T2>&)
    00126     { return false; }
    00127
    00128   template<typename _Tp>
    00129     inline bool
    00130     operator!=(const allocator<_Tp>&, const allocator<_Tp>&)
    00131     { return false; }
    00132
    00133   // Inhibit implicit instantiations for required instantiations,
    00134   // which are defined via explicit instantiations elsewhere.
    00135   // NB: This syntax is a GNU extension.
    00136 #if _GLIBCXX_EXTERN_TEMPLATE
    00137   extern template class allocator<char>;
    00138   extern template class allocator<wchar_t>;
    00139 #endif
    00140
    00141   // Undefine.
    00142 #undef __glibcxx_base_allocator
    00143
    00144   // To implement Option 3 of DR 431.
    00145   template<typename _Alloc, bool = std::__is_empty<_Alloc>::__value>
    00146     struct __alloc_swap
    00147     { static void _S_do_it(_Alloc&, _Alloc&) { } };
    00148
    00149   template<typename _Alloc>
    00150     struct __alloc_swap<_Alloc, false>
    00151     {
    00152       static void
    00153       _S_do_it(_Alloc& __one, _Alloc& __two)
    00154       {
    00155     // Precondition: swappable allocators.
    00156     if (__one != __two)
    00157       swap(__one, __two);
    00158       }
    00159     };
    00160
    00161 _GLIBCXX_END_NAMESPACE
    00162
    00163 #endif