]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libstdc++-v3/contrib/libstdc++-v3-4.3.3/include/debug/functions.h
Inital import
[l4.git] / l4 / pkg / libstdc++-v3 / contrib / libstdc++-v3-4.3.3 / include / debug / functions.h
1 // Debugging support implementation -*- C++ -*-
2
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
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 /** @file debug/functions.h
32  *  This file is a GNU debug extension to the Standard C++ Library.
33  */
34
35 #ifndef _GLIBCXX_DEBUG_FUNCTIONS_H
36 #define _GLIBCXX_DEBUG_FUNCTIONS_H 1
37
38 #include <bits/c++config.h>
39 #include <cstddef>                       // for ptrdiff_t
40 #include <bits/stl_iterator_base_types.h> // for iterator_traits, categories
41 #include <bits/cpp_type_traits.h>         // for __is_integer
42
43 namespace __gnu_debug
44 {
45   template<typename _Iterator, typename _Sequence>
46     class _Safe_iterator;
47
48   // An arbitrary iterator pointer is not singular.
49   inline bool
50   __check_singular_aux(const void*) { return false; }
51
52   // We may have an iterator that derives from _Safe_iterator_base but isn't
53   // a _Safe_iterator.
54   template<typename _Iterator>
55     inline bool
56     __check_singular(_Iterator& __x)
57     { return __check_singular_aux(&__x); }
58
59   /** Non-NULL pointers are nonsingular. */
60   template<typename _Tp>
61     inline bool
62     __check_singular(const _Tp* __ptr)
63     { return __ptr == 0; }
64
65   /** Safe iterators know if they are singular. */
66   template<typename _Iterator, typename _Sequence>
67     inline bool
68     __check_singular(const _Safe_iterator<_Iterator, _Sequence>& __x)
69     { return __x._M_singular(); }
70
71   /** Assume that some arbitrary iterator is dereferenceable, because we
72       can't prove that it isn't. */
73   template<typename _Iterator>
74     inline bool
75     __check_dereferenceable(_Iterator&)
76     { return true; }
77
78   /** Non-NULL pointers are dereferenceable. */
79   template<typename _Tp>
80     inline bool
81     __check_dereferenceable(const _Tp* __ptr)
82     { return __ptr; }
83
84   /** Safe iterators know if they are singular. */
85   template<typename _Iterator, typename _Sequence>
86     inline bool
87     __check_dereferenceable(const _Safe_iterator<_Iterator, _Sequence>& __x)
88     { return __x._M_dereferenceable(); }
89
90   /** If the distance between two random access iterators is
91    *  nonnegative, assume the range is valid.
92   */
93   template<typename _RandomAccessIterator>
94     inline bool
95     __valid_range_aux2(const _RandomAccessIterator& __first,
96                        const _RandomAccessIterator& __last,
97                        std::random_access_iterator_tag)
98     { return __last - __first >= 0; }
99
100   /** Can't test for a valid range with input iterators, because
101    *  iteration may be destructive. So we just assume that the range
102    *  is valid.
103   */
104   template<typename _InputIterator>
105     inline bool
106     __valid_range_aux2(const _InputIterator&, const _InputIterator&,
107                        std::input_iterator_tag)
108     { return true; }
109
110   /** We say that integral types for a valid range, and defer to other
111    *  routines to realize what to do with integral types instead of
112    *  iterators.
113   */
114   template<typename _Integral>
115     inline bool
116     __valid_range_aux(const _Integral&, const _Integral&, std::__true_type)
117     { return true; }
118
119   /** We have iterators, so figure out what kind of iterators that are
120    *  to see if we can check the range ahead of time.
121   */
122   template<typename _InputIterator>
123     inline bool
124     __valid_range_aux(const _InputIterator& __first,
125                       const _InputIterator& __last, std::__false_type)
126   {
127     typedef typename std::iterator_traits<_InputIterator>::iterator_category
128       _Category;
129     return __valid_range_aux2(__first, __last, _Category());
130   }
131
132   /** Don't know what these iterators are, or if they are even
133    *  iterators (we may get an integral type for InputIterator), so
134    *  see if they are integral and pass them on to the next phase
135    *  otherwise.
136   */
137   template<typename _InputIterator>
138     inline bool
139     __valid_range(const _InputIterator& __first, const _InputIterator& __last)
140     {
141       typedef typename std::__is_integer<_InputIterator>::__type _Integral;
142       return __valid_range_aux(__first, __last, _Integral());
143     }
144
145   /** Safe iterators know how to check if they form a valid range. */
146   template<typename _Iterator, typename _Sequence>
147     inline bool
148     __valid_range(const _Safe_iterator<_Iterator, _Sequence>& __first,
149                   const _Safe_iterator<_Iterator, _Sequence>& __last)
150     { return __first._M_valid_range(__last); }
151
152   /* Checks that [first, last) is a valid range, and then returns
153    * __first. This routine is useful when we can't use a separate
154    * assertion statement because, e.g., we are in a constructor.
155   */
156   template<typename _InputIterator>
157     inline _InputIterator
158     __check_valid_range(const _InputIterator& __first,
159                         const _InputIterator& __last
160                         __attribute__((__unused__)))
161     {
162       _GLIBCXX_DEBUG_ASSERT(__valid_range(__first, __last));
163       return __first;
164     }
165
166   /** Checks that __s is non-NULL or __n == 0, and then returns __s. */
167   template<typename _CharT, typename _Integer>
168     inline const _CharT*
169     __check_string(const _CharT* __s,
170                    const _Integer& __n __attribute__((__unused__)))
171     {
172 #ifdef _GLIBCXX_DEBUG_PEDANTIC
173       _GLIBCXX_DEBUG_ASSERT(__s != 0 || __n == 0);
174 #endif
175       return __s;
176     }
177
178   /** Checks that __s is non-NULL and then returns __s. */
179   template<typename _CharT>
180     inline const _CharT*
181     __check_string(const _CharT* __s)
182     {
183 #ifdef _GLIBCXX_DEBUG_PEDANTIC
184       _GLIBCXX_DEBUG_ASSERT(__s != 0);
185 #endif
186       return __s;
187     }
188
189   // Can't check if an input iterator sequence is sorted, because we
190   // can't step through the sequence.
191   template<typename _InputIterator>
192     inline bool
193     __check_sorted_aux(const _InputIterator&, const _InputIterator&,
194                        std::input_iterator_tag)
195     { return true; }
196
197   // Can verify if a forward iterator sequence is in fact sorted using
198   // std::__is_sorted
199   template<typename _ForwardIterator>
200     inline bool
201     __check_sorted_aux(_ForwardIterator __first, _ForwardIterator __last,
202                        std::forward_iterator_tag)
203     {
204       if (__first == __last)
205         return true;
206
207       _ForwardIterator __next = __first;
208       for (++__next; __next != __last; __first = __next, ++__next)
209         if (*__next < *__first)
210           return false;
211
212       return true;
213     }
214
215   // Can't check if an input iterator sequence is sorted, because we can't step
216   // through the sequence.
217   template<typename _InputIterator, typename _Predicate>
218     inline bool
219     __check_sorted_aux(const _InputIterator&, const _InputIterator&,
220                        _Predicate, std::input_iterator_tag)
221     { return true; }
222
223   // Can verify if a forward iterator sequence is in fact sorted using
224   // std::__is_sorted
225   template<typename _ForwardIterator, typename _Predicate>
226     inline bool
227     __check_sorted_aux(_ForwardIterator __first, _ForwardIterator __last,
228                        _Predicate __pred, std::forward_iterator_tag)
229     {
230       if (__first == __last)
231         return true;
232
233       _ForwardIterator __next = __first;
234       for (++__next; __next != __last; __first = __next, ++__next)
235         if (__pred(*__next, *__first))
236           return false;
237
238       return true;
239     }
240
241   // Determine if a sequence is sorted.
242   template<typename _InputIterator>
243     inline bool
244     __check_sorted(const _InputIterator& __first, const _InputIterator& __last)
245     {
246       typedef typename std::iterator_traits<_InputIterator>::iterator_category
247         _Category;
248
249       // Verify that the < operator for elements in the sequence is a
250       // StrictWeakOrdering by checking that it is irreflexive.
251       _GLIBCXX_DEBUG_ASSERT(__first == __last || !(*__first < *__first));
252
253       return __check_sorted_aux(__first, __last, _Category());
254     }
255
256   template<typename _InputIterator, typename _Predicate>
257     inline bool
258     __check_sorted(const _InputIterator& __first, const _InputIterator& __last,
259                    _Predicate __pred)
260     {
261       typedef typename std::iterator_traits<_InputIterator>::iterator_category
262         _Category;
263
264       // Verify that the predicate is StrictWeakOrdering by checking that it
265       // is irreflexive.
266       _GLIBCXX_DEBUG_ASSERT(__first == __last || !__pred(*__first, *__first));
267
268       return __check_sorted_aux(__first, __last, __pred, _Category());
269     }
270
271   template<typename _InputIterator>
272     inline bool
273     __check_sorted_set_aux(const _InputIterator& __first,
274                            const _InputIterator& __last,
275                            std::__true_type)
276     { return __check_sorted(__first, __last); }
277
278   template<typename _InputIterator>
279     inline bool
280     __check_sorted_set_aux(const _InputIterator&,
281                            const _InputIterator&,
282                            std::__false_type)
283     { return true; }
284
285   template<typename _InputIterator, typename _Predicate>
286     inline bool
287     __check_sorted_set_aux(const _InputIterator& __first,
288                            const _InputIterator& __last,
289                            _Predicate __pred, std::__true_type)
290     { return __check_sorted(__first, __last, __pred); }
291
292   template<typename _InputIterator, typename _Predicate>
293     inline bool
294     __check_sorted_set_aux(const _InputIterator&,
295                            const _InputIterator&, _Predicate,
296                            std::__false_type)
297     { return true; }
298
299   // ... special variant used in std::merge, std::includes, std::set_*.
300   template<typename _InputIterator1, typename _InputIterator2>
301     inline bool
302     __check_sorted_set(const _InputIterator1& __first,
303                        const _InputIterator1& __last,
304                        const _InputIterator2&)
305     {
306       typedef typename std::iterator_traits<_InputIterator1>::value_type
307         _ValueType1;
308       typedef typename std::iterator_traits<_InputIterator2>::value_type
309         _ValueType2;
310
311       typedef typename std::__are_same<_ValueType1, _ValueType2>::__type
312         _SameType;
313       return __check_sorted_set_aux(__first, __last, _SameType());
314     }
315
316   template<typename _InputIterator1, typename _InputIterator2,
317            typename _Predicate>
318     inline bool
319     __check_sorted_set(const _InputIterator1& __first,
320                        const _InputIterator1& __last,
321                        const _InputIterator2&, _Predicate __pred)
322     {
323       typedef typename std::iterator_traits<_InputIterator1>::value_type
324         _ValueType1;
325       typedef typename std::iterator_traits<_InputIterator2>::value_type
326         _ValueType2;
327
328       typedef typename std::__are_same<_ValueType1, _ValueType2>::__type
329         _SameType;
330       return __check_sorted_set_aux(__first, __last, __pred, _SameType());
331    }
332
333   // _GLIBCXX_RESOLVE_LIB_DEFECTS
334   // 270. Binary search requirements overly strict
335   // Determine if a sequence is partitioned w.r.t. this element.
336   template<typename _ForwardIterator, typename _Tp>
337     inline bool
338     __check_partitioned_lower(_ForwardIterator __first,
339                               _ForwardIterator __last, const _Tp& __value)
340     {
341       while (__first != __last && *__first < __value)
342         ++__first;
343       while (__first != __last && !(*__first < __value))
344         ++__first;
345       return __first == __last;
346     }
347
348   template<typename _ForwardIterator, typename _Tp>
349     inline bool
350     __check_partitioned_upper(_ForwardIterator __first,
351                               _ForwardIterator __last, const _Tp& __value)
352     {
353       while (__first != __last && !(__value < *__first))
354         ++__first;
355       while (__first != __last && __value < *__first)
356         ++__first;
357       return __first == __last;
358     }
359
360   // Determine if a sequence is partitioned w.r.t. this element.
361   template<typename _ForwardIterator, typename _Tp, typename _Pred>
362     inline bool
363     __check_partitioned_lower(_ForwardIterator __first,
364                               _ForwardIterator __last, const _Tp& __value,
365                               _Pred __pred)
366     {
367       while (__first != __last && bool(__pred(*__first, __value)))
368         ++__first;
369       while (__first != __last && !bool(__pred(*__first, __value)))
370         ++__first;
371       return __first == __last;
372     }
373
374   template<typename _ForwardIterator, typename _Tp, typename _Pred>
375     inline bool
376     __check_partitioned_upper(_ForwardIterator __first,
377                               _ForwardIterator __last, const _Tp& __value,
378                               _Pred __pred)
379     {
380       while (__first != __last && !bool(__pred(__value, *__first)))
381         ++__first;
382       while (__first != __last && bool(__pred(__value, *__first)))
383         ++__first;
384       return __first == __last;
385     }
386 } // namespace __gnu_debug
387
388 #endif