functions.h

Go to the documentation of this file.
00001 // Debugging support implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2003, 2005
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 2, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 #ifndef _GLIBCXX_DEBUG_FUNCTIONS_H
00032 #define _GLIBCXX_DEBUG_FUNCTIONS_H 1
00033 
00034 #include <stddef.h>                       // for ptrdiff_t
00035 #include <bits/stl_iterator_base_types.h> // for iterator_traits, categories
00036 #include <bits/cpp_type_traits.h>         // for __is_integer
00037 
00038 namespace __gnu_debug
00039 {
00040   template<typename _Iterator, typename _Sequence>
00041     class _Safe_iterator;
00042 
00043   // An arbitrary iterator pointer is not singular.
00044   inline bool
00045   __check_singular_aux(const void*) { return false; }
00046 
00047   // We may have an iterator that derives from _Safe_iterator_base but isn't
00048   // a _Safe_iterator.
00049   template<typename _Iterator>
00050     inline bool
00051     __check_singular(_Iterator& __x)
00052     { return __gnu_debug::__check_singular_aux(&__x); }
00053 
00055   template<typename _Tp>
00056     inline bool
00057     __check_singular(const _Tp* __ptr)
00058     { return __ptr == 0; }
00059 
00061   template<typename _Iterator, typename _Sequence>
00062     inline bool
00063     __check_singular(const _Safe_iterator<_Iterator, _Sequence>& __x)
00064     { return __x._M_singular(); }
00065 
00068   template<typename _Iterator>
00069     inline bool
00070     __check_dereferenceable(_Iterator&)
00071     { return true; }
00072 
00074   template<typename _Tp>
00075     inline bool
00076     __check_dereferenceable(const _Tp* __ptr)
00077     { return __ptr; }
00078 
00080   template<typename _Iterator, typename _Sequence>
00081     inline bool
00082     __check_dereferenceable(const _Safe_iterator<_Iterator, _Sequence>& __x)
00083     { return __x._M_dereferenceable(); }
00084 
00088   template<typename _RandomAccessIterator>
00089     inline bool
00090     __valid_range_aux2(const _RandomAccessIterator& __first,
00091                const _RandomAccessIterator& __last,
00092                std::random_access_iterator_tag)
00093     { return __last - __first >= 0; }
00094 
00099   template<typename _InputIterator>
00100     inline bool
00101     __valid_range_aux2(const _InputIterator&, const _InputIterator&,
00102                std::input_iterator_tag)
00103     { return true; }
00104 
00109   template<typename _Integral>
00110     inline bool
00111     __valid_range_aux(const _Integral&, const _Integral&, __true_type)
00112     { return true; }
00113 
00117   template<typename _InputIterator>
00118     inline bool
00119     __valid_range_aux(const _InputIterator& __first,
00120               const _InputIterator& __last, __false_type)
00121   {
00122     typedef typename std::iterator_traits<_InputIterator>::iterator_category
00123       _Category;
00124     return __gnu_debug::__valid_range_aux2(__first, __last, _Category());
00125   }
00126 
00132   template<typename _InputIterator>
00133     inline bool
00134     __valid_range(const _InputIterator& __first, const _InputIterator& __last)
00135     {
00136       typedef typename std::__is_integer<_InputIterator>::__type _Integral;
00137       return __gnu_debug::__valid_range_aux(__first, __last, _Integral());
00138     }
00139 
00141   template<typename _Iterator, typename _Sequence>
00142     inline bool
00143     __valid_range(const _Safe_iterator<_Iterator, _Sequence>& __first,
00144           const _Safe_iterator<_Iterator, _Sequence>& __last)
00145     { return __first._M_valid_range(__last); }
00146 
00147   /* Checks that [first, last) is a valid range, and then returns
00148    * __first. This routine is useful when we can't use a separate
00149    * assertion statement because, e.g., we are in a constructor.
00150   */
00151   template<typename _InputIterator>
00152     inline _InputIterator
00153     __check_valid_range(const _InputIterator& __first,
00154             const _InputIterator& __last
00155             __attribute__((__unused__)))
00156     {
00157       _GLIBCXX_DEBUG_ASSERT(__gnu_debug::__valid_range(__first, __last));
00158       return __first;
00159     }
00160 
00162   template<typename _CharT, typename _Integer>
00163     inline const _CharT*
00164     __check_string(const _CharT* __s,
00165            const _Integer& __n __attribute__((__unused__)))
00166     {
00167 #ifdef _GLIBCXX_DEBUG_PEDANTIC
00168       _GLIBCXX_DEBUG_ASSERT(__s != 0 || __n == 0);
00169 #endif
00170       return __s;
00171     }
00172 
00174   template<typename _CharT>
00175     inline const _CharT*
00176     __check_string(const _CharT* __s)
00177     {
00178 #ifdef _GLIBCXX_DEBUG_PEDANTIC
00179       _GLIBCXX_DEBUG_ASSERT(__s != 0);
00180 #endif
00181       return __s;
00182     }
00183 
00184   // Can't check if an input iterator sequence is sorted, because we
00185   // can't step through the sequence.
00186   template<typename _InputIterator>
00187     inline bool
00188     __check_sorted_aux(const _InputIterator&, const _InputIterator&,
00189                        std::input_iterator_tag)
00190     { return true; }
00191 
00192   // Can verify if a forward iterator sequence is in fact sorted using
00193   // std::__is_sorted
00194   template<typename _ForwardIterator>
00195     inline bool
00196     __check_sorted_aux(_ForwardIterator __first, _ForwardIterator __last,
00197                        std::forward_iterator_tag)
00198     {
00199       if (__first == __last)
00200         return true;
00201 
00202       _ForwardIterator __next = __first;
00203       for (++__next; __next != __last; __first = __next, ++__next) {
00204         if (*__next < *__first)
00205           return false;
00206       }
00207 
00208       return true;
00209     }
00210 
00211   // Can't check if an input iterator sequence is sorted, because we can't step
00212   // through the sequence.
00213   template<typename _InputIterator, typename _Predicate>
00214     inline bool
00215     __check_sorted_aux(const _InputIterator&, const _InputIterator&,
00216                        _Predicate, std::input_iterator_tag)
00217     { return true; }
00218 
00219   // Can verify if a forward iterator sequence is in fact sorted using
00220   // std::__is_sorted
00221   template<typename _ForwardIterator, typename _Predicate>
00222     inline bool
00223     __check_sorted_aux(_ForwardIterator __first, _ForwardIterator __last,
00224                        _Predicate __pred, std::forward_iterator_tag)
00225     {
00226       if (__first == __last)
00227         return true;
00228 
00229       _ForwardIterator __next = __first;
00230       for (++__next; __next != __last; __first = __next, ++__next) {
00231         if (__pred(*__next, *__first))
00232           return false;
00233       }
00234 
00235       return true;
00236     }
00237 
00238   // Determine if a sequence is sorted.
00239   template<typename _InputIterator>
00240     inline bool
00241     __check_sorted(const _InputIterator& __first, const _InputIterator& __last)
00242     {
00243       typedef typename std::iterator_traits<_InputIterator>::iterator_category
00244         _Category;
00245       return __gnu_debug::__check_sorted_aux(__first, __last, _Category());
00246     }
00247 
00248   template<typename _InputIterator, typename _Predicate>
00249     inline bool
00250     __check_sorted(const _InputIterator& __first, const _InputIterator& __last,
00251                    _Predicate __pred)
00252     {
00253       typedef typename std::iterator_traits<_InputIterator>::iterator_category
00254         _Category;
00255       return __gnu_debug::__check_sorted_aux(__first, __last, __pred,
00256                          _Category());
00257     }
00258 
00259   // _GLIBCXX_RESOLVE_LIB_DEFECTS
00260   // 270. Binary search requirements overly strict
00261   // Determine if a sequence is partitioned w.r.t. this element.
00262   template<typename _ForwardIterator, typename _Tp>
00263     inline bool
00264     __check_partitioned(_ForwardIterator __first, _ForwardIterator __last,
00265             const _Tp& __value)
00266     {
00267       while (__first != __last && *__first < __value)
00268     ++__first;
00269       while (__first != __last && !(*__first < __value))
00270     ++__first;
00271       return __first == __last;
00272     }
00273 
00274   // Determine if a sequence is partitioned w.r.t. this element.
00275   template<typename _ForwardIterator, typename _Tp, typename _Pred>
00276     inline bool
00277     __check_partitioned(_ForwardIterator __first, _ForwardIterator __last,
00278             const _Tp& __value, _Pred __pred)
00279     {
00280       while (__first != __last && __pred(*__first, __value))
00281     ++__first;
00282       while (__first != __last && !__pred(*__first, __value))
00283     ++__first;
00284       return __first == __last;
00285     }
00286 } // namespace __gnu_debug
00287 
00288 #endif

Generated on Tue Feb 2 16:55:58 2010 for GNU C++ STL by  doxygen 1.4.7