# 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
    00042 _GLIBCXX_BEGIN_NAMESPACE(std)
    00043
    00044   template<typename _CharT, typename _Traits>
    00045     inline void
    00046     __ostream_write(basic_ostream<_CharT, _Traits>& __out,
    00047             const _CharT* __s, streamsize __n)
    00048     {
    00049       typedef basic_ostream<_CharT, _Traits>       __ostream_type;
    00050       typedef typename __ostream_type::ios_base    __ios_base;
    00051
    00052       const streamsize __put = __out.rdbuf()->sputn(__s, __n);
    00053       if (__put != __n)
    00054     __out.setstate(__ios_base::badbit);
    00055     }
    00056
    00057   template<typename _CharT, typename _Traits>
    00058     inline void
    00059     __ostream_fill(basic_ostream<_CharT, _Traits>& __out, streamsize __n)
    00060     {
    00061       typedef basic_ostream<_CharT, _Traits>       __ostream_type;
    00062       typedef typename __ostream_type::ios_base    __ios_base;
    00063
    00064       const _CharT __c = __out.fill();
    00065       for (; __n > 0; --__n)
    00066     {
    00067       const typename _Traits::int_type __put = __out.rdbuf()->sputc(__c);
    00068       if (_Traits::eq_int_type(__put, _Traits::eof()))
    00069         {
    00070           __out.setstate(__ios_base::badbit);
    00071           break;
    00072         }
    00073     }
    00074     }
    00075
    00076   template<typename _CharT, typename _Traits>
    00077     basic_ostream<_CharT, _Traits>&
    00078     __ostream_insert(basic_ostream<_CharT, _Traits>& __out,
    00079              const _CharT* __s, streamsize __n)
    00080     {
    00081       typedef basic_ostream<_CharT, _Traits>       __ostream_type;
    00082       typedef typename __ostream_type::ios_base    __ios_base;
    00083
    00084       typename __ostream_type::sentry __cerb(__out);
    00085       if (__cerb)
    00086     {
    00087       try
    00088         {
    00089           const streamsize __w = __out.width();
    00090           if (__w > __n)
    00091         {
    00092           const bool __left = ((__out.flags()
    00093                     & __ios_base::adjustfield)
    00094                        == __ios_base::left);
    00095           if (!__left)
    00096             __ostream_fill(__out, __w - __n);
    00097           if (__out.good())
    00098             __ostream_write(__out, __s, __n);
    00099           if (__left && __out.good())
    00100             __ostream_fill(__out, __w - __n);
    00101         }
    00102           else
    00103         __ostream_write(__out, __s, __n);
    00104           __out.width(0);
    00105         }
    00106       catch(...)
    00107         { __out._M_setstate(__ios_base::badbit); }
    00108     }
    00109       return __out;
    00110     }
    00111
    00112   // Inhibit implicit instantiations for required instantiations,
    00113   // which are defined via explicit instantiations elsewhere.
    00114   // NB:  This syntax is a GNU extension.
    00115 #if _GLIBCXX_EXTERN_TEMPLATE
    00116   extern template ostream& __ostream_insert(ostream&, const char*, streamsize);
    00117
    00118 #ifdef _GLIBCXX_USE_WCHAR_T
    00119   extern template wostream& __ostream_insert(wostream&, const wchar_t*,
    00120                          streamsize);
    00121 #endif
    00122 #endif
    00123
    00124 _GLIBCXX_END_NAMESPACE
    00125
    00126 #endif /* _OSTREAM_INSERT_H */