blob: d1b8c43a8ac9c46eaab30e1cc3463286154a0565 [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>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400223#include <iosfwd>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000224#include <system_error>
Mark de Weverce8f12c2021-12-22 18:14:14 +0100225#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000227#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Marshall Clowf9698ab2013-10-12 19:13:52 +0000228#include <atomic> // for __xindex_
229#endif
230
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000231#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500232# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000233#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000234
235_LIBCPP_BEGIN_NAMESPACE_STD
236
237typedef ptrdiff_t streamsize;
238
Howard Hinnant8331b762013-03-06 23:30:19 +0000239class _LIBCPP_TYPE_VIS ios_base
Howard Hinnantc51e1022010-05-11 19:42:16 +0000240{
241public:
Eric Fiselierfb7fc952016-04-21 23:00:33 +0000242 class _LIBCPP_EXCEPTION_ABI failure;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000243
244 typedef unsigned int fmtflags;
245 static const fmtflags boolalpha = 0x0001;
246 static const fmtflags dec = 0x0002;
247 static const fmtflags fixed = 0x0004;
248 static const fmtflags hex = 0x0008;
249 static const fmtflags internal = 0x0010;
250 static const fmtflags left = 0x0020;
251 static const fmtflags oct = 0x0040;
252 static const fmtflags right = 0x0080;
253 static const fmtflags scientific = 0x0100;
254 static const fmtflags showbase = 0x0200;
255 static const fmtflags showpoint = 0x0400;
256 static const fmtflags showpos = 0x0800;
257 static const fmtflags skipws = 0x1000;
258 static const fmtflags unitbuf = 0x2000;
259 static const fmtflags uppercase = 0x4000;
260 static const fmtflags adjustfield = left | right | internal;
261 static const fmtflags basefield = dec | oct | hex;
262 static const fmtflags floatfield = scientific | fixed;
263
264 typedef unsigned int iostate;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000265 static const iostate badbit = 0x1;
266 static const iostate eofbit = 0x2;
267 static const iostate failbit = 0x4;
268 static const iostate goodbit = 0x0;
269
270 typedef unsigned int openmode;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000271 static const openmode app = 0x01;
272 static const openmode ate = 0x02;
273 static const openmode binary = 0x04;
274 static const openmode in = 0x08;
275 static const openmode out = 0x10;
276 static const openmode trunc = 0x20;
277
278 enum seekdir {beg, cur, end};
Marshall Clow57dbb062015-10-29 05:43:30 +0000279
280#if _LIBCPP_STD_VER <= 14
281 typedef iostate io_state;
282 typedef openmode open_mode;
283 typedef seekdir seek_dir;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000284
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000285 typedef _VSTD::streamoff streamoff;
286 typedef _VSTD::streampos streampos;
Marshall Clow57dbb062015-10-29 05:43:30 +0000287#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000288
Howard Hinnant8331b762013-03-06 23:30:19 +0000289 class _LIBCPP_TYPE_VIS Init;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000290
291 // 27.5.2.2 fmtflags state:
Howard Hinnantcf823322010-12-17 14:46:43 +0000292 _LIBCPP_INLINE_VISIBILITY fmtflags flags() const;
293 _LIBCPP_INLINE_VISIBILITY fmtflags flags(fmtflags __fmtfl);
294 _LIBCPP_INLINE_VISIBILITY fmtflags setf(fmtflags __fmtfl);
295 _LIBCPP_INLINE_VISIBILITY fmtflags setf(fmtflags __fmtfl, fmtflags __mask);
296 _LIBCPP_INLINE_VISIBILITY void unsetf(fmtflags __mask);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000297
Howard Hinnantcf823322010-12-17 14:46:43 +0000298 _LIBCPP_INLINE_VISIBILITY streamsize precision() const;
299 _LIBCPP_INLINE_VISIBILITY streamsize precision(streamsize __prec);
300 _LIBCPP_INLINE_VISIBILITY streamsize width() const;
301 _LIBCPP_INLINE_VISIBILITY streamsize width(streamsize __wide);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000302
303 // 27.5.2.3 locales:
304 locale imbue(const locale& __loc);
305 locale getloc() const;
306
307 // 27.5.2.5 storage:
308 static int xalloc();
309 long& iword(int __index);
310 void*& pword(int __index);
311
312 // destructor
313 virtual ~ios_base();
314
315 // 27.5.2.6 callbacks;
316 enum event { erase_event, imbue_event, copyfmt_event };
317 typedef void (*event_callback)(event, ios_base&, int __index);
318 void register_callback(event_callback __fn, int __index);
319
Nikolas Klauser07088642021-12-08 10:57:12 +0100320 ios_base(const ios_base&) = delete;
321 ios_base& operator=(const ios_base&) = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000322
Howard Hinnantc51e1022010-05-11 19:42:16 +0000323 static bool sync_with_stdio(bool __sync = true);
324
Howard Hinnantcf823322010-12-17 14:46:43 +0000325 _LIBCPP_INLINE_VISIBILITY iostate rdstate() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000326 void clear(iostate __state = goodbit);
Howard Hinnantcf823322010-12-17 14:46:43 +0000327 _LIBCPP_INLINE_VISIBILITY void setstate(iostate __state);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000328
Howard Hinnantcf823322010-12-17 14:46:43 +0000329 _LIBCPP_INLINE_VISIBILITY bool good() const;
330 _LIBCPP_INLINE_VISIBILITY bool eof() const;
331 _LIBCPP_INLINE_VISIBILITY bool fail() const;
332 _LIBCPP_INLINE_VISIBILITY bool bad() const;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000333
Howard Hinnantcf823322010-12-17 14:46:43 +0000334 _LIBCPP_INLINE_VISIBILITY iostate exceptions() const;
Howard Hinnantf8850802013-10-06 21:00:29 +0000335 _LIBCPP_INLINE_VISIBILITY void exceptions(iostate __iostate);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000336
337 void __set_badbit_and_consider_rethrow();
338 void __set_failbit_and_consider_rethrow();
339
Louis Dionne94ee3a02019-04-05 16:33:37 +0000340 _LIBCPP_INLINE_VISIBILITY
341 void __setstate_nothrow(iostate __state)
342 {
343 if (__rdbuf_)
344 __rdstate_ |= __state;
345 else
346 __rdstate_ |= __state | ios_base::badbit;
347 }
348
Howard Hinnantc51e1022010-05-11 19:42:16 +0000349protected:
Howard Hinnant64da2602010-09-22 15:29:08 +0000350 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000351 ios_base() {// purposefully does no initialization
352 }
353
354 void init(void* __sb);
Louis Dionne16fe2952018-07-11 23:14:33 +0000355 _LIBCPP_INLINE_VISIBILITY void* rdbuf() const {return __rdbuf_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000356
Louis Dionne16fe2952018-07-11 23:14:33 +0000357 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000358 void rdbuf(void* __sb)
359 {
360 __rdbuf_ = __sb;
361 clear();
362 }
363
364 void __call_callbacks(event);
365 void copyfmt(const ios_base&);
366 void move(ios_base&);
Howard Hinnant770a3662012-07-21 01:03:40 +0000367 void swap(ios_base&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000368
Louis Dionne16fe2952018-07-11 23:14:33 +0000369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000370 void set_rdbuf(void* __sb)
371 {
372 __rdbuf_ = __sb;
373 }
374
375private:
376 // All data members must be scalars
377 fmtflags __fmtflags_;
378 streamsize __precision_;
379 streamsize __width_;
380 iostate __rdstate_;
381 iostate __exceptions_;
382 void* __rdbuf_;
383 void* __loc_;
384 event_callback* __fn_;
385 int* __index_;
386 size_t __event_size_;
387 size_t __event_cap_;
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000388// TODO(EricWF): Enable this for both Clang and GCC. Currently it is only
389// enabled with clang.
390#if defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
Marshall Clowf9698ab2013-10-12 19:13:52 +0000391 static atomic<int> __xindex_;
392#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393 static int __xindex_;
Marshall Clowf9698ab2013-10-12 19:13:52 +0000394#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000395 long* __iarray_;
396 size_t __iarray_size_;
397 size_t __iarray_cap_;
398 void** __parray_;
399 size_t __parray_size_;
400 size_t __parray_cap_;
401};
402
403//enum class io_errc
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000404_LIBCPP_DECLARE_STRONG_ENUM(io_errc)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000405{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406 stream = 1
407};
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000408_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(io_errc)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409
Howard Hinnant64da2602010-09-22 15:29:08 +0000410template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000411struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<io_errc> : public true_type { };
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000412
Mark de Wever337f8932022-02-11 19:34:33 +0100413#ifdef _LIBCPP_CXX03_LANG
Howard Hinnant64da2602010-09-22 15:29:08 +0000414template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000415struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<io_errc::__lx> : public true_type { };
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000416#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000417
Howard Hinnant8331b762013-03-06 23:30:19 +0000418_LIBCPP_FUNC_VIS
Marshall Clow3f138dc2013-10-12 22:49:56 +0000419const error_category& iostream_category() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000420
421inline _LIBCPP_INLINE_VISIBILITY
422error_code
Marshall Clow3f138dc2013-10-12 22:49:56 +0000423make_error_code(io_errc __e) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000424{
425 return error_code(static_cast<int>(__e), iostream_category());
426}
427
428inline _LIBCPP_INLINE_VISIBILITY
429error_condition
Marshall Clow3f138dc2013-10-12 22:49:56 +0000430make_error_condition(io_errc __e) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000431{
432 return error_condition(static_cast<int>(__e), iostream_category());
433}
434
Howard Hinnant64da2602010-09-22 15:29:08 +0000435class _LIBCPP_EXCEPTION_ABI ios_base::failure
Howard Hinnantc51e1022010-05-11 19:42:16 +0000436 : public system_error
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000437{
438public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000439 explicit failure(const string& __msg, const error_code& __ec = io_errc::stream);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000440 explicit failure(const char* __msg, const error_code& __ec = io_errc::stream);
Dimitry Andric47269ce2020-03-13 19:36:26 +0100441 failure(const failure&) _NOEXCEPT = default;
442 virtual ~failure() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000443};
444
Louis Dionne2b239162019-02-12 16:06:02 +0000445_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
446void __throw_failure(char const* __msg) {
447#ifndef _LIBCPP_NO_EXCEPTIONS
448 throw ios_base::failure(__msg);
449#else
450 ((void)__msg);
451 _VSTD::abort();
452#endif
453}
454
Howard Hinnant8331b762013-03-06 23:30:19 +0000455class _LIBCPP_TYPE_VIS ios_base::Init
Howard Hinnantc51e1022010-05-11 19:42:16 +0000456{
457public:
458 Init();
459 ~Init();
460};
461
462// fmtflags
463
464inline _LIBCPP_INLINE_VISIBILITY
465ios_base::fmtflags
466ios_base::flags() const
467{
468 return __fmtflags_;
469}
470
471inline _LIBCPP_INLINE_VISIBILITY
472ios_base::fmtflags
473ios_base::flags(fmtflags __fmtfl)
474{
475 fmtflags __r = __fmtflags_;
476 __fmtflags_ = __fmtfl;
477 return __r;
478}
479
480inline _LIBCPP_INLINE_VISIBILITY
481ios_base::fmtflags
482ios_base::setf(fmtflags __fmtfl)
483{
484 fmtflags __r = __fmtflags_;
485 __fmtflags_ |= __fmtfl;
486 return __r;
487}
488
489inline _LIBCPP_INLINE_VISIBILITY
490void
491ios_base::unsetf(fmtflags __mask)
492{
493 __fmtflags_ &= ~__mask;
494}
495
496inline _LIBCPP_INLINE_VISIBILITY
497ios_base::fmtflags
498ios_base::setf(fmtflags __fmtfl, fmtflags __mask)
499{
500 fmtflags __r = __fmtflags_;
501 unsetf(__mask);
502 __fmtflags_ |= __fmtfl & __mask;
503 return __r;
504}
505
506// precision
507
508inline _LIBCPP_INLINE_VISIBILITY
509streamsize
510ios_base::precision() const
511{
512 return __precision_;
513}
514
515inline _LIBCPP_INLINE_VISIBILITY
516streamsize
517ios_base::precision(streamsize __prec)
518{
519 streamsize __r = __precision_;
520 __precision_ = __prec;
521 return __r;
522}
523
524// width
525
526inline _LIBCPP_INLINE_VISIBILITY
527streamsize
528ios_base::width() const
529{
530 return __width_;
531}
532
533inline _LIBCPP_INLINE_VISIBILITY
534streamsize
535ios_base::width(streamsize __wide)
536{
537 streamsize __r = __width_;
538 __width_ = __wide;
539 return __r;
540}
541
542// iostate
543
544inline _LIBCPP_INLINE_VISIBILITY
545ios_base::iostate
546ios_base::rdstate() const
547{
548 return __rdstate_;
549}
550
551inline _LIBCPP_INLINE_VISIBILITY
552void
553ios_base::setstate(iostate __state)
554{
555 clear(__rdstate_ | __state);
556}
557
558inline _LIBCPP_INLINE_VISIBILITY
559bool
560ios_base::good() const
561{
562 return __rdstate_ == 0;
563}
564
565inline _LIBCPP_INLINE_VISIBILITY
566bool
567ios_base::eof() const
568{
Marshall Clow11de4872013-10-21 14:41:05 +0000569 return (__rdstate_ & eofbit) != 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000570}
571
572inline _LIBCPP_INLINE_VISIBILITY
573bool
574ios_base::fail() const
575{
Marshall Clow11de4872013-10-21 14:41:05 +0000576 return (__rdstate_ & (failbit | badbit)) != 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000577}
578
579inline _LIBCPP_INLINE_VISIBILITY
580bool
581ios_base::bad() const
582{
Marshall Clow11de4872013-10-21 14:41:05 +0000583 return (__rdstate_ & badbit) != 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000584}
585
586inline _LIBCPP_INLINE_VISIBILITY
587ios_base::iostate
588ios_base::exceptions() const
589{
590 return __exceptions_;
591}
592
593inline _LIBCPP_INLINE_VISIBILITY
594void
Howard Hinnantf8850802013-10-06 21:00:29 +0000595ios_base::exceptions(iostate __iostate)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000596{
Howard Hinnantf8850802013-10-06 21:00:29 +0000597 __exceptions_ = __iostate;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000598 clear(__rdstate_);
599}
600
601template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000602class _LIBCPP_TEMPLATE_VIS basic_ios
Howard Hinnantc51e1022010-05-11 19:42:16 +0000603 : public ios_base
604{
605public:
606 // types:
607 typedef _CharT char_type;
608 typedef _Traits traits_type;
609
610 typedef typename traits_type::int_type int_type;
611 typedef typename traits_type::pos_type pos_type;
612 typedef typename traits_type::off_type off_type;
613
Marshall Clowd151d182018-02-01 03:55:27 +0000614 static_assert((is_same<_CharT, typename traits_type::char_type>::value),
615 "traits_type::char_type must be the same type as CharT");
616
Arthur O'Dwyerde7f7b42021-08-06 14:50:09 -0400617#ifdef _LIBCPP_CXX03_LANG
618 // Preserve the ability to compare with literal 0,
619 // and implicitly convert to bool, but not implicitly convert to int.
620 _LIBCPP_INLINE_VISIBILITY
621 operator void*() const {return fail() ? nullptr : (void*)this;}
622#else
Louis Dionne16fe2952018-07-11 23:14:33 +0000623 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer6c9c9a72021-06-15 12:57:54 -0400624 explicit operator bool() const {return !fail();}
Arthur O'Dwyerde7f7b42021-08-06 14:50:09 -0400625#endif
Eric Fiselier6a5f6f92016-12-30 14:05:52 +0000626
Louis Dionne16fe2952018-07-11 23:14:33 +0000627 _LIBCPP_INLINE_VISIBILITY bool operator!() const {return fail();}
628 _LIBCPP_INLINE_VISIBILITY iostate rdstate() const {return ios_base::rdstate();}
629 _LIBCPP_INLINE_VISIBILITY void clear(iostate __state = goodbit) {ios_base::clear(__state);}
630 _LIBCPP_INLINE_VISIBILITY void setstate(iostate __state) {ios_base::setstate(__state);}
631 _LIBCPP_INLINE_VISIBILITY bool good() const {return ios_base::good();}
632 _LIBCPP_INLINE_VISIBILITY bool eof() const {return ios_base::eof();}
633 _LIBCPP_INLINE_VISIBILITY bool fail() const {return ios_base::fail();}
634 _LIBCPP_INLINE_VISIBILITY bool bad() const {return ios_base::bad();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000635
Louis Dionne16fe2952018-07-11 23:14:33 +0000636 _LIBCPP_INLINE_VISIBILITY iostate exceptions() const {return ios_base::exceptions();}
637 _LIBCPP_INLINE_VISIBILITY void exceptions(iostate __iostate) {ios_base::exceptions(__iostate);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000638
639 // 27.5.4.1 Constructor/destructor:
Howard Hinnantcf823322010-12-17 14:46:43 +0000640 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000641 explicit basic_ios(basic_streambuf<char_type,traits_type>* __sb);
642 virtual ~basic_ios();
643
644 // 27.5.4.2 Members:
Louis Dionne173f29e2019-05-29 16:01:36 +0000645 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000646 basic_ostream<char_type, traits_type>* tie() const;
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(basic_ostream<char_type, traits_type>* __tiestr);
649
Louis Dionne173f29e2019-05-29 16:01:36 +0000650 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000651 basic_streambuf<char_type, traits_type>* rdbuf() const;
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(basic_streambuf<char_type, traits_type>* __sb);
654
655 basic_ios& copyfmt(const basic_ios& __rhs);
656
Louis Dionne173f29e2019-05-29 16:01:36 +0000657 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000658 char_type fill() const;
Louis Dionne173f29e2019-05-29 16:01:36 +0000659 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000660 char_type fill(char_type __ch);
661
Louis Dionne173f29e2019-05-29 16:01:36 +0000662 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000663 locale imbue(const locale& __loc);
664
Louis Dionne173f29e2019-05-29 16:01:36 +0000665 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000666 char narrow(char_type __c, char __dfault) const;
Louis Dionne173f29e2019-05-29 16:01:36 +0000667 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000668 char_type widen(char __c) const;
669
670protected:
Louis Dionne16fe2952018-07-11 23:14:33 +0000671 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000672 basic_ios() {// purposefully does no initialization
673 }
Louis Dionne173f29e2019-05-29 16:01:36 +0000674 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000675 void init(basic_streambuf<char_type, traits_type>* __sb);
676
Louis Dionne173f29e2019-05-29 16:01:36 +0000677 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000678 void move(basic_ios& __rhs);
Louis Dionne16fe2952018-07-11 23:14:33 +0000679 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000680 void move(basic_ios&& __rhs) {move(__rhs);}
Louis Dionne173f29e2019-05-29 16:01:36 +0000681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant770a3662012-07-21 01:03:40 +0000682 void swap(basic_ios& __rhs) _NOEXCEPT;
Louis Dionne173f29e2019-05-29 16:01:36 +0000683 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000684 void set_rdbuf(basic_streambuf<char_type, traits_type>* __sb);
685private:
686 basic_ostream<char_type, traits_type>* __tie_;
Bruce Mitchenerca134c52018-02-14 00:29:38 +0000687 mutable int_type __fill_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000688};
689
690template <class _CharT, class _Traits>
691inline _LIBCPP_INLINE_VISIBILITY
692basic_ios<_CharT, _Traits>::basic_ios(basic_streambuf<char_type,traits_type>* __sb)
693{
694 init(__sb);
695}
696
697template <class _CharT, class _Traits>
698basic_ios<_CharT, _Traits>::~basic_ios()
699{
700}
701
702template <class _CharT, class _Traits>
703inline _LIBCPP_INLINE_VISIBILITY
704void
705basic_ios<_CharT, _Traits>::init(basic_streambuf<char_type, traits_type>* __sb)
706{
707 ios_base::init(__sb);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500708 __tie_ = nullptr;
Howard Hinnant2efe10d2012-08-26 18:05:35 +0000709 __fill_ = traits_type::eof();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000710}
711
712template <class _CharT, class _Traits>
713inline _LIBCPP_INLINE_VISIBILITY
714basic_ostream<_CharT, _Traits>*
715basic_ios<_CharT, _Traits>::tie() const
716{
717 return __tie_;
718}
719
720template <class _CharT, class _Traits>
721inline _LIBCPP_INLINE_VISIBILITY
722basic_ostream<_CharT, _Traits>*
723basic_ios<_CharT, _Traits>::tie(basic_ostream<char_type, traits_type>* __tiestr)
724{
725 basic_ostream<char_type, traits_type>* __r = __tie_;
726 __tie_ = __tiestr;
727 return __r;
728}
729
730template <class _CharT, class _Traits>
731inline _LIBCPP_INLINE_VISIBILITY
732basic_streambuf<_CharT, _Traits>*
733basic_ios<_CharT, _Traits>::rdbuf() const
734{
735 return static_cast<basic_streambuf<char_type, traits_type>*>(ios_base::rdbuf());
736}
737
738template <class _CharT, class _Traits>
739inline _LIBCPP_INLINE_VISIBILITY
740basic_streambuf<_CharT, _Traits>*
741basic_ios<_CharT, _Traits>::rdbuf(basic_streambuf<char_type, traits_type>* __sb)
742{
743 basic_streambuf<char_type, traits_type>* __r = rdbuf();
744 ios_base::rdbuf(__sb);
745 return __r;
746}
747
748template <class _CharT, class _Traits>
749inline _LIBCPP_INLINE_VISIBILITY
750locale
751basic_ios<_CharT, _Traits>::imbue(const locale& __loc)
752{
753 locale __r = getloc();
754 ios_base::imbue(__loc);
755 if (rdbuf())
756 rdbuf()->pubimbue(__loc);
757 return __r;
758}
759
760template <class _CharT, class _Traits>
761inline _LIBCPP_INLINE_VISIBILITY
762char
763basic_ios<_CharT, _Traits>::narrow(char_type __c, char __dfault) const
764{
765 return use_facet<ctype<char_type> >(getloc()).narrow(__c, __dfault);
766}
767
768template <class _CharT, class _Traits>
769inline _LIBCPP_INLINE_VISIBILITY
770_CharT
771basic_ios<_CharT, _Traits>::widen(char __c) const
772{
773 return use_facet<ctype<char_type> >(getloc()).widen(__c);
774}
775
776template <class _CharT, class _Traits>
777inline _LIBCPP_INLINE_VISIBILITY
778_CharT
779basic_ios<_CharT, _Traits>::fill() const
780{
Howard Hinnant2efe10d2012-08-26 18:05:35 +0000781 if (traits_type::eq_int_type(traits_type::eof(), __fill_))
782 __fill_ = widen(' ');
Howard Hinnantc51e1022010-05-11 19:42:16 +0000783 return __fill_;
784}
785
786template <class _CharT, class _Traits>
787inline _LIBCPP_INLINE_VISIBILITY
788_CharT
789basic_ios<_CharT, _Traits>::fill(char_type __ch)
790{
Jake Egana7505342022-03-03 09:28:23 -0500791 if (traits_type::eq_int_type(traits_type::eof(), __fill_))
792 __fill_ = widen(' ');
Howard Hinnantc51e1022010-05-11 19:42:16 +0000793 char_type __r = __fill_;
794 __fill_ = __ch;
795 return __r;
796}
797
798template <class _CharT, class _Traits>
799basic_ios<_CharT, _Traits>&
800basic_ios<_CharT, _Traits>::copyfmt(const basic_ios& __rhs)
801{
802 if (this != &__rhs)
803 {
804 __call_callbacks(erase_event);
805 ios_base::copyfmt(__rhs);
806 __tie_ = __rhs.__tie_;
807 __fill_ = __rhs.__fill_;
808 __call_callbacks(copyfmt_event);
809 exceptions(__rhs.exceptions());
810 }
811 return *this;
812}
813
814template <class _CharT, class _Traits>
815inline _LIBCPP_INLINE_VISIBILITY
816void
817basic_ios<_CharT, _Traits>::move(basic_ios& __rhs)
818{
819 ios_base::move(__rhs);
820 __tie_ = __rhs.__tie_;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500821 __rhs.__tie_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000822 __fill_ = __rhs.__fill_;
823}
824
825template <class _CharT, class _Traits>
826inline _LIBCPP_INLINE_VISIBILITY
827void
Howard Hinnant770a3662012-07-21 01:03:40 +0000828basic_ios<_CharT, _Traits>::swap(basic_ios& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000829{
830 ios_base::swap(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000831 _VSTD::swap(__tie_, __rhs.__tie_);
832 _VSTD::swap(__fill_, __rhs.__fill_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000833}
834
835template <class _CharT, class _Traits>
836inline _LIBCPP_INLINE_VISIBILITY
837void
838basic_ios<_CharT, _Traits>::set_rdbuf(basic_streambuf<char_type, traits_type>* __sb)
839{
840 ios_base::set_rdbuf(__sb);
841}
842
Louis Dionnee29136f2020-07-13 11:53:48 -0400843inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000844ios_base&
845boolalpha(ios_base& __str)
846{
847 __str.setf(ios_base::boolalpha);
848 return __str;
849}
850
Louis Dionnee29136f2020-07-13 11:53:48 -0400851inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000852ios_base&
853noboolalpha(ios_base& __str)
854{
855 __str.unsetf(ios_base::boolalpha);
856 return __str;
857}
858
Louis Dionnee29136f2020-07-13 11:53:48 -0400859inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000860ios_base&
861showbase(ios_base& __str)
862{
863 __str.setf(ios_base::showbase);
864 return __str;
865}
866
Louis Dionnee29136f2020-07-13 11:53:48 -0400867inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000868ios_base&
869noshowbase(ios_base& __str)
870{
871 __str.unsetf(ios_base::showbase);
872 return __str;
873}
874
Louis Dionnee29136f2020-07-13 11:53:48 -0400875inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000876ios_base&
877showpoint(ios_base& __str)
878{
879 __str.setf(ios_base::showpoint);
880 return __str;
881}
882
Louis Dionnee29136f2020-07-13 11:53:48 -0400883inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000884ios_base&
885noshowpoint(ios_base& __str)
886{
887 __str.unsetf(ios_base::showpoint);
888 return __str;
889}
890
Louis Dionnee29136f2020-07-13 11:53:48 -0400891inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000892ios_base&
893showpos(ios_base& __str)
894{
895 __str.setf(ios_base::showpos);
896 return __str;
897}
898
Louis Dionnee29136f2020-07-13 11:53:48 -0400899inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000900ios_base&
901noshowpos(ios_base& __str)
902{
903 __str.unsetf(ios_base::showpos);
904 return __str;
905}
906
Louis Dionnee29136f2020-07-13 11:53:48 -0400907inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000908ios_base&
909skipws(ios_base& __str)
910{
911 __str.setf(ios_base::skipws);
912 return __str;
913}
914
Louis Dionnee29136f2020-07-13 11:53:48 -0400915inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000916ios_base&
917noskipws(ios_base& __str)
918{
919 __str.unsetf(ios_base::skipws);
920 return __str;
921}
922
Louis Dionnee29136f2020-07-13 11:53:48 -0400923inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000924ios_base&
925uppercase(ios_base& __str)
926{
927 __str.setf(ios_base::uppercase);
928 return __str;
929}
930
Louis Dionnee29136f2020-07-13 11:53:48 -0400931inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000932ios_base&
933nouppercase(ios_base& __str)
934{
935 __str.unsetf(ios_base::uppercase);
936 return __str;
937}
938
Louis Dionnee29136f2020-07-13 11:53:48 -0400939inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000940ios_base&
941unitbuf(ios_base& __str)
942{
943 __str.setf(ios_base::unitbuf);
944 return __str;
945}
946
Louis Dionnee29136f2020-07-13 11:53:48 -0400947inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000948ios_base&
949nounitbuf(ios_base& __str)
950{
951 __str.unsetf(ios_base::unitbuf);
952 return __str;
953}
954
Louis Dionnee29136f2020-07-13 11:53:48 -0400955inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956ios_base&
957internal(ios_base& __str)
958{
959 __str.setf(ios_base::internal, ios_base::adjustfield);
960 return __str;
961}
962
Louis Dionnee29136f2020-07-13 11:53:48 -0400963inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964ios_base&
965left(ios_base& __str)
966{
967 __str.setf(ios_base::left, ios_base::adjustfield);
968 return __str;
969}
970
Louis Dionnee29136f2020-07-13 11:53:48 -0400971inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000972ios_base&
973right(ios_base& __str)
974{
975 __str.setf(ios_base::right, ios_base::adjustfield);
976 return __str;
977}
978
Louis Dionnee29136f2020-07-13 11:53:48 -0400979inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980ios_base&
981dec(ios_base& __str)
982{
983 __str.setf(ios_base::dec, ios_base::basefield);
984 return __str;
985}
986
Louis Dionnee29136f2020-07-13 11:53:48 -0400987inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988ios_base&
989hex(ios_base& __str)
990{
991 __str.setf(ios_base::hex, ios_base::basefield);
992 return __str;
993}
994
Louis Dionnee29136f2020-07-13 11:53:48 -0400995inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000996ios_base&
997oct(ios_base& __str)
998{
999 __str.setf(ios_base::oct, ios_base::basefield);
1000 return __str;
1001}
1002
Louis Dionnee29136f2020-07-13 11:53:48 -04001003inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004ios_base&
1005fixed(ios_base& __str)
1006{
1007 __str.setf(ios_base::fixed, ios_base::floatfield);
1008 return __str;
1009}
1010
Louis Dionnee29136f2020-07-13 11:53:48 -04001011inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001012ios_base&
1013scientific(ios_base& __str)
1014{
1015 __str.setf(ios_base::scientific, ios_base::floatfield);
1016 return __str;
1017}
1018
Louis Dionnee29136f2020-07-13 11:53:48 -04001019inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001020ios_base&
1021hexfloat(ios_base& __str)
1022{
1023 __str.setf(ios_base::fixed | ios_base::scientific, ios_base::floatfield);
1024 return __str;
1025}
1026
Louis Dionnee29136f2020-07-13 11:53:48 -04001027inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001028ios_base&
1029defaultfloat(ios_base& __str)
1030{
1031 __str.unsetf(ios_base::floatfield);
1032 return __str;
1033}
1034
Howard Hinnantc51e1022010-05-11 19:42:16 +00001035_LIBCPP_END_NAMESPACE_STD
1036
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001037#endif // _LIBCPP_IOS