ostream_insert.h

Go to the documentation of this file.
00001 // Helpers for ostream inserters -*- C++ -*-
00002 
00003 // Copyright (C) 2007 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 2, 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 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free software
00022 // library without restriction.  Specifically, if other files instantiate
00023 // templates or use macros or inline functions from this file, or you compile
00024 // this file and link it with other files to produce an executable, this
00025 // file does not by itself cause the resulting executable to be covered by
00026 // the GNU General Public License.  This exception does not however
00027 // invalidate any other reasons why the executable file might be covered by
00028 // the GNU General Public License.
00029 
00030 /** @file ostream_insert.h
00031  *  This is an internal header file, included by other library headers.
00032  *  You should not attempt to use it directly.
00033  */
00034 
00035 #ifndef _OSTREAM_INSERT_H
00036 #define _OSTREAM_INSERT_H 1
00037 
00038 #pragma GCC system_header
00039 
00040 #include <iosfwd>
00041 #include <cxxabi-forced.h>
00042 
00043 _GLIBCXX_BEGIN_NAMESPACE(std)
00044 
00045   template<typename _CharT, typename _Traits>
00046     inline void
00047     __ostream_write(basic_ostream<_CharT, _Traits>& __out,
00048             const _CharT* __s, streamsize __n)
00049     {
00050       typedef basic_ostream<_CharT, _Traits>       __ostream_type;      
00051       typedef typename __ostream_type::ios_base    __ios_base;
00052 
00053       const streamsize __put = __out.rdbuf()->sputn(__s, __n);
00054       if (__put != __n)
00055     __out.setstate(__ios_base::badbit);
00056     }
00057 
00058   template<typename _CharT, typename _Traits>
00059     inline void
00060     __ostream_fill(basic_ostream<_CharT, _Traits>& __out, streamsize __n)
00061     {
00062       typedef basic_ostream<_CharT, _Traits>       __ostream_type;      
00063       typedef typename __ostream_type::ios_base    __ios_base;
00064 
00065       const _CharT __c = __out.fill();
00066       for (; __n > 0; --__n)
00067     {
00068       const typename _Traits::int_type __put = __out.rdbuf()->sputc(__c);
00069       if (_Traits::eq_int_type(__put, _Traits::eof()))
00070         {
00071           __out.setstate(__ios_base::badbit);
00072           break;
00073         }
00074     }
00075     }
00076 
00077   template<typename _CharT, typename _Traits>
00078     basic_ostream<_CharT, _Traits>&
00079     __ostream_insert(basic_ostream<_CharT, _Traits>& __out,
00080              const _CharT* __s, streamsize __n)
00081     {
00082       typedef basic_ostream<_CharT, _Traits>       __ostream_type;
00083       typedef typename __ostream_type::ios_base    __ios_base;
00084 
00085       typename __ostream_type::sentry __cerb(__out);
00086       if (__cerb)
00087     {
00088       try
00089         {
00090           const streamsize __w = __out.width();
00091           if (__w > __n)
00092         {
00093           const bool __left = ((__out.flags()
00094                     & __ios_base::adjustfield)
00095                        == __ios_base::left);
00096           if (!__left)
00097             __ostream_fill(__out, __w - __n);
00098           if (__out.good())
00099             __ostream_write(__out, __s, __n);
00100           if (__left && __out.good())
00101             __ostream_fill(__out, __w - __n);
00102         }
00103           else
00104         __ostream_write(__out, __s, __n);
00105           __out.width(0);
00106         }
00107       catch(__cxxabiv1::__forced_unwind&)
00108         {
00109           __out._M_setstate(__ios_base::badbit);
00110           __throw_exception_again;
00111         }
00112       catch(...)
00113         { __out._M_setstate(__ios_base::badbit); }
00114     }
00115       return __out;
00116     }
00117 
00118   // Inhibit implicit instantiations for required instantiations,
00119   // which are defined via explicit instantiations elsewhere.
00120   // NB:  This syntax is a GNU extension.
00121 #if _GLIBCXX_EXTERN_TEMPLATE
00122   extern template ostream& __ostream_insert(ostream&, const char*, streamsize);
00123 
00124 #ifdef _GLIBCXX_USE_WCHAR_T
00125   extern template wostream& __ostream_insert(wostream&, const wchar_t*,
00126                          streamsize);
00127 #endif
00128 #endif
00129 
00130 _GLIBCXX_END_NAMESPACE
00131 
00132 #endif /* _OSTREAM_INSERT_H */

Generated on Wed Mar 26 00:43:05 2008 for libstdc++ by  doxygen 1.5.1