]>
Commit | Line | Data |
---|---|---|
0611ce44 PC |
1 | // Debugging array implementation -*- C++ -*- |
2 | ||
8d9254fc | 3 | // Copyright (C) 2012-2020 Free Software Foundation, Inc. |
0611ce44 PC |
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 debug/array | |
26 | * This is a Standard C++ Library header. | |
27 | */ | |
28 | ||
29 | #ifndef _GLIBCXX_DEBUG_ARRAY | |
30 | #define _GLIBCXX_DEBUG_ARRAY 1 | |
31 | ||
32 | #pragma GCC system_header | |
33 | ||
7d17de7f FD |
34 | #include <array> |
35 | ||
adad2a7d FD |
36 | #include <debug/formatter.h> |
37 | #include <debug/macros.h> | |
0611ce44 | 38 | |
0611ce44 PC |
39 | namespace std _GLIBCXX_VISIBILITY(default) |
40 | { | |
41 | namespace __debug | |
42 | { | |
43 | template<typename _Tp, std::size_t _Nm> | |
44 | struct array | |
45 | { | |
46 | typedef _Tp value_type; | |
47 | typedef value_type* pointer; | |
48 | typedef const value_type* const_pointer; | |
49 | typedef value_type& reference; | |
50 | typedef const value_type& const_reference; | |
51 | typedef value_type* iterator; | |
52 | typedef const value_type* const_iterator; | |
53 | typedef std::size_t size_type; | |
54 | typedef std::ptrdiff_t difference_type; | |
55 | typedef std::reverse_iterator<iterator> reverse_iterator; | |
56 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; | |
57 | ||
58 | // Support for zero-sized arrays mandatory. | |
59 | typedef _GLIBCXX_STD_C::__array_traits<_Tp, _Nm> _AT_Type; | |
60 | typename _AT_Type::_Type _M_elems; | |
61 | ||
62 | template<std::size_t _Size> | |
63 | struct _Array_check_subscript | |
64 | { | |
65 | std::size_t size() { return _Size; } | |
66 | ||
67 | _Array_check_subscript(std::size_t __index) | |
68 | { __glibcxx_check_subscript(__index); } | |
69 | }; | |
70 | ||
71 | template<std::size_t _Size> | |
72 | struct _Array_check_nonempty | |
73 | { | |
d715f554 | 74 | _GLIBCXX_NODISCARD bool empty() { return _Size == 0; } |
0611ce44 PC |
75 | |
76 | _Array_check_nonempty() | |
77 | { __glibcxx_check_nonempty(); } | |
78 | }; | |
79 | ||
80 | // No explicit construct/copy/destroy for aggregate type. | |
81 | ||
82 | // DR 776. | |
59d37e97 | 83 | _GLIBCXX20_CONSTEXPR void |
0611ce44 PC |
84 | fill(const value_type& __u) |
85 | { std::fill_n(begin(), size(), __u); } | |
86 | ||
59d37e97 | 87 | _GLIBCXX20_CONSTEXPR void |
0611ce44 | 88 | swap(array& __other) |
e615c24c | 89 | noexcept(_AT_Type::_Is_nothrow_swappable::value) |
0611ce44 PC |
90 | { std::swap_ranges(begin(), end(), __other.begin()); } |
91 | ||
92 | // Iterators. | |
e90a8010 | 93 | _GLIBCXX17_CONSTEXPR iterator |
0611ce44 PC |
94 | begin() noexcept |
95 | { return iterator(data()); } | |
96 | ||
e90a8010 | 97 | _GLIBCXX17_CONSTEXPR const_iterator |
0611ce44 PC |
98 | begin() const noexcept |
99 | { return const_iterator(data()); } | |
100 | ||
e90a8010 | 101 | _GLIBCXX17_CONSTEXPR iterator |
0611ce44 PC |
102 | end() noexcept |
103 | { return iterator(data() + _Nm); } | |
104 | ||
e90a8010 | 105 | _GLIBCXX17_CONSTEXPR const_iterator |
0611ce44 PC |
106 | end() const noexcept |
107 | { return const_iterator(data() + _Nm); } | |
108 | ||
e90a8010 | 109 | _GLIBCXX17_CONSTEXPR reverse_iterator |
0611ce44 PC |
110 | rbegin() noexcept |
111 | { return reverse_iterator(end()); } | |
112 | ||
e90a8010 | 113 | _GLIBCXX17_CONSTEXPR const_reverse_iterator |
0611ce44 PC |
114 | rbegin() const noexcept |
115 | { return const_reverse_iterator(end()); } | |
116 | ||
e90a8010 | 117 | _GLIBCXX17_CONSTEXPR reverse_iterator |
0611ce44 PC |
118 | rend() noexcept |
119 | { return reverse_iterator(begin()); } | |
120 | ||
e90a8010 | 121 | _GLIBCXX17_CONSTEXPR const_reverse_iterator |
0611ce44 PC |
122 | rend() const noexcept |
123 | { return const_reverse_iterator(begin()); } | |
124 | ||
e90a8010 | 125 | _GLIBCXX17_CONSTEXPR const_iterator |
0611ce44 PC |
126 | cbegin() const noexcept |
127 | { return const_iterator(data()); } | |
128 | ||
e90a8010 | 129 | _GLIBCXX17_CONSTEXPR const_iterator |
0611ce44 PC |
130 | cend() const noexcept |
131 | { return const_iterator(data() + _Nm); } | |
132 | ||
e90a8010 | 133 | _GLIBCXX17_CONSTEXPR const_reverse_iterator |
0611ce44 PC |
134 | crbegin() const noexcept |
135 | { return const_reverse_iterator(end()); } | |
136 | ||
e90a8010 | 137 | _GLIBCXX17_CONSTEXPR const_reverse_iterator |
0611ce44 PC |
138 | crend() const noexcept |
139 | { return const_reverse_iterator(begin()); } | |
140 | ||
141 | // Capacity. | |
e90a8010 | 142 | constexpr size_type |
0611ce44 PC |
143 | size() const noexcept { return _Nm; } |
144 | ||
e90a8010 | 145 | constexpr size_type |
0611ce44 PC |
146 | max_size() const noexcept { return _Nm; } |
147 | ||
d715f554 | 148 | _GLIBCXX_NODISCARD constexpr bool |
0611ce44 PC |
149 | empty() const noexcept { return size() == 0; } |
150 | ||
151 | // Element access. | |
e90a8010 | 152 | _GLIBCXX17_CONSTEXPR reference |
b4efa80e | 153 | operator[](size_type __n) noexcept |
0611ce44 PC |
154 | { |
155 | __glibcxx_check_subscript(__n); | |
156 | return _AT_Type::_S_ref(_M_elems, __n); | |
157 | } | |
158 | ||
159 | constexpr const_reference | |
160 | operator[](size_type __n) const noexcept | |
161 | { | |
162 | return __n < _Nm ? _AT_Type::_S_ref(_M_elems, __n) | |
54ba39f5 | 163 | : (_GLIBCXX_THROW_OR_ABORT(_Array_check_subscript<_Nm>(__n)), |
0611ce44 PC |
164 | _AT_Type::_S_ref(_M_elems, 0)); |
165 | } | |
166 | ||
e90a8010 | 167 | _GLIBCXX17_CONSTEXPR reference |
0611ce44 PC |
168 | at(size_type __n) |
169 | { | |
170 | if (__n >= _Nm) | |
e615c24c FD |
171 | std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) " |
172 | ">= _Nm (which is %zu)"), | |
9779c871 | 173 | __n, _Nm); |
0611ce44 PC |
174 | return _AT_Type::_S_ref(_M_elems, __n); |
175 | } | |
176 | ||
177 | constexpr const_reference | |
178 | at(size_type __n) const | |
179 | { | |
180 | // Result of conditional expression must be an lvalue so use | |
181 | // boolean ? lvalue : (throw-expr, lvalue) | |
182 | return __n < _Nm ? _AT_Type::_S_ref(_M_elems, __n) | |
9779c871 PP |
183 | : (std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) " |
184 | ">= _Nm (which is %zu)"), | |
185 | __n, _Nm), | |
0611ce44 PC |
186 | _AT_Type::_S_ref(_M_elems, 0)); |
187 | } | |
188 | ||
e90a8010 | 189 | _GLIBCXX17_CONSTEXPR reference |
b4efa80e | 190 | front() noexcept |
0611ce44 PC |
191 | { |
192 | __glibcxx_check_nonempty(); | |
193 | return *begin(); | |
194 | } | |
195 | ||
e90a8010 | 196 | constexpr const_reference |
b4efa80e | 197 | front() const noexcept |
0611ce44 PC |
198 | { |
199 | return _Nm ? _AT_Type::_S_ref(_M_elems, 0) | |
54ba39f5 | 200 | : (_GLIBCXX_THROW_OR_ABORT(_Array_check_nonempty<_Nm>()), |
0611ce44 PC |
201 | _AT_Type::_S_ref(_M_elems, 0)); |
202 | } | |
203 | ||
e90a8010 | 204 | _GLIBCXX17_CONSTEXPR reference |
b4efa80e | 205 | back() noexcept |
0611ce44 PC |
206 | { |
207 | __glibcxx_check_nonempty(); | |
208 | return _Nm ? *(end() - 1) : *end(); | |
209 | } | |
210 | ||
e90a8010 | 211 | constexpr const_reference |
b4efa80e | 212 | back() const noexcept |
0611ce44 PC |
213 | { |
214 | return _Nm ? _AT_Type::_S_ref(_M_elems, _Nm - 1) | |
54ba39f5 | 215 | : (_GLIBCXX_THROW_OR_ABORT(_Array_check_nonempty<_Nm>()), |
0611ce44 PC |
216 | _AT_Type::_S_ref(_M_elems, 0)); |
217 | } | |
218 | ||
e90a8010 | 219 | _GLIBCXX17_CONSTEXPR pointer |
0611ce44 | 220 | data() noexcept |
ac3efa77 | 221 | { return _AT_Type::_S_ptr(_M_elems); } |
0611ce44 | 222 | |
e90a8010 | 223 | _GLIBCXX17_CONSTEXPR const_pointer |
0611ce44 | 224 | data() const noexcept |
ac3efa77 | 225 | { return _AT_Type::_S_ptr(_M_elems); } |
0611ce44 PC |
226 | }; |
227 | ||
af181c91 JW |
228 | #if __cpp_deduction_guides >= 201606 |
229 | template<typename _Tp, typename... _Up> | |
230 | array(_Tp, _Up...) | |
231 | -> array<std::enable_if_t<(std::is_same_v<_Tp, _Up> && ...), _Tp>, | |
232 | 1 + sizeof...(_Up)>; | |
233 | #endif | |
234 | ||
0611ce44 PC |
235 | // Array comparisons. |
236 | template<typename _Tp, std::size_t _Nm> | |
2d2ad752 | 237 | _GLIBCXX20_CONSTEXPR |
e90a8010 | 238 | inline bool |
0611ce44 PC |
239 | operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) |
240 | { return std::equal(__one.begin(), __one.end(), __two.begin()); } | |
241 | ||
242 | template<typename _Tp, std::size_t _Nm> | |
2d2ad752 | 243 | _GLIBCXX20_CONSTEXPR |
0611ce44 PC |
244 | inline bool |
245 | operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) | |
246 | { return !(__one == __two); } | |
247 | ||
248 | template<typename _Tp, std::size_t _Nm> | |
2d2ad752 | 249 | _GLIBCXX20_CONSTEXPR |
0611ce44 PC |
250 | inline bool |
251 | operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b) | |
e90a8010 | 252 | { |
0611ce44 | 253 | return std::lexicographical_compare(__a.begin(), __a.end(), |
e90a8010 | 254 | __b.begin(), __b.end()); |
0611ce44 PC |
255 | } |
256 | ||
257 | template<typename _Tp, std::size_t _Nm> | |
2d2ad752 | 258 | _GLIBCXX20_CONSTEXPR |
0611ce44 PC |
259 | inline bool |
260 | operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) | |
261 | { return __two < __one; } | |
262 | ||
263 | template<typename _Tp, std::size_t _Nm> | |
2d2ad752 | 264 | _GLIBCXX20_CONSTEXPR |
0611ce44 PC |
265 | inline bool |
266 | operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) | |
267 | { return !(__one > __two); } | |
268 | ||
269 | template<typename _Tp, std::size_t _Nm> | |
2d2ad752 | 270 | _GLIBCXX20_CONSTEXPR |
0611ce44 PC |
271 | inline bool |
272 | operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) | |
273 | { return !(__one < __two); } | |
274 | ||
275 | // Specialized algorithms. | |
1d752b4f JW |
276 | |
277 | #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 | |
278 | template<typename _Tp, size_t _Nm> | |
279 | typename enable_if< | |
280 | !_GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::_Is_swappable::value>::type | |
281 | swap(array<_Tp, _Nm>&, array<_Tp, _Nm>&) = delete; | |
282 | #endif | |
283 | ||
0611ce44 | 284 | template<typename _Tp, std::size_t _Nm> |
59d37e97 | 285 | _GLIBCXX20_CONSTEXPR |
0611ce44 PC |
286 | inline void |
287 | swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two) | |
288 | noexcept(noexcept(__one.swap(__two))) | |
289 | { __one.swap(__two); } | |
290 | ||
291 | template<std::size_t _Int, typename _Tp, std::size_t _Nm> | |
292 | constexpr _Tp& | |
293 | get(array<_Tp, _Nm>& __arr) noexcept | |
294 | { | |
295 | static_assert(_Int < _Nm, "index is out of bounds"); | |
296 | return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>:: | |
297 | _S_ref(__arr._M_elems, _Int); | |
298 | } | |
299 | ||
300 | template<std::size_t _Int, typename _Tp, std::size_t _Nm> | |
301 | constexpr _Tp&& | |
302 | get(array<_Tp, _Nm>&& __arr) noexcept | |
303 | { | |
304 | static_assert(_Int < _Nm, "index is out of bounds"); | |
b82f988e | 305 | return std::move(__debug::get<_Int>(__arr)); |
0611ce44 PC |
306 | } |
307 | ||
308 | template<std::size_t _Int, typename _Tp, std::size_t _Nm> | |
309 | constexpr const _Tp& | |
310 | get(const array<_Tp, _Nm>& __arr) noexcept | |
311 | { | |
312 | static_assert(_Int < _Nm, "index is out of bounds"); | |
313 | return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>:: | |
314 | _S_ref(__arr._M_elems, _Int); | |
315 | } | |
ccbbf8df VV |
316 | |
317 | template<std::size_t _Int, typename _Tp, std::size_t _Nm> | |
318 | constexpr const _Tp&& | |
319 | get(const array<_Tp, _Nm>&& __arr) noexcept | |
320 | { | |
321 | static_assert(_Int < _Nm, "index is out of bounds"); | |
322 | return std::move(__debug::get<_Int>(__arr)); | |
323 | } | |
c9fb0a85 JW |
324 | |
325 | #if __cplusplus > 201703L | |
326 | #define __cpp_lib_to_array 201907L | |
327 | ||
328 | template<bool _Move = false, typename _Tp, size_t... _Idx> | |
329 | constexpr array<remove_cv_t<_Tp>, sizeof...(_Idx)> | |
330 | __to_array(_Tp (&__a)[sizeof...(_Idx)], index_sequence<_Idx...>) | |
331 | { | |
332 | if constexpr (_Move) | |
333 | return {{std::move(__a[_Idx])...}}; | |
334 | else | |
335 | return {{__a[_Idx]...}}; | |
336 | } | |
337 | ||
338 | template<typename _Tp, size_t _Nm> | |
339 | constexpr array<remove_cv_t<_Tp>, _Nm> | |
340 | to_array(_Tp (&__a)[_Nm]) | |
341 | noexcept(is_nothrow_constructible_v<_Tp, _Tp&>) | |
342 | { | |
343 | static_assert(!is_array_v<_Tp>); | |
344 | static_assert(is_constructible_v<_Tp, _Tp&>); | |
345 | if constexpr (is_constructible_v<_Tp, _Tp&>) | |
346 | return __debug::__to_array(__a, make_index_sequence<_Nm>{}); | |
347 | __builtin_unreachable(); // FIXME: see PR c++/91388 | |
348 | } | |
349 | ||
350 | template<typename _Tp, size_t _Nm> | |
351 | constexpr array<remove_cv_t<_Tp>, _Nm> | |
352 | to_array(_Tp (&&__a)[_Nm]) | |
353 | noexcept(is_nothrow_move_constructible_v<_Tp>) | |
354 | { | |
355 | static_assert(!is_array_v<_Tp>); | |
356 | static_assert(is_move_constructible_v<_Tp>); | |
357 | if constexpr (is_move_constructible_v<_Tp>) | |
358 | return __debug::__to_array<1>(__a, make_index_sequence<_Nm>{}); | |
359 | __builtin_unreachable(); // FIXME: see PR c++/91388 | |
360 | } | |
361 | #endif // C++20 | |
362 | ||
0611ce44 PC |
363 | } // namespace __debug |
364 | ||
7d17de7f | 365 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
0611ce44 PC |
366 | // Tuple interface to class template array. |
367 | ||
368 | /// tuple_size | |
0611ce44 | 369 | template<typename _Tp, std::size_t _Nm> |
7d17de7f | 370 | struct tuple_size<std::__debug::array<_Tp, _Nm>> |
0611ce44 PC |
371 | : public integral_constant<std::size_t, _Nm> { }; |
372 | ||
373 | /// tuple_element | |
0611ce44 | 374 | template<std::size_t _Int, typename _Tp, std::size_t _Nm> |
7d17de7f | 375 | struct tuple_element<_Int, std::__debug::array<_Tp, _Nm>> |
0611ce44 PC |
376 | { |
377 | static_assert(_Int < _Nm, "index is out of bounds"); | |
378 | typedef _Tp type; | |
379 | }; | |
7d17de7f FD |
380 | |
381 | template<typename _Tp, std::size_t _Nm> | |
382 | struct __is_tuple_like_impl<std::__debug::array<_Tp, _Nm>> : true_type | |
383 | { }; | |
384 | ||
385 | _GLIBCXX_END_NAMESPACE_VERSION | |
0611ce44 PC |
386 | } // namespace std |
387 | ||
0611ce44 | 388 | #endif // _GLIBCXX_DEBUG_ARRAY |