Merge from mainline (168000:168310).
[official-gcc/graphite-test-results.git] / libstdc++-v3 / include / bits / allocator.h
blob43006799397bb9075250d96dac28d3b42125b98f
1 // Allocators -*- C++ -*-
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.
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.
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.
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/>.
27 * Copyright (c) 1996-1997
28 * Silicon Graphics Computer Systems, Inc.
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.
39 /** @file bits/allocator.h
40 * This is an internal header file, included by other library headers.
41 * Do not attempt to use it directly. @headername{memory}
44 #ifndef _ALLOCATOR_H
45 #define _ALLOCATOR_H 1
47 // Define the base class to std::allocator.
48 #include <bits/c++allocator.h>
50 #ifdef __GXX_EXPERIMENTAL_CXX0X__
51 #include <type_traits> // For _GLIBCXX_HAS_NESTED_TYPE
52 #endif
54 _GLIBCXX_BEGIN_NAMESPACE(std)
56 /**
57 * @defgroup allocators Allocators
58 * @ingroup memory
60 * Classes encapsulating memory operations.
63 template<typename _Tp>
64 class allocator;
66 /// allocator<void> specialization.
67 template<>
68 class allocator<void>
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;
77 template<typename _Tp1>
78 struct rebind
79 { typedef allocator<_Tp1> other; };
82 /**
83 * @brief The @a standard allocator, as per [20.4].
84 * @ingroup allocators
86 * Further details:
87 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt04ch11.html
89 template<typename _Tp>
90 class allocator: public __glibcxx_base_allocator<_Tp>
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;
101 template<typename _Tp1>
102 struct rebind
103 { typedef allocator<_Tp1> other; };
105 allocator() throw() { }
107 allocator(const allocator& __a) throw()
108 : __glibcxx_base_allocator<_Tp>(__a) { }
110 template<typename _Tp1>
111 allocator(const allocator<_Tp1>&) throw() { }
113 ~allocator() throw() { }
115 // Inherit everything else.
118 template<typename _T1, typename _T2>
119 inline bool
120 operator==(const allocator<_T1>&, const allocator<_T2>&)
121 { return true; }
123 template<typename _Tp>
124 inline bool
125 operator==(const allocator<_Tp>&, const allocator<_Tp>&)
126 { return true; }
128 template<typename _T1, typename _T2>
129 inline bool
130 operator!=(const allocator<_T1>&, const allocator<_T2>&)
131 { return false; }
133 template<typename _Tp>
134 inline bool
135 operator!=(const allocator<_Tp>&, const allocator<_Tp>&)
136 { return false; }
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
146 // Undefine.
147 #undef __glibcxx_base_allocator
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&) { } };
154 template<typename _Alloc>
155 struct __alloc_swap<_Alloc, false>
157 static void
158 _S_do_it(_Alloc& __one, _Alloc& __two)
160 // Precondition: swappable allocators.
161 if (__one != __two)
162 swap(__one, __two);
166 // Optimize for stateless allocators.
167 template<typename _Alloc, bool = __is_empty(_Alloc)>
168 struct __alloc_neq
170 static bool
171 _S_do_it(const _Alloc&, const _Alloc&)
172 { return false; }
175 template<typename _Alloc>
176 struct __alloc_neq<_Alloc, false>
178 static bool
179 _S_do_it(const _Alloc& __one, const _Alloc& __two)
180 { return __one != __two; }
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&) { } };
195 template<typename _Tp>
196 struct __shrink_to_fit<_Tp, true>
198 static void
199 _S_do_it(_Tp& __v)
201 __try
202 { _Tp(__v).swap(__v); }
203 __catch(...) { }
208 /// [allocator.tag]
209 struct allocator_arg_t { };
211 constexpr allocator_arg_t allocator_arg = allocator_arg_t();
213 _GLIBCXX_HAS_NESTED_TYPE(allocator_type)
215 template<typename _Tp, typename _Alloc,
216 bool = __has_allocator_type<_Tp>::value>
217 struct __uses_allocator_helper
218 : public false_type { };
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 { };
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 { };
233 #endif
235 _GLIBCXX_END_NAMESPACE
237 #endif