1 // Debug-mode error formatting implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
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 2, or (at your option)
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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 #ifndef _GLIBCXX_DEBUG_FORMATTER_H
31 #define _GLIBCXX_DEBUG_FORMATTER_H 1
34 #include <debug/debug.h>
40 /** Determine if the two types are the same. */
41 template<typename _Type1
, typename _Type2
>
44 static const bool value
= false;
47 template<typename _Type
>
48 struct __is_same
<_Type
, _Type
>
50 static const bool value
= true;
53 template<bool> struct __truth
{ };
55 class _Safe_sequence_base
;
57 template<typename _Iterator
, typename _Sequence
>
60 template<typename _Sequence
>
67 __msg_insert_singular
,
68 __msg_insert_different
,
70 __msg_erase_different
,
74 __msg_unpartitioned_pred
,
80 __msg_bad_bitset_write
,
81 __msg_bad_bitset_read
,
82 __msg_bad_bitset_flip
,
91 __msg_init_copy_singular
,
92 __msg_init_const_singular
,
97 __msg_iter_subscript_oob
,
100 __msg_iter_compare_bad
,
101 __msg_compare_different
,
102 __msg_iter_order_bad
,
103 __msg_order_different
,
105 __msg_distance_different
,
110 __msg_output_ostream
,
111 // istreambuf_iterator
112 __msg_deref_istreambuf
,
116 class _Error_formatter
118 /// Whether an iterator is constant, mutable, or unknown
127 // The state of the iterator (fine-grained), if we know it.
131 __singular
, // singular, may still be attached to a sequence
132 __begin
, // dereferenceable, and at the beginning
133 __middle
, // dereferenceable, not at the beginning
134 __end
, // past-the-end, may be at beginning if sequence empty
138 // Tags denoting the type of parameter for construction
139 struct _Is_iterator
{ };
140 struct _Is_sequence
{ };
142 // A parameter that may be referenced by an error message
156 // When _M_kind == __iterator
160 const void* _M_address
;
161 const type_info
* _M_type
;
162 _Constness _M_constness
;
163 _Iterator_state _M_state
;
164 const void* _M_sequence
;
165 const type_info
* _M_seq_type
;
168 // When _M_kind == __sequence
172 const void* _M_address
;
173 const type_info
* _M_type
;
176 // When _M_kind == __integer
183 // When _M_kind == __string
187 const char* _M_value
;
191 _Parameter() : _M_kind(__unused_param
), _M_variant() { }
193 _Parameter(long __value
, const char* __name
)
194 : _M_kind(__integer
), _M_variant()
196 _M_variant
._M_integer
._M_name
= __name
;
197 _M_variant
._M_integer
._M_value
= __value
;
200 _Parameter(const char* __value
, const char* __name
)
201 : _M_kind(__string
), _M_variant()
203 _M_variant
._M_string
._M_name
= __name
;
204 _M_variant
._M_string
._M_value
= __value
;
207 template<typename _Iterator
, typename _Sequence
>
208 _Parameter(const _Safe_iterator
<_Iterator
, _Sequence
>& __it
,
209 const char* __name
, _Is_iterator
)
210 : _M_kind(__iterator
), _M_variant()
212 _M_variant
._M_iterator
._M_name
= __name
;
213 _M_variant
._M_iterator
._M_address
= &__it
;
214 _M_variant
._M_iterator
._M_type
= &typeid(__it
);
215 _M_variant
._M_iterator
._M_constness
=
216 __is_same
<_Safe_iterator
<_Iterator
, _Sequence
>,
217 typename
_Sequence::iterator
>::
218 value
? __mutable_iterator
: __const_iterator
;
219 _M_variant
._M_iterator
._M_sequence
= __it
._M_get_sequence();
220 _M_variant
._M_iterator
._M_seq_type
= &typeid(_Sequence
);
222 if (__it
._M_singular())
223 _M_variant
._M_iterator
._M_state
= __singular
;
226 bool __is_begin
= __it
._M_is_begin();
227 bool __is_end
= __it
._M_is_end();
229 _M_variant
._M_iterator
._M_state
= __end
;
231 _M_variant
._M_iterator
._M_state
= __begin
;
233 _M_variant
._M_iterator
._M_state
= __middle
;
237 template<typename _Type
>
238 _Parameter(const _Type
*& __it
, const char* __name
, _Is_iterator
)
239 : _M_kind(__iterator
), _M_variant()
241 _M_variant
._M_iterator
._M_name
= __name
;
242 _M_variant
._M_iterator
._M_address
= &__it
;
243 _M_variant
._M_iterator
._M_type
= &typeid(__it
);
244 _M_variant
._M_iterator
._M_constness
= __mutable_iterator
;
245 _M_variant
._M_iterator
._M_state
= __it
? __unknown_state
: __singular
;
246 _M_variant
._M_iterator
._M_sequence
= 0;
247 _M_variant
._M_iterator
._M_seq_type
= 0;
250 template<typename _Type
>
251 _Parameter(_Type
*& __it
, const char* __name
, _Is_iterator
)
252 : _M_kind(__iterator
), _M_variant()
254 _M_variant
._M_iterator
._M_name
= __name
;
255 _M_variant
._M_iterator
._M_address
= &__it
;
256 _M_variant
._M_iterator
._M_type
= &typeid(__it
);
257 _M_variant
._M_iterator
._M_constness
= __const_iterator
;
258 _M_variant
._M_iterator
._M_state
= __it
? __unknown_state
: __singular
;
259 _M_variant
._M_iterator
._M_sequence
= 0;
260 _M_variant
._M_iterator
._M_seq_type
= 0;
263 template<typename _Iterator
>
264 _Parameter(const _Iterator
& __it
, const char* __name
, _Is_iterator
)
265 : _M_kind(__iterator
), _M_variant()
267 _M_variant
._M_iterator
._M_name
= __name
;
268 _M_variant
._M_iterator
._M_address
= &__it
;
269 _M_variant
._M_iterator
._M_type
= &typeid(__it
);
270 _M_variant
._M_iterator
._M_constness
= __unknown_constness
;
271 _M_variant
._M_iterator
._M_state
=
272 __gnu_debug::__check_singular(__it
)? __singular
: __unknown_state
;
273 _M_variant
._M_iterator
._M_sequence
= 0;
274 _M_variant
._M_iterator
._M_seq_type
= 0;
277 template<typename _Sequence
>
278 _Parameter(const _Safe_sequence
<_Sequence
>& __seq
,
279 const char* __name
, _Is_sequence
)
280 : _M_kind(__sequence
), _M_variant()
282 _M_variant
._M_sequence
._M_name
= __name
;
283 _M_variant
._M_sequence
._M_address
=
284 static_cast<const _Sequence
*>(&__seq
);
285 _M_variant
._M_sequence
._M_type
= &typeid(_Sequence
);
288 template<typename _Sequence
>
289 _Parameter(const _Sequence
& __seq
, const char* __name
, _Is_sequence
)
290 : _M_kind(__sequence
), _M_variant()
292 _M_variant
._M_sequence
._M_name
= __name
;
293 _M_variant
._M_sequence
._M_address
= &__seq
;
294 _M_variant
._M_sequence
._M_type
= &typeid(_Sequence
);
298 _M_print_field(const _Error_formatter
* __formatter
,
299 const char* __name
) const;
302 _M_print_description(const _Error_formatter
* __formatter
) const;
305 friend struct _Parameter
;
308 template<typename _Iterator
>
309 const _Error_formatter
&
310 _M_iterator(const _Iterator
& __it
, const char* __name
= 0) const
312 if (_M_num_parameters
< size_t(__max_parameters
))
313 _M_parameters
[_M_num_parameters
++] = _Parameter(__it
, __name
,
318 const _Error_formatter
&
319 _M_integer(long __value
, const char* __name
= 0) const
321 if (_M_num_parameters
< size_t(__max_parameters
))
322 _M_parameters
[_M_num_parameters
++] = _Parameter(__value
, __name
);
326 const _Error_formatter
&
327 _M_string(const char* __value
, const char* __name
= 0) const
329 if (_M_num_parameters
< size_t(__max_parameters
))
330 _M_parameters
[_M_num_parameters
++] = _Parameter(__value
, __name
);
334 template<typename _Sequence
>
335 const _Error_formatter
&
336 _M_sequence(const _Sequence
& __seq
, const char* __name
= 0) const
338 if (_M_num_parameters
< size_t(__max_parameters
))
339 _M_parameters
[_M_num_parameters
++] = _Parameter(__seq
, __name
,
344 const _Error_formatter
&
345 _M_message(const char* __text
) const
346 { _M_text
= __text
; return *this; }
348 const _Error_formatter
&
349 _M_message(_Debug_msg_id __id
) const;
355 _Error_formatter(const char* __file
, size_t __line
)
356 : _M_file(__file
), _M_line(__line
), _M_num_parameters(0), _M_text(0),
357 _M_max_length(78), _M_column(1), _M_first_line(true), _M_wordwrap(false)
360 template<typename _Tp
>
362 _M_format_word(char*, int, const char*, _Tp
) const;
365 _M_print_word(const char* __word
) const;
368 _M_print_string(const char* __string
) const;
370 enum { __max_parameters
= 9 };
374 mutable _Parameter _M_parameters
[__max_parameters
];
375 mutable size_t _M_num_parameters
;
376 mutable const char* _M_text
;
377 mutable size_t _M_max_length
;
378 enum { _M_indent
= 4 } ;
379 mutable size_t _M_column
;
380 mutable bool _M_first_line
;
381 mutable bool _M_wordwrap
;
384 static _Error_formatter
385 _M_at(const char* __file
, size_t __line
)
386 { return _Error_formatter(__file
, __line
); }
388 } // namespace __gnu_debug