* c-opts.c (c_common_finish): Write dependency information even if
[official-gcc.git] / libstdc++-v3 / include / debug / helper_functions.h
blob80e0e80c6854873ab72504735eceff73ef113797
1 // Debugging support implementation -*- C++ -*-
3 // Copyright (C) 2003-2017 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file debug/helper_functions.h
26 * This file is a GNU debug extension to the Standard C++ Library.
29 #ifndef _GLIBCXX_DEBUG_HELPER_FUNCTIONS_H
30 #define _GLIBCXX_DEBUG_HELPER_FUNCTIONS_H 1
32 #include <bits/stl_iterator_base_types.h> // for iterator_traits,
33 // categories and _Iter_base
34 #include <bits/cpp_type_traits.h> // for __is_integer
36 #include <bits/stl_pair.h> // for pair
38 namespace __gnu_debug
40 /** The precision to which we can calculate the distance between
41 * two iterators.
43 enum _Distance_precision
45 __dp_none, // Not even an iterator type
46 __dp_equality, //< Can compare iterator equality, only
47 __dp_sign, //< Can determine equality and ordering
48 __dp_exact //< Can determine distance precisely
51 template<typename _Iterator,
52 typename = typename std::__is_integer<_Iterator>::__type>
53 struct _Distance_traits
55 private:
56 typedef
57 typename std::iterator_traits<_Iterator>::difference_type _ItDiffType;
59 template<typename _DiffType,
60 typename = typename std::__is_void<_DiffType>::__type>
61 struct _DiffTraits
62 { typedef _DiffType __type; };
64 template<typename _DiffType>
65 struct _DiffTraits<_DiffType, std::__true_type>
66 { typedef std::ptrdiff_t __type; };
68 typedef typename _DiffTraits<_ItDiffType>::__type _DiffType;
70 public:
71 typedef std::pair<_DiffType, _Distance_precision> __type;
74 template<typename _Integral>
75 struct _Distance_traits<_Integral, std::__true_type>
76 { typedef std::pair<std::ptrdiff_t, _Distance_precision> __type; };
78 /** Determine the distance between two iterators with some known
79 * precision.
81 template<typename _Iterator>
82 inline typename _Distance_traits<_Iterator>::__type
83 __get_distance(const _Iterator& __lhs, const _Iterator& __rhs,
84 std::random_access_iterator_tag)
85 { return std::make_pair(__rhs - __lhs, __dp_exact); }
87 template<typename _Iterator>
88 inline typename _Distance_traits<_Iterator>::__type
89 __get_distance(const _Iterator& __lhs, const _Iterator& __rhs,
90 std::input_iterator_tag)
92 if (__lhs == __rhs)
93 return std::make_pair(0, __dp_exact);
95 return std::make_pair(1, __dp_equality);
98 template<typename _Iterator>
99 inline typename _Distance_traits<_Iterator>::__type
100 __get_distance(const _Iterator& __lhs, const _Iterator& __rhs)
101 { return __get_distance(__lhs, __rhs, std::__iterator_category(__lhs)); }
103 /** We say that integral types for a valid range, and defer to other
104 * routines to realize what to do with integral types instead of
105 * iterators.
107 template<typename _Integral>
108 inline bool
109 __valid_range_aux(const _Integral&, const _Integral&,
110 typename _Distance_traits<_Integral>::__type& __dist,
111 std::__true_type)
113 __dist = std::make_pair(0, __dp_none);
114 return true;
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.
120 template<typename _InputIterator>
121 inline bool
122 __valid_range_aux(const _InputIterator& __first,
123 const _InputIterator& __last,
124 typename _Distance_traits<_InputIterator>::__type& __dist,
125 std::__false_type)
127 __dist = __get_distance(__first, __last);
128 switch (__dist.second)
130 case __dp_none:
131 break;
132 case __dp_equality:
133 if (__dist.first == 0)
134 return true;
135 break;
136 case __dp_sign:
137 case __dp_exact:
138 return __dist.first >= 0;
141 // Can't tell so assume it is fine.
142 return true;
145 /** Don't know what these iterators are, or if they are even
146 * iterators (we may get an integral type for InputIterator), so
147 * see if they are integral and pass them on to the next phase
148 * otherwise.
150 template<typename _InputIterator>
151 inline bool
152 __valid_range(const _InputIterator& __first, const _InputIterator& __last,
153 typename _Distance_traits<_InputIterator>::__type& __dist)
155 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
156 return __valid_range_aux(__first, __last, __dist, _Integral());
159 template<typename _InputIterator>
160 inline bool
161 __valid_range(const _InputIterator& __first, const _InputIterator& __last)
163 typename _Distance_traits<_InputIterator>::__type __dist;
164 return __valid_range(__first, __last, __dist);
167 #if __cplusplus < 201103L
168 // Helper struct to detect random access safe iterators.
169 template<typename _Iterator>
170 struct __is_safe_random_iterator
172 enum { __value = 0 };
173 typedef std::__false_type __type;
176 template<typename _Iterator>
177 struct _Siter_base
178 : std::_Iter_base<_Iterator, __is_safe_random_iterator<_Iterator>::__value>
179 { };
181 /** Helper function to extract base iterator of random access safe iterator
182 in order to reduce performance impact of debug mode. Limited to random
183 access iterator because it is the only category for which it is possible
184 to check for correct iterators order in the __valid_range function
185 thanks to the < operator.
187 template<typename _Iterator>
188 inline typename _Siter_base<_Iterator>::iterator_type
189 __base(_Iterator __it)
190 { return _Siter_base<_Iterator>::_S_base(__it); }
191 #else
192 template<typename _Iterator>
193 inline _Iterator
194 __base(_Iterator __it)
195 { return __it; }
196 #endif
198 #if __cplusplus < 201103L
199 template<typename _Iterator>
200 struct _Unsafe_type
201 { typedef _Iterator _Type; };
202 #endif
204 /* Remove debug mode safe iterator layer, if any. */
205 template<typename _Iterator>
206 inline _Iterator
207 __unsafe(_Iterator __it)
208 { return __it; }
211 #endif