formatter.h

Go to the documentation of this file.
00001 // Debug-mode error formatting implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file debug/formatter.h
00026  *  This file is a GNU debug extension to the Standard C++ Library.
00027  */
00028 
00029 #ifndef _GLIBCXX_DEBUG_FORMATTER_H
00030 #define _GLIBCXX_DEBUG_FORMATTER_H 1
00031 
00032 #include <typeinfo>
00033 #include <debug/debug.h>
00034 
00035 namespace __gnu_debug
00036 {
00037   using std::type_info;
00038 
00039   /** Determine if the two types are the same. */
00040   template<typename _Type1, typename _Type2>
00041     struct __is_same
00042     {
00043       static const bool value = false;
00044     };
00045 
00046   template<typename _Type>
00047     struct __is_same<_Type, _Type>
00048     {
00049       static const bool value = true;
00050     };
00051 
00052   template<bool> struct __truth { };
00053 
00054   class _Safe_sequence_base;
00055 
00056   template<typename _Iterator, typename _Sequence>
00057     class _Safe_iterator;
00058 
00059   template<typename _Sequence>
00060     class _Safe_sequence;
00061 
00062   enum _Debug_msg_id
00063   {
00064     // General checks
00065     __msg_valid_range,
00066     __msg_insert_singular,
00067     __msg_insert_different,
00068     __msg_erase_bad,
00069     __msg_erase_different,
00070     __msg_subscript_oob,
00071     __msg_empty,
00072     __msg_unpartitioned,
00073     __msg_unpartitioned_pred,
00074     __msg_unsorted,
00075     __msg_unsorted_pred,
00076     __msg_not_heap,
00077     __msg_not_heap_pred,
00078     // std::bitset checks
00079     __msg_bad_bitset_write,
00080     __msg_bad_bitset_read,
00081     __msg_bad_bitset_flip,
00082     // std::list checks
00083     __msg_self_splice,
00084     __msg_splice_alloc,
00085     __msg_splice_bad,
00086     __msg_splice_other,
00087     __msg_splice_overlap,
00088     // iterator checks
00089     __msg_init_singular,
00090     __msg_init_copy_singular,
00091     __msg_init_const_singular,
00092     __msg_copy_singular,
00093     __msg_bad_deref,
00094     __msg_bad_inc,
00095     __msg_bad_dec,
00096     __msg_iter_subscript_oob,
00097     __msg_advance_oob,
00098     __msg_retreat_oob,
00099     __msg_iter_compare_bad,
00100     __msg_compare_different,
00101     __msg_iter_order_bad,
00102     __msg_order_different,
00103     __msg_distance_bad,
00104     __msg_distance_different,
00105     // istream_iterator
00106     __msg_deref_istream,
00107     __msg_inc_istream,
00108     // ostream_iterator
00109     __msg_output_ostream,
00110     // istreambuf_iterator
00111     __msg_deref_istreambuf,
00112     __msg_inc_istreambuf
00113   };
00114 
00115   class _Error_formatter
00116   {
00117     /// Whether an iterator is constant, mutable, or unknown
00118     enum _Constness
00119     {
00120       __unknown_constness,
00121       __const_iterator,
00122       __mutable_iterator,
00123       __last_constness
00124     };
00125 
00126     // The state of the iterator (fine-grained), if we know it.
00127     enum _Iterator_state
00128     {
00129       __unknown_state,
00130       __singular,      // singular, may still be attached to a sequence
00131       __begin,         // dereferenceable, and at the beginning
00132       __middle,        // dereferenceable, not at the beginning
00133       __end,           // past-the-end, may be at beginning if sequence empty
00134       __last_state
00135     };
00136 
00137     // Tags denoting the type of parameter for construction
00138     struct _Is_iterator { };
00139     struct _Is_sequence { };
00140 
00141     // A parameter that may be referenced by an error message
00142     struct _Parameter
00143     {
00144       enum
00145       {
00146     __unused_param,
00147     __iterator,
00148     __sequence,
00149     __integer,
00150     __string
00151       } _M_kind;
00152 
00153       union
00154       {
00155     // When _M_kind == __iterator
00156     struct
00157     {
00158       const char*      _M_name;
00159       const void*      _M_address;
00160       const type_info* _M_type;
00161       _Constness       _M_constness;
00162       _Iterator_state  _M_state;
00163       const void*      _M_sequence;
00164       const type_info* _M_seq_type;
00165     } _M_iterator;
00166 
00167     // When _M_kind == __sequence
00168     struct
00169     {
00170       const char*      _M_name;
00171       const void*      _M_address;
00172       const type_info* _M_type;
00173     } _M_sequence;
00174 
00175     // When _M_kind == __integer
00176     struct
00177     {
00178       const char* _M_name;
00179       long        _M_value;
00180     } _M_integer;
00181 
00182     // When _M_kind == __string
00183     struct
00184     {
00185       const char* _M_name;
00186       const char* _M_value;
00187     } _M_string;
00188       } _M_variant;
00189 
00190       _Parameter() : _M_kind(__unused_param), _M_variant() { }
00191 
00192       _Parameter(long __value, const char* __name) 
00193       : _M_kind(__integer), _M_variant()
00194       {
00195     _M_variant._M_integer._M_name = __name;
00196     _M_variant._M_integer._M_value = __value;
00197       }
00198 
00199       _Parameter(const char* __value, const char* __name) 
00200       : _M_kind(__string), _M_variant()
00201       {
00202     _M_variant._M_string._M_name = __name;
00203     _M_variant._M_string._M_value = __value;
00204       }
00205 
00206       template<typename _Iterator, typename _Sequence>
00207         _Parameter(const _Safe_iterator<_Iterator, _Sequence>& __it,
00208            const char* __name, _Is_iterator)
00209     : _M_kind(__iterator),  _M_variant()
00210         {
00211       _M_variant._M_iterator._M_name = __name;
00212       _M_variant._M_iterator._M_address = &__it;
00213       _M_variant._M_iterator._M_type = &typeid(__it);
00214       _M_variant._M_iterator._M_constness =
00215         __is_same<_Safe_iterator<_Iterator, _Sequence>,
00216                              typename _Sequence::iterator>::
00217           value? __mutable_iterator : __const_iterator;
00218       _M_variant._M_iterator._M_sequence = __it._M_get_sequence();
00219       _M_variant._M_iterator._M_seq_type = &typeid(_Sequence);
00220 
00221       if (__it._M_singular())
00222         _M_variant._M_iterator._M_state = __singular;
00223       else
00224         {
00225           bool __is_begin = __it._M_is_begin();
00226           bool __is_end = __it._M_is_end();
00227           if (__is_end)
00228         _M_variant._M_iterator._M_state = __end;
00229           else if (__is_begin)
00230         _M_variant._M_iterator._M_state = __begin;
00231           else
00232         _M_variant._M_iterator._M_state = __middle;
00233         }
00234     }
00235 
00236       template<typename _Type>
00237         _Parameter(const _Type*& __it, const char* __name, _Is_iterator)
00238         : _M_kind(__iterator), _M_variant()
00239         {
00240       _M_variant._M_iterator._M_name = __name;
00241       _M_variant._M_iterator._M_address = &__it;
00242       _M_variant._M_iterator._M_type = &typeid(__it);
00243       _M_variant._M_iterator._M_constness = __mutable_iterator;
00244       _M_variant._M_iterator._M_state = __it? __unknown_state : __singular;
00245       _M_variant._M_iterator._M_sequence = 0;
00246       _M_variant._M_iterator._M_seq_type = 0;
00247     }
00248 
00249       template<typename _Type>
00250         _Parameter(_Type*& __it, const char* __name, _Is_iterator)
00251         : _M_kind(__iterator), _M_variant()
00252         {
00253       _M_variant._M_iterator._M_name = __name;
00254       _M_variant._M_iterator._M_address = &__it;
00255       _M_variant._M_iterator._M_type = &typeid(__it);
00256       _M_variant._M_iterator._M_constness = __const_iterator;
00257       _M_variant._M_iterator._M_state = __it? __unknown_state : __singular;
00258       _M_variant._M_iterator._M_sequence = 0;
00259       _M_variant._M_iterator._M_seq_type = 0;
00260     }
00261 
00262       template<typename _Iterator>
00263         _Parameter(const _Iterator& __it, const char* __name, _Is_iterator)
00264         : _M_kind(__iterator), _M_variant()
00265         {
00266       _M_variant._M_iterator._M_name = __name;
00267       _M_variant._M_iterator._M_address = &__it;
00268       _M_variant._M_iterator._M_type = &typeid(__it);
00269       _M_variant._M_iterator._M_constness = __unknown_constness;
00270       _M_variant._M_iterator._M_state =
00271         __gnu_debug::__check_singular(__it)? __singular : __unknown_state;
00272       _M_variant._M_iterator._M_sequence = 0;
00273       _M_variant._M_iterator._M_seq_type = 0;
00274     }
00275 
00276       template<typename _Sequence>
00277         _Parameter(const _Safe_sequence<_Sequence>& __seq,
00278            const char* __name, _Is_sequence)
00279         : _M_kind(__sequence), _M_variant()
00280         {
00281       _M_variant._M_sequence._M_name = __name;
00282       _M_variant._M_sequence._M_address =
00283         static_cast<const _Sequence*>(&__seq);
00284       _M_variant._M_sequence._M_type = &typeid(_Sequence);
00285     }
00286 
00287       template<typename _Sequence>
00288         _Parameter(const _Sequence& __seq, const char* __name, _Is_sequence)
00289         : _M_kind(__sequence), _M_variant()
00290         {
00291       _M_variant._M_sequence._M_name = __name;
00292       _M_variant._M_sequence._M_address = &__seq;
00293       _M_variant._M_sequence._M_type = &typeid(_Sequence);
00294     }
00295 
00296       void
00297       _M_print_field(const _Error_formatter* __formatter,
00298              const char* __name) const;
00299 
00300       void
00301       _M_print_description(const _Error_formatter* __formatter) const;
00302     };
00303 
00304     friend struct _Parameter;
00305 
00306   public:
00307     template<typename _Iterator>
00308       const _Error_formatter&
00309       _M_iterator(const _Iterator& __it, const char* __name = 0)  const
00310       {
00311     if (_M_num_parameters < size_t(__max_parameters))
00312       _M_parameters[_M_num_parameters++] = _Parameter(__it, __name,
00313                               _Is_iterator());
00314     return *this;
00315       }
00316 
00317     const _Error_formatter&
00318     _M_integer(long __value, const char* __name = 0) const
00319     {
00320       if (_M_num_parameters < size_t(__max_parameters))
00321     _M_parameters[_M_num_parameters++] = _Parameter(__value, __name);
00322       return *this;
00323     }
00324 
00325     const _Error_formatter&
00326     _M_string(const char* __value, const char* __name = 0) const
00327     {
00328       if (_M_num_parameters < size_t(__max_parameters))
00329     _M_parameters[_M_num_parameters++] = _Parameter(__value, __name);
00330       return *this;
00331     }
00332 
00333     template<typename _Sequence>
00334       const _Error_formatter&
00335       _M_sequence(const _Sequence& __seq, const char* __name = 0) const
00336       {
00337     if (_M_num_parameters < size_t(__max_parameters))
00338       _M_parameters[_M_num_parameters++] = _Parameter(__seq, __name,
00339                               _Is_sequence());
00340     return *this;
00341       }
00342 
00343     const _Error_formatter&
00344     _M_message(const char* __text) const
00345     { _M_text = __text; return *this; }
00346 
00347     const _Error_formatter&
00348     _M_message(_Debug_msg_id __id) const;
00349 
00350     void
00351     _M_error() const;
00352 
00353   private:
00354     _Error_formatter(const char* __file, size_t __line)
00355     : _M_file(__file), _M_line(__line), _M_num_parameters(0), _M_text(0),
00356       _M_max_length(78), _M_column(1), _M_first_line(true), _M_wordwrap(false)
00357     { _M_get_max_length(); }
00358 
00359     template<typename _Tp>
00360       void
00361       _M_format_word(char*, int, const char*, _Tp) const;
00362 
00363     void
00364     _M_print_word(const char* __word) const;
00365 
00366     void
00367     _M_print_string(const char* __string) const;
00368 
00369     void
00370     _M_get_max_length() const;
00371 
00372     enum { __max_parameters = 9 };
00373 
00374     const char*         _M_file;
00375     size_t              _M_line;
00376     mutable _Parameter  _M_parameters[__max_parameters];
00377     mutable size_t      _M_num_parameters;
00378     mutable const char* _M_text;
00379     mutable size_t      _M_max_length;
00380     enum { _M_indent = 4 } ;
00381     mutable size_t      _M_column;
00382     mutable bool        _M_first_line;
00383     mutable bool        _M_wordwrap;
00384 
00385   public:
00386     static _Error_formatter
00387     _M_at(const char* __file, size_t __line)
00388     { return _Error_formatter(__file, __line); }
00389   };
00390 } // namespace __gnu_debug
00391 
00392 #endif

Generated on Thu Jul 23 21:16:04 2009 for libstdc++ by  doxygen 1.5.8