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