re PR libstdc++/20534 (Erroneous #include of <cassert>)
[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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 <stddef.h> // for ptrdiff_t
35 #include <bits/stl_iterator_base_types.h> // for iterator_traits, categories
36 #include <bits/cpp_type_traits.h> // for __is_integer
37
38 namespace __gnu_debug
39 {
40 template<typename _Iterator, typename _Sequence>
41 class _Safe_iterator;
42
43 // An arbitrary iterator pointer is not singular.
44 inline bool
45 __check_singular_aux(const void*) { return false; }
46
47 // We may have an iterator that derives from _Safe_iterator_base but isn't
48 // a _Safe_iterator.
49 template<typename _Iterator>
50 inline bool
51 __check_singular(_Iterator& __x)
52 { return __gnu_debug::__check_singular_aux(&__x); }
53
54 /** Non-NULL pointers are nonsingular. */
55 template<typename _Tp>
56 inline bool
57 __check_singular(const _Tp* __ptr)
58 { return __ptr == 0; }
59
60 /** Safe iterators know if they are singular. */
61 template<typename _Iterator, typename _Sequence>
62 inline bool
63 __check_singular(const _Safe_iterator<_Iterator, _Sequence>& __x)
64 { return __x._M_singular(); }
65
66 /** Assume that some arbitrary iterator is dereferenceable, because we
67 can't prove that it isn't. */
68 template<typename _Iterator>
69 inline bool
70 __check_dereferenceable(_Iterator&)
71 { return true; }
72
73 /** Non-NULL pointers are dereferenceable. */
74 template<typename _Tp>
75 inline bool
76 __check_dereferenceable(const _Tp* __ptr)
77 { return __ptr; }
78
79 /** Safe iterators know if they are singular. */
80 template<typename _Iterator, typename _Sequence>
81 inline bool
82 __check_dereferenceable(const _Safe_iterator<_Iterator, _Sequence>& __x)
83 { return __x._M_dereferenceable(); }
84
85 /** If the distance between two random access iterators is
86 * nonnegative, assume the range is valid.
87 */
88 template<typename _RandomAccessIterator>
89 inline bool
90 __valid_range_aux2(const _RandomAccessIterator& __first,
91 const _RandomAccessIterator& __last,
92 std::random_access_iterator_tag)
93 { return __last - __first >= 0; }
94
95 /** Can't test for a valid range with input iterators, because
96 * iteration may be destructive. So we just assume that the range
97 * is valid.
98 */
99 template<typename _InputIterator>
100 inline bool
101 __valid_range_aux2(const _InputIterator&, const _InputIterator&,
102 std::input_iterator_tag)
103 { return true; }
104
105 /** We say that integral types for a valid range, and defer to other
106 * routines to realize what to do with integral types instead of
107 * iterators.
108 */
109 template<typename _Integral>
110 inline bool
111 __valid_range_aux(const _Integral&, const _Integral&, __true_type)
112 { return true; }
113
114 /** We have iterators, so figure out what kind of iterators that are
115 * to see if we can check the range ahead of time.
116 */
117 template<typename _InputIterator>
118 inline bool
119 __valid_range_aux(const _InputIterator& __first,
120 const _InputIterator& __last, __false_type)
121 {
122 typedef typename std::iterator_traits<_InputIterator>::iterator_category
123 _Category;
124 return __gnu_debug::__valid_range_aux2(__first, __last, _Category());
125 }
126
127 /** Don't know what these iterators are, or if they are even
128 * iterators (we may get an integral type for InputIterator), so
129 * see if they are integral and pass them on to the next phase
130 * otherwise.
131 */
132 template<typename _InputIterator>
133 inline bool
134 __valid_range(const _InputIterator& __first, const _InputIterator& __last)
135 {
136 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
137 return __gnu_debug::__valid_range_aux(__first, __last, _Integral());
138 }
139
140 /** Safe iterators know how to check if they form a valid range. */
141 template<typename _Iterator, typename _Sequence>
142 inline bool
143 __valid_range(const _Safe_iterator<_Iterator, _Sequence>& __first,
144 const _Safe_iterator<_Iterator, _Sequence>& __last)
145 { return __first._M_valid_range(__last); }
146
147 /* Checks that [first, last) is a valid range, and then returns
148 * __first. This routine is useful when we can't use a separate
149 * assertion statement because, e.g., we are in a constructor.
150 */
151 template<typename _InputIterator>
152 inline _InputIterator
153 __check_valid_range(const _InputIterator& __first,
154 const _InputIterator& __last)
155 {
156 _GLIBCXX_DEBUG_ASSERT(__gnu_debug::__valid_range(__first, __last));
157 return __first;
158 }
159
160 /** Checks that __s is non-NULL or __n == 0, and then returns __s. */
161 template<typename _CharT, typename _Integer>
162 inline const _CharT*
163 __check_string(const _CharT* __s, const _Integer& __n)
164 {
165 #ifdef _GLIBCXX_DEBUG_PEDANTIC
166 _GLIBCXX_DEBUG_ASSERT(__s != 0 || __n == 0);
167 #endif
168 return __s;
169 }
170
171 /** Checks that __s is non-NULL and then returns __s. */
172 template<typename _CharT>
173 inline const _CharT*
174 __check_string(const _CharT* __s)
175 {
176 #ifdef _GLIBCXX_DEBUG_PEDANTIC
177 _GLIBCXX_DEBUG_ASSERT(__s != 0);
178 #endif
179 return __s;
180 }
181
182 // Can't check if an input iterator sequence is sorted, because we
183 // can't step through the sequence.
184 template<typename _InputIterator>
185 inline bool
186 __check_sorted_aux(const _InputIterator&, const _InputIterator&,
187 std::input_iterator_tag)
188 { return true; }
189
190 // Can verify if a forward iterator sequence is in fact sorted using
191 // std::__is_sorted
192 template<typename _ForwardIterator>
193 inline bool
194 __check_sorted_aux(_ForwardIterator __first, _ForwardIterator __last,
195 std::forward_iterator_tag)
196 {
197 if (__first == __last)
198 return true;
199
200 _ForwardIterator __next = __first;
201 for (++__next; __next != __last; __first = __next, ++__next) {
202 if (*__next < *__first)
203 return false;
204 }
205
206 return true;
207 }
208
209 // Can't check if an input iterator sequence is sorted, because we can't step
210 // through the sequence.
211 template<typename _InputIterator, typename _Predicate>
212 inline bool
213 __check_sorted_aux(const _InputIterator&, const _InputIterator&,
214 _Predicate, std::input_iterator_tag)
215 { return true; }
216
217 // Can verify if a forward iterator sequence is in fact sorted using
218 // std::__is_sorted
219 template<typename _ForwardIterator, typename _Predicate>
220 inline bool
221 __check_sorted_aux(_ForwardIterator __first, _ForwardIterator __last,
222 _Predicate __pred, std::forward_iterator_tag)
223 {
224 if (__first == __last)
225 return true;
226
227 _ForwardIterator __next = __first;
228 for (++__next; __next != __last; __first = __next, ++__next) {
229 if (__pred(*__next, *__first))
230 return false;
231 }
232
233 return true;
234 }
235
236 // Determine if a sequence is sorted.
237 template<typename _InputIterator>
238 inline bool
239 __check_sorted(const _InputIterator& __first, const _InputIterator& __last)
240 {
241 typedef typename std::iterator_traits<_InputIterator>::iterator_category
242 _Category;
243 return __gnu_debug::__check_sorted_aux(__first, __last, _Category());
244 }
245
246 template<typename _InputIterator, typename _Predicate>
247 inline bool
248 __check_sorted(const _InputIterator& __first, const _InputIterator& __last,
249 _Predicate __pred)
250 {
251 typedef typename std::iterator_traits<_InputIterator>::iterator_category
252 _Category;
253 return __gnu_debug::__check_sorted_aux(__first, __last, __pred,
254 _Category());
255 }
256
257 // _GLIBCXX_RESOLVE_LIB_DEFECTS
258 // 270. Binary search requirements overly strict
259 // Determine if a sequence is partitioned w.r.t. this element.
260 template<typename _ForwardIterator, typename _Tp>
261 inline bool
262 __check_partitioned(_ForwardIterator __first, _ForwardIterator __last,
263 const _Tp& __value)
264 {
265 while (__first != __last && *__first < __value)
266 ++__first;
267 while (__first != __last && !(*__first < __value))
268 ++__first;
269 return __first == __last;
270 }
271
272 // Determine if a sequence is partitioned w.r.t. this element.
273 template<typename _ForwardIterator, typename _Tp, typename _Pred>
274 inline bool
275 __check_partitioned(_ForwardIterator __first, _ForwardIterator __last,
276 const _Tp& __value, _Pred __pred)
277 {
278 while (__first != __last && __pred(*__first, __value))
279 ++__first;
280 while (__first != __last && !__pred(*__first, __value))
281 ++__first;
282 return __first == __last;
283 }
284 } // namespace __gnu_debug
285
286 #endif