]>
Commit | Line | Data |
---|---|---|
e8eb60bd JW |
1 | // Allocator traits -*- C++ -*- |
2 | ||
7adcbafe | 3 | // Copyright (C) 2011-2022 Free Software Foundation, Inc. |
e8eb60bd JW |
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 | ||
e8eb60bd | 34 | # include <bits/alloc_traits.h> |
85f24114 | 35 | #if __cplusplus < 201103L |
e8eb60bd JW |
36 | # include <bits/allocator.h> // for __alloc_swap |
37 | #endif | |
38 | ||
39 | namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) | |
40 | { | |
41 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
42 | ||
d526242c | 43 | /** |
a2b5fdcb | 44 | * @brief Uniform interface to C++98 and C++11 allocators. |
d526242c JW |
45 | * @ingroup allocators |
46 | */ | |
6d82c562 | 47 | template<typename _Alloc, typename = typename _Alloc::value_type> |
e8eb60bd | 48 | struct __alloc_traits |
734f5023 | 49 | #if __cplusplus >= 201103L |
e8eb60bd JW |
50 | : std::allocator_traits<_Alloc> |
51 | #endif | |
52 | { | |
53 | typedef _Alloc allocator_type; | |
734f5023 | 54 | #if __cplusplus >= 201103L |
e8eb60bd JW |
55 | typedef std::allocator_traits<_Alloc> _Base_type; |
56 | typedef typename _Base_type::value_type value_type; | |
57 | typedef typename _Base_type::pointer pointer; | |
58 | typedef typename _Base_type::const_pointer const_pointer; | |
59 | typedef typename _Base_type::size_type size_type; | |
605aed29 | 60 | typedef typename _Base_type::difference_type difference_type; |
20067423 | 61 | // C++11 allocators do not define reference or const_reference |
e8eb60bd JW |
62 | typedef value_type& reference; |
63 | typedef const value_type& const_reference; | |
64 | using _Base_type::allocate; | |
65 | using _Base_type::deallocate; | |
66 | using _Base_type::construct; | |
67 | using _Base_type::destroy; | |
73f05031 | 68 | using _Base_type::max_size; |
e8eb60bd | 69 | |
d526242c JW |
70 | private: |
71 | template<typename _Ptr> | |
20067423 JW |
72 | using __is_custom_pointer |
73 | = std::__and_<std::is_same<pointer, _Ptr>, | |
74 | std::__not_<std::is_pointer<_Ptr>>>; | |
d526242c JW |
75 | |
76 | public: | |
73f05031 | 77 | // overload construct for non-standard pointer types |
d526242c | 78 | template<typename _Ptr, typename... _Args> |
540218dd JW |
79 | static _GLIBCXX14_CONSTEXPR |
80 | std::__enable_if_t<__is_custom_pointer<_Ptr>::value> | |
d526242c | 81 | construct(_Alloc& __a, _Ptr __p, _Args&&... __args) |
0f317ef7 MG |
82 | noexcept(noexcept(_Base_type::construct(__a, std::__to_address(__p), |
83 | std::forward<_Args>(__args)...))) | |
d526242c | 84 | { |
2af96386 | 85 | _Base_type::construct(__a, std::__to_address(__p), |
d526242c JW |
86 | std::forward<_Args>(__args)...); |
87 | } | |
88 | ||
73f05031 | 89 | // overload destroy for non-standard pointer types |
d526242c | 90 | template<typename _Ptr> |
540218dd JW |
91 | static _GLIBCXX14_CONSTEXPR |
92 | std::__enable_if_t<__is_custom_pointer<_Ptr>::value> | |
d526242c | 93 | destroy(_Alloc& __a, _Ptr __p) |
0f317ef7 | 94 | noexcept(noexcept(_Base_type::destroy(__a, std::__to_address(__p)))) |
2af96386 | 95 | { _Base_type::destroy(__a, std::__to_address(__p)); } |
d526242c | 96 | |
85f24114 | 97 | static constexpr _Alloc _S_select_on_copy(const _Alloc& __a) |
e8eb60bd JW |
98 | { return _Base_type::select_on_container_copy_construction(__a); } |
99 | ||
540218dd | 100 | static _GLIBCXX14_CONSTEXPR void _S_on_swap(_Alloc& __a, _Alloc& __b) |
e8eb60bd JW |
101 | { std::__alloc_on_swap(__a, __b); } |
102 | ||
103 | static constexpr bool _S_propagate_on_copy_assign() | |
104 | { return _Base_type::propagate_on_container_copy_assignment::value; } | |
105 | ||
106 | static constexpr bool _S_propagate_on_move_assign() | |
107 | { return _Base_type::propagate_on_container_move_assignment::value; } | |
108 | ||
109 | static constexpr bool _S_propagate_on_swap() | |
110 | { return _Base_type::propagate_on_container_swap::value; } | |
111 | ||
d526242c | 112 | static constexpr bool _S_always_equal() |
a2b5fdcb | 113 | { return _Base_type::is_always_equal::value; } |
d526242c JW |
114 | |
115 | static constexpr bool _S_nothrow_move() | |
116 | { return _S_propagate_on_move_assign() || _S_always_equal(); } | |
117 | ||
73f05031 JW |
118 | template<typename _Tp> |
119 | struct rebind | |
7579d15b | 120 | { typedef typename _Base_type::template rebind_alloc<_Tp> other; }; |
85f24114 | 121 | #else // ! C++11 |
e8eb60bd JW |
122 | |
123 | typedef typename _Alloc::pointer pointer; | |
124 | typedef typename _Alloc::const_pointer const_pointer; | |
125 | typedef typename _Alloc::value_type value_type; | |
126 | typedef typename _Alloc::reference reference; | |
127 | typedef typename _Alloc::const_reference const_reference; | |
128 | typedef typename _Alloc::size_type size_type; | |
605aed29 | 129 | typedef typename _Alloc::difference_type difference_type; |
e8eb60bd | 130 | |
d715f554 | 131 | _GLIBCXX_NODISCARD static pointer |
e8eb60bd JW |
132 | allocate(_Alloc& __a, size_type __n) |
133 | { return __a.allocate(__n); } | |
134 | ||
603aec67 JW |
135 | template<typename _Hint> |
136 | _GLIBCXX_NODISCARD static pointer | |
137 | allocate(_Alloc& __a, size_type __n, _Hint __hint) | |
138 | { return __a.allocate(__n, __hint); } | |
139 | ||
e8eb60bd JW |
140 | static void deallocate(_Alloc& __a, pointer __p, size_type __n) |
141 | { __a.deallocate(__p, __n); } | |
142 | ||
143 | template<typename _Tp> | |
144 | static void construct(_Alloc& __a, pointer __p, const _Tp& __arg) | |
145 | { __a.construct(__p, __arg); } | |
146 | ||
147 | static void destroy(_Alloc& __a, pointer __p) | |
148 | { __a.destroy(__p); } | |
149 | ||
73f05031 JW |
150 | static size_type max_size(const _Alloc& __a) |
151 | { return __a.max_size(); } | |
152 | ||
e8eb60bd JW |
153 | static const _Alloc& _S_select_on_copy(const _Alloc& __a) { return __a; } |
154 | ||
155 | static void _S_on_swap(_Alloc& __a, _Alloc& __b) | |
156 | { | |
157 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
158 | // 431. Swapping containers with unequal allocators. | |
159 | std::__alloc_swap<_Alloc>::_S_do_it(__a, __b); | |
160 | } | |
73f05031 JW |
161 | |
162 | template<typename _Tp> | |
163 | struct rebind | |
164 | { typedef typename _Alloc::template rebind<_Tp>::other other; }; | |
85f24114 | 165 | #endif // C++11 |
e8eb60bd JW |
166 | }; |
167 | ||
168 | _GLIBCXX_END_NAMESPACE_VERSION | |
484dc599 | 169 | } // namespace __gnu_cxx |
e8eb60bd JW |
170 | |
171 | #endif |