]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/debug/functions.h
c++config: Add in revised namespace associations.
[thirdparty/gcc.git] / libstdc++-v3 / include / debug / functions.h
1 // Debugging support implementation -*- C++ -*-
2
3 // Copyright (C) 2003, 2005
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 2, or (at your option)
10 // any later version.
11
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.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 #ifndef _GLIBCXX_DEBUG_FUNCTIONS_H
32 #define _GLIBCXX_DEBUG_FUNCTIONS_H 1
33
34 #include <bits/c++config.h>
35 #include <stddef.h> // for ptrdiff_t
36 #include <bits/stl_iterator_base_types.h> // for iterator_traits, categories
37 #include <bits/cpp_type_traits.h> // for __is_integer
38
39 namespace std
40 {
41 namespace __gnu_debug
42 {
43 template<typename _Iterator, typename _Sequence>
44 class _Safe_iterator;
45
46 // An arbitrary iterator pointer is not singular.
47 inline bool
48 __check_singular_aux(const void*) { return false; }
49
50 // We may have an iterator that derives from _Safe_iterator_base but isn't
51 // a _Safe_iterator.
52 template<typename _Iterator>
53 inline bool
54 __check_singular(_Iterator& __x)
55 { return __check_singular_aux(&__x); }
56
57 /** Non-NULL pointers are nonsingular. */
58 template<typename _Tp>
59 inline bool
60 __check_singular(const _Tp* __ptr)
61 { return __ptr == 0; }
62
63 /** Safe iterators know if they are singular. */
64 template<typename _Iterator, typename _Sequence>
65 inline bool
66 __check_singular(const _Safe_iterator<_Iterator, _Sequence>& __x)
67 { return __x._M_singular(); }
68
69 /** Assume that some arbitrary iterator is dereferenceable, because we
70 can't prove that it isn't. */
71 template<typename _Iterator>
72 inline bool
73 __check_dereferenceable(_Iterator&)
74 { return true; }
75
76 /** Non-NULL pointers are dereferenceable. */
77 template<typename _Tp>
78 inline bool
79 __check_dereferenceable(const _Tp* __ptr)
80 { return __ptr; }
81
82 /** Safe iterators know if they are singular. */
83 template<typename _Iterator, typename _Sequence>
84 inline bool
85 __check_dereferenceable(const _Safe_iterator<_Iterator, _Sequence>& __x)
86 { return __x._M_dereferenceable(); }
87
88 /** If the distance between two random access iterators is
89 * nonnegative, assume the range is valid.
90 */
91 template<typename _RandomAccessIterator>
92 inline bool
93 __valid_range_aux2(const _RandomAccessIterator& __first,
94 const _RandomAccessIterator& __last,
95 std::random_access_iterator_tag)
96 { return __last - __first >= 0; }
97
98 /** Can't test for a valid range with input iterators, because
99 * iteration may be destructive. So we just assume that the range
100 * is valid.
101 */
102 template<typename _InputIterator>
103 inline bool
104 __valid_range_aux2(const _InputIterator&, const _InputIterator&,
105 std::input_iterator_tag)
106 { return true; }
107
108 /** We say that integral types for a valid range, and defer to other
109 * routines to realize what to do with integral types instead of
110 * iterators.
111 */
112 template<typename _Integral>
113 inline bool
114 __valid_range_aux(const _Integral&, const _Integral&, __true_type)
115 { return true; }
116
117 /** We have iterators, so figure out what kind of iterators that are
118 * to see if we can check the range ahead of time.
119 */
120 template<typename _InputIterator>
121 inline bool
122 __valid_range_aux(const _InputIterator& __first,
123 const _InputIterator& __last, __false_type)
124 {
125 typedef typename std::iterator_traits<_InputIterator>::iterator_category
126 _Category;
127 return __valid_range_aux2(__first, __last, _Category());
128 }
129
130 /** Don't know what these iterators are, or if they are even
131 * iterators (we may get an integral type for InputIterator), so
132 * see if they are integral and pass them on to the next phase
133 * otherwise.
134 */
135 template<typename _InputIterator>
136 inline bool
137 __valid_range(const _InputIterator& __first, const _InputIterator& __last)
138 {
139 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
140 return __valid_range_aux(__first, __last, _Integral());
141 }
142
143 /** Safe iterators know how to check if they form a valid range. */
144 template<typename _Iterator, typename _Sequence>
145 inline bool
146 __valid_range(const _Safe_iterator<_Iterator, _Sequence>& __first,
147 const _Safe_iterator<_Iterator, _Sequence>& __last)
148 { return __first._M_valid_range(__last); }
149
150 /* Checks that [first, last) is a valid range, and then returns
151 * __first. This routine is useful when we can't use a separate
152 * assertion statement because, e.g., we are in a constructor.
153 */
154 template<typename _InputIterator>
155 inline _InputIterator
156 __check_valid_range(const _InputIterator& __first,
157 const _InputIterator& __last)
158 {
159 _GLIBCXX_DEBUG_ASSERT(__valid_range(__first, __last));
160 return __first;
161 }
162
163 /** Checks that __s is non-NULL or __n == 0, and then returns __s. */
164 template<typename _CharT, typename _Integer>
165 inline const _CharT*
166 __check_string(const _CharT* __s, const _Integer& __n)
167 {
168 #ifdef _GLIBCXX_DEBUG_PEDANTIC
169 _GLIBCXX_DEBUG_ASSERT(__s != 0 || __n == 0);
170 #endif
171 return __s;
172 }
173
174 /** Checks that __s is non-NULL and then returns __s. */
175 template<typename _CharT>
176 inline const _CharT*
177 __check_string(const _CharT* __s)
178 {
179 #ifdef _GLIBCXX_DEBUG_PEDANTIC
180 _GLIBCXX_DEBUG_ASSERT(__s != 0);
181 #endif
182 return __s;
183 }
184
185 // Can't check if an input iterator sequence is sorted, because we
186 // can't step through the sequence.
187 template<typename _InputIterator>
188 inline bool
189 __check_sorted_aux(const _InputIterator&, const _InputIterator&,
190 std::input_iterator_tag)
191 { return true; }
192
193 // Can verify if a forward iterator sequence is in fact sorted using
194 // std::__is_sorted
195 template<typename _ForwardIterator>
196 inline bool
197 __check_sorted_aux(_ForwardIterator __first, _ForwardIterator __last,
198 std::forward_iterator_tag)
199 {
200 if (__first == __last)
201 return true;
202
203 _ForwardIterator __next = __first;
204 for (++__next; __next != __last; __first = __next, ++__next) {
205 if (*__next < *__first)
206 return false;
207 }
208
209 return true;
210 }
211
212 // Can't check if an input iterator sequence is sorted, because we can't step
213 // through the sequence.
214 template<typename _InputIterator, typename _Predicate>
215 inline bool
216 __check_sorted_aux(const _InputIterator&, const _InputIterator&,
217 _Predicate, std::input_iterator_tag)
218 { return true; }
219
220 // Can verify if a forward iterator sequence is in fact sorted using
221 // std::__is_sorted
222 template<typename _ForwardIterator, typename _Predicate>
223 inline bool
224 __check_sorted_aux(_ForwardIterator __first, _ForwardIterator __last,
225 _Predicate __pred, std::forward_iterator_tag)
226 {
227 if (__first == __last)
228 return true;
229
230 _ForwardIterator __next = __first;
231 for (++__next; __next != __last; __first = __next, ++__next) {
232 if (__pred(*__next, *__first))
233 return false;
234 }
235
236 return true;
237 }
238
239 // Determine if a sequence is sorted.
240 template<typename _InputIterator>
241 inline bool
242 __check_sorted(const _InputIterator& __first, const _InputIterator& __last)
243 {
244 typedef typename std::iterator_traits<_InputIterator>::iterator_category
245 _Category;
246 return __check_sorted_aux(__first, __last, _Category());
247 }
248
249 template<typename _InputIterator, typename _Predicate>
250 inline bool
251 __check_sorted(const _InputIterator& __first, const _InputIterator& __last,
252 _Predicate __pred)
253 {
254 typedef typename std::iterator_traits<_InputIterator>::iterator_category
255 _Category;
256 return __check_sorted_aux(__first, __last, __pred,
257 _Category());
258 }
259
260 // _GLIBCXX_RESOLVE_LIB_DEFECTS
261 // 270. Binary search requirements overly strict
262 // Determine if a sequence is partitioned w.r.t. this element.
263 template<typename _ForwardIterator, typename _Tp>
264 inline bool
265 __check_partitioned(_ForwardIterator __first, _ForwardIterator __last,
266 const _Tp& __value)
267 {
268 while (__first != __last && *__first < __value)
269 ++__first;
270 while (__first != __last && !(*__first < __value))
271 ++__first;
272 return __first == __last;
273 }
274
275 // Determine if a sequence is partitioned w.r.t. this element.
276 template<typename _ForwardIterator, typename _Tp, typename _Pred>
277 inline bool
278 __check_partitioned(_ForwardIterator __first, _ForwardIterator __last,
279 const _Tp& __value, _Pred __pred)
280 {
281 while (__first != __last && __pred(*__first, __value))
282 ++__first;
283 while (__first != __last && !__pred(*__first, __value))
284 ++__first;
285 return __first == __last;
286 }
287 } // namespace __gnu_debug
288 } // namespace std
289
290 #endif