libstdc++
ostream_insert.h
Go to the documentation of this file.
1 // Helpers for ostream inserters -*- C++ -*-
2 
3 // Copyright (C) 2007-2025 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /** @file bits/ostream_insert.h
26  * This is an internal header file, included by other library headers.
27  * Do not attempt to use it directly. @headername{ostream}
28  */
29 
30 #ifndef _OSTREAM_INSERT_H
31 #define _OSTREAM_INSERT_H 1
32 
33 #ifdef _GLIBCXX_SYSHDR
34 #pragma GCC system_header
35 #endif
36 
37 #include <iosfwd>
38 #include <bits/cxxabi_forced.h>
39 #include <bits/exception_defines.h>
40 
41 #pragma GCC diagnostic push
42 #pragma GCC diagnostic ignored "-Wc++11-extensions" // extern template
43 
44 namespace std _GLIBCXX_VISIBILITY(default)
45 {
46 _GLIBCXX_BEGIN_NAMESPACE_VERSION
47 
48  /// @cond undocumented
49 
50  template<typename _CharT, typename _Traits>
51  inline void
52  __ostream_write(basic_ostream<_CharT, _Traits>& __out,
53  const _CharT* __s, streamsize __n)
54  {
55  typedef basic_ostream<_CharT, _Traits> __ostream_type;
56  typedef typename __ostream_type::ios_base __ios_base;
57 
58  const streamsize __put = __out.rdbuf()->sputn(__s, __n);
59  if (__put != __n)
60  __out.setstate(__ios_base::badbit);
61  }
62 
63  template<typename _CharT, typename _Traits>
64  inline void
65  __ostream_fill(basic_ostream<_CharT, _Traits>& __out, streamsize __n)
66  {
67  typedef basic_ostream<_CharT, _Traits> __ostream_type;
68  typedef typename __ostream_type::ios_base __ios_base;
69 
70  const _CharT __c = __out.fill();
71  for (; __n > 0; --__n)
72  {
73  const typename _Traits::int_type __put = __out.rdbuf()->sputc(__c);
74  if (_Traits::eq_int_type(__put, _Traits::eof()))
75  {
76  __out.setstate(__ios_base::badbit);
77  break;
78  }
79  }
80  }
81 
82  template<typename _CharT, typename _Traits>
83  basic_ostream<_CharT, _Traits>&
84  __ostream_insert(basic_ostream<_CharT, _Traits>& __out,
85  const _CharT* __s, streamsize __n)
86  {
87  typedef basic_ostream<_CharT, _Traits> __ostream_type;
88  typedef typename __ostream_type::ios_base __ios_base;
89 
90  typename __ostream_type::sentry __cerb(__out);
91  if (__cerb)
92  {
93  __try
94  {
95  const streamsize __w = __out.width();
96  if (__w > __n)
97  {
98  const bool __left = ((__out.flags()
99  & __ios_base::adjustfield)
100  == __ios_base::left);
101  if (!__left)
102  __ostream_fill(__out, __w - __n);
103  if (__out.good())
104  __ostream_write(__out, __s, __n);
105  if (__left && __out.good())
106  __ostream_fill(__out, __w - __n);
107  }
108  else
109  __ostream_write(__out, __s, __n);
110  __out.width(0);
111  }
113  {
114  __out._M_setstate(__ios_base::badbit);
115  __throw_exception_again;
116  }
117  __catch(...)
118  { __out._M_setstate(__ios_base::badbit); }
119  }
120  return __out;
121  }
122 
123  // Inhibit implicit instantiations for required instantiations,
124  // which are defined via explicit instantiations elsewhere.
125 #if _GLIBCXX_EXTERN_TEMPLATE
126  extern template ostream& __ostream_insert(ostream&, const char*, streamsize);
127 
128 #ifdef _GLIBCXX_USE_WCHAR_T
129  extern template wostream& __ostream_insert(wostream&, const wchar_t*,
130  streamsize);
131 #endif
132 #endif
133 
134  /// @endcond
135 
136 _GLIBCXX_END_NAMESPACE_VERSION
137 } // namespace std
138 
139 #pragma GCC diagnostic pop
140 #endif /* _OSTREAM_INSERT_H */
basic_ostream< char > ostream
Base class for char output streams.
Definition: iosfwd:145
basic_ostream< wchar_t > wostream
Base class for wchar_t output streams.
Definition: iosfwd:185
ISO C++ entities toplevel namespace is std.
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.
Definition: postypes.h:73
ios_base & left(ios_base &__base)
Calls base.setf(ios_base::left, ios_base::adjustfield).
Definition: ios_base.h:1077
Thrown as part of forced unwinding.
Definition: cxxabi_forced.h:51