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