OSDN Git Service

2010-10-28 Paolo Carlini <paolo.carlini@oracle.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / allocator.h
1 // Allocators -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library.  This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 // <http://www.gnu.org/licenses/>.
25
26 /*
27  * Copyright (c) 1996-1997
28  * Silicon Graphics Computer Systems, Inc.
29  *
30  * Permission to use, copy, modify, distribute and sell this software
31  * and its documentation for any purpose is hereby granted without fee,
32  * provided that the above copyright notice appear in all copies and
33  * that both that copyright notice and this permission notice appear
34  * in supporting documentation.  Silicon Graphics makes no
35  * representations about the suitability of this software for any
36  * purpose.  It is provided "as is" without express or implied warranty.
37  */
38
39 /** @file allocator.h
40  *  This is an internal header file, included by other library headers.
41  *  You should not attempt to use it directly.
42  */
43
44 #ifndef _ALLOCATOR_H
45 #define _ALLOCATOR_H 1
46
47 // Define the base class to std::allocator.
48 #include <bits/c++allocator.h>
49
50 #ifdef __GXX_EXPERIMENTAL_CXX0X__
51 #include <type_traits> // For _GLIBCXX_HAS_NESTED_TYPE
52 #endif
53
54 _GLIBCXX_BEGIN_NAMESPACE(std)
55
56   /**
57    * @defgroup allocators Allocators
58    * @ingroup memory
59    *
60    * Classes encapsulating memory operations.
61    */
62
63   template<typename _Tp>
64     class allocator;
65
66   /// allocator<void> specialization.
67   template<>
68     class allocator<void>
69     {
70     public:
71       typedef size_t      size_type;
72       typedef ptrdiff_t   difference_type;
73       typedef void*       pointer;
74       typedef const void* const_pointer;
75       typedef void        value_type;
76
77       template<typename _Tp1>
78         struct rebind
79         { typedef allocator<_Tp1> other; };
80     };
81
82   /**
83    * @brief  The @a standard allocator, as per [20.4].
84    * @ingroup allocators
85    *
86    *  Further details:
87    *  http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt04ch11.html
88    */
89   template<typename _Tp>
90     class allocator: public __glibcxx_base_allocator<_Tp>
91     {
92    public:
93       typedef size_t     size_type;
94       typedef ptrdiff_t  difference_type;
95       typedef _Tp*       pointer;
96       typedef const _Tp* const_pointer;
97       typedef _Tp&       reference;
98       typedef const _Tp& const_reference;
99       typedef _Tp        value_type;
100
101       template<typename _Tp1>
102         struct rebind
103         { typedef allocator<_Tp1> other; };
104
105       allocator() throw() { }
106
107       allocator(const allocator& __a) throw()
108       : __glibcxx_base_allocator<_Tp>(__a) { }
109
110       template<typename _Tp1>
111         allocator(const allocator<_Tp1>&) throw() { }
112
113       ~allocator() throw() { }
114
115       // Inherit everything else.
116     };
117
118   template<typename _T1, typename _T2>
119     inline bool
120     operator==(const allocator<_T1>&, const allocator<_T2>&)
121     { return true; }
122
123   template<typename _Tp>
124     inline bool
125     operator==(const allocator<_Tp>&, const allocator<_Tp>&)
126     { return true; }
127
128   template<typename _T1, typename _T2>
129     inline bool
130     operator!=(const allocator<_T1>&, const allocator<_T2>&)
131     { return false; }
132
133   template<typename _Tp>
134     inline bool
135     operator!=(const allocator<_Tp>&, const allocator<_Tp>&)
136     { return false; }
137
138   // Inhibit implicit instantiations for required instantiations,
139   // which are defined via explicit instantiations elsewhere.
140   // NB: This syntax is a GNU extension.
141 #if _GLIBCXX_EXTERN_TEMPLATE
142   extern template class allocator<char>;
143   extern template class allocator<wchar_t>;
144 #endif
145
146   // Undefine.
147 #undef __glibcxx_base_allocator
148
149   // To implement Option 3 of DR 431.
150   template<typename _Alloc, bool = __is_empty(_Alloc)>
151     struct __alloc_swap
152     { static void _S_do_it(_Alloc&, _Alloc&) { } };
153
154   template<typename _Alloc>
155     struct __alloc_swap<_Alloc, false>
156     {
157       static void
158       _S_do_it(_Alloc& __one, _Alloc& __two)
159       {
160         // Precondition: swappable allocators.
161         if (__one != __two)
162           swap(__one, __two);
163       }
164     };
165
166   // Optimize for stateless allocators.
167   template<typename _Alloc, bool = __is_empty(_Alloc)>
168     struct __alloc_neq
169     {
170       static bool
171       _S_do_it(const _Alloc&, const _Alloc&)
172       { return false; }
173     };
174
175   template<typename _Alloc>
176     struct __alloc_neq<_Alloc, false>
177     {
178       static bool
179       _S_do_it(const _Alloc& __one, const _Alloc& __two)
180       { return __one != __two; }
181     };
182
183 #ifdef __GXX_EXPERIMENTAL_CXX0X__
184   // A very basic implementation for now.  In general we have to wait for
185   // the availability of the infrastructure described in N2983:  we should
186   // try when either T has a move constructor which cannot throw or T is
187   // CopyContructible.
188   // NB: This code doesn't properly belong here, we should find a more
189   // suited place common to std::vector and std::deque.
190   template<typename _Tp,
191            bool = __has_trivial_copy(typename _Tp::value_type)>
192     struct __shrink_to_fit
193     { static void _S_do_it(_Tp&) { } };
194
195   template<typename _Tp>
196     struct __shrink_to_fit<_Tp, true>
197     {
198       static void
199       _S_do_it(_Tp& __v)
200       {
201         __try
202           { _Tp(__v).swap(__v); }
203         __catch(...) { }
204       }
205     };
206
207
208   /// [allocator.tag]
209   struct allocator_arg_t { };
210
211   static const allocator_arg_t allocator_arg = allocator_arg_t();
212
213 _GLIBCXX_HAS_NESTED_TYPE(allocator_type)
214
215   template<typename _Tp, typename _Alloc,
216            bool = __has_allocator_type<_Tp>::value>
217     struct __uses_allocator_helper
218     : public false_type { };
219
220   template<typename _Tp, typename _Alloc>
221     struct __uses_allocator_helper<_Tp, _Alloc, true>
222     : public integral_constant<bool, is_convertible<_Alloc,
223                                      typename _Tp::allocator_type>::value>
224     { };
225
226   /// [allocator.uses.trait]
227   template<typename _Tp, typename _Alloc>
228     struct uses_allocator
229     : public integral_constant<bool,
230                                __uses_allocator_helper<_Tp, _Alloc>::value>
231     { };
232
233 #endif
234
235 _GLIBCXX_END_NAMESPACE
236
237 #endif