libstdc++
extptr_allocator.h
Go to the documentation of this file.
00001 // <extptr_allocator.h> -*- C++ -*-
00002 
00003 // Copyright (C) 2008, 2009, 2010 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  *  @file ext/extptr_allocator.h
00027  *  This file is a GNU extension to the Standard C++ Library.
00028  *
00029  *  @author Bob Walters
00030  *
00031  * An example allocator which uses an alternative pointer type from
00032  * bits/pointer.h.  Supports test cases which confirm container support
00033  * for alternative pointers.
00034  */
00035 
00036 #ifndef _EXTPTR_ALLOCATOR_H
00037 #define _EXTPTR_ALLOCATOR_H 1
00038 
00039 #include <memory>
00040 #include <limits>
00041 #include <ext/pointer.h>
00042 
00043 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
00044 {
00045 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00046 
00047   /**
00048    * @brief An example allocator which uses a non-standard pointer type.
00049    * @ingroup allocators
00050    *
00051    * This allocator specifies that containers use a 'relative pointer' as it's
00052    * pointer type.  (See ext/pointer.h)  Memory allocation in this example
00053    * is still performed using std::allocator.
00054    */
00055   template<typename _Tp>
00056     class _ExtPtr_allocator
00057     {
00058     public:
00059       typedef std::size_t     size_type;
00060       typedef std::ptrdiff_t  difference_type;
00061 
00062       // Note the non-standard pointer types.
00063       typedef _Pointer_adapter<_Relative_pointer_impl<_Tp> >       pointer;
00064       typedef _Pointer_adapter<_Relative_pointer_impl<const _Tp> > 
00065                                                              const_pointer;
00066 
00067       typedef _Tp&       reference;
00068       typedef const _Tp& const_reference;
00069       typedef _Tp        value_type;
00070 
00071       template<typename _Up>
00072         struct rebind
00073         { typedef _ExtPtr_allocator<_Up> other; };
00074 
00075       _ExtPtr_allocator() throw() 
00076       : _M_real_alloc() { }
00077 
00078       _ExtPtr_allocator(const _ExtPtr_allocator &__rarg) throw()
00079       : _M_real_alloc(__rarg._M_real_alloc) { }
00080 
00081       template<typename _Up>
00082         _ExtPtr_allocator(const _ExtPtr_allocator<_Up>& __rarg) throw()
00083         : _M_real_alloc(__rarg._M_getUnderlyingImp()) { }
00084 
00085       ~_ExtPtr_allocator() throw()
00086       { }
00087 
00088       pointer address(reference __x) const
00089       { return std::__addressof(__x); }
00090 
00091       const_pointer address(const_reference __x) const
00092       { return std::__addressof(__x); }
00093 
00094       pointer allocate(size_type __n, void* __hint = 0)
00095       { return _M_real_alloc.allocate(__n,__hint); }
00096 
00097       void deallocate(pointer __p, size_type __n)
00098       { _M_real_alloc.deallocate(__p.get(), __n); }
00099 
00100       size_type max_size() const throw()
00101       { return std::numeric_limits<size_type>::max() / sizeof(_Tp); }
00102 
00103       void construct(pointer __p, const _Tp& __val)
00104       { ::new(__p.get()) _Tp(__val); }
00105 
00106 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00107       template<typename... _Args>
00108         void
00109         construct(pointer __p, _Args&&... __args)
00110         { ::new(__p.get()) _Tp(std::forward<_Args>(__args)...); }
00111 #endif
00112 
00113       void destroy(pointer __p)
00114       { __p->~_Tp(); }
00115 
00116       template<typename _Up>
00117         inline bool
00118         operator==(const _ExtPtr_allocator<_Up>& __rarg)
00119         { return _M_real_alloc == __rarg._M_getUnderlyingImp(); }
00120 
00121       inline bool
00122       operator==(const _ExtPtr_allocator& __rarg)
00123       { return _M_real_alloc == __rarg._M_real_alloc; }
00124 
00125       template<typename _Up>
00126         inline bool
00127         operator!=(const _ExtPtr_allocator<_Up>& __rarg)
00128         { return _M_real_alloc != __rarg._M_getUnderlyingImp(); }
00129 
00130       inline bool
00131       operator!=(const _ExtPtr_allocator& __rarg)
00132       { return _M_real_alloc != __rarg._M_real_alloc; }
00133 
00134       template<typename _Up>
00135         inline friend void
00136         swap(_ExtPtr_allocator<_Up>&, _ExtPtr_allocator<_Up>&);
00137 
00138       // A method specific to this implementation.
00139       const std::allocator<_Tp>&
00140       _M_getUnderlyingImp() const
00141       { return _M_real_alloc; }
00142 
00143     private:
00144       std::allocator<_Tp>  _M_real_alloc;
00145     };
00146 
00147   // _ExtPtr_allocator<void> specialization.
00148   template<>
00149     class _ExtPtr_allocator<void>
00150     {
00151     public:
00152       typedef std::size_t      size_type;
00153       typedef std::ptrdiff_t   difference_type;
00154       typedef void             value_type;
00155 
00156       // Note the non-standard pointer types
00157       typedef _Pointer_adapter<_Relative_pointer_impl<void> >       pointer;
00158       typedef _Pointer_adapter<_Relative_pointer_impl<const void> >
00159                                                               const_pointer;
00160 
00161       template<typename _Up>
00162         struct rebind
00163         { typedef _ExtPtr_allocator<_Up> other; };
00164 
00165     private:
00166       std::allocator<void>  _M_real_alloc;
00167     };
00168 
00169   template<typename _Tp>
00170     inline void
00171     swap(_ExtPtr_allocator<_Tp>& __larg, _ExtPtr_allocator<_Tp>& __rarg)
00172     {
00173       std::allocator<_Tp> __tmp( __rarg._M_real_alloc );
00174       __rarg._M_real_alloc = __larg._M_real_alloc;
00175       __larg._M_real_alloc = __tmp;
00176     }
00177 
00178 _GLIBCXX_END_NAMESPACE_VERSION
00179 } // namespace
00180 
00181 #endif /* _EXTPTR_ALLOCATOR_H */