]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/ostream_insert.h
Add random numbers and fix some bugs.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / ostream_insert.h
CommitLineData
11202768
PC
1// Helpers for ostream inserters -*- C++ -*-
2
a945c346 3// Copyright (C) 2007-2024 Free Software Foundation, Inc.
11202768
PC
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
748086b7 8// Free Software Foundation; either version 3, or (at your option)
11202768
PC
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
748086b7
JJ
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/>.
11202768 24
f910786b 25/** @file bits/ostream_insert.h
11202768 26 * This is an internal header file, included by other library headers.
f910786b 27 * Do not attempt to use it directly. @headername{ostream}
11202768
PC
28 */
29
30#ifndef _OSTREAM_INSERT_H
31#define _OSTREAM_INSERT_H 1
32
33#pragma GCC system_header
34
35#include <iosfwd>
7c3e9502 36#include <bits/cxxabi_forced.h>
47cca028 37#include <bits/exception_defines.h>
11202768 38
d3a7302e
JM
39#pragma GCC diagnostic push
40#pragma GCC diagnostic ignored "-Wc++11-extensions" // extern template
41
12ffa228
BK
42namespace std _GLIBCXX_VISIBILITY(default)
43{
44_GLIBCXX_BEGIN_NAMESPACE_VERSION
11202768 45
e6149254
JW
46 /// @cond undocumented
47
11202768
PC
48 template<typename _CharT, typename _Traits>
49 inline void
50 __ostream_write(basic_ostream<_CharT, _Traits>& __out,
51 const _CharT* __s, streamsize __n)
52 {
53 typedef basic_ostream<_CharT, _Traits> __ostream_type;
54 typedef typename __ostream_type::ios_base __ios_base;
55
56 const streamsize __put = __out.rdbuf()->sputn(__s, __n);
57 if (__put != __n)
58 __out.setstate(__ios_base::badbit);
59 }
60
61 template<typename _CharT, typename _Traits>
62 inline void
63 __ostream_fill(basic_ostream<_CharT, _Traits>& __out, streamsize __n)
64 {
65 typedef basic_ostream<_CharT, _Traits> __ostream_type;
66 typedef typename __ostream_type::ios_base __ios_base;
67
68 const _CharT __c = __out.fill();
69 for (; __n > 0; --__n)
70 {
71 const typename _Traits::int_type __put = __out.rdbuf()->sputc(__c);
72 if (_Traits::eq_int_type(__put, _Traits::eof()))
73 {
74 __out.setstate(__ios_base::badbit);
75 break;
76 }
77 }
78 }
79
80 template<typename _CharT, typename _Traits>
81 basic_ostream<_CharT, _Traits>&
82 __ostream_insert(basic_ostream<_CharT, _Traits>& __out,
83 const _CharT* __s, streamsize __n)
84 {
85 typedef basic_ostream<_CharT, _Traits> __ostream_type;
86 typedef typename __ostream_type::ios_base __ios_base;
87
88 typename __ostream_type::sentry __cerb(__out);
880b527f 89 if (__cerb)
11202768 90 {
bc2631e0 91 __try
11202768
PC
92 {
93 const streamsize __w = __out.width();
94 if (__w > __n)
95 {
96 const bool __left = ((__out.flags()
97 & __ios_base::adjustfield)
98 == __ios_base::left);
99 if (!__left)
100 __ostream_fill(__out, __w - __n);
101 if (__out.good())
102 __ostream_write(__out, __s, __n);
103 if (__left && __out.good())
104 __ostream_fill(__out, __w - __n);
105 }
106 else
107 __ostream_write(__out, __s, __n);
108 __out.width(0);
109 }
bc2631e0 110 __catch(__cxxabiv1::__forced_unwind&)
d05f74f1
JM
111 {
112 __out._M_setstate(__ios_base::badbit);
113 __throw_exception_again;
114 }
bc2631e0 115 __catch(...)
11202768
PC
116 { __out._M_setstate(__ios_base::badbit); }
117 }
118 return __out;
119 }
120
84b31797
PC
121 // Inhibit implicit instantiations for required instantiations,
122 // which are defined via explicit instantiations elsewhere.
84b31797
PC
123#if _GLIBCXX_EXTERN_TEMPLATE
124 extern template ostream& __ostream_insert(ostream&, const char*, streamsize);
125
126#ifdef _GLIBCXX_USE_WCHAR_T
127 extern template wostream& __ostream_insert(wostream&, const wchar_t*,
128 streamsize);
129#endif
130#endif
131
e6149254
JW
132 /// @endcond
133
12ffa228 134_GLIBCXX_END_NAMESPACE_VERSION
ed4f96af 135} // namespace std
11202768 136
d3a7302e 137#pragma GCC diagnostic pop
11202768 138#endif /* _OSTREAM_INSERT_H */