libstdc++
functions.h
Go to the documentation of this file.
1 // Debugging support implementation -*- C++ -*-
2 
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20 
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
25 
26 /** @file debug/functions.h
27  * This file is a GNU debug extension to the Standard C++ Library.
28  */
29 
30 #ifndef _GLIBCXX_DEBUG_FUNCTIONS_H
31 #define _GLIBCXX_DEBUG_FUNCTIONS_H 1
32 
33 #include <bits/c++config.h>
34 #include <bits/stl_iterator_base_types.h> // for iterator_traits, categories
35 #include <bits/cpp_type_traits.h> // for __is_integer
36 #include <debug/formatter.h>
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 __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
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
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
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,
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&,
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&, std::__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, std::__false_type)
121  {
122  typedef typename std::iterator_traits<_InputIterator>::iterator_category
123  _Category;
124  return __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 __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
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  __attribute__((__unused__)))
156  {
157  __glibcxx_check_valid_range(__first, __last);
158  return __first;
159  }
160 
161  /** Checks that __s is non-NULL or __n == 0, and then returns __s. */
162  template<typename _CharT, typename _Integer>
163  inline const _CharT*
164  __check_string(const _CharT* __s,
165  const _Integer& __n __attribute__((__unused__)))
166  {
167 #ifdef _GLIBCXX_DEBUG_PEDANTIC
168  __glibcxx_assert(__s != 0 || __n == 0);
169 #endif
170  return __s;
171  }
172 
173  /** Checks that __s is non-NULL and then returns __s. */
174  template<typename _CharT>
175  inline const _CharT*
176  __check_string(const _CharT* __s)
177  {
178 #ifdef _GLIBCXX_DEBUG_PEDANTIC
179  __glibcxx_assert(__s != 0);
180 #endif
181  return __s;
182  }
183 
184  // Can't check if an input iterator sequence is sorted, because we
185  // can't step through the sequence.
186  template<typename _InputIterator>
187  inline bool
188  __check_sorted_aux(const _InputIterator&, const _InputIterator&,
190  { return true; }
191 
192  // Can verify if a forward iterator sequence is in fact sorted using
193  // std::__is_sorted
194  template<typename _ForwardIterator>
195  inline bool
196  __check_sorted_aux(_ForwardIterator __first, _ForwardIterator __last,
198  {
199  if (__first == __last)
200  return true;
201 
202  _ForwardIterator __next = __first;
203  for (++__next; __next != __last; __first = __next, ++__next)
204  if (*__next < *__first)
205  return false;
206 
207  return true;
208  }
209 
210  // Can't check if an input iterator sequence is sorted, because we can't step
211  // through the sequence.
212  template<typename _InputIterator, typename _Predicate>
213  inline bool
214  __check_sorted_aux(const _InputIterator&, const _InputIterator&,
215  _Predicate, std::input_iterator_tag)
216  { return true; }
217 
218  // Can verify if a forward iterator sequence is in fact sorted using
219  // std::__is_sorted
220  template<typename _ForwardIterator, typename _Predicate>
221  inline bool
222  __check_sorted_aux(_ForwardIterator __first, _ForwardIterator __last,
223  _Predicate __pred, std::forward_iterator_tag)
224  {
225  if (__first == __last)
226  return true;
227 
228  _ForwardIterator __next = __first;
229  for (++__next; __next != __last; __first = __next, ++__next)
230  if (__pred(*__next, *__first))
231  return false;
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 
244  // Verify that the < operator for elements in the sequence is a
245  // StrictWeakOrdering by checking that it is irreflexive.
246  __glibcxx_assert(__first == __last || !(*__first < *__first));
247 
248  return __check_sorted_aux(__first, __last, _Category());
249  }
250 
251  template<typename _InputIterator, typename _Predicate>
252  inline bool
253  __check_sorted(const _InputIterator& __first, const _InputIterator& __last,
254  _Predicate __pred)
255  {
256  typedef typename std::iterator_traits<_InputIterator>::iterator_category
257  _Category;
258 
259  // Verify that the predicate is StrictWeakOrdering by checking that it
260  // is irreflexive.
261  __glibcxx_assert(__first == __last || !__pred(*__first, *__first));
262 
263  return __check_sorted_aux(__first, __last, __pred, _Category());
264  }
265 
266  template<typename _InputIterator>
267  inline bool
268  __check_sorted_set_aux(const _InputIterator& __first,
269  const _InputIterator& __last,
270  std::__true_type)
271  { return __check_sorted(__first, __last); }
272 
273  template<typename _InputIterator>
274  inline bool
275  __check_sorted_set_aux(const _InputIterator&,
276  const _InputIterator&,
277  std::__false_type)
278  { return true; }
279 
280  template<typename _InputIterator, typename _Predicate>
281  inline bool
282  __check_sorted_set_aux(const _InputIterator& __first,
283  const _InputIterator& __last,
284  _Predicate __pred, std::__true_type)
285  { return __check_sorted(__first, __last, __pred); }
286 
287  template<typename _InputIterator, typename _Predicate>
288  inline bool
289  __check_sorted_set_aux(const _InputIterator&,
290  const _InputIterator&, _Predicate,
291  std::__false_type)
292  { return true; }
293 
294  // ... special variant used in std::merge, std::includes, std::set_*.
295  template<typename _InputIterator1, typename _InputIterator2>
296  inline bool
297  __check_sorted_set(const _InputIterator1& __first,
298  const _InputIterator1& __last,
299  const _InputIterator2&)
300  {
301  typedef typename std::iterator_traits<_InputIterator1>::value_type
302  _ValueType1;
303  typedef typename std::iterator_traits<_InputIterator2>::value_type
304  _ValueType2;
305 
306  typedef typename std::__are_same<_ValueType1, _ValueType2>::__type
307  _SameType;
308  return __check_sorted_set_aux(__first, __last, _SameType());
309  }
310 
311  template<typename _InputIterator1, typename _InputIterator2,
312  typename _Predicate>
313  inline bool
314  __check_sorted_set(const _InputIterator1& __first,
315  const _InputIterator1& __last,
316  const _InputIterator2&, _Predicate __pred)
317  {
318  typedef typename std::iterator_traits<_InputIterator1>::value_type
319  _ValueType1;
320  typedef typename std::iterator_traits<_InputIterator2>::value_type
321  _ValueType2;
322 
323  typedef typename std::__are_same<_ValueType1, _ValueType2>::__type
324  _SameType;
325  return __check_sorted_set_aux(__first, __last, __pred, _SameType());
326  }
327 
328  // _GLIBCXX_RESOLVE_LIB_DEFECTS
329  // 270. Binary search requirements overly strict
330  // Determine if a sequence is partitioned w.r.t. this element.
331  template<typename _ForwardIterator, typename _Tp>
332  inline bool
333  __check_partitioned_lower(_ForwardIterator __first,
334  _ForwardIterator __last, const _Tp& __value)
335  {
336  while (__first != __last && *__first < __value)
337  ++__first;
338  while (__first != __last && !(*__first < __value))
339  ++__first;
340  return __first == __last;
341  }
342 
343  template<typename _ForwardIterator, typename _Tp>
344  inline bool
345  __check_partitioned_upper(_ForwardIterator __first,
346  _ForwardIterator __last, const _Tp& __value)
347  {
348  while (__first != __last && !(__value < *__first))
349  ++__first;
350  while (__first != __last && __value < *__first)
351  ++__first;
352  return __first == __last;
353  }
354 
355  // Determine if a sequence is partitioned w.r.t. this element.
356  template<typename _ForwardIterator, typename _Tp, typename _Pred>
357  inline bool
358  __check_partitioned_lower(_ForwardIterator __first,
359  _ForwardIterator __last, const _Tp& __value,
360  _Pred __pred)
361  {
362  while (__first != __last && bool(__pred(*__first, __value)))
363  ++__first;
364  while (__first != __last && !bool(__pred(*__first, __value)))
365  ++__first;
366  return __first == __last;
367  }
368 
369  template<typename _ForwardIterator, typename _Tp, typename _Pred>
370  inline bool
371  __check_partitioned_upper(_ForwardIterator __first,
372  _ForwardIterator __last, const _Tp& __value,
373  _Pred __pred)
374  {
375  while (__first != __last && !bool(__pred(__value, *__first)))
376  ++__first;
377  while (__first != __last && bool(__pred(__value, *__first)))
378  ++__first;
379  return __first == __last;
380  }
381 } // namespace __gnu_debug
382 
383 #endif
bool __valid_range_aux2(const _RandomAccessIterator &__first, const _RandomAccessIterator &__last, std::random_access_iterator_tag)
Definition: functions.h:90
bool __check_dereferenceable(_Iterator &)
Definition: functions.h:70
bool __valid_range(const _InputIterator &__first, const _InputIterator &__last)
Definition: functions.h:134
Safe iterator wrapper.
Definition: formatter.h:47
Forward iterators support a superset of input iterator operations.
bool __valid_range_aux(const _Integral &, const _Integral &, std::__true_type)
Definition: functions.h:111
bool __check_singular(const _Safe_iterator< _Iterator, _Sequence > &__x)
Definition: functions.h:63
bool _M_dereferenceable() const
Is the iterator dereferenceable?
const _CharT * __check_string(const _CharT *__s, const _Integer &__n __attribute__((__unused__)))
Definition: functions.h:164
Marking input iterators.
Random-access iterators support a superset of bidirectional iterator operations.