libstdc++
formatter.h
Go to the documentation of this file.
00001 // Debug-mode error formatting implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2003-2015 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 <bits/c++config.h>
00033 #include <bits/cpp_type_traits.h>
00034 #include <typeinfo>
00035 
00036 namespace __gnu_debug
00037 {
00038   using std::type_info;
00039 
00040   template<typename _Iterator>
00041     bool __check_singular(const _Iterator&);
00042 
00043   class _Safe_sequence_base;
00044 
00045   template<typename _Iterator, typename _Sequence>
00046     class _Safe_iterator;
00047 
00048   template<typename _Iterator, typename _Sequence>
00049     class _Safe_local_iterator;
00050 
00051   template<typename _Sequence>
00052     class _Safe_sequence;
00053 
00054   enum _Debug_msg_id
00055   {
00056     // General checks
00057     __msg_valid_range,
00058     __msg_insert_singular,
00059     __msg_insert_different,
00060     __msg_erase_bad,
00061     __msg_erase_different,
00062     __msg_subscript_oob,
00063     __msg_empty,
00064     __msg_unpartitioned,
00065     __msg_unpartitioned_pred,
00066     __msg_unsorted,
00067     __msg_unsorted_pred,
00068     __msg_not_heap,
00069     __msg_not_heap_pred,
00070     // std::bitset checks
00071     __msg_bad_bitset_write,
00072     __msg_bad_bitset_read,
00073     __msg_bad_bitset_flip,
00074     // std::list checks
00075     __msg_self_splice,
00076     __msg_splice_alloc,
00077     __msg_splice_bad,
00078     __msg_splice_other,
00079     __msg_splice_overlap,
00080     // iterator checks
00081     __msg_init_singular,
00082     __msg_init_copy_singular,
00083     __msg_init_const_singular,
00084     __msg_copy_singular,
00085     __msg_bad_deref,
00086     __msg_bad_inc,
00087     __msg_bad_dec,
00088     __msg_iter_subscript_oob,
00089     __msg_advance_oob,
00090     __msg_retreat_oob,
00091     __msg_iter_compare_bad,
00092     __msg_compare_different,
00093     __msg_iter_order_bad,
00094     __msg_order_different,
00095     __msg_distance_bad,
00096     __msg_distance_different,
00097     // istream_iterator
00098     __msg_deref_istream,
00099     __msg_inc_istream,
00100     // ostream_iterator
00101     __msg_output_ostream,
00102     // istreambuf_iterator
00103     __msg_deref_istreambuf,
00104     __msg_inc_istreambuf,
00105     // forward_list
00106     __msg_insert_after_end,
00107     __msg_erase_after_bad,
00108     __msg_valid_range2,
00109     // unordered container local iterators
00110     __msg_local_iter_compare_bad,
00111     __msg_non_empty_range,
00112     // self move assign
00113     __msg_self_move_assign,
00114     // unordered container buckets
00115     __msg_bucket_index_oob,
00116     __msg_valid_load_factor,
00117     // others
00118     __msg_equal_allocs,
00119     __msg_insert_range_from_self
00120   };
00121 
00122   class _Error_formatter
00123   {
00124     /// Whether an iterator is constant, mutable, or unknown
00125     enum _Constness
00126     {
00127       __unknown_constness,
00128       __const_iterator,
00129       __mutable_iterator,
00130       __last_constness
00131     };
00132 
00133     // The state of the iterator (fine-grained), if we know it.
00134     enum _Iterator_state
00135     {
00136       __unknown_state,
00137       __singular,      // singular, may still be attached to a sequence
00138       __begin,         // dereferenceable, and at the beginning
00139       __middle,        // dereferenceable, not at the beginning
00140       __end,           // past-the-end, may be at beginning if sequence empty
00141       __before_begin,  // before begin
00142       __last_state
00143     };
00144 
00145     // Tags denoting the type of parameter for construction
00146     struct _Is_iterator { };
00147     struct _Is_sequence { };
00148 
00149     // A parameter that may be referenced by an error message
00150     struct _Parameter
00151     {
00152       enum
00153       {
00154         __unused_param,
00155         __iterator,
00156         __sequence,
00157         __integer,
00158         __string
00159       } _M_kind;
00160 
00161       union
00162       {
00163         // When _M_kind == __iterator
00164         struct
00165         {
00166           const char*      _M_name;
00167           const void*      _M_address;
00168           const type_info* _M_type;
00169           _Constness       _M_constness;
00170           _Iterator_state  _M_state;
00171           const void*      _M_sequence;
00172           const type_info* _M_seq_type;
00173         } _M_iterator;
00174 
00175         // When _M_kind == __sequence
00176         struct
00177         {
00178           const char*      _M_name;
00179           const void*      _M_address;
00180           const type_info* _M_type;
00181         } _M_sequence;
00182 
00183         // When _M_kind == __integer
00184         struct
00185         {
00186           const char* _M_name;
00187           long        _M_value;
00188         } _M_integer;
00189 
00190         // When _M_kind == __string
00191         struct
00192         {
00193           const char* _M_name;
00194           const char* _M_value;
00195         } _M_string;
00196       } _M_variant;
00197 
00198       _Parameter() : _M_kind(__unused_param), _M_variant() { }
00199 
00200       _Parameter(long __value, const char* __name) 
00201       : _M_kind(__integer), _M_variant()
00202       {
00203         _M_variant._M_integer._M_name = __name;
00204         _M_variant._M_integer._M_value = __value;
00205       }
00206 
00207       _Parameter(const char* __value, const char* __name) 
00208       : _M_kind(__string), _M_variant()
00209       {
00210         _M_variant._M_string._M_name = __name;
00211         _M_variant._M_string._M_value = __value;
00212       }
00213 
00214       template<typename _Iterator, typename _Sequence>
00215         _Parameter(const _Safe_iterator<_Iterator, _Sequence>& __it,
00216                    const char* __name, _Is_iterator)
00217         : _M_kind(__iterator),  _M_variant()
00218         {
00219           _M_variant._M_iterator._M_name = __name;
00220           _M_variant._M_iterator._M_address = &__it;
00221 #if __cpp_rtti
00222           _M_variant._M_iterator._M_type = &typeid(__it);
00223 #else
00224           _M_variant._M_iterator._M_type = 0;
00225 #endif
00226           _M_variant._M_iterator._M_constness =
00227             std::__are_same<_Safe_iterator<_Iterator, _Sequence>,
00228                             typename _Sequence::iterator>::
00229               __value ? __mutable_iterator : __const_iterator;
00230           _M_variant._M_iterator._M_sequence = __it._M_get_sequence();
00231 #if __cpp_rtti
00232           _M_variant._M_iterator._M_seq_type = &typeid(_Sequence);
00233 #else
00234           _M_variant._M_iterator._M_seq_type = 0;
00235 #endif
00236 
00237           if (__it._M_singular())
00238             _M_variant._M_iterator._M_state = __singular;
00239           else
00240             {
00241               if (__it._M_is_before_begin())
00242                 _M_variant._M_iterator._M_state = __before_begin;
00243               else if (__it._M_is_end())
00244                 _M_variant._M_iterator._M_state = __end;
00245               else if (__it._M_is_begin())
00246                 _M_variant._M_iterator._M_state = __begin;
00247               else
00248                 _M_variant._M_iterator._M_state = __middle;
00249             }
00250         }
00251 
00252       template<typename _Iterator, typename _Sequence>
00253         _Parameter(const _Safe_local_iterator<_Iterator, _Sequence>& __it,
00254                    const char* __name, _Is_iterator)
00255         : _M_kind(__iterator),  _M_variant()
00256         {
00257           _M_variant._M_iterator._M_name = __name;
00258           _M_variant._M_iterator._M_address = &__it;
00259 #if __cpp_rtti
00260           _M_variant._M_iterator._M_type = &typeid(__it);
00261 #else
00262           _M_variant._M_iterator._M_type = 0;
00263 #endif
00264           _M_variant._M_iterator._M_constness =
00265             std::__are_same<_Safe_local_iterator<_Iterator, _Sequence>,
00266                             typename _Sequence::local_iterator>::
00267               __value ? __mutable_iterator : __const_iterator;
00268           _M_variant._M_iterator._M_sequence = __it._M_get_sequence();
00269 #if __cpp_rtti
00270           _M_variant._M_iterator._M_seq_type = &typeid(_Sequence);
00271 #else
00272           _M_variant._M_iterator._M_seq_type = 0;
00273 #endif
00274 
00275           if (__it._M_singular())
00276             _M_variant._M_iterator._M_state = __singular;
00277           else
00278             {
00279               if (__it._M_is_end())
00280                 _M_variant._M_iterator._M_state = __end;
00281               else if (__it._M_is_begin())
00282                 _M_variant._M_iterator._M_state = __begin;
00283               else
00284                 _M_variant._M_iterator._M_state = __middle;
00285             }
00286         }
00287 
00288       template<typename _Type>
00289         _Parameter(const _Type*& __it, const char* __name, _Is_iterator)
00290         : _M_kind(__iterator), _M_variant()
00291         {
00292           _M_variant._M_iterator._M_name = __name;
00293           _M_variant._M_iterator._M_address = &__it;
00294 #if __cpp_rtti
00295           _M_variant._M_iterator._M_type = &typeid(__it);
00296 #else
00297           _M_variant._M_iterator._M_type = 0;
00298 #endif
00299           _M_variant._M_iterator._M_constness = __mutable_iterator;
00300           _M_variant._M_iterator._M_state = __it? __unknown_state : __singular;
00301           _M_variant._M_iterator._M_sequence = 0;
00302           _M_variant._M_iterator._M_seq_type = 0;
00303         }
00304 
00305       template<typename _Type>
00306         _Parameter(_Type*& __it, const char* __name, _Is_iterator)
00307         : _M_kind(__iterator), _M_variant()
00308         {
00309           _M_variant._M_iterator._M_name = __name;
00310           _M_variant._M_iterator._M_address = &__it;
00311 #if __cpp_rtti
00312           _M_variant._M_iterator._M_type = &typeid(__it);
00313 #else
00314           _M_variant._M_iterator._M_type = 0;
00315 #endif
00316           _M_variant._M_iterator._M_constness = __const_iterator;
00317           _M_variant._M_iterator._M_state = __it? __unknown_state : __singular;
00318           _M_variant._M_iterator._M_sequence = 0;
00319           _M_variant._M_iterator._M_seq_type = 0;
00320         }
00321 
00322       template<typename _Iterator>
00323         _Parameter(const _Iterator& __it, const char* __name, _Is_iterator)
00324         : _M_kind(__iterator), _M_variant()
00325         {
00326           _M_variant._M_iterator._M_name = __name;
00327           _M_variant._M_iterator._M_address = &__it;
00328 #if __cpp_rtti
00329           _M_variant._M_iterator._M_type = &typeid(__it);
00330 #else
00331           _M_variant._M_iterator._M_type = 0;
00332 #endif
00333           _M_variant._M_iterator._M_constness = __unknown_constness;
00334           _M_variant._M_iterator._M_state =
00335             __gnu_debug::__check_singular(__it)? __singular : __unknown_state;
00336           _M_variant._M_iterator._M_sequence = 0;
00337           _M_variant._M_iterator._M_seq_type = 0;
00338         }
00339 
00340       template<typename _Sequence>
00341         _Parameter(const _Safe_sequence<_Sequence>& __seq,
00342                    const char* __name, _Is_sequence)
00343         : _M_kind(__sequence), _M_variant()
00344         {
00345           _M_variant._M_sequence._M_name = __name;
00346           _M_variant._M_sequence._M_address =
00347             static_cast<const _Sequence*>(&__seq);
00348 #if __cpp_rtti
00349           _M_variant._M_sequence._M_type = &typeid(_Sequence);
00350 #else
00351           _M_variant._M_sequence._M_type = 0;
00352 #endif
00353         }
00354 
00355       template<typename _Sequence>
00356         _Parameter(const _Sequence& __seq, const char* __name, _Is_sequence)
00357         : _M_kind(__sequence), _M_variant()
00358         {
00359           _M_variant._M_sequence._M_name = __name;
00360           _M_variant._M_sequence._M_address = &__seq;
00361 #if __cpp_rtti
00362           _M_variant._M_sequence._M_type = &typeid(_Sequence);
00363 #else
00364           _M_variant._M_sequence._M_type = 0;
00365 #endif
00366         }
00367 
00368       void
00369       _M_print_field(const _Error_formatter* __formatter,
00370                      const char* __name) const;
00371 
00372       void
00373       _M_print_description(const _Error_formatter* __formatter) const;
00374     };
00375 
00376     friend struct _Parameter;
00377 
00378   public:
00379     template<typename _Iterator>
00380       const _Error_formatter&
00381       _M_iterator(const _Iterator& __it, const char* __name = 0)  const
00382       {
00383         if (_M_num_parameters < std::size_t(__max_parameters))
00384           _M_parameters[_M_num_parameters++] = _Parameter(__it, __name,
00385                                                           _Is_iterator());
00386         return *this;
00387       }
00388 
00389     const _Error_formatter&
00390     _M_integer(long __value, const char* __name = 0) const
00391     {
00392       if (_M_num_parameters < std::size_t(__max_parameters))
00393         _M_parameters[_M_num_parameters++] = _Parameter(__value, __name);
00394       return *this;
00395     }
00396 
00397     const _Error_formatter&
00398     _M_string(const char* __value, const char* __name = 0) const
00399     {
00400       if (_M_num_parameters < std::size_t(__max_parameters))
00401         _M_parameters[_M_num_parameters++] = _Parameter(__value, __name);
00402       return *this;
00403     }
00404 
00405     template<typename _Sequence>
00406       const _Error_formatter&
00407       _M_sequence(const _Sequence& __seq, const char* __name = 0) const
00408       {
00409         if (_M_num_parameters < std::size_t(__max_parameters))
00410           _M_parameters[_M_num_parameters++] = _Parameter(__seq, __name,
00411                                                           _Is_sequence());
00412         return *this;
00413       }
00414 
00415     const _Error_formatter&
00416     _M_message(const char* __text) const
00417     { _M_text = __text; return *this; }
00418 
00419     const _Error_formatter&
00420     _M_message(_Debug_msg_id __id) const throw ();
00421 
00422     _GLIBCXX_NORETURN void
00423     _M_error() const;
00424 
00425   private:
00426     _Error_formatter(const char* __file, std::size_t __line)
00427     : _M_file(__file), _M_line(__line), _M_num_parameters(0), _M_text(0),
00428       _M_max_length(78), _M_column(1), _M_first_line(true), _M_wordwrap(false)
00429     { _M_get_max_length(); }
00430 
00431     template<typename _Tp>
00432       void
00433       _M_format_word(char*, int, const char*, _Tp) const throw ();
00434 
00435     void
00436     _M_print_word(const char* __word) const;
00437 
00438     void
00439     _M_print_string(const char* __string) const;
00440 
00441     void
00442     _M_get_max_length() const throw ();
00443 
00444     enum { __max_parameters = 9 };
00445 
00446     const char*         _M_file;
00447     std::size_t         _M_line;
00448     mutable _Parameter  _M_parameters[__max_parameters];
00449     mutable std::size_t _M_num_parameters;
00450     mutable const char* _M_text;
00451     mutable std::size_t _M_max_length;
00452     enum { _M_indent = 4 } ;
00453     mutable std::size_t _M_column;
00454     mutable bool        _M_first_line;
00455     mutable bool        _M_wordwrap;
00456 
00457   public:
00458     static _Error_formatter
00459     _M_at(const char* __file, std::size_t __line)
00460     { return _Error_formatter(__file, __line); }
00461   };
00462 } // namespace __gnu_debug
00463 
00464 #endif