1 // Debug-mode error formatting implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006 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 /** @file debug/formatter.h
31 * This file is a GNU debug extension to the Standard C++ Library.
34 #ifndef _GLIBCXX_DEBUG_FORMATTER_H
35 #define _GLIBCXX_DEBUG_FORMATTER_H 1
38 #include <debug/debug.h>
44 /** Determine if the two types are the same. */
45 template<typename _Type1, typename _Type2>
48 static const bool value = false;
51 template<typename _Type>
52 struct __is_same<_Type, _Type>
54 static const bool value = true;
57 template<bool> struct __truth { };
59 class _Safe_sequence_base;
61 template<typename _Iterator, typename _Sequence>
64 template<typename _Sequence>
71 __msg_insert_singular,
72 __msg_insert_different,
74 __msg_erase_different,
78 __msg_unpartitioned_pred,
84 __msg_bad_bitset_write,
85 __msg_bad_bitset_read,
86 __msg_bad_bitset_flip,
95 __msg_init_copy_singular,
96 __msg_init_const_singular,
101 __msg_iter_subscript_oob,
104 __msg_iter_compare_bad,
105 __msg_compare_different,
106 __msg_iter_order_bad,
107 __msg_order_different,
109 __msg_distance_different,
114 __msg_output_ostream,
115 // istreambuf_iterator
116 __msg_deref_istreambuf,
120 class _Error_formatter
122 /// Whether an iterator is constant, mutable, or unknown
131 // The state of the iterator (fine-grained), if we know it.
135 __singular, // singular, may still be attached to a sequence
136 __begin, // dereferenceable, and at the beginning
137 __middle, // dereferenceable, not at the beginning
138 __end, // past-the-end, may be at beginning if sequence empty
142 // Tags denoting the type of parameter for construction
143 struct _Is_iterator { };
144 struct _Is_sequence { };
146 // A parameter that may be referenced by an error message
160 // When _M_kind == __iterator
164 const void* _M_address;
165 const type_info* _M_type;
166 _Constness _M_constness;
167 _Iterator_state _M_state;
168 const void* _M_sequence;
169 const type_info* _M_seq_type;
172 // When _M_kind == __sequence
176 const void* _M_address;
177 const type_info* _M_type;
180 // When _M_kind == __integer
187 // When _M_kind == __string
191 const char* _M_value;
195 _Parameter() : _M_kind(__unused_param), _M_variant() { }
197 _Parameter(long __value, const char* __name)
198 : _M_kind(__integer), _M_variant()
200 _M_variant._M_integer._M_name = __name;
201 _M_variant._M_integer._M_value = __value;
204 _Parameter(const char* __value, const char* __name)
205 : _M_kind(__string), _M_variant()
207 _M_variant._M_string._M_name = __name;
208 _M_variant._M_string._M_value = __value;
211 template<typename _Iterator, typename _Sequence>
212 _Parameter(const _Safe_iterator<_Iterator, _Sequence>& __it,
213 const char* __name, _Is_iterator)
214 : _M_kind(__iterator), _M_variant()
216 _M_variant._M_iterator._M_name = __name;
217 _M_variant._M_iterator._M_address = &__it;
218 _M_variant._M_iterator._M_type = &typeid(__it);
219 _M_variant._M_iterator._M_constness =
220 __is_same<_Safe_iterator<_Iterator, _Sequence>,
221 typename _Sequence::iterator>::
222 value? __mutable_iterator : __const_iterator;
223 _M_variant._M_iterator._M_sequence = __it._M_get_sequence();
224 _M_variant._M_iterator._M_seq_type = &typeid(_Sequence);
226 if (__it._M_singular())
227 _M_variant._M_iterator._M_state = __singular;
230 bool __is_begin = __it._M_is_begin();
231 bool __is_end = __it._M_is_end();
233 _M_variant._M_iterator._M_state = __end;
235 _M_variant._M_iterator._M_state = __begin;
237 _M_variant._M_iterator._M_state = __middle;
241 template<typename _Type>
242 _Parameter(const _Type*& __it, const char* __name, _Is_iterator)
243 : _M_kind(__iterator), _M_variant()
245 _M_variant._M_iterator._M_name = __name;
246 _M_variant._M_iterator._M_address = &__it;
247 _M_variant._M_iterator._M_type = &typeid(__it);
248 _M_variant._M_iterator._M_constness = __mutable_iterator;
249 _M_variant._M_iterator._M_state = __it? __unknown_state : __singular;
250 _M_variant._M_iterator._M_sequence = 0;
251 _M_variant._M_iterator._M_seq_type = 0;
254 template<typename _Type>
255 _Parameter(_Type*& __it, const char* __name, _Is_iterator)
256 : _M_kind(__iterator), _M_variant()
258 _M_variant._M_iterator._M_name = __name;
259 _M_variant._M_iterator._M_address = &__it;
260 _M_variant._M_iterator._M_type = &typeid(__it);
261 _M_variant._M_iterator._M_constness = __const_iterator;
262 _M_variant._M_iterator._M_state = __it? __unknown_state : __singular;
263 _M_variant._M_iterator._M_sequence = 0;
264 _M_variant._M_iterator._M_seq_type = 0;
267 template<typename _Iterator>
268 _Parameter(const _Iterator& __it, const char* __name, _Is_iterator)
269 : _M_kind(__iterator), _M_variant()
271 _M_variant._M_iterator._M_name = __name;
272 _M_variant._M_iterator._M_address = &__it;
273 _M_variant._M_iterator._M_type = &typeid(__it);
274 _M_variant._M_iterator._M_constness = __unknown_constness;
275 _M_variant._M_iterator._M_state =
276 __gnu_debug::__check_singular(__it)? __singular : __unknown_state;
277 _M_variant._M_iterator._M_sequence = 0;
278 _M_variant._M_iterator._M_seq_type = 0;
281 template<typename _Sequence>
282 _Parameter(const _Safe_sequence<_Sequence>& __seq,
283 const char* __name, _Is_sequence)
284 : _M_kind(__sequence), _M_variant()
286 _M_variant._M_sequence._M_name = __name;
287 _M_variant._M_sequence._M_address =
288 static_cast<const _Sequence*>(&__seq);
289 _M_variant._M_sequence._M_type = &typeid(_Sequence);
292 template<typename _Sequence>
293 _Parameter(const _Sequence& __seq, const char* __name, _Is_sequence)
294 : _M_kind(__sequence), _M_variant()
296 _M_variant._M_sequence._M_name = __name;
297 _M_variant._M_sequence._M_address = &__seq;
298 _M_variant._M_sequence._M_type = &typeid(_Sequence);
302 _M_print_field(const _Error_formatter* __formatter,
303 const char* __name) const;
306 _M_print_description(const _Error_formatter* __formatter) const;
309 friend struct _Parameter;
312 template<typename _Iterator>
313 const _Error_formatter&
314 _M_iterator(const _Iterator& __it, const char* __name = 0) const
316 if (_M_num_parameters < size_t(__max_parameters))
317 _M_parameters[_M_num_parameters++] = _Parameter(__it, __name,
322 const _Error_formatter&
323 _M_integer(long __value, const char* __name = 0) const
325 if (_M_num_parameters < size_t(__max_parameters))
326 _M_parameters[_M_num_parameters++] = _Parameter(__value, __name);
330 const _Error_formatter&
331 _M_string(const char* __value, const char* __name = 0) const
333 if (_M_num_parameters < size_t(__max_parameters))
334 _M_parameters[_M_num_parameters++] = _Parameter(__value, __name);
338 template<typename _Sequence>
339 const _Error_formatter&
340 _M_sequence(const _Sequence& __seq, const char* __name = 0) const
342 if (_M_num_parameters < size_t(__max_parameters))
343 _M_parameters[_M_num_parameters++] = _Parameter(__seq, __name,
348 const _Error_formatter&
349 _M_message(const char* __text) const
350 { _M_text = __text; return *this; }
352 const _Error_formatter&
353 _M_message(_Debug_msg_id __id) const;
359 _Error_formatter(const char* __file, size_t __line)
360 : _M_file(__file), _M_line(__line), _M_num_parameters(0), _M_text(0),
361 _M_max_length(78), _M_column(1), _M_first_line(true), _M_wordwrap(false)
364 template<typename _Tp>
366 _M_format_word(char*, int, const char*, _Tp) const;
369 _M_print_word(const char* __word) const;
372 _M_print_string(const char* __string) const;
374 enum { __max_parameters = 9 };
378 mutable _Parameter _M_parameters[__max_parameters];
379 mutable size_t _M_num_parameters;
380 mutable const char* _M_text;
381 mutable size_t _M_max_length;
382 enum { _M_indent = 4 } ;
383 mutable size_t _M_column;
384 mutable bool _M_first_line;
385 mutable bool _M_wordwrap;
388 static _Error_formatter
389 _M_at(const char* __file, size_t __line)
390 { return _Error_formatter(__file, __line); }
392 } // namespace __gnu_debug