2012-11-20 Matthias Klose <doko@ubuntu.com>
[official-gcc.git] / libstdc++-v3 / include / bits / allocator.h
blob4bfabac0ced3e6a316a7f82d45291b5b08b5a60b
1 // Allocators -*- C++ -*-
3 // Copyright (C) 2001-2012 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.
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.
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.
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/>.
26 * Copyright (c) 1996-1997
27 * Silicon Graphics Computer Systems, Inc.
29 * Permission to use, copy, modify, distribute and sell this software
30 * and its documentation for any purpose is hereby granted without fee,
31 * provided that the above copyright notice appear in all copies and
32 * that both that copyright notice and this permission notice appear
33 * in supporting documentation. Silicon Graphics makes no
34 * representations about the suitability of this software for any
35 * purpose. It is provided "as is" without express or implied warranty.
38 /** @file bits/allocator.h
39 * This is an internal header file, included by other library headers.
40 * Do not attempt to use it directly. @headername{memory}
43 #ifndef _ALLOCATOR_H
44 #define _ALLOCATOR_H 1
46 // Define the base class to std::allocator.
47 #include <bits/c++allocator.h>
48 #if __cplusplus >= 201103L
49 #include <type_traits>
50 #endif
52 namespace std _GLIBCXX_VISIBILITY(default)
54 _GLIBCXX_BEGIN_NAMESPACE_VERSION
56 /**
57 * @defgroup allocators Allocators
58 * @ingroup memory
60 * Classes encapsulating memory operations.
62 * @{
65 template<typename _Tp>
66 class allocator;
68 /// allocator<void> specialization.
69 template<>
70 class allocator<void>
72 public:
73 typedef size_t size_type;
74 typedef ptrdiff_t difference_type;
75 typedef void* pointer;
76 typedef const void* const_pointer;
77 typedef void value_type;
79 template<typename _Tp1>
80 struct rebind
81 { typedef allocator<_Tp1> other; };
83 #if __cplusplus >= 201103L
84 // _GLIBCXX_RESOLVE_LIB_DEFECTS
85 // 2103. std::allocator propagate_on_container_move_assignment
86 typedef true_type propagate_on_container_move_assignment;
87 #endif
90 /**
91 * @brief The @a standard allocator, as per [20.4].
93 * See http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt04ch11.html
94 * for further details.
96 * @tparam _Tp Type of allocated object.
98 template<typename _Tp>
99 class allocator: public __allocator_base<_Tp>
101 public:
102 typedef size_t size_type;
103 typedef ptrdiff_t difference_type;
104 typedef _Tp* pointer;
105 typedef const _Tp* const_pointer;
106 typedef _Tp& reference;
107 typedef const _Tp& const_reference;
108 typedef _Tp value_type;
110 template<typename _Tp1>
111 struct rebind
112 { typedef allocator<_Tp1> other; };
114 #if __cplusplus >= 201103L
115 // _GLIBCXX_RESOLVE_LIB_DEFECTS
116 // 2103. std::allocator propagate_on_container_move_assignment
117 typedef true_type propagate_on_container_move_assignment;
118 #endif
120 allocator() throw() { }
122 allocator(const allocator& __a) throw()
123 : __allocator_base<_Tp>(__a) { }
125 template<typename _Tp1>
126 allocator(const allocator<_Tp1>&) throw() { }
128 ~allocator() throw() { }
130 // Inherit everything else.
133 template<typename _T1, typename _T2>
134 inline bool
135 operator==(const allocator<_T1>&, const allocator<_T2>&)
136 { return true; }
138 template<typename _Tp>
139 inline bool
140 operator==(const allocator<_Tp>&, const allocator<_Tp>&)
141 { return true; }
143 template<typename _T1, typename _T2>
144 inline bool
145 operator!=(const allocator<_T1>&, const allocator<_T2>&)
146 { return false; }
148 template<typename _Tp>
149 inline bool
150 operator!=(const allocator<_Tp>&, const allocator<_Tp>&)
151 { return false; }
153 /// Declare uses_allocator so it can be specialized in \<queue\> etc.
154 template<typename, typename>
155 struct uses_allocator;
158 * @}
161 // Inhibit implicit instantiations for required instantiations,
162 // which are defined via explicit instantiations elsewhere.
163 #if _GLIBCXX_EXTERN_TEMPLATE
164 extern template class allocator<char>;
165 extern template class allocator<wchar_t>;
166 #endif
168 // Undefine.
169 #undef __allocator_base
171 // To implement Option 3 of DR 431.
172 template<typename _Alloc, bool = __is_empty(_Alloc)>
173 struct __alloc_swap
174 { static void _S_do_it(_Alloc&, _Alloc&) { } };
176 template<typename _Alloc>
177 struct __alloc_swap<_Alloc, false>
179 static void
180 _S_do_it(_Alloc& __one, _Alloc& __two)
182 // Precondition: swappable allocators.
183 if (__one != __two)
184 swap(__one, __two);
188 // Optimize for stateless allocators.
189 template<typename _Alloc, bool = __is_empty(_Alloc)>
190 struct __alloc_neq
192 static bool
193 _S_do_it(const _Alloc&, const _Alloc&)
194 { return false; }
197 template<typename _Alloc>
198 struct __alloc_neq<_Alloc, false>
200 static bool
201 _S_do_it(const _Alloc& __one, const _Alloc& __two)
202 { return __one != __two; }
205 #if __cplusplus >= 201103L
206 template<typename _Tp, bool
207 = __or_<is_copy_constructible<typename _Tp::value_type>,
208 is_nothrow_move_constructible<typename _Tp::value_type>>::value>
209 struct __shrink_to_fit_aux
210 { static bool _S_do_it(_Tp&) { return false; } };
212 template<typename _Tp>
213 struct __shrink_to_fit_aux<_Tp, true>
215 static bool
216 _S_do_it(_Tp& __c)
218 __try
220 _Tp(__make_move_if_noexcept_iterator(__c.begin()),
221 __make_move_if_noexcept_iterator(__c.end()),
222 __c.get_allocator()).swap(__c);
223 return true;
225 __catch(...)
226 { return false; }
229 #endif
231 _GLIBCXX_END_NAMESPACE_VERSION
232 } // namespace std
234 #endif