OSDN Git Service

2011-01-14 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / ext / extptr_allocator.h
1 // <extptr_allocator.h> -*- C++ -*-
2
3 // Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /**
26  *  @file ext/extptr_allocator.h
27  *  This file is a GNU extension to the Standard C++ Library.
28  *
29  *  @author Bob Walters
30  *
31  * An example allocator which uses an alternative pointer type from
32  * bits/pointer.h.  Supports test cases which confirm container support
33  * for alternative pointers.
34  */
35
36 #ifndef _EXTPTR_ALLOCATOR_H
37 #define _EXTPTR_ALLOCATOR_H 1
38
39 #include <memory>
40 #include <limits>
41 #include <ext/pointer.h>
42
43 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
44
45   /**
46    * @brief An example allocator which uses a non-standard pointer type.
47    * @ingroup allocators
48    *
49    * This allocator specifies that containers use a 'relative pointer' as it's
50    * pointer type.  (See ext/pointer.h)  Memory allocation in this example
51    * is still performed using std::allocator.
52    */
53   template<typename _Tp>
54     class _ExtPtr_allocator
55     {
56     public:
57       typedef std::size_t     size_type;
58       typedef std::ptrdiff_t  difference_type;
59
60       // Note the non-standard pointer types.
61       typedef _Pointer_adapter<_Relative_pointer_impl<_Tp> >       pointer;
62       typedef _Pointer_adapter<_Relative_pointer_impl<const _Tp> > 
63                                                              const_pointer;
64
65       typedef _Tp&       reference;
66       typedef const _Tp& const_reference;
67       typedef _Tp        value_type;
68
69       template<typename _Up>
70         struct rebind
71         { typedef _ExtPtr_allocator<_Up> other; };
72
73       _ExtPtr_allocator() throw() 
74       : _M_real_alloc() { }
75
76       _ExtPtr_allocator(const _ExtPtr_allocator &__rarg) throw()
77       : _M_real_alloc(__rarg._M_real_alloc) { }
78
79       template<typename _Up>
80         _ExtPtr_allocator(const _ExtPtr_allocator<_Up>& __rarg) throw()
81         : _M_real_alloc(__rarg._M_getUnderlyingImp()) { }
82
83       ~_ExtPtr_allocator() throw()
84       { }
85
86       pointer address(reference __x) const
87       { return std::__addressof(__x); }
88
89       const_pointer address(const_reference __x) const
90       { return std::__addressof(__x); }
91
92       pointer allocate(size_type __n, void* __hint = 0)
93       { return _M_real_alloc.allocate(__n,__hint); }
94
95       void deallocate(pointer __p, size_type __n)
96       { _M_real_alloc.deallocate(__p.get(), __n); }
97
98       size_type max_size() const throw()
99       { return std::numeric_limits<size_type>::max() / sizeof(_Tp); }
100
101       void construct(pointer __p, const _Tp& __val)
102       { ::new(__p.get()) _Tp(__val); }
103
104 #ifdef __GXX_EXPERIMENTAL_CXX0X__
105       template<typename... _Args>
106         void
107         construct(pointer __p, _Args&&... __args)
108         { ::new(__p.get()) _Tp(std::forward<_Args>(__args)...); }
109 #endif
110
111       void destroy(pointer __p)
112       { __p->~_Tp(); }
113
114       template<typename _Up>
115         inline bool
116         operator==(const _ExtPtr_allocator<_Up>& __rarg)
117         { return _M_real_alloc == __rarg._M_getUnderlyingImp(); }
118
119       inline bool
120       operator==(const _ExtPtr_allocator& __rarg)
121       { return _M_real_alloc == __rarg._M_real_alloc; }
122
123       template<typename _Up>
124         inline bool
125         operator!=(const _ExtPtr_allocator<_Up>& __rarg)
126         { return _M_real_alloc != __rarg._M_getUnderlyingImp(); }
127
128       inline bool
129       operator!=(const _ExtPtr_allocator& __rarg)
130       { return _M_real_alloc != __rarg._M_real_alloc; }
131
132       template<typename _Up>
133         inline friend void
134         swap(_ExtPtr_allocator<_Up>&, _ExtPtr_allocator<_Up>&);
135
136       // A method specific to this implementation.
137       const std::allocator<_Tp>&
138       _M_getUnderlyingImp() const
139       { return _M_real_alloc; }
140
141     private:
142       std::allocator<_Tp>  _M_real_alloc;
143     };
144
145   // _ExtPtr_allocator<void> specialization.
146   template<>
147     class _ExtPtr_allocator<void>
148     {
149     public:
150       typedef std::size_t      size_type;
151       typedef std::ptrdiff_t   difference_type;
152       typedef void             value_type;
153
154       // Note the non-standard pointer types
155       typedef _Pointer_adapter<_Relative_pointer_impl<void> >       pointer;
156       typedef _Pointer_adapter<_Relative_pointer_impl<const void> >
157                                                               const_pointer;
158
159       template<typename _Up>
160         struct rebind
161         { typedef _ExtPtr_allocator<_Up> other; };
162
163     private:
164       std::allocator<void>  _M_real_alloc;
165     };
166
167   template<typename _Tp>
168     inline void
169     swap(_ExtPtr_allocator<_Tp>& __larg, _ExtPtr_allocator<_Tp>& __rarg)
170     {
171       std::allocator<_Tp> __tmp( __rarg._M_real_alloc );
172       __rarg._M_real_alloc = __larg._M_real_alloc;
173       __larg._M_real_alloc = __tmp;
174     }
175
176 _GLIBCXX_END_NAMESPACE
177
178 #endif /* _EXTPTR_ALLOCATOR_H */