OSDN Git Service

2003-11-11 Doug Gregor <gregod@cs.rpi.edu>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / debug / formatter.h
1 // Debug-mode error formatting implementation -*- C++ -*-
2
3 // Copyright (C) 2003
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library.  This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING.  If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction.  Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License.  This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 #ifndef _GLIBCXX_DEBUG_FORMATTER_H
32 #define _GLIBCXX_DEBUG_FORMATTER_H 1
33
34 #include <typeinfo>
35 #include <debug/debug.h>
36
37 namespace __gnu_debug
38 {
39   /** Determine if the two types are the same. */
40   template<typename _Type1, typename _Type2>
41     struct __is_same
42     {
43       static const bool value = false;
44     };
45
46   template<typename _Type>
47     struct __is_same<_Type, _Type>
48     {
49       static const bool value = true;
50     };
51
52   template<bool> struct __truth { };
53
54   class _Safe_sequence_base;
55
56   template<typename _Iterator, typename _Sequence> 
57     class _Safe_iterator;
58
59   template<typename _Sequence> 
60     class _Safe_sequence;
61
62   enum _Debug_msg_id
63   {
64     // General checks
65     __msg_valid_range,
66     __msg_insert_singular,
67     __msg_insert_different,
68     __msg_erase_bad,
69     __msg_erase_different,
70     __msg_subscript_oob,
71     __msg_empty,
72     __msg_unpartitioned,
73     __msg_unpartitioned_pred,
74     __msg_unsorted,
75     __msg_unsorted_pred,
76     __msg_not_heap,
77     __msg_not_heap_pred,
78     // std::bitset checks
79     __msg_bad_bitset_write,
80     __msg_bad_bitset_read,
81     __msg_bad_bitset_flip,
82     // std::list checks
83     __msg_self_splice,
84     __msg_splice_alloc,
85     __msg_splice_bad,
86     __msg_splice_other,
87     __msg_splice_overlap,
88     // iterator checks
89     __msg_init_singular,
90     __msg_init_copy_singular,
91     __msg_init_const_singular,
92     __msg_copy_singular,
93     __msg_bad_deref,
94     __msg_bad_inc,
95     __msg_bad_dec,
96     __msg_iter_subscript_oob,
97     __msg_advance_oob,
98     __msg_retreat_oob,
99     __msg_iter_compare_bad,
100     __msg_compare_different,
101     __msg_iter_order_bad,
102     __msg_order_different,
103     __msg_distance_bad,
104     __msg_distance_different,
105     // istream_iterator
106     __msg_deref_istream,
107     __msg_inc_istream,
108     // ostream_iterator
109     __msg_output_ostream,
110     // istreambuf_iterator
111     __msg_deref_istreambuf,
112     __msg_inc_istreambuf
113   };
114
115   class _Error_formatter
116   {
117     /// Whether an iterator is constant, mutable, or unknown
118     enum _Constness
119     {
120       __unknown_constness,
121       __const_iterator,
122       __mutable_iterator,
123       __last_constness
124     }; 
125
126     // The state of the iterator (fine-grained), if we know it.
127     enum _Iterator_state
128     {
129       __unknown_state,
130       __singular,      // singular, may still be attached to a sequence
131       __begin,         // dereferenceable, and at the beginning
132       __middle,        // dereferenceable, not at the beginning
133       __end,           // past-the-end, may be at beginning if sequence empty
134       __last_state
135     };
136
137     // Tags denoting the type of parameter for construction
138     struct _Is_iterator { };
139     struct _Is_sequence { };
140
141     // A parameter that may be referenced by an error message
142     struct _Parameter
143     {
144       enum 
145       { 
146         __unused_param, 
147         __iterator, 
148         __sequence, 
149         __integer,
150         __string
151       } _M_kind;
152       
153       union
154       {
155         // When _M_kind == __iterator
156         struct 
157         {
158           const char*      _M_name;      
159           const void*      _M_address;   
160           const type_info* _M_type;   
161           _Constness       _M_constness;
162           _Iterator_state  _M_state;
163           const void*      _M_sequence;  
164           const type_info* _M_seq_type;
165         } _M_iterator;
166         
167         // When _M_kind == __sequence
168         struct
169         {
170           const char*      _M_name;
171           const void*      _M_address;
172           const type_info* _M_type;
173         } _M_sequence;
174
175         // When _M_kind == __integer
176         struct
177         {
178           const char* _M_name;
179           long        _M_value;
180         } _M_integer;
181
182         // When _M_kind == __string
183         struct
184         {
185           const char* _M_name;
186           const char* _M_value;
187         } _M_string;
188       } _M_variant;
189
190       _Parameter() : _M_kind(__unused_param) { }
191       
192       _Parameter(long __value, const char* __name) 
193       : _M_kind(__integer)
194       { 
195         _M_variant._M_integer._M_name = __name;
196         _M_variant._M_integer._M_value = __value; 
197       }
198
199       _Parameter(const char* __value, const char* __name)
200       : _M_kind(__string)
201       {
202         _M_variant._M_string._M_name = __name;
203         _M_variant._M_string._M_value = __value; 
204       }
205
206       template<typename _Iterator, typename _Sequence>
207         _Parameter(const _Safe_iterator<_Iterator, _Sequence>& __it,
208                    const char* __name, _Is_iterator)
209         : _M_kind(__iterator)
210         {
211           _M_variant._M_iterator._M_name = __name;
212           _M_variant._M_iterator._M_address = &__it;
213           _M_variant._M_iterator._M_type = &typeid(__it);
214           _M_variant._M_iterator._M_constness = 
215             __is_same<_Safe_iterator<_Iterator, _Sequence>,
216                                  typename _Sequence::iterator>::
217               value? __mutable_iterator : __const_iterator;
218           _M_variant._M_iterator._M_sequence = __it._M_get_sequence();
219           _M_variant._M_iterator._M_seq_type = &typeid(_Sequence);
220
221           if (__it._M_singular())
222             _M_variant._M_iterator._M_state = __singular;
223           else
224             {
225               bool __is_begin = __it._M_is_begin();
226               bool __is_end = __it._M_is_end();
227               if (__is_end)
228                 _M_variant._M_iterator._M_state = __end;
229               else if (__is_begin)
230                 _M_variant._M_iterator._M_state = __begin;
231               else
232                 _M_variant._M_iterator._M_state = __middle;
233             }
234         }
235
236       template<typename _Type>
237         _Parameter(const _Type*& __it, const char* __name, _Is_iterator)
238         : _M_kind(__iterator)
239         {
240           _M_variant._M_iterator._M_name = __name;
241           _M_variant._M_iterator._M_address = &__it;
242           _M_variant._M_iterator._M_type = &typeid(__it);
243           _M_variant._M_iterator._M_constness = __mutable_iterator;
244           _M_variant._M_iterator._M_state = __it? __unknown_state : __singular;
245           _M_variant._M_iterator._M_sequence = 0;
246           _M_variant._M_iterator._M_seq_type = 0;
247         }
248
249       template<typename _Type>
250         _Parameter(_Type*& __it, const char* __name, _Is_iterator)
251         : _M_kind(__iterator)
252         {
253           _M_variant._M_iterator._M_name = __name;
254           _M_variant._M_iterator._M_address = &__it;
255           _M_variant._M_iterator._M_type = &typeid(__it);
256           _M_variant._M_iterator._M_constness = __const_iterator;
257           _M_variant._M_iterator._M_state = __it? __unknown_state : __singular;
258           _M_variant._M_iterator._M_sequence = 0;
259           _M_variant._M_iterator._M_seq_type = 0;
260         }
261       
262       template<typename _Iterator>
263         _Parameter(const _Iterator& __it, const char* __name, _Is_iterator)
264         : _M_kind(__iterator)
265         {
266           _M_variant._M_iterator._M_name = __name;
267           _M_variant._M_iterator._M_address = &__it;
268           _M_variant._M_iterator._M_type = &typeid(__it);
269           _M_variant._M_iterator._M_constness = __unknown_constness;
270           _M_variant._M_iterator._M_state = 
271             __gnu_debug::__check_singular(__it)? __singular : __unknown_state;
272           _M_variant._M_iterator._M_sequence = 0;
273           _M_variant._M_iterator._M_seq_type = 0;
274         }
275
276       template<typename _Sequence>
277         _Parameter(const _Safe_sequence<_Sequence>& __seq,
278                    const char* __name, _Is_sequence)
279         : _M_kind(__sequence)
280         {
281           _M_variant._M_sequence._M_name = __name;
282           _M_variant._M_sequence._M_address = 
283             static_cast<const _Sequence*>(&__seq);
284           _M_variant._M_sequence._M_type = &typeid(_Sequence);
285         }
286
287       template<typename _Sequence>
288         _Parameter(const _Sequence& __seq, const char* __name, _Is_sequence)
289         : _M_kind(__sequence)
290         {
291           _M_variant._M_sequence._M_name = __name;
292           _M_variant._M_sequence._M_address = &__seq;
293           _M_variant._M_sequence._M_type = &typeid(_Sequence);
294         }
295       
296       void
297       _M_print_field(const _Error_formatter* __formatter, 
298                      const char* __name) const;
299                                          
300       void
301       _M_print_description(const _Error_formatter* __formatter) const;
302     };
303
304     friend struct _Parameter;
305
306   public:    
307     template<typename _Iterator>
308       const _Error_formatter&
309       _M_iterator(const _Iterator& __it, const char* __name = 0)  const
310       {
311         if (_M_num_parameters < __max_parameters)
312           _M_parameters[_M_num_parameters++] = _Parameter(__it, __name,
313                                                           _Is_iterator());
314         return *this;
315       }
316
317     const _Error_formatter&
318     _M_integer(long __value, const char* __name = 0) const
319     {
320       if (_M_num_parameters < __max_parameters)
321         _M_parameters[_M_num_parameters++] = _Parameter(__value, __name);
322       return *this;
323     }
324
325     const _Error_formatter&
326     _M_string(const char* __value, const char* __name = 0) const
327     {
328       if (_M_num_parameters < __max_parameters)
329         _M_parameters[_M_num_parameters++] = _Parameter(__value, __name);
330       return *this;
331     }
332
333     template<typename _Sequence>
334       const _Error_formatter&
335       _M_sequence(const _Sequence& __seq, const char* __name = 0) const
336       {
337         if (_M_num_parameters < __max_parameters)
338           _M_parameters[_M_num_parameters++] = _Parameter(__seq, __name, 
339                                                           _Is_sequence());
340         return *this;
341       }
342
343     const _Error_formatter&
344     _M_message(const char* __text) const
345     { _M_text = __text; return *this; }
346
347     const _Error_formatter&
348     _M_message(_Debug_msg_id __id) const;
349
350     void 
351     _M_error() const;
352
353   private:
354     _Error_formatter(const char* __file, size_t __line)
355     : _M_file(__file), _M_line(__line), _M_num_parameters(0), _M_text(0),
356       _M_max_length(78), _M_column(1), _M_first_line(true), _M_wordwrap(false)
357     { }
358
359     void 
360     _M_print_word(const char* __word) const;
361
362     void 
363     _M_print_string(const char* __string) const;
364
365     enum { __max_parameters = 9 };
366
367     const char*         _M_file;
368     size_t              _M_line;
369     mutable _Parameter  _M_parameters[__max_parameters];
370     mutable size_t      _M_num_parameters;
371     mutable const char* _M_text;
372     mutable size_t      _M_max_length;
373     enum { _M_indent = 4 } ;
374     mutable size_t      _M_column;
375     mutable bool        _M_first_line;
376     mutable bool        _M_wordwrap;
377
378   public:
379     static _Error_formatter
380     _M_at(const char* __file, size_t __line)
381     { return _Error_formatter(__file, __line); }
382   };
383 } // namespace __gnu_debug
384
385 #endif