blob: 74c3c63bd347f26764da6bc788b23a16df1e831b [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>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000214#include <__locale>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400215#include <iosfwd>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000216#include <system_error>
Mark de Weverce8f12c2021-12-22 18:14:14 +0100217#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000218
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000219#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Marshall Clowf9698ab2013-10-12 19:13:52 +0000220#include <atomic> // for __xindex_
221#endif
222
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000223#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000224#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000225#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226
227_LIBCPP_BEGIN_NAMESPACE_STD
228
229typedef ptrdiff_t streamsize;
230
Howard Hinnant8331b762013-03-06 23:30:19 +0000231class _LIBCPP_TYPE_VIS ios_base
Howard Hinnantc51e1022010-05-11 19:42:16 +0000232{
233public:
Eric Fiselierfb7fc952016-04-21 23:00:33 +0000234 class _LIBCPP_EXCEPTION_ABI failure;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000235
236 typedef unsigned int fmtflags;
237 static const fmtflags boolalpha = 0x0001;
238 static const fmtflags dec = 0x0002;
239 static const fmtflags fixed = 0x0004;
240 static const fmtflags hex = 0x0008;
241 static const fmtflags internal = 0x0010;
242 static const fmtflags left = 0x0020;
243 static const fmtflags oct = 0x0040;
244 static const fmtflags right = 0x0080;
245 static const fmtflags scientific = 0x0100;
246 static const fmtflags showbase = 0x0200;
247 static const fmtflags showpoint = 0x0400;
248 static const fmtflags showpos = 0x0800;
249 static const fmtflags skipws = 0x1000;
250 static const fmtflags unitbuf = 0x2000;
251 static const fmtflags uppercase = 0x4000;
252 static const fmtflags adjustfield = left | right | internal;
253 static const fmtflags basefield = dec | oct | hex;
254 static const fmtflags floatfield = scientific | fixed;
255
256 typedef unsigned int iostate;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000257 static const iostate badbit = 0x1;
258 static const iostate eofbit = 0x2;
259 static const iostate failbit = 0x4;
260 static const iostate goodbit = 0x0;
261
262 typedef unsigned int openmode;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000263 static const openmode app = 0x01;
264 static const openmode ate = 0x02;
265 static const openmode binary = 0x04;
266 static const openmode in = 0x08;
267 static const openmode out = 0x10;
268 static const openmode trunc = 0x20;
269
270 enum seekdir {beg, cur, end};
Marshall Clow57dbb062015-10-29 05:43:30 +0000271
272#if _LIBCPP_STD_VER <= 14
273 typedef iostate io_state;
274 typedef openmode open_mode;
275 typedef seekdir seek_dir;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000276
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000277 typedef _VSTD::streamoff streamoff;
278 typedef _VSTD::streampos streampos;
Marshall Clow57dbb062015-10-29 05:43:30 +0000279#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000280
Howard Hinnant8331b762013-03-06 23:30:19 +0000281 class _LIBCPP_TYPE_VIS Init;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000282
283 // 27.5.2.2 fmtflags state:
Howard Hinnantcf823322010-12-17 14:46:43 +0000284 _LIBCPP_INLINE_VISIBILITY fmtflags flags() const;
285 _LIBCPP_INLINE_VISIBILITY fmtflags flags(fmtflags __fmtfl);
286 _LIBCPP_INLINE_VISIBILITY fmtflags setf(fmtflags __fmtfl);
287 _LIBCPP_INLINE_VISIBILITY fmtflags setf(fmtflags __fmtfl, fmtflags __mask);
288 _LIBCPP_INLINE_VISIBILITY void unsetf(fmtflags __mask);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000289
Howard Hinnantcf823322010-12-17 14:46:43 +0000290 _LIBCPP_INLINE_VISIBILITY streamsize precision() const;
291 _LIBCPP_INLINE_VISIBILITY streamsize precision(streamsize __prec);
292 _LIBCPP_INLINE_VISIBILITY streamsize width() const;
293 _LIBCPP_INLINE_VISIBILITY streamsize width(streamsize __wide);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000294
295 // 27.5.2.3 locales:
296 locale imbue(const locale& __loc);
297 locale getloc() const;
298
299 // 27.5.2.5 storage:
300 static int xalloc();
301 long& iword(int __index);
302 void*& pword(int __index);
303
304 // destructor
305 virtual ~ios_base();
306
307 // 27.5.2.6 callbacks;
308 enum event { erase_event, imbue_event, copyfmt_event };
309 typedef void (*event_callback)(event, ios_base&, int __index);
310 void register_callback(event_callback __fn, int __index);
311
Nikolas Klauser07088642021-12-08 10:57:12 +0100312 ios_base(const ios_base&) = delete;
313 ios_base& operator=(const ios_base&) = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000314
Howard Hinnantc51e1022010-05-11 19:42:16 +0000315 static bool sync_with_stdio(bool __sync = true);
316
Howard Hinnantcf823322010-12-17 14:46:43 +0000317 _LIBCPP_INLINE_VISIBILITY iostate rdstate() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000318 void clear(iostate __state = goodbit);
Howard Hinnantcf823322010-12-17 14:46:43 +0000319 _LIBCPP_INLINE_VISIBILITY void setstate(iostate __state);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000320
Howard Hinnantcf823322010-12-17 14:46:43 +0000321 _LIBCPP_INLINE_VISIBILITY bool good() const;
322 _LIBCPP_INLINE_VISIBILITY bool eof() const;
323 _LIBCPP_INLINE_VISIBILITY bool fail() const;
324 _LIBCPP_INLINE_VISIBILITY bool bad() const;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000325
Howard Hinnantcf823322010-12-17 14:46:43 +0000326 _LIBCPP_INLINE_VISIBILITY iostate exceptions() const;
Howard Hinnantf8850802013-10-06 21:00:29 +0000327 _LIBCPP_INLINE_VISIBILITY void exceptions(iostate __iostate);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000328
329 void __set_badbit_and_consider_rethrow();
330 void __set_failbit_and_consider_rethrow();
331
Louis Dionne94ee3a02019-04-05 16:33:37 +0000332 _LIBCPP_INLINE_VISIBILITY
333 void __setstate_nothrow(iostate __state)
334 {
335 if (__rdbuf_)
336 __rdstate_ |= __state;
337 else
338 __rdstate_ |= __state | ios_base::badbit;
339 }
340
Howard Hinnantc51e1022010-05-11 19:42:16 +0000341protected:
Howard Hinnant64da2602010-09-22 15:29:08 +0000342 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000343 ios_base() {// purposefully does no initialization
344 }
345
346 void init(void* __sb);
Louis Dionne16fe2952018-07-11 23:14:33 +0000347 _LIBCPP_INLINE_VISIBILITY void* rdbuf() const {return __rdbuf_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000348
Louis Dionne16fe2952018-07-11 23:14:33 +0000349 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000350 void rdbuf(void* __sb)
351 {
352 __rdbuf_ = __sb;
353 clear();
354 }
355
356 void __call_callbacks(event);
357 void copyfmt(const ios_base&);
358 void move(ios_base&);
Howard Hinnant770a3662012-07-21 01:03:40 +0000359 void swap(ios_base&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000360
Louis Dionne16fe2952018-07-11 23:14:33 +0000361 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000362 void set_rdbuf(void* __sb)
363 {
364 __rdbuf_ = __sb;
365 }
366
367private:
368 // All data members must be scalars
369 fmtflags __fmtflags_;
370 streamsize __precision_;
371 streamsize __width_;
372 iostate __rdstate_;
373 iostate __exceptions_;
374 void* __rdbuf_;
375 void* __loc_;
376 event_callback* __fn_;
377 int* __index_;
378 size_t __event_size_;
379 size_t __event_cap_;
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000380// TODO(EricWF): Enable this for both Clang and GCC. Currently it is only
381// enabled with clang.
382#if defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
Marshall Clowf9698ab2013-10-12 19:13:52 +0000383 static atomic<int> __xindex_;
384#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000385 static int __xindex_;
Marshall Clowf9698ab2013-10-12 19:13:52 +0000386#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000387 long* __iarray_;
388 size_t __iarray_size_;
389 size_t __iarray_cap_;
390 void** __parray_;
391 size_t __parray_size_;
392 size_t __parray_cap_;
393};
394
395//enum class io_errc
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000396_LIBCPP_DECLARE_STRONG_ENUM(io_errc)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000397{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000398 stream = 1
399};
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000400_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(io_errc)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000401
Howard Hinnant64da2602010-09-22 15:29:08 +0000402template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000403struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<io_errc> : public true_type { };
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000404
405#ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
Howard Hinnant64da2602010-09-22 15:29:08 +0000406template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000407struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<io_errc::__lx> : public true_type { };
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000408#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409
Howard Hinnant8331b762013-03-06 23:30:19 +0000410_LIBCPP_FUNC_VIS
Marshall Clow3f138dc2013-10-12 22:49:56 +0000411const error_category& iostream_category() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000412
413inline _LIBCPP_INLINE_VISIBILITY
414error_code
Marshall Clow3f138dc2013-10-12 22:49:56 +0000415make_error_code(io_errc __e) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416{
417 return error_code(static_cast<int>(__e), iostream_category());
418}
419
420inline _LIBCPP_INLINE_VISIBILITY
421error_condition
Marshall Clow3f138dc2013-10-12 22:49:56 +0000422make_error_condition(io_errc __e) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423{
424 return error_condition(static_cast<int>(__e), iostream_category());
425}
426
Howard Hinnant64da2602010-09-22 15:29:08 +0000427class _LIBCPP_EXCEPTION_ABI ios_base::failure
Howard Hinnantc51e1022010-05-11 19:42:16 +0000428 : public system_error
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000429{
430public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000431 explicit failure(const string& __msg, const error_code& __ec = io_errc::stream);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000432 explicit failure(const char* __msg, const error_code& __ec = io_errc::stream);
Dimitry Andric47269ce2020-03-13 19:36:26 +0100433 failure(const failure&) _NOEXCEPT = default;
434 virtual ~failure() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000435};
436
Louis Dionne2b239162019-02-12 16:06:02 +0000437_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
438void __throw_failure(char const* __msg) {
439#ifndef _LIBCPP_NO_EXCEPTIONS
440 throw ios_base::failure(__msg);
441#else
442 ((void)__msg);
443 _VSTD::abort();
444#endif
445}
446
Howard Hinnant8331b762013-03-06 23:30:19 +0000447class _LIBCPP_TYPE_VIS ios_base::Init
Howard Hinnantc51e1022010-05-11 19:42:16 +0000448{
449public:
450 Init();
451 ~Init();
452};
453
454// fmtflags
455
456inline _LIBCPP_INLINE_VISIBILITY
457ios_base::fmtflags
458ios_base::flags() const
459{
460 return __fmtflags_;
461}
462
463inline _LIBCPP_INLINE_VISIBILITY
464ios_base::fmtflags
465ios_base::flags(fmtflags __fmtfl)
466{
467 fmtflags __r = __fmtflags_;
468 __fmtflags_ = __fmtfl;
469 return __r;
470}
471
472inline _LIBCPP_INLINE_VISIBILITY
473ios_base::fmtflags
474ios_base::setf(fmtflags __fmtfl)
475{
476 fmtflags __r = __fmtflags_;
477 __fmtflags_ |= __fmtfl;
478 return __r;
479}
480
481inline _LIBCPP_INLINE_VISIBILITY
482void
483ios_base::unsetf(fmtflags __mask)
484{
485 __fmtflags_ &= ~__mask;
486}
487
488inline _LIBCPP_INLINE_VISIBILITY
489ios_base::fmtflags
490ios_base::setf(fmtflags __fmtfl, fmtflags __mask)
491{
492 fmtflags __r = __fmtflags_;
493 unsetf(__mask);
494 __fmtflags_ |= __fmtfl & __mask;
495 return __r;
496}
497
498// precision
499
500inline _LIBCPP_INLINE_VISIBILITY
501streamsize
502ios_base::precision() const
503{
504 return __precision_;
505}
506
507inline _LIBCPP_INLINE_VISIBILITY
508streamsize
509ios_base::precision(streamsize __prec)
510{
511 streamsize __r = __precision_;
512 __precision_ = __prec;
513 return __r;
514}
515
516// width
517
518inline _LIBCPP_INLINE_VISIBILITY
519streamsize
520ios_base::width() const
521{
522 return __width_;
523}
524
525inline _LIBCPP_INLINE_VISIBILITY
526streamsize
527ios_base::width(streamsize __wide)
528{
529 streamsize __r = __width_;
530 __width_ = __wide;
531 return __r;
532}
533
534// iostate
535
536inline _LIBCPP_INLINE_VISIBILITY
537ios_base::iostate
538ios_base::rdstate() const
539{
540 return __rdstate_;
541}
542
543inline _LIBCPP_INLINE_VISIBILITY
544void
545ios_base::setstate(iostate __state)
546{
547 clear(__rdstate_ | __state);
548}
549
550inline _LIBCPP_INLINE_VISIBILITY
551bool
552ios_base::good() const
553{
554 return __rdstate_ == 0;
555}
556
557inline _LIBCPP_INLINE_VISIBILITY
558bool
559ios_base::eof() const
560{
Marshall Clow11de4872013-10-21 14:41:05 +0000561 return (__rdstate_ & eofbit) != 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000562}
563
564inline _LIBCPP_INLINE_VISIBILITY
565bool
566ios_base::fail() const
567{
Marshall Clow11de4872013-10-21 14:41:05 +0000568 return (__rdstate_ & (failbit | badbit)) != 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000569}
570
571inline _LIBCPP_INLINE_VISIBILITY
572bool
573ios_base::bad() const
574{
Marshall Clow11de4872013-10-21 14:41:05 +0000575 return (__rdstate_ & badbit) != 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000576}
577
578inline _LIBCPP_INLINE_VISIBILITY
579ios_base::iostate
580ios_base::exceptions() const
581{
582 return __exceptions_;
583}
584
585inline _LIBCPP_INLINE_VISIBILITY
586void
Howard Hinnantf8850802013-10-06 21:00:29 +0000587ios_base::exceptions(iostate __iostate)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000588{
Howard Hinnantf8850802013-10-06 21:00:29 +0000589 __exceptions_ = __iostate;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000590 clear(__rdstate_);
591}
592
593template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000594class _LIBCPP_TEMPLATE_VIS basic_ios
Howard Hinnantc51e1022010-05-11 19:42:16 +0000595 : public ios_base
596{
597public:
598 // types:
599 typedef _CharT char_type;
600 typedef _Traits traits_type;
601
602 typedef typename traits_type::int_type int_type;
603 typedef typename traits_type::pos_type pos_type;
604 typedef typename traits_type::off_type off_type;
605
Marshall Clowd151d182018-02-01 03:55:27 +0000606 static_assert((is_same<_CharT, typename traits_type::char_type>::value),
607 "traits_type::char_type must be the same type as CharT");
608
Arthur O'Dwyerde7f7b42021-08-06 14:50:09 -0400609#ifdef _LIBCPP_CXX03_LANG
610 // Preserve the ability to compare with literal 0,
611 // and implicitly convert to bool, but not implicitly convert to int.
612 _LIBCPP_INLINE_VISIBILITY
613 operator void*() const {return fail() ? nullptr : (void*)this;}
614#else
Louis Dionne16fe2952018-07-11 23:14:33 +0000615 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer6c9c9a72021-06-15 12:57:54 -0400616 explicit operator bool() const {return !fail();}
Arthur O'Dwyerde7f7b42021-08-06 14:50:09 -0400617#endif
Eric Fiselier6a5f6f92016-12-30 14:05:52 +0000618
Louis Dionne16fe2952018-07-11 23:14:33 +0000619 _LIBCPP_INLINE_VISIBILITY bool operator!() const {return fail();}
620 _LIBCPP_INLINE_VISIBILITY iostate rdstate() const {return ios_base::rdstate();}
621 _LIBCPP_INLINE_VISIBILITY void clear(iostate __state = goodbit) {ios_base::clear(__state);}
622 _LIBCPP_INLINE_VISIBILITY void setstate(iostate __state) {ios_base::setstate(__state);}
623 _LIBCPP_INLINE_VISIBILITY bool good() const {return ios_base::good();}
624 _LIBCPP_INLINE_VISIBILITY bool eof() const {return ios_base::eof();}
625 _LIBCPP_INLINE_VISIBILITY bool fail() const {return ios_base::fail();}
626 _LIBCPP_INLINE_VISIBILITY bool bad() const {return ios_base::bad();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000627
Louis Dionne16fe2952018-07-11 23:14:33 +0000628 _LIBCPP_INLINE_VISIBILITY iostate exceptions() const {return ios_base::exceptions();}
629 _LIBCPP_INLINE_VISIBILITY void exceptions(iostate __iostate) {ios_base::exceptions(__iostate);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000630
631 // 27.5.4.1 Constructor/destructor:
Howard Hinnantcf823322010-12-17 14:46:43 +0000632 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000633 explicit basic_ios(basic_streambuf<char_type,traits_type>* __sb);
634 virtual ~basic_ios();
635
636 // 27.5.4.2 Members:
Louis Dionne173f29e2019-05-29 16:01:36 +0000637 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000638 basic_ostream<char_type, traits_type>* tie() const;
Louis Dionne173f29e2019-05-29 16:01:36 +0000639 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000640 basic_ostream<char_type, traits_type>* tie(basic_ostream<char_type, traits_type>* __tiestr);
641
Louis Dionne173f29e2019-05-29 16:01:36 +0000642 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000643 basic_streambuf<char_type, traits_type>* rdbuf() const;
Louis Dionne173f29e2019-05-29 16:01:36 +0000644 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000645 basic_streambuf<char_type, traits_type>* rdbuf(basic_streambuf<char_type, traits_type>* __sb);
646
647 basic_ios& copyfmt(const basic_ios& __rhs);
648
Louis Dionne173f29e2019-05-29 16:01:36 +0000649 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000650 char_type fill() const;
Louis Dionne173f29e2019-05-29 16:01:36 +0000651 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000652 char_type fill(char_type __ch);
653
Louis Dionne173f29e2019-05-29 16:01:36 +0000654 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000655 locale imbue(const locale& __loc);
656
Louis Dionne173f29e2019-05-29 16:01:36 +0000657 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000658 char narrow(char_type __c, char __dfault) const;
Louis Dionne173f29e2019-05-29 16:01:36 +0000659 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000660 char_type widen(char __c) const;
661
662protected:
Louis Dionne16fe2952018-07-11 23:14:33 +0000663 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000664 basic_ios() {// purposefully does no initialization
665 }
Louis Dionne173f29e2019-05-29 16:01:36 +0000666 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000667 void init(basic_streambuf<char_type, traits_type>* __sb);
668
Louis Dionne173f29e2019-05-29 16:01:36 +0000669 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000670 void move(basic_ios& __rhs);
Louis Dionne16fe2952018-07-11 23:14:33 +0000671 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000672 void move(basic_ios&& __rhs) {move(__rhs);}
Louis Dionne173f29e2019-05-29 16:01:36 +0000673 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant770a3662012-07-21 01:03:40 +0000674 void swap(basic_ios& __rhs) _NOEXCEPT;
Louis Dionne173f29e2019-05-29 16:01:36 +0000675 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000676 void set_rdbuf(basic_streambuf<char_type, traits_type>* __sb);
677private:
678 basic_ostream<char_type, traits_type>* __tie_;
Bruce Mitchenerca134c52018-02-14 00:29:38 +0000679 mutable int_type __fill_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000680};
681
682template <class _CharT, class _Traits>
683inline _LIBCPP_INLINE_VISIBILITY
684basic_ios<_CharT, _Traits>::basic_ios(basic_streambuf<char_type,traits_type>* __sb)
685{
686 init(__sb);
687}
688
689template <class _CharT, class _Traits>
690basic_ios<_CharT, _Traits>::~basic_ios()
691{
692}
693
694template <class _CharT, class _Traits>
695inline _LIBCPP_INLINE_VISIBILITY
696void
697basic_ios<_CharT, _Traits>::init(basic_streambuf<char_type, traits_type>* __sb)
698{
699 ios_base::init(__sb);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500700 __tie_ = nullptr;
Howard Hinnant2efe10d2012-08-26 18:05:35 +0000701 __fill_ = traits_type::eof();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000702}
703
704template <class _CharT, class _Traits>
705inline _LIBCPP_INLINE_VISIBILITY
706basic_ostream<_CharT, _Traits>*
707basic_ios<_CharT, _Traits>::tie() const
708{
709 return __tie_;
710}
711
712template <class _CharT, class _Traits>
713inline _LIBCPP_INLINE_VISIBILITY
714basic_ostream<_CharT, _Traits>*
715basic_ios<_CharT, _Traits>::tie(basic_ostream<char_type, traits_type>* __tiestr)
716{
717 basic_ostream<char_type, traits_type>* __r = __tie_;
718 __tie_ = __tiestr;
719 return __r;
720}
721
722template <class _CharT, class _Traits>
723inline _LIBCPP_INLINE_VISIBILITY
724basic_streambuf<_CharT, _Traits>*
725basic_ios<_CharT, _Traits>::rdbuf() const
726{
727 return static_cast<basic_streambuf<char_type, traits_type>*>(ios_base::rdbuf());
728}
729
730template <class _CharT, class _Traits>
731inline _LIBCPP_INLINE_VISIBILITY
732basic_streambuf<_CharT, _Traits>*
733basic_ios<_CharT, _Traits>::rdbuf(basic_streambuf<char_type, traits_type>* __sb)
734{
735 basic_streambuf<char_type, traits_type>* __r = rdbuf();
736 ios_base::rdbuf(__sb);
737 return __r;
738}
739
740template <class _CharT, class _Traits>
741inline _LIBCPP_INLINE_VISIBILITY
742locale
743basic_ios<_CharT, _Traits>::imbue(const locale& __loc)
744{
745 locale __r = getloc();
746 ios_base::imbue(__loc);
747 if (rdbuf())
748 rdbuf()->pubimbue(__loc);
749 return __r;
750}
751
752template <class _CharT, class _Traits>
753inline _LIBCPP_INLINE_VISIBILITY
754char
755basic_ios<_CharT, _Traits>::narrow(char_type __c, char __dfault) const
756{
757 return use_facet<ctype<char_type> >(getloc()).narrow(__c, __dfault);
758}
759
760template <class _CharT, class _Traits>
761inline _LIBCPP_INLINE_VISIBILITY
762_CharT
763basic_ios<_CharT, _Traits>::widen(char __c) const
764{
765 return use_facet<ctype<char_type> >(getloc()).widen(__c);
766}
767
768template <class _CharT, class _Traits>
769inline _LIBCPP_INLINE_VISIBILITY
770_CharT
771basic_ios<_CharT, _Traits>::fill() const
772{
Howard Hinnant2efe10d2012-08-26 18:05:35 +0000773 if (traits_type::eq_int_type(traits_type::eof(), __fill_))
774 __fill_ = widen(' ');
Howard Hinnantc51e1022010-05-11 19:42:16 +0000775 return __fill_;
776}
777
778template <class _CharT, class _Traits>
779inline _LIBCPP_INLINE_VISIBILITY
780_CharT
781basic_ios<_CharT, _Traits>::fill(char_type __ch)
782{
783 char_type __r = __fill_;
784 __fill_ = __ch;
785 return __r;
786}
787
788template <class _CharT, class _Traits>
789basic_ios<_CharT, _Traits>&
790basic_ios<_CharT, _Traits>::copyfmt(const basic_ios& __rhs)
791{
792 if (this != &__rhs)
793 {
794 __call_callbacks(erase_event);
795 ios_base::copyfmt(__rhs);
796 __tie_ = __rhs.__tie_;
797 __fill_ = __rhs.__fill_;
798 __call_callbacks(copyfmt_event);
799 exceptions(__rhs.exceptions());
800 }
801 return *this;
802}
803
804template <class _CharT, class _Traits>
805inline _LIBCPP_INLINE_VISIBILITY
806void
807basic_ios<_CharT, _Traits>::move(basic_ios& __rhs)
808{
809 ios_base::move(__rhs);
810 __tie_ = __rhs.__tie_;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500811 __rhs.__tie_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000812 __fill_ = __rhs.__fill_;
813}
814
815template <class _CharT, class _Traits>
816inline _LIBCPP_INLINE_VISIBILITY
817void
Howard Hinnant770a3662012-07-21 01:03:40 +0000818basic_ios<_CharT, _Traits>::swap(basic_ios& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000819{
820 ios_base::swap(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000821 _VSTD::swap(__tie_, __rhs.__tie_);
822 _VSTD::swap(__fill_, __rhs.__fill_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000823}
824
825template <class _CharT, class _Traits>
826inline _LIBCPP_INLINE_VISIBILITY
827void
828basic_ios<_CharT, _Traits>::set_rdbuf(basic_streambuf<char_type, traits_type>* __sb)
829{
830 ios_base::set_rdbuf(__sb);
831}
832
Louis Dionnee29136f2020-07-13 11:53:48 -0400833inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000834ios_base&
835boolalpha(ios_base& __str)
836{
837 __str.setf(ios_base::boolalpha);
838 return __str;
839}
840
Louis Dionnee29136f2020-07-13 11:53:48 -0400841inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000842ios_base&
843noboolalpha(ios_base& __str)
844{
845 __str.unsetf(ios_base::boolalpha);
846 return __str;
847}
848
Louis Dionnee29136f2020-07-13 11:53:48 -0400849inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000850ios_base&
851showbase(ios_base& __str)
852{
853 __str.setf(ios_base::showbase);
854 return __str;
855}
856
Louis Dionnee29136f2020-07-13 11:53:48 -0400857inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000858ios_base&
859noshowbase(ios_base& __str)
860{
861 __str.unsetf(ios_base::showbase);
862 return __str;
863}
864
Louis Dionnee29136f2020-07-13 11:53:48 -0400865inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000866ios_base&
867showpoint(ios_base& __str)
868{
869 __str.setf(ios_base::showpoint);
870 return __str;
871}
872
Louis Dionnee29136f2020-07-13 11:53:48 -0400873inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000874ios_base&
875noshowpoint(ios_base& __str)
876{
877 __str.unsetf(ios_base::showpoint);
878 return __str;
879}
880
Louis Dionnee29136f2020-07-13 11:53:48 -0400881inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000882ios_base&
883showpos(ios_base& __str)
884{
885 __str.setf(ios_base::showpos);
886 return __str;
887}
888
Louis Dionnee29136f2020-07-13 11:53:48 -0400889inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000890ios_base&
891noshowpos(ios_base& __str)
892{
893 __str.unsetf(ios_base::showpos);
894 return __str;
895}
896
Louis Dionnee29136f2020-07-13 11:53:48 -0400897inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000898ios_base&
899skipws(ios_base& __str)
900{
901 __str.setf(ios_base::skipws);
902 return __str;
903}
904
Louis Dionnee29136f2020-07-13 11:53:48 -0400905inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000906ios_base&
907noskipws(ios_base& __str)
908{
909 __str.unsetf(ios_base::skipws);
910 return __str;
911}
912
Louis Dionnee29136f2020-07-13 11:53:48 -0400913inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000914ios_base&
915uppercase(ios_base& __str)
916{
917 __str.setf(ios_base::uppercase);
918 return __str;
919}
920
Louis Dionnee29136f2020-07-13 11:53:48 -0400921inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000922ios_base&
923nouppercase(ios_base& __str)
924{
925 __str.unsetf(ios_base::uppercase);
926 return __str;
927}
928
Louis Dionnee29136f2020-07-13 11:53:48 -0400929inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000930ios_base&
931unitbuf(ios_base& __str)
932{
933 __str.setf(ios_base::unitbuf);
934 return __str;
935}
936
Louis Dionnee29136f2020-07-13 11:53:48 -0400937inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000938ios_base&
939nounitbuf(ios_base& __str)
940{
941 __str.unsetf(ios_base::unitbuf);
942 return __str;
943}
944
Louis Dionnee29136f2020-07-13 11:53:48 -0400945inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000946ios_base&
947internal(ios_base& __str)
948{
949 __str.setf(ios_base::internal, ios_base::adjustfield);
950 return __str;
951}
952
Louis Dionnee29136f2020-07-13 11:53:48 -0400953inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000954ios_base&
955left(ios_base& __str)
956{
957 __str.setf(ios_base::left, ios_base::adjustfield);
958 return __str;
959}
960
Louis Dionnee29136f2020-07-13 11:53:48 -0400961inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962ios_base&
963right(ios_base& __str)
964{
965 __str.setf(ios_base::right, ios_base::adjustfield);
966 return __str;
967}
968
Louis Dionnee29136f2020-07-13 11:53:48 -0400969inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000970ios_base&
971dec(ios_base& __str)
972{
973 __str.setf(ios_base::dec, ios_base::basefield);
974 return __str;
975}
976
Louis Dionnee29136f2020-07-13 11:53:48 -0400977inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000978ios_base&
979hex(ios_base& __str)
980{
981 __str.setf(ios_base::hex, ios_base::basefield);
982 return __str;
983}
984
Louis Dionnee29136f2020-07-13 11:53:48 -0400985inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000986ios_base&
987oct(ios_base& __str)
988{
989 __str.setf(ios_base::oct, ios_base::basefield);
990 return __str;
991}
992
Louis Dionnee29136f2020-07-13 11:53:48 -0400993inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000994ios_base&
995fixed(ios_base& __str)
996{
997 __str.setf(ios_base::fixed, ios_base::floatfield);
998 return __str;
999}
1000
Louis Dionnee29136f2020-07-13 11:53:48 -04001001inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001002ios_base&
1003scientific(ios_base& __str)
1004{
1005 __str.setf(ios_base::scientific, ios_base::floatfield);
1006 return __str;
1007}
1008
Louis Dionnee29136f2020-07-13 11:53:48 -04001009inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010ios_base&
1011hexfloat(ios_base& __str)
1012{
1013 __str.setf(ios_base::fixed | ios_base::scientific, ios_base::floatfield);
1014 return __str;
1015}
1016
Louis Dionnee29136f2020-07-13 11:53:48 -04001017inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001018ios_base&
1019defaultfloat(ios_base& __str)
1020{
1021 __str.unsetf(ios_base::floatfield);
1022 return __str;
1023}
1024
Howard Hinnantc51e1022010-05-11 19:42:16 +00001025_LIBCPP_END_NAMESPACE_STD
1026
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001027#endif // _LIBCPP_IOS