libstdc++
ext/alloc_traits.h
Go to the documentation of this file.
1 // Allocator traits -*- C++ -*-
2 
3 // Copyright (C) 2011-2013 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 /** @file ext/alloc_traits.h
26  * This file is a GNU extension to the Standard C++ Library.
27  */
28 
29 #ifndef _EXT_ALLOC_TRAITS_H
30 #define _EXT_ALLOC_TRAITS_H 1
31 
32 #pragma GCC system_header
33 
34 #if __cplusplus >= 201103L
35 # include <bits/move.h>
36 # include <bits/alloc_traits.h>
37 #else
38 # include <bits/allocator.h> // for __alloc_swap
39 #endif
40 
41 namespace std _GLIBCXX_VISIBILITY(default)
42 {
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44  template<typename> struct allocator;
45 _GLIBCXX_END_NAMESPACE_VERSION
46 } // namespace
47 
48 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
49 {
50 _GLIBCXX_BEGIN_NAMESPACE_VERSION
51 
52 #if __cplusplus >= 201103L
53  template<typename _Alloc>
54  struct __allocator_always_compares_equal
55  { static const bool value = false; };
56 
57  template<typename _Alloc>
58  const bool __allocator_always_compares_equal<_Alloc>::value;
59 
60  template<typename _Tp>
61  struct __allocator_always_compares_equal<std::allocator<_Tp>>
62  { static const bool value = true; };
63 
64  template<typename _Tp>
65  const bool __allocator_always_compares_equal<std::allocator<_Tp>>::value;
66 
67  template<typename, typename> struct array_allocator;
68 
69  template<typename _Tp, typename _Array>
70  struct __allocator_always_compares_equal<array_allocator<_Tp, _Array>>
71  { static const bool value = true; };
72 
73  template<typename _Tp, typename _Array>
74  const bool
75  __allocator_always_compares_equal<array_allocator<_Tp, _Array>>::value;
76 
77  template<typename> struct bitmap_allocator;
78 
79  template<typename _Tp>
80  struct __allocator_always_compares_equal<bitmap_allocator<_Tp>>
81  { static const bool value = true; };
82 
83  template<typename _Tp>
84  const bool __allocator_always_compares_equal<bitmap_allocator<_Tp>>::value;
85 
86  template<typename> struct malloc_allocator;
87 
88  template<typename _Tp>
89  struct __allocator_always_compares_equal<malloc_allocator<_Tp>>
90  { static const bool value = true; };
91 
92  template<typename _Tp>
93  const bool __allocator_always_compares_equal<malloc_allocator<_Tp>>::value;
94 
95  template<typename> struct mt_allocator;
96 
97  template<typename _Tp>
98  struct __allocator_always_compares_equal<mt_allocator<_Tp>>
99  { static const bool value = true; };
100 
101  template<typename _Tp>
102  const bool __allocator_always_compares_equal<mt_allocator<_Tp>>::value;
103 
104  template<typename> struct new_allocator;
105 
106  template<typename _Tp>
107  struct __allocator_always_compares_equal<new_allocator<_Tp>>
108  { static const bool value = true; };
109 
110  template<typename _Tp>
111  const bool __allocator_always_compares_equal<new_allocator<_Tp>>::value;
112 
113  template<typename> struct pool_allocator;
114 
115  template<typename _Tp>
116  struct __allocator_always_compares_equal<pool_allocator<_Tp>>
117  { static const bool value = true; };
118 
119  template<typename _Tp>
120  const bool __allocator_always_compares_equal<pool_allocator<_Tp>>::value;
121 #endif
122 
123 /**
124  * @brief Uniform interface to C++98 and C++0x allocators.
125  * @ingroup allocators
126 */
127 template<typename _Alloc>
129 #if __cplusplus >= 201103L
131 #endif
132  {
133  typedef _Alloc allocator_type;
134 #if __cplusplus >= 201103L
136  typedef typename _Base_type::value_type value_type;
137  typedef typename _Base_type::pointer pointer;
138  typedef typename _Base_type::const_pointer const_pointer;
139  typedef typename _Base_type::size_type size_type;
141  // C++0x allocators do not define reference or const_reference
142  typedef value_type& reference;
143  typedef const value_type& const_reference;
144  using _Base_type::allocate;
145  using _Base_type::deallocate;
146  using _Base_type::construct;
147  using _Base_type::destroy;
148  using _Base_type::max_size;
149 
150  private:
151  template<typename _Ptr>
152  struct __is_custom_pointer
153  : std::integral_constant<bool, std::is_same<pointer, _Ptr>::value
154  && !std::is_pointer<_Ptr>::value>
155  { };
156 
157  public:
158  // overload construct for non-standard pointer types
159  template<typename _Ptr, typename... _Args>
161  construct(_Alloc& __a, _Ptr __p, _Args&&... __args)
162  {
163  _Base_type::construct(__a, std::addressof(*__p),
164  std::forward<_Args>(__args)...);
165  }
166 
167  // overload destroy for non-standard pointer types
168  template<typename _Ptr>
170  destroy(_Alloc& __a, _Ptr __p)
171  { _Base_type::destroy(__a, std::addressof(*__p)); }
172 
173  static _Alloc _S_select_on_copy(const _Alloc& __a)
174  { return _Base_type::select_on_container_copy_construction(__a); }
175 
176  static void _S_on_swap(_Alloc& __a, _Alloc& __b)
177  { std::__alloc_on_swap(__a, __b); }
178 
179  static constexpr bool _S_propagate_on_copy_assign()
180  { return _Base_type::propagate_on_container_copy_assignment::value; }
181 
182  static constexpr bool _S_propagate_on_move_assign()
183  { return _Base_type::propagate_on_container_move_assignment::value; }
184 
185  static constexpr bool _S_propagate_on_swap()
186  { return _Base_type::propagate_on_container_swap::value; }
187 
188  static constexpr bool _S_always_equal()
189  { return __allocator_always_compares_equal<_Alloc>::value; }
190 
191  static constexpr bool _S_nothrow_move()
192  { return _S_propagate_on_move_assign() || _S_always_equal(); }
193 
194  static constexpr bool _S_nothrow_swap()
195  {
196  using std::swap;
197  return !_S_propagate_on_swap()
198  || noexcept(swap(std::declval<_Alloc&>(), std::declval<_Alloc&>()));
199  }
200 
201  template<typename _Tp>
202  struct rebind
203  { typedef typename _Base_type::template rebind_alloc<_Tp> other; };
204 #else
205 
206  typedef typename _Alloc::pointer pointer;
207  typedef typename _Alloc::const_pointer const_pointer;
208  typedef typename _Alloc::value_type value_type;
209  typedef typename _Alloc::reference reference;
210  typedef typename _Alloc::const_reference const_reference;
211  typedef typename _Alloc::size_type size_type;
212  typedef typename _Alloc::difference_type difference_type;
213 
214  static pointer
215  allocate(_Alloc& __a, size_type __n)
216  { return __a.allocate(__n); }
217 
218  static void deallocate(_Alloc& __a, pointer __p, size_type __n)
219  { __a.deallocate(__p, __n); }
220 
221  template<typename _Tp>
222  static void construct(_Alloc& __a, pointer __p, const _Tp& __arg)
223  { __a.construct(__p, __arg); }
224 
225  static void destroy(_Alloc& __a, pointer __p)
226  { __a.destroy(__p); }
227 
228  static size_type max_size(const _Alloc& __a)
229  { return __a.max_size(); }
230 
231  static const _Alloc& _S_select_on_copy(const _Alloc& __a) { return __a; }
232 
233  static void _S_on_swap(_Alloc& __a, _Alloc& __b)
234  {
235  // _GLIBCXX_RESOLVE_LIB_DEFECTS
236  // 431. Swapping containers with unequal allocators.
237  std::__alloc_swap<_Alloc>::_S_do_it(__a, __b);
238  }
239 
240  template<typename _Tp>
241  struct rebind
242  { typedef typename _Alloc::template rebind<_Tp>::other other; };
243 #endif
244  };
245 
246 _GLIBCXX_END_NAMESPACE_VERSION
247 } // namespace
248 
249 #endif