OSDN Git Service

2006-11-29 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / debug / formatter.h
1 // Debug-mode error formatting implementation -*- C++ -*-
2
3 // Copyright (C) 2003, 2004, 2005, 2006 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 2, or (at your option)
9 // any later version.
10
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.
15
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,
19 // USA.
20
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.
29
30 /** @file debug/formatter.h
31  *  This file is a GNU debug extension to the Standard C++ Library.
32  */
33
34 #ifndef _GLIBCXX_DEBUG_FORMATTER_H
35 #define _GLIBCXX_DEBUG_FORMATTER_H 1
36
37 #include <typeinfo>
38 #include <debug/debug.h>
39
40 namespace __gnu_debug
41 {
42   using std::type_info;
43
44   /** Determine if the two types are the same. */
45   template<typename _Type1, typename _Type2>
46     struct __is_same
47     {
48       static const bool value = false;
49     };
50
51   template<typename _Type>
52     struct __is_same<_Type, _Type>
53     {
54       static const bool value = true;
55     };
56
57   template<bool> struct __truth { };
58
59   class _Safe_sequence_base;
60
61   template<typename _Iterator, typename _Sequence>
62     class _Safe_iterator;
63
64   template<typename _Sequence>
65     class _Safe_sequence;
66
67   enum _Debug_msg_id
68   {
69     // General checks
70     __msg_valid_range,
71     __msg_insert_singular,
72     __msg_insert_different,
73     __msg_erase_bad,
74     __msg_erase_different,
75     __msg_subscript_oob,
76     __msg_empty,
77     __msg_unpartitioned,
78     __msg_unpartitioned_pred,
79     __msg_unsorted,
80     __msg_unsorted_pred,
81     __msg_not_heap,
82     __msg_not_heap_pred,
83     // std::bitset checks
84     __msg_bad_bitset_write,
85     __msg_bad_bitset_read,
86     __msg_bad_bitset_flip,
87     // std::list checks
88     __msg_self_splice,
89     __msg_splice_alloc,
90     __msg_splice_bad,
91     __msg_splice_other,
92     __msg_splice_overlap,
93     // iterator checks
94     __msg_init_singular,
95     __msg_init_copy_singular,
96     __msg_init_const_singular,
97     __msg_copy_singular,
98     __msg_bad_deref,
99     __msg_bad_inc,
100     __msg_bad_dec,
101     __msg_iter_subscript_oob,
102     __msg_advance_oob,
103     __msg_retreat_oob,
104     __msg_iter_compare_bad,
105     __msg_compare_different,
106     __msg_iter_order_bad,
107     __msg_order_different,
108     __msg_distance_bad,
109     __msg_distance_different,
110     // istream_iterator
111     __msg_deref_istream,
112     __msg_inc_istream,
113     // ostream_iterator
114     __msg_output_ostream,
115     // istreambuf_iterator
116     __msg_deref_istreambuf,
117     __msg_inc_istreambuf
118   };
119
120   class _Error_formatter
121   {
122     /// Whether an iterator is constant, mutable, or unknown
123     enum _Constness
124     {
125       __unknown_constness,
126       __const_iterator,
127       __mutable_iterator,
128       __last_constness
129     };
130
131     // The state of the iterator (fine-grained), if we know it.
132     enum _Iterator_state
133     {
134       __unknown_state,
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
139       __last_state
140     };
141
142     // Tags denoting the type of parameter for construction
143     struct _Is_iterator { };
144     struct _Is_sequence { };
145
146     // A parameter that may be referenced by an error message
147     struct _Parameter
148     {
149       enum
150       {
151         __unused_param,
152         __iterator,
153         __sequence,
154         __integer,
155         __string
156       } _M_kind;
157
158       union
159       {
160         // When _M_kind == __iterator
161         struct
162         {
163           const char*      _M_name;
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;
170         } _M_iterator;
171
172         // When _M_kind == __sequence
173         struct
174         {
175           const char*      _M_name;
176           const void*      _M_address;
177           const type_info* _M_type;
178         } _M_sequence;
179
180         // When _M_kind == __integer
181         struct
182         {
183           const char* _M_name;
184           long        _M_value;
185         } _M_integer;
186
187         // When _M_kind == __string
188         struct
189         {
190           const char* _M_name;
191           const char* _M_value;
192         } _M_string;
193       } _M_variant;
194
195       _Parameter() : _M_kind(__unused_param), _M_variant() { }
196
197       _Parameter(long __value, const char* __name) 
198       : _M_kind(__integer), _M_variant()
199       {
200         _M_variant._M_integer._M_name = __name;
201         _M_variant._M_integer._M_value = __value;
202       }
203
204       _Parameter(const char* __value, const char* __name) 
205       : _M_kind(__string), _M_variant()
206       {
207         _M_variant._M_string._M_name = __name;
208         _M_variant._M_string._M_value = __value;
209       }
210
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()
215         {
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);
225
226           if (__it._M_singular())
227             _M_variant._M_iterator._M_state = __singular;
228           else
229             {
230               bool __is_begin = __it._M_is_begin();
231               bool __is_end = __it._M_is_end();
232               if (__is_end)
233                 _M_variant._M_iterator._M_state = __end;
234               else if (__is_begin)
235                 _M_variant._M_iterator._M_state = __begin;
236               else
237                 _M_variant._M_iterator._M_state = __middle;
238             }
239         }
240
241       template<typename _Type>
242         _Parameter(const _Type*& __it, const char* __name, _Is_iterator)
243         : _M_kind(__iterator), _M_variant()
244         {
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;
252         }
253
254       template<typename _Type>
255         _Parameter(_Type*& __it, const char* __name, _Is_iterator)
256         : _M_kind(__iterator), _M_variant()
257         {
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;
265         }
266
267       template<typename _Iterator>
268         _Parameter(const _Iterator& __it, const char* __name, _Is_iterator)
269         : _M_kind(__iterator), _M_variant()
270         {
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;
279         }
280
281       template<typename _Sequence>
282         _Parameter(const _Safe_sequence<_Sequence>& __seq,
283                    const char* __name, _Is_sequence)
284         : _M_kind(__sequence), _M_variant()
285         {
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);
290         }
291
292       template<typename _Sequence>
293         _Parameter(const _Sequence& __seq, const char* __name, _Is_sequence)
294         : _M_kind(__sequence), _M_variant()
295         {
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);
299         }
300
301       void
302       _M_print_field(const _Error_formatter* __formatter,
303                      const char* __name) const;
304
305       void
306       _M_print_description(const _Error_formatter* __formatter) const;
307     };
308
309     friend struct _Parameter;
310
311   public:
312     template<typename _Iterator>
313       const _Error_formatter&
314       _M_iterator(const _Iterator& __it, const char* __name = 0)  const
315       {
316         if (_M_num_parameters < size_t(__max_parameters))
317           _M_parameters[_M_num_parameters++] = _Parameter(__it, __name,
318                                                           _Is_iterator());
319         return *this;
320       }
321
322     const _Error_formatter&
323     _M_integer(long __value, const char* __name = 0) const
324     {
325       if (_M_num_parameters < size_t(__max_parameters))
326         _M_parameters[_M_num_parameters++] = _Parameter(__value, __name);
327       return *this;
328     }
329
330     const _Error_formatter&
331     _M_string(const char* __value, const char* __name = 0) const
332     {
333       if (_M_num_parameters < size_t(__max_parameters))
334         _M_parameters[_M_num_parameters++] = _Parameter(__value, __name);
335       return *this;
336     }
337
338     template<typename _Sequence>
339       const _Error_formatter&
340       _M_sequence(const _Sequence& __seq, const char* __name = 0) const
341       {
342         if (_M_num_parameters < size_t(__max_parameters))
343           _M_parameters[_M_num_parameters++] = _Parameter(__seq, __name,
344                                                           _Is_sequence());
345         return *this;
346       }
347
348     const _Error_formatter&
349     _M_message(const char* __text) const
350     { _M_text = __text; return *this; }
351
352     const _Error_formatter&
353     _M_message(_Debug_msg_id __id) const;
354
355     void
356     _M_error() const;
357
358   private:
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)
362     { }
363
364     template<typename _Tp>
365       void
366       _M_format_word(char*, int, const char*, _Tp) const;
367
368     void
369     _M_print_word(const char* __word) const;
370
371     void
372     _M_print_string(const char* __string) const;
373
374     enum { __max_parameters = 9 };
375
376     const char*         _M_file;
377     size_t              _M_line;
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;
386
387   public:
388     static _Error_formatter
389     _M_at(const char* __file, size_t __line)
390     { return _Error_formatter(__file, __line); }
391   };
392 } // namespace __gnu_debug
393
394 #endif