# ostream_insert.h

Go to the documentation of this file.

    00001 // Helpers for ostream inserters -*- C++ -*-
    00002
    00003 // Copyright (C) 2007, 2008, 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 ostream_insert.h
    00026  *  This is an internal header file, included by other library headers.
    00027  *  You should not attempt to use it directly.
    00028  */
    00029
    00030 #ifndef _OSTREAM_INSERT_H
    00031 #define _OSTREAM_INSERT_H 1
    00032
    00033 #pragma GCC system_header
    00034
    00035 #include <iosfwd>
    00036 #include <cxxabi-forced.h>
    00037
    00038 _GLIBCXX_BEGIN_NAMESPACE(std)
    00039
    00040   template<typename _CharT, typename _Traits>
    00041     inline void
    00042     __ostream_write(basic_ostream<_CharT, _Traits>& __out,
    00043             const _CharT* __s, streamsize __n)
    00044     {
    00045       typedef basic_ostream<_CharT, _Traits>       __ostream_type;
    00046       typedef typename __ostream_type::ios_base    __ios_base;
    00047
    00048       const streamsize __put = __out.rdbuf()->sputn(__s, __n);
    00049       if (__put != __n)
    00050     __out.setstate(__ios_base::badbit);
    00051     }
    00052
    00053   template<typename _CharT, typename _Traits>
    00054     inline void
    00055     __ostream_fill(basic_ostream<_CharT, _Traits>& __out, streamsize __n)
    00056     {
    00057       typedef basic_ostream<_CharT, _Traits>       __ostream_type;
    00058       typedef typename __ostream_type::ios_base    __ios_base;
    00059
    00060       const _CharT __c = __out.fill();
    00061       for (; __n > 0; --__n)
    00062     {
    00063       const typename _Traits::int_type __put = __out.rdbuf()->sputc(__c);
    00064       if (_Traits::eq_int_type(__put, _Traits::eof()))
    00065         {
    00066           __out.setstate(__ios_base::badbit);
    00067           break;
    00068         }
    00069     }
    00070     }
    00071
    00072   template<typename _CharT, typename _Traits>
    00073     basic_ostream<_CharT, _Traits>&
    00074     __ostream_insert(basic_ostream<_CharT, _Traits>& __out,
    00075              const _CharT* __s, streamsize __n)
    00076     {
    00077       typedef basic_ostream<_CharT, _Traits>       __ostream_type;
    00078       typedef typename __ostream_type::ios_base    __ios_base;
    00079
    00080       typename __ostream_type::sentry __cerb(__out);
    00081       if (__cerb)
    00082     {
    00083       __try
    00084         {
    00085           const streamsize __w = __out.width();
    00086           if (__w > __n)
    00087         {
    00088           const bool __left = ((__out.flags()
    00089                     & __ios_base::adjustfield)
    00090                        == __ios_base::left);
    00091           if (!__left)
    00092             __ostream_fill(__out, __w - __n);
    00093           if (__out.good())
    00094             __ostream_write(__out, __s, __n);
    00095           if (__left && __out.good())
    00096             __ostream_fill(__out, __w - __n);
    00097         }
    00098           else
    00099         __ostream_write(__out, __s, __n);
    00100           __out.width(0);
    00101         }
    00102       __catch(__cxxabiv1::__forced_unwind&)
    00103         {
    00104           __out._M_setstate(__ios_base::badbit);
    00105           __throw_exception_again;
    00106         }
    00107       __catch(...)
    00108         { __out._M_setstate(__ios_base::badbit); }
    00109     }
    00110       return __out;
    00111     }
    00112
    00113   // Inhibit implicit instantiations for required instantiations,
    00114   // which are defined via explicit instantiations elsewhere.
    00115   // NB:  This syntax is a GNU extension.
    00116 #if _GLIBCXX_EXTERN_TEMPLATE
    00117   extern template ostream& __ostream_insert(ostream&, const char*, streamsize);
    00118
    00119 #ifdef _GLIBCXX_USE_WCHAR_T
    00120   extern template wostream& __ostream_insert(wostream&, const wchar_t*,
    00121                          streamsize);
    00122 #endif
    00123 #endif
    00124
    00125 _GLIBCXX_END_NAMESPACE
    00126
    00127 #endif /* _OSTREAM_INSERT_H */