blob: 2f9efcc3c772ef87316d925b6043d22c68a8cfb1 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_IOS
11#define _LIBCPP_IOS
12
13/*
14 ios synopsis
15
16#include <iosfwd>
17
18namespace std
19{
20
21typedef OFF_T streamoff;
22typedef SZ_T streamsize;
23template <class stateT> class fpos;
24
25class ios_base
26{
27public:
28 class failure;
29
30 typedef T1 fmtflags;
Howard Hinnant770a3662012-07-21 01:03:40 +000031 static constexpr fmtflags boolalpha;
32 static constexpr fmtflags dec;
33 static constexpr fmtflags fixed;
34 static constexpr fmtflags hex;
35 static constexpr fmtflags internal;
36 static constexpr fmtflags left;
37 static constexpr fmtflags oct;
38 static constexpr fmtflags right;
39 static constexpr fmtflags scientific;
40 static constexpr fmtflags showbase;
41 static constexpr fmtflags showpoint;
42 static constexpr fmtflags showpos;
43 static constexpr fmtflags skipws;
44 static constexpr fmtflags unitbuf;
45 static constexpr fmtflags uppercase;
46 static constexpr fmtflags adjustfield;
47 static constexpr fmtflags basefield;
48 static constexpr fmtflags floatfield;
Howard Hinnantc51e1022010-05-11 19:42:16 +000049
50 typedef T2 iostate;
Howard Hinnant770a3662012-07-21 01:03:40 +000051 static constexpr iostate badbit;
52 static constexpr iostate eofbit;
53 static constexpr iostate failbit;
54 static constexpr iostate goodbit;
Howard Hinnantc51e1022010-05-11 19:42:16 +000055
56 typedef T3 openmode;
Howard Hinnant770a3662012-07-21 01:03:40 +000057 static constexpr openmode app;
58 static constexpr openmode ate;
59 static constexpr openmode binary;
60 static constexpr openmode in;
61 static constexpr openmode out;
62 static constexpr openmode trunc;
Howard Hinnantc51e1022010-05-11 19:42:16 +000063
64 typedef T4 seekdir;
Howard Hinnant770a3662012-07-21 01:03:40 +000065 static constexpr seekdir beg;
66 static constexpr seekdir cur;
67 static constexpr seekdir end;
Howard Hinnantc51e1022010-05-11 19:42:16 +000068
69 class Init;
70
71 // 27.5.2.2 fmtflags state:
72 fmtflags flags() const;
73 fmtflags flags(fmtflags fmtfl);
74 fmtflags setf(fmtflags fmtfl);
75 fmtflags setf(fmtflags fmtfl, fmtflags mask);
76 void unsetf(fmtflags mask);
77
78 streamsize precision() const;
79 streamsize precision(streamsize prec);
80 streamsize width() const;
81 streamsize width(streamsize wide);
82
83 // 27.5.2.3 locales:
84 locale imbue(const locale& loc);
85 locale getloc() const;
86
87 // 27.5.2.5 storage:
88 static int xalloc();
89 long& iword(int index);
90 void*& pword(int index);
91
92 // destructor
93 virtual ~ios_base();
94
95 // 27.5.2.6 callbacks;
96 enum event { erase_event, imbue_event, copyfmt_event };
97 typedef void (*event_callback)(event, ios_base&, int index);
98 void register_callback(event_callback fn, int index);
99
100 ios_base(const ios_base&) = delete;
101 ios_base& operator=(const ios_base&) = delete;
102
103 static bool sync_with_stdio(bool sync = true);
104
105protected:
106 ios_base();
107};
108
109template <class charT, class traits = char_traits<charT> >
110class basic_ios
111 : public ios_base
112{
113public:
114 // types:
115 typedef charT char_type;
Marshall Clow57dbb062015-10-29 05:43:30 +0000116 typedef typename traits::int_type int_type; // removed in C++17
117 typedef typename traits::pos_type pos_type; // removed in C++17
118 typedef typename traits::off_type off_type; // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000119 typedef traits traits_type;
120
121 operator unspecified-bool-type() const;
122 bool operator!() const;
123 iostate rdstate() const;
124 void clear(iostate state = goodbit);
125 void setstate(iostate state);
126 bool good() const;
127 bool eof() const;
128 bool fail() const;
129 bool bad() const;
130
131 iostate exceptions() const;
132 void exceptions(iostate except);
133
134 // 27.5.4.1 Constructor/destructor:
135 explicit basic_ios(basic_streambuf<charT,traits>* sb);
136 virtual ~basic_ios();
137
138 // 27.5.4.2 Members:
139 basic_ostream<charT,traits>* tie() const;
140 basic_ostream<charT,traits>* tie(basic_ostream<charT,traits>* tiestr);
141
142 basic_streambuf<charT,traits>* rdbuf() const;
143 basic_streambuf<charT,traits>* rdbuf(basic_streambuf<charT,traits>* sb);
144
145 basic_ios& copyfmt(const basic_ios& rhs);
146
147 char_type fill() const;
148 char_type fill(char_type ch);
149
150 locale imbue(const locale& loc);
151
152 char narrow(char_type c, char dfault) const;
153 char_type widen(char c) const;
154
155 basic_ios(const basic_ios& ) = delete;
156 basic_ios& operator=(const basic_ios&) = delete;
157
158protected:
159 basic_ios();
160 void init(basic_streambuf<charT,traits>* sb);
161 void move(basic_ios& rhs);
Howard Hinnant770a3662012-07-21 01:03:40 +0000162 void swap(basic_ios& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000163 void set_rdbuf(basic_streambuf<charT, traits>* sb);
164};
165
166// 27.5.5, manipulators:
167ios_base& boolalpha (ios_base& str);
168ios_base& noboolalpha(ios_base& str);
169ios_base& showbase (ios_base& str);
170ios_base& noshowbase (ios_base& str);
171ios_base& showpoint (ios_base& str);
172ios_base& noshowpoint(ios_base& str);
173ios_base& showpos (ios_base& str);
174ios_base& noshowpos (ios_base& str);
175ios_base& skipws (ios_base& str);
176ios_base& noskipws (ios_base& str);
177ios_base& uppercase (ios_base& str);
178ios_base& nouppercase(ios_base& str);
179ios_base& unitbuf (ios_base& str);
180ios_base& nounitbuf (ios_base& str);
181
182// 27.5.5.2 adjustfield:
183ios_base& internal (ios_base& str);
184ios_base& left (ios_base& str);
185ios_base& right (ios_base& str);
186
187// 27.5.5.3 basefield:
188ios_base& dec (ios_base& str);
189ios_base& hex (ios_base& str);
190ios_base& oct (ios_base& str);
191
192// 27.5.5.4 floatfield:
193ios_base& fixed (ios_base& str);
194ios_base& scientific (ios_base& str);
195ios_base& hexfloat (ios_base& str);
196ios_base& defaultfloat(ios_base& str);
197
198// 27.5.5.5 error reporting:
199enum class io_errc
200{
201 stream = 1
202};
203
204concept_map ErrorCodeEnum<io_errc> { };
Louis Dionne173f29e2019-05-29 16:01:36 +0000205error_code make_error_code(io_errc e) noexcept;
206error_condition make_error_condition(io_errc e) noexcept;
Marshall Clow3f138dc2013-10-12 22:49:56 +0000207storage-class-specifier const error_category& iostream_category() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000208
209} // std
210
211*/
212
213#include <__config>
Louis Dionne685c4702022-05-18 13:17:14 -0400214
215#if defined(_LIBCPP_HAS_NO_LOCALIZATION)
216# error "The iostreams library is not supported since libc++ has been configured without support for localization."
217#endif
218
219#include <__assert> // all public C++ headers provide the assertion handler
Nikolas Klauser80840272022-02-04 13:04:33 +0100220#include <__ios/fpos.h>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000221#include <__locale>
Nikolas Klauser8fccd622022-03-05 19:17:07 +0100222#include <__utility/swap.h>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000223#include <system_error>
Mark de Weverce8f12c2021-12-22 18:14:14 +0100224#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000225
Nikolas Klausera0e0edb2022-06-16 22:43:46 +0200226// standard-mandated includes
227#include <iosfwd>
228
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000229#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Marshall Clowf9698ab2013-10-12 19:13:52 +0000230#include <atomic> // for __xindex_
231#endif
232
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000233#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500234# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000235#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000236
237_LIBCPP_BEGIN_NAMESPACE_STD
238
239typedef ptrdiff_t streamsize;
240
Howard Hinnant8331b762013-03-06 23:30:19 +0000241class _LIBCPP_TYPE_VIS ios_base
Howard Hinnantc51e1022010-05-11 19:42:16 +0000242{
243public:
Eric Fiselierfb7fc952016-04-21 23:00:33 +0000244 class _LIBCPP_EXCEPTION_ABI failure;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000245
246 typedef unsigned int fmtflags;
247 static const fmtflags boolalpha = 0x0001;
248 static const fmtflags dec = 0x0002;
249 static const fmtflags fixed = 0x0004;
250 static const fmtflags hex = 0x0008;
251 static const fmtflags internal = 0x0010;
252 static const fmtflags left = 0x0020;
253 static const fmtflags oct = 0x0040;
254 static const fmtflags right = 0x0080;
255 static const fmtflags scientific = 0x0100;
256 static const fmtflags showbase = 0x0200;
257 static const fmtflags showpoint = 0x0400;
258 static const fmtflags showpos = 0x0800;
259 static const fmtflags skipws = 0x1000;
260 static const fmtflags unitbuf = 0x2000;
261 static const fmtflags uppercase = 0x4000;
262 static const fmtflags adjustfield = left | right | internal;
263 static const fmtflags basefield = dec | oct | hex;
264 static const fmtflags floatfield = scientific | fixed;
265
266 typedef unsigned int iostate;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000267 static const iostate badbit = 0x1;
268 static const iostate eofbit = 0x2;
269 static const iostate failbit = 0x4;
270 static const iostate goodbit = 0x0;
271
272 typedef unsigned int openmode;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000273 static const openmode app = 0x01;
274 static const openmode ate = 0x02;
275 static const openmode binary = 0x04;
276 static const openmode in = 0x08;
277 static const openmode out = 0x10;
278 static const openmode trunc = 0x20;
279
280 enum seekdir {beg, cur, end};
Marshall Clow57dbb062015-10-29 05:43:30 +0000281
282#if _LIBCPP_STD_VER <= 14
283 typedef iostate io_state;
284 typedef openmode open_mode;
285 typedef seekdir seek_dir;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000286
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000287 typedef _VSTD::streamoff streamoff;
288 typedef _VSTD::streampos streampos;
Marshall Clow57dbb062015-10-29 05:43:30 +0000289#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000290
Howard Hinnant8331b762013-03-06 23:30:19 +0000291 class _LIBCPP_TYPE_VIS Init;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000292
293 // 27.5.2.2 fmtflags state:
Howard Hinnantcf823322010-12-17 14:46:43 +0000294 _LIBCPP_INLINE_VISIBILITY fmtflags flags() const;
295 _LIBCPP_INLINE_VISIBILITY fmtflags flags(fmtflags __fmtfl);
296 _LIBCPP_INLINE_VISIBILITY fmtflags setf(fmtflags __fmtfl);
297 _LIBCPP_INLINE_VISIBILITY fmtflags setf(fmtflags __fmtfl, fmtflags __mask);
298 _LIBCPP_INLINE_VISIBILITY void unsetf(fmtflags __mask);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000299
Howard Hinnantcf823322010-12-17 14:46:43 +0000300 _LIBCPP_INLINE_VISIBILITY streamsize precision() const;
301 _LIBCPP_INLINE_VISIBILITY streamsize precision(streamsize __prec);
302 _LIBCPP_INLINE_VISIBILITY streamsize width() const;
303 _LIBCPP_INLINE_VISIBILITY streamsize width(streamsize __wide);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000304
305 // 27.5.2.3 locales:
306 locale imbue(const locale& __loc);
307 locale getloc() const;
308
309 // 27.5.2.5 storage:
310 static int xalloc();
311 long& iword(int __index);
312 void*& pword(int __index);
313
314 // destructor
315 virtual ~ios_base();
316
317 // 27.5.2.6 callbacks;
318 enum event { erase_event, imbue_event, copyfmt_event };
319 typedef void (*event_callback)(event, ios_base&, int __index);
320 void register_callback(event_callback __fn, int __index);
321
Nikolas Klauser07088642021-12-08 10:57:12 +0100322 ios_base(const ios_base&) = delete;
323 ios_base& operator=(const ios_base&) = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000324
Howard Hinnantc51e1022010-05-11 19:42:16 +0000325 static bool sync_with_stdio(bool __sync = true);
326
Howard Hinnantcf823322010-12-17 14:46:43 +0000327 _LIBCPP_INLINE_VISIBILITY iostate rdstate() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000328 void clear(iostate __state = goodbit);
Howard Hinnantcf823322010-12-17 14:46:43 +0000329 _LIBCPP_INLINE_VISIBILITY void setstate(iostate __state);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000330
Howard Hinnantcf823322010-12-17 14:46:43 +0000331 _LIBCPP_INLINE_VISIBILITY bool good() const;
332 _LIBCPP_INLINE_VISIBILITY bool eof() const;
333 _LIBCPP_INLINE_VISIBILITY bool fail() const;
334 _LIBCPP_INLINE_VISIBILITY bool bad() const;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000335
Howard Hinnantcf823322010-12-17 14:46:43 +0000336 _LIBCPP_INLINE_VISIBILITY iostate exceptions() const;
Howard Hinnantf8850802013-10-06 21:00:29 +0000337 _LIBCPP_INLINE_VISIBILITY void exceptions(iostate __iostate);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000338
339 void __set_badbit_and_consider_rethrow();
340 void __set_failbit_and_consider_rethrow();
341
Louis Dionne94ee3a02019-04-05 16:33:37 +0000342 _LIBCPP_INLINE_VISIBILITY
343 void __setstate_nothrow(iostate __state)
344 {
345 if (__rdbuf_)
346 __rdstate_ |= __state;
347 else
348 __rdstate_ |= __state | ios_base::badbit;
349 }
350
Howard Hinnantc51e1022010-05-11 19:42:16 +0000351protected:
Howard Hinnant64da2602010-09-22 15:29:08 +0000352 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000353 ios_base() {// purposefully does no initialization
354 }
355
356 void init(void* __sb);
Louis Dionne16fe2952018-07-11 23:14:33 +0000357 _LIBCPP_INLINE_VISIBILITY void* rdbuf() const {return __rdbuf_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000358
Louis Dionne16fe2952018-07-11 23:14:33 +0000359 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000360 void rdbuf(void* __sb)
361 {
362 __rdbuf_ = __sb;
363 clear();
364 }
365
366 void __call_callbacks(event);
367 void copyfmt(const ios_base&);
368 void move(ios_base&);
Howard Hinnant770a3662012-07-21 01:03:40 +0000369 void swap(ios_base&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000370
Louis Dionne16fe2952018-07-11 23:14:33 +0000371 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000372 void set_rdbuf(void* __sb)
373 {
374 __rdbuf_ = __sb;
375 }
376
377private:
378 // All data members must be scalars
379 fmtflags __fmtflags_;
380 streamsize __precision_;
381 streamsize __width_;
382 iostate __rdstate_;
383 iostate __exceptions_;
384 void* __rdbuf_;
385 void* __loc_;
386 event_callback* __fn_;
387 int* __index_;
388 size_t __event_size_;
389 size_t __event_cap_;
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000390// TODO(EricWF): Enable this for both Clang and GCC. Currently it is only
391// enabled with clang.
392#if defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
Marshall Clowf9698ab2013-10-12 19:13:52 +0000393 static atomic<int> __xindex_;
394#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000395 static int __xindex_;
Marshall Clowf9698ab2013-10-12 19:13:52 +0000396#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000397 long* __iarray_;
398 size_t __iarray_size_;
399 size_t __iarray_cap_;
400 void** __parray_;
401 size_t __parray_size_;
402 size_t __parray_cap_;
403};
404
405//enum class io_errc
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000406_LIBCPP_DECLARE_STRONG_ENUM(io_errc)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000407{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000408 stream = 1
409};
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000410_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(io_errc)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000411
Howard Hinnant64da2602010-09-22 15:29:08 +0000412template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000413struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<io_errc> : public true_type { };
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000414
Mark de Wever337f8932022-02-11 19:34:33 +0100415#ifdef _LIBCPP_CXX03_LANG
Howard Hinnant64da2602010-09-22 15:29:08 +0000416template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000417struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<io_errc::__lx> : public true_type { };
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000418#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419
Howard Hinnant8331b762013-03-06 23:30:19 +0000420_LIBCPP_FUNC_VIS
Marshall Clow3f138dc2013-10-12 22:49:56 +0000421const error_category& iostream_category() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000422
423inline _LIBCPP_INLINE_VISIBILITY
424error_code
Marshall Clow3f138dc2013-10-12 22:49:56 +0000425make_error_code(io_errc __e) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426{
427 return error_code(static_cast<int>(__e), iostream_category());
428}
429
430inline _LIBCPP_INLINE_VISIBILITY
431error_condition
Marshall Clow3f138dc2013-10-12 22:49:56 +0000432make_error_condition(io_errc __e) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000433{
434 return error_condition(static_cast<int>(__e), iostream_category());
435}
436
Howard Hinnant64da2602010-09-22 15:29:08 +0000437class _LIBCPP_EXCEPTION_ABI ios_base::failure
Howard Hinnantc51e1022010-05-11 19:42:16 +0000438 : public system_error
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000439{
440public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000441 explicit failure(const string& __msg, const error_code& __ec = io_errc::stream);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000442 explicit failure(const char* __msg, const error_code& __ec = io_errc::stream);
Dimitry Andric47269ce2020-03-13 19:36:26 +0100443 failure(const failure&) _NOEXCEPT = default;
Nikolas Klauseraa3a6cd2022-08-24 02:14:29 +0200444 ~failure() _NOEXCEPT override;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000445};
446
Louis Dionne2b239162019-02-12 16:06:02 +0000447_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
448void __throw_failure(char const* __msg) {
449#ifndef _LIBCPP_NO_EXCEPTIONS
450 throw ios_base::failure(__msg);
451#else
452 ((void)__msg);
453 _VSTD::abort();
454#endif
455}
456
Howard Hinnant8331b762013-03-06 23:30:19 +0000457class _LIBCPP_TYPE_VIS ios_base::Init
Howard Hinnantc51e1022010-05-11 19:42:16 +0000458{
459public:
460 Init();
461 ~Init();
462};
463
464// fmtflags
465
466inline _LIBCPP_INLINE_VISIBILITY
467ios_base::fmtflags
468ios_base::flags() const
469{
470 return __fmtflags_;
471}
472
473inline _LIBCPP_INLINE_VISIBILITY
474ios_base::fmtflags
475ios_base::flags(fmtflags __fmtfl)
476{
477 fmtflags __r = __fmtflags_;
478 __fmtflags_ = __fmtfl;
479 return __r;
480}
481
482inline _LIBCPP_INLINE_VISIBILITY
483ios_base::fmtflags
484ios_base::setf(fmtflags __fmtfl)
485{
486 fmtflags __r = __fmtflags_;
487 __fmtflags_ |= __fmtfl;
488 return __r;
489}
490
491inline _LIBCPP_INLINE_VISIBILITY
492void
493ios_base::unsetf(fmtflags __mask)
494{
495 __fmtflags_ &= ~__mask;
496}
497
498inline _LIBCPP_INLINE_VISIBILITY
499ios_base::fmtflags
500ios_base::setf(fmtflags __fmtfl, fmtflags __mask)
501{
502 fmtflags __r = __fmtflags_;
503 unsetf(__mask);
504 __fmtflags_ |= __fmtfl & __mask;
505 return __r;
506}
507
508// precision
509
510inline _LIBCPP_INLINE_VISIBILITY
511streamsize
512ios_base::precision() const
513{
514 return __precision_;
515}
516
517inline _LIBCPP_INLINE_VISIBILITY
518streamsize
519ios_base::precision(streamsize __prec)
520{
521 streamsize __r = __precision_;
522 __precision_ = __prec;
523 return __r;
524}
525
526// width
527
528inline _LIBCPP_INLINE_VISIBILITY
529streamsize
530ios_base::width() const
531{
532 return __width_;
533}
534
535inline _LIBCPP_INLINE_VISIBILITY
536streamsize
537ios_base::width(streamsize __wide)
538{
539 streamsize __r = __width_;
540 __width_ = __wide;
541 return __r;
542}
543
544// iostate
545
546inline _LIBCPP_INLINE_VISIBILITY
547ios_base::iostate
548ios_base::rdstate() const
549{
550 return __rdstate_;
551}
552
553inline _LIBCPP_INLINE_VISIBILITY
554void
555ios_base::setstate(iostate __state)
556{
557 clear(__rdstate_ | __state);
558}
559
560inline _LIBCPP_INLINE_VISIBILITY
561bool
562ios_base::good() const
563{
564 return __rdstate_ == 0;
565}
566
567inline _LIBCPP_INLINE_VISIBILITY
568bool
569ios_base::eof() const
570{
Marshall Clow11de4872013-10-21 14:41:05 +0000571 return (__rdstate_ & eofbit) != 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000572}
573
574inline _LIBCPP_INLINE_VISIBILITY
575bool
576ios_base::fail() const
577{
Marshall Clow11de4872013-10-21 14:41:05 +0000578 return (__rdstate_ & (failbit | badbit)) != 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000579}
580
581inline _LIBCPP_INLINE_VISIBILITY
582bool
583ios_base::bad() const
584{
Marshall Clow11de4872013-10-21 14:41:05 +0000585 return (__rdstate_ & badbit) != 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000586}
587
588inline _LIBCPP_INLINE_VISIBILITY
589ios_base::iostate
590ios_base::exceptions() const
591{
592 return __exceptions_;
593}
594
595inline _LIBCPP_INLINE_VISIBILITY
596void
Howard Hinnantf8850802013-10-06 21:00:29 +0000597ios_base::exceptions(iostate __iostate)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000598{
Howard Hinnantf8850802013-10-06 21:00:29 +0000599 __exceptions_ = __iostate;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000600 clear(__rdstate_);
601}
602
603template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000604class _LIBCPP_TEMPLATE_VIS basic_ios
Howard Hinnantc51e1022010-05-11 19:42:16 +0000605 : public ios_base
606{
607public:
608 // types:
609 typedef _CharT char_type;
610 typedef _Traits traits_type;
611
612 typedef typename traits_type::int_type int_type;
613 typedef typename traits_type::pos_type pos_type;
614 typedef typename traits_type::off_type off_type;
615
Marshall Clowd151d182018-02-01 03:55:27 +0000616 static_assert((is_same<_CharT, typename traits_type::char_type>::value),
617 "traits_type::char_type must be the same type as CharT");
618
Arthur O'Dwyerde7f7b42021-08-06 14:50:09 -0400619#ifdef _LIBCPP_CXX03_LANG
620 // Preserve the ability to compare with literal 0,
621 // and implicitly convert to bool, but not implicitly convert to int.
622 _LIBCPP_INLINE_VISIBILITY
623 operator void*() const {return fail() ? nullptr : (void*)this;}
624#else
Louis Dionne16fe2952018-07-11 23:14:33 +0000625 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer6c9c9a72021-06-15 12:57:54 -0400626 explicit operator bool() const {return !fail();}
Arthur O'Dwyerde7f7b42021-08-06 14:50:09 -0400627#endif
Eric Fiselier6a5f6f92016-12-30 14:05:52 +0000628
Louis Dionne16fe2952018-07-11 23:14:33 +0000629 _LIBCPP_INLINE_VISIBILITY bool operator!() const {return fail();}
630 _LIBCPP_INLINE_VISIBILITY iostate rdstate() const {return ios_base::rdstate();}
631 _LIBCPP_INLINE_VISIBILITY void clear(iostate __state = goodbit) {ios_base::clear(__state);}
632 _LIBCPP_INLINE_VISIBILITY void setstate(iostate __state) {ios_base::setstate(__state);}
633 _LIBCPP_INLINE_VISIBILITY bool good() const {return ios_base::good();}
634 _LIBCPP_INLINE_VISIBILITY bool eof() const {return ios_base::eof();}
635 _LIBCPP_INLINE_VISIBILITY bool fail() const {return ios_base::fail();}
636 _LIBCPP_INLINE_VISIBILITY bool bad() const {return ios_base::bad();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000637
Louis Dionne16fe2952018-07-11 23:14:33 +0000638 _LIBCPP_INLINE_VISIBILITY iostate exceptions() const {return ios_base::exceptions();}
639 _LIBCPP_INLINE_VISIBILITY void exceptions(iostate __iostate) {ios_base::exceptions(__iostate);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000640
641 // 27.5.4.1 Constructor/destructor:
Howard Hinnantcf823322010-12-17 14:46:43 +0000642 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000643 explicit basic_ios(basic_streambuf<char_type,traits_type>* __sb);
Nikolas Klauseraa3a6cd2022-08-24 02:14:29 +0200644 ~basic_ios() override;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000645
646 // 27.5.4.2 Members:
Louis Dionne173f29e2019-05-29 16:01:36 +0000647 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000648 basic_ostream<char_type, traits_type>* tie() const;
Louis Dionne173f29e2019-05-29 16:01:36 +0000649 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000650 basic_ostream<char_type, traits_type>* tie(basic_ostream<char_type, traits_type>* __tiestr);
651
Louis Dionne173f29e2019-05-29 16:01:36 +0000652 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000653 basic_streambuf<char_type, traits_type>* rdbuf() const;
Louis Dionne173f29e2019-05-29 16:01:36 +0000654 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000655 basic_streambuf<char_type, traits_type>* rdbuf(basic_streambuf<char_type, traits_type>* __sb);
656
657 basic_ios& copyfmt(const basic_ios& __rhs);
658
Louis Dionne173f29e2019-05-29 16:01:36 +0000659 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000660 char_type fill() const;
Louis Dionne173f29e2019-05-29 16:01:36 +0000661 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000662 char_type fill(char_type __ch);
663
Louis Dionne173f29e2019-05-29 16:01:36 +0000664 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000665 locale imbue(const locale& __loc);
666
Louis Dionne173f29e2019-05-29 16:01:36 +0000667 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000668 char narrow(char_type __c, char __dfault) const;
Louis Dionne173f29e2019-05-29 16:01:36 +0000669 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000670 char_type widen(char __c) const;
671
672protected:
Louis Dionne16fe2952018-07-11 23:14:33 +0000673 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000674 basic_ios() {// purposefully does no initialization
675 }
Louis Dionne173f29e2019-05-29 16:01:36 +0000676 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000677 void init(basic_streambuf<char_type, traits_type>* __sb);
678
Louis Dionne173f29e2019-05-29 16:01:36 +0000679 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000680 void move(basic_ios& __rhs);
Louis Dionne16fe2952018-07-11 23:14:33 +0000681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000682 void move(basic_ios&& __rhs) {move(__rhs);}
Louis Dionne173f29e2019-05-29 16:01:36 +0000683 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant770a3662012-07-21 01:03:40 +0000684 void swap(basic_ios& __rhs) _NOEXCEPT;
Louis Dionne173f29e2019-05-29 16:01:36 +0000685 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000686 void set_rdbuf(basic_streambuf<char_type, traits_type>* __sb);
687private:
688 basic_ostream<char_type, traits_type>* __tie_;
Bruce Mitchenerca134c52018-02-14 00:29:38 +0000689 mutable int_type __fill_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000690};
691
692template <class _CharT, class _Traits>
693inline _LIBCPP_INLINE_VISIBILITY
694basic_ios<_CharT, _Traits>::basic_ios(basic_streambuf<char_type,traits_type>* __sb)
695{
696 init(__sb);
697}
698
699template <class _CharT, class _Traits>
700basic_ios<_CharT, _Traits>::~basic_ios()
701{
702}
703
704template <class _CharT, class _Traits>
705inline _LIBCPP_INLINE_VISIBILITY
706void
707basic_ios<_CharT, _Traits>::init(basic_streambuf<char_type, traits_type>* __sb)
708{
709 ios_base::init(__sb);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500710 __tie_ = nullptr;
Howard Hinnant2efe10d2012-08-26 18:05:35 +0000711 __fill_ = traits_type::eof();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000712}
713
714template <class _CharT, class _Traits>
715inline _LIBCPP_INLINE_VISIBILITY
716basic_ostream<_CharT, _Traits>*
717basic_ios<_CharT, _Traits>::tie() const
718{
719 return __tie_;
720}
721
722template <class _CharT, class _Traits>
723inline _LIBCPP_INLINE_VISIBILITY
724basic_ostream<_CharT, _Traits>*
725basic_ios<_CharT, _Traits>::tie(basic_ostream<char_type, traits_type>* __tiestr)
726{
727 basic_ostream<char_type, traits_type>* __r = __tie_;
728 __tie_ = __tiestr;
729 return __r;
730}
731
732template <class _CharT, class _Traits>
733inline _LIBCPP_INLINE_VISIBILITY
734basic_streambuf<_CharT, _Traits>*
735basic_ios<_CharT, _Traits>::rdbuf() const
736{
737 return static_cast<basic_streambuf<char_type, traits_type>*>(ios_base::rdbuf());
738}
739
740template <class _CharT, class _Traits>
741inline _LIBCPP_INLINE_VISIBILITY
742basic_streambuf<_CharT, _Traits>*
743basic_ios<_CharT, _Traits>::rdbuf(basic_streambuf<char_type, traits_type>* __sb)
744{
745 basic_streambuf<char_type, traits_type>* __r = rdbuf();
746 ios_base::rdbuf(__sb);
747 return __r;
748}
749
750template <class _CharT, class _Traits>
751inline _LIBCPP_INLINE_VISIBILITY
752locale
753basic_ios<_CharT, _Traits>::imbue(const locale& __loc)
754{
755 locale __r = getloc();
756 ios_base::imbue(__loc);
757 if (rdbuf())
758 rdbuf()->pubimbue(__loc);
759 return __r;
760}
761
762template <class _CharT, class _Traits>
763inline _LIBCPP_INLINE_VISIBILITY
764char
765basic_ios<_CharT, _Traits>::narrow(char_type __c, char __dfault) const
766{
767 return use_facet<ctype<char_type> >(getloc()).narrow(__c, __dfault);
768}
769
770template <class _CharT, class _Traits>
771inline _LIBCPP_INLINE_VISIBILITY
772_CharT
773basic_ios<_CharT, _Traits>::widen(char __c) const
774{
775 return use_facet<ctype<char_type> >(getloc()).widen(__c);
776}
777
778template <class _CharT, class _Traits>
779inline _LIBCPP_INLINE_VISIBILITY
780_CharT
781basic_ios<_CharT, _Traits>::fill() const
782{
Howard Hinnant2efe10d2012-08-26 18:05:35 +0000783 if (traits_type::eq_int_type(traits_type::eof(), __fill_))
784 __fill_ = widen(' ');
Howard Hinnantc51e1022010-05-11 19:42:16 +0000785 return __fill_;
786}
787
788template <class _CharT, class _Traits>
789inline _LIBCPP_INLINE_VISIBILITY
790_CharT
791basic_ios<_CharT, _Traits>::fill(char_type __ch)
792{
Jake Egana7505342022-03-03 09:28:23 -0500793 if (traits_type::eq_int_type(traits_type::eof(), __fill_))
794 __fill_ = widen(' ');
Howard Hinnantc51e1022010-05-11 19:42:16 +0000795 char_type __r = __fill_;
796 __fill_ = __ch;
797 return __r;
798}
799
800template <class _CharT, class _Traits>
801basic_ios<_CharT, _Traits>&
802basic_ios<_CharT, _Traits>::copyfmt(const basic_ios& __rhs)
803{
804 if (this != &__rhs)
805 {
806 __call_callbacks(erase_event);
807 ios_base::copyfmt(__rhs);
808 __tie_ = __rhs.__tie_;
809 __fill_ = __rhs.__fill_;
810 __call_callbacks(copyfmt_event);
811 exceptions(__rhs.exceptions());
812 }
813 return *this;
814}
815
816template <class _CharT, class _Traits>
817inline _LIBCPP_INLINE_VISIBILITY
818void
819basic_ios<_CharT, _Traits>::move(basic_ios& __rhs)
820{
821 ios_base::move(__rhs);
822 __tie_ = __rhs.__tie_;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500823 __rhs.__tie_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000824 __fill_ = __rhs.__fill_;
825}
826
827template <class _CharT, class _Traits>
828inline _LIBCPP_INLINE_VISIBILITY
829void
Howard Hinnant770a3662012-07-21 01:03:40 +0000830basic_ios<_CharT, _Traits>::swap(basic_ios& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000831{
832 ios_base::swap(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000833 _VSTD::swap(__tie_, __rhs.__tie_);
834 _VSTD::swap(__fill_, __rhs.__fill_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000835}
836
837template <class _CharT, class _Traits>
838inline _LIBCPP_INLINE_VISIBILITY
839void
840basic_ios<_CharT, _Traits>::set_rdbuf(basic_streambuf<char_type, traits_type>* __sb)
841{
842 ios_base::set_rdbuf(__sb);
843}
844
Nikolas Klausera9ad6702022-08-13 13:23:16 +0200845_LIBCPP_HIDE_FROM_ABI inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000846ios_base&
847boolalpha(ios_base& __str)
848{
849 __str.setf(ios_base::boolalpha);
850 return __str;
851}
852
Nikolas Klausera9ad6702022-08-13 13:23:16 +0200853_LIBCPP_HIDE_FROM_ABI inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000854ios_base&
855noboolalpha(ios_base& __str)
856{
857 __str.unsetf(ios_base::boolalpha);
858 return __str;
859}
860
Nikolas Klausera9ad6702022-08-13 13:23:16 +0200861_LIBCPP_HIDE_FROM_ABI inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000862ios_base&
863showbase(ios_base& __str)
864{
865 __str.setf(ios_base::showbase);
866 return __str;
867}
868
Nikolas Klausera9ad6702022-08-13 13:23:16 +0200869_LIBCPP_HIDE_FROM_ABI inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000870ios_base&
871noshowbase(ios_base& __str)
872{
873 __str.unsetf(ios_base::showbase);
874 return __str;
875}
876
Nikolas Klausera9ad6702022-08-13 13:23:16 +0200877_LIBCPP_HIDE_FROM_ABI inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000878ios_base&
879showpoint(ios_base& __str)
880{
881 __str.setf(ios_base::showpoint);
882 return __str;
883}
884
Nikolas Klausera9ad6702022-08-13 13:23:16 +0200885_LIBCPP_HIDE_FROM_ABI inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000886ios_base&
887noshowpoint(ios_base& __str)
888{
889 __str.unsetf(ios_base::showpoint);
890 return __str;
891}
892
Nikolas Klausera9ad6702022-08-13 13:23:16 +0200893_LIBCPP_HIDE_FROM_ABI inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000894ios_base&
895showpos(ios_base& __str)
896{
897 __str.setf(ios_base::showpos);
898 return __str;
899}
900
Nikolas Klausera9ad6702022-08-13 13:23:16 +0200901_LIBCPP_HIDE_FROM_ABI inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000902ios_base&
903noshowpos(ios_base& __str)
904{
905 __str.unsetf(ios_base::showpos);
906 return __str;
907}
908
Nikolas Klausera9ad6702022-08-13 13:23:16 +0200909_LIBCPP_HIDE_FROM_ABI inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000910ios_base&
911skipws(ios_base& __str)
912{
913 __str.setf(ios_base::skipws);
914 return __str;
915}
916
Nikolas Klausera9ad6702022-08-13 13:23:16 +0200917_LIBCPP_HIDE_FROM_ABI inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000918ios_base&
919noskipws(ios_base& __str)
920{
921 __str.unsetf(ios_base::skipws);
922 return __str;
923}
924
Nikolas Klausera9ad6702022-08-13 13:23:16 +0200925_LIBCPP_HIDE_FROM_ABI inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000926ios_base&
927uppercase(ios_base& __str)
928{
929 __str.setf(ios_base::uppercase);
930 return __str;
931}
932
Nikolas Klausera9ad6702022-08-13 13:23:16 +0200933_LIBCPP_HIDE_FROM_ABI inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000934ios_base&
935nouppercase(ios_base& __str)
936{
937 __str.unsetf(ios_base::uppercase);
938 return __str;
939}
940
Nikolas Klausera9ad6702022-08-13 13:23:16 +0200941_LIBCPP_HIDE_FROM_ABI inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000942ios_base&
943unitbuf(ios_base& __str)
944{
945 __str.setf(ios_base::unitbuf);
946 return __str;
947}
948
Nikolas Klausera9ad6702022-08-13 13:23:16 +0200949_LIBCPP_HIDE_FROM_ABI inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000950ios_base&
951nounitbuf(ios_base& __str)
952{
953 __str.unsetf(ios_base::unitbuf);
954 return __str;
955}
956
Nikolas Klausera9ad6702022-08-13 13:23:16 +0200957_LIBCPP_HIDE_FROM_ABI inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000958ios_base&
959internal(ios_base& __str)
960{
961 __str.setf(ios_base::internal, ios_base::adjustfield);
962 return __str;
963}
964
Nikolas Klausera9ad6702022-08-13 13:23:16 +0200965_LIBCPP_HIDE_FROM_ABI inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000966ios_base&
967left(ios_base& __str)
968{
969 __str.setf(ios_base::left, ios_base::adjustfield);
970 return __str;
971}
972
Nikolas Klausera9ad6702022-08-13 13:23:16 +0200973_LIBCPP_HIDE_FROM_ABI inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000974ios_base&
975right(ios_base& __str)
976{
977 __str.setf(ios_base::right, ios_base::adjustfield);
978 return __str;
979}
980
Nikolas Klausera9ad6702022-08-13 13:23:16 +0200981_LIBCPP_HIDE_FROM_ABI inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000982ios_base&
983dec(ios_base& __str)
984{
985 __str.setf(ios_base::dec, ios_base::basefield);
986 return __str;
987}
988
Nikolas Klausera9ad6702022-08-13 13:23:16 +0200989_LIBCPP_HIDE_FROM_ABI inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000990ios_base&
991hex(ios_base& __str)
992{
993 __str.setf(ios_base::hex, ios_base::basefield);
994 return __str;
995}
996
Nikolas Klausera9ad6702022-08-13 13:23:16 +0200997_LIBCPP_HIDE_FROM_ABI inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000998ios_base&
999oct(ios_base& __str)
1000{
1001 __str.setf(ios_base::oct, ios_base::basefield);
1002 return __str;
1003}
1004
Nikolas Klausera9ad6702022-08-13 13:23:16 +02001005_LIBCPP_HIDE_FROM_ABI inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006ios_base&
1007fixed(ios_base& __str)
1008{
1009 __str.setf(ios_base::fixed, ios_base::floatfield);
1010 return __str;
1011}
1012
Nikolas Klausera9ad6702022-08-13 13:23:16 +02001013_LIBCPP_HIDE_FROM_ABI inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001014ios_base&
1015scientific(ios_base& __str)
1016{
1017 __str.setf(ios_base::scientific, ios_base::floatfield);
1018 return __str;
1019}
1020
Nikolas Klausera9ad6702022-08-13 13:23:16 +02001021_LIBCPP_HIDE_FROM_ABI inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001022ios_base&
1023hexfloat(ios_base& __str)
1024{
1025 __str.setf(ios_base::fixed | ios_base::scientific, ios_base::floatfield);
1026 return __str;
1027}
1028
Nikolas Klausera9ad6702022-08-13 13:23:16 +02001029_LIBCPP_HIDE_FROM_ABI inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001030ios_base&
1031defaultfloat(ios_base& __str)
1032{
1033 __str.unsetf(ios_base::floatfield);
1034 return __str;
1035}
1036
Howard Hinnantc51e1022010-05-11 19:42:16 +00001037_LIBCPP_END_NAMESPACE_STD
1038
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001039#endif // _LIBCPP_IOS