blob: eefb58f55be13dee3bc353768b2a3ca41382f1f4 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- ios -------------------------------------===//
3//
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>
217
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000218#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Marshall Clowf9698ab2013-10-12 19:13:52 +0000219#include <atomic> // for __xindex_
220#endif
221
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000222#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000223#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000224#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000225
226_LIBCPP_BEGIN_NAMESPACE_STD
227
228typedef ptrdiff_t streamsize;
229
Howard Hinnant8331b762013-03-06 23:30:19 +0000230class _LIBCPP_TYPE_VIS ios_base
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231{
232public:
Eric Fiselierfb7fc952016-04-21 23:00:33 +0000233 class _LIBCPP_EXCEPTION_ABI failure;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000234
235 typedef unsigned int fmtflags;
236 static const fmtflags boolalpha = 0x0001;
237 static const fmtflags dec = 0x0002;
238 static const fmtflags fixed = 0x0004;
239 static const fmtflags hex = 0x0008;
240 static const fmtflags internal = 0x0010;
241 static const fmtflags left = 0x0020;
242 static const fmtflags oct = 0x0040;
243 static const fmtflags right = 0x0080;
244 static const fmtflags scientific = 0x0100;
245 static const fmtflags showbase = 0x0200;
246 static const fmtflags showpoint = 0x0400;
247 static const fmtflags showpos = 0x0800;
248 static const fmtflags skipws = 0x1000;
249 static const fmtflags unitbuf = 0x2000;
250 static const fmtflags uppercase = 0x4000;
251 static const fmtflags adjustfield = left | right | internal;
252 static const fmtflags basefield = dec | oct | hex;
253 static const fmtflags floatfield = scientific | fixed;
254
255 typedef unsigned int iostate;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000256 static const iostate badbit = 0x1;
257 static const iostate eofbit = 0x2;
258 static const iostate failbit = 0x4;
259 static const iostate goodbit = 0x0;
260
261 typedef unsigned int openmode;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000262 static const openmode app = 0x01;
263 static const openmode ate = 0x02;
264 static const openmode binary = 0x04;
265 static const openmode in = 0x08;
266 static const openmode out = 0x10;
267 static const openmode trunc = 0x20;
268
269 enum seekdir {beg, cur, end};
Marshall Clow57dbb062015-10-29 05:43:30 +0000270
271#if _LIBCPP_STD_VER <= 14
272 typedef iostate io_state;
273 typedef openmode open_mode;
274 typedef seekdir seek_dir;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000275
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000276 typedef _VSTD::streamoff streamoff;
277 typedef _VSTD::streampos streampos;
Marshall Clow57dbb062015-10-29 05:43:30 +0000278#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000279
Howard Hinnant8331b762013-03-06 23:30:19 +0000280 class _LIBCPP_TYPE_VIS Init;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000281
282 // 27.5.2.2 fmtflags state:
Howard Hinnantcf823322010-12-17 14:46:43 +0000283 _LIBCPP_INLINE_VISIBILITY fmtflags flags() const;
284 _LIBCPP_INLINE_VISIBILITY fmtflags flags(fmtflags __fmtfl);
285 _LIBCPP_INLINE_VISIBILITY fmtflags setf(fmtflags __fmtfl);
286 _LIBCPP_INLINE_VISIBILITY fmtflags setf(fmtflags __fmtfl, fmtflags __mask);
287 _LIBCPP_INLINE_VISIBILITY void unsetf(fmtflags __mask);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000288
Howard Hinnantcf823322010-12-17 14:46:43 +0000289 _LIBCPP_INLINE_VISIBILITY streamsize precision() const;
290 _LIBCPP_INLINE_VISIBILITY streamsize precision(streamsize __prec);
291 _LIBCPP_INLINE_VISIBILITY streamsize width() const;
292 _LIBCPP_INLINE_VISIBILITY streamsize width(streamsize __wide);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000293
294 // 27.5.2.3 locales:
295 locale imbue(const locale& __loc);
296 locale getloc() const;
297
298 // 27.5.2.5 storage:
299 static int xalloc();
300 long& iword(int __index);
301 void*& pword(int __index);
302
303 // destructor
304 virtual ~ios_base();
305
306 // 27.5.2.6 callbacks;
307 enum event { erase_event, imbue_event, copyfmt_event };
308 typedef void (*event_callback)(event, ios_base&, int __index);
309 void register_callback(event_callback __fn, int __index);
310
311private:
312 ios_base(const ios_base&); // = delete;
313 ios_base& operator=(const ios_base&); // = delete;
314
315public:
316 static bool sync_with_stdio(bool __sync = true);
317
Howard Hinnantcf823322010-12-17 14:46:43 +0000318 _LIBCPP_INLINE_VISIBILITY iostate rdstate() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000319 void clear(iostate __state = goodbit);
Howard Hinnantcf823322010-12-17 14:46:43 +0000320 _LIBCPP_INLINE_VISIBILITY void setstate(iostate __state);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000321
Howard Hinnantcf823322010-12-17 14:46:43 +0000322 _LIBCPP_INLINE_VISIBILITY bool good() const;
323 _LIBCPP_INLINE_VISIBILITY bool eof() const;
324 _LIBCPP_INLINE_VISIBILITY bool fail() const;
325 _LIBCPP_INLINE_VISIBILITY bool bad() const;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000326
Howard Hinnantcf823322010-12-17 14:46:43 +0000327 _LIBCPP_INLINE_VISIBILITY iostate exceptions() const;
Howard Hinnantf8850802013-10-06 21:00:29 +0000328 _LIBCPP_INLINE_VISIBILITY void exceptions(iostate __iostate);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000329
330 void __set_badbit_and_consider_rethrow();
331 void __set_failbit_and_consider_rethrow();
332
Louis Dionne94ee3a02019-04-05 16:33:37 +0000333 _LIBCPP_INLINE_VISIBILITY
334 void __setstate_nothrow(iostate __state)
335 {
336 if (__rdbuf_)
337 __rdstate_ |= __state;
338 else
339 __rdstate_ |= __state | ios_base::badbit;
340 }
341
Howard Hinnantc51e1022010-05-11 19:42:16 +0000342protected:
Howard Hinnant64da2602010-09-22 15:29:08 +0000343 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000344 ios_base() {// purposefully does no initialization
345 }
346
347 void init(void* __sb);
Louis Dionne16fe2952018-07-11 23:14:33 +0000348 _LIBCPP_INLINE_VISIBILITY void* rdbuf() const {return __rdbuf_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000349
Louis Dionne16fe2952018-07-11 23:14:33 +0000350 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000351 void rdbuf(void* __sb)
352 {
353 __rdbuf_ = __sb;
354 clear();
355 }
356
357 void __call_callbacks(event);
358 void copyfmt(const ios_base&);
359 void move(ios_base&);
Howard Hinnant770a3662012-07-21 01:03:40 +0000360 void swap(ios_base&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000361
Louis Dionne16fe2952018-07-11 23:14:33 +0000362 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000363 void set_rdbuf(void* __sb)
364 {
365 __rdbuf_ = __sb;
366 }
367
368private:
369 // All data members must be scalars
370 fmtflags __fmtflags_;
371 streamsize __precision_;
372 streamsize __width_;
373 iostate __rdstate_;
374 iostate __exceptions_;
375 void* __rdbuf_;
376 void* __loc_;
377 event_callback* __fn_;
378 int* __index_;
379 size_t __event_size_;
380 size_t __event_cap_;
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000381// TODO(EricWF): Enable this for both Clang and GCC. Currently it is only
382// enabled with clang.
383#if defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
Marshall Clowf9698ab2013-10-12 19:13:52 +0000384 static atomic<int> __xindex_;
385#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000386 static int __xindex_;
Marshall Clowf9698ab2013-10-12 19:13:52 +0000387#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000388 long* __iarray_;
389 size_t __iarray_size_;
390 size_t __iarray_cap_;
391 void** __parray_;
392 size_t __parray_size_;
393 size_t __parray_cap_;
394};
395
396//enum class io_errc
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000397_LIBCPP_DECLARE_STRONG_ENUM(io_errc)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000398{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399 stream = 1
400};
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000401_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(io_errc)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000402
Howard Hinnant64da2602010-09-22 15:29:08 +0000403template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000404struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<io_errc> : public true_type { };
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000405
406#ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
Howard Hinnant64da2602010-09-22 15:29:08 +0000407template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000408struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<io_errc::__lx> : public true_type { };
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000409#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000410
Howard Hinnant8331b762013-03-06 23:30:19 +0000411_LIBCPP_FUNC_VIS
Marshall Clow3f138dc2013-10-12 22:49:56 +0000412const error_category& iostream_category() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413
414inline _LIBCPP_INLINE_VISIBILITY
415error_code
Marshall Clow3f138dc2013-10-12 22:49:56 +0000416make_error_code(io_errc __e) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000417{
418 return error_code(static_cast<int>(__e), iostream_category());
419}
420
421inline _LIBCPP_INLINE_VISIBILITY
422error_condition
Marshall Clow3f138dc2013-10-12 22:49:56 +0000423make_error_condition(io_errc __e) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000424{
425 return error_condition(static_cast<int>(__e), iostream_category());
426}
427
Howard Hinnant64da2602010-09-22 15:29:08 +0000428class _LIBCPP_EXCEPTION_ABI ios_base::failure
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429 : public system_error
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000430{
431public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000432 explicit failure(const string& __msg, const error_code& __ec = io_errc::stream);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000433 explicit failure(const char* __msg, const error_code& __ec = io_errc::stream);
Dimitry Andric47269ce2020-03-13 19:36:26 +0100434 failure(const failure&) _NOEXCEPT = default;
435 virtual ~failure() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000436};
437
Louis Dionne2b239162019-02-12 16:06:02 +0000438_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
439void __throw_failure(char const* __msg) {
440#ifndef _LIBCPP_NO_EXCEPTIONS
441 throw ios_base::failure(__msg);
442#else
443 ((void)__msg);
444 _VSTD::abort();
445#endif
446}
447
Howard Hinnant8331b762013-03-06 23:30:19 +0000448class _LIBCPP_TYPE_VIS ios_base::Init
Howard Hinnantc51e1022010-05-11 19:42:16 +0000449{
450public:
451 Init();
452 ~Init();
453};
454
455// fmtflags
456
457inline _LIBCPP_INLINE_VISIBILITY
458ios_base::fmtflags
459ios_base::flags() const
460{
461 return __fmtflags_;
462}
463
464inline _LIBCPP_INLINE_VISIBILITY
465ios_base::fmtflags
466ios_base::flags(fmtflags __fmtfl)
467{
468 fmtflags __r = __fmtflags_;
469 __fmtflags_ = __fmtfl;
470 return __r;
471}
472
473inline _LIBCPP_INLINE_VISIBILITY
474ios_base::fmtflags
475ios_base::setf(fmtflags __fmtfl)
476{
477 fmtflags __r = __fmtflags_;
478 __fmtflags_ |= __fmtfl;
479 return __r;
480}
481
482inline _LIBCPP_INLINE_VISIBILITY
483void
484ios_base::unsetf(fmtflags __mask)
485{
486 __fmtflags_ &= ~__mask;
487}
488
489inline _LIBCPP_INLINE_VISIBILITY
490ios_base::fmtflags
491ios_base::setf(fmtflags __fmtfl, fmtflags __mask)
492{
493 fmtflags __r = __fmtflags_;
494 unsetf(__mask);
495 __fmtflags_ |= __fmtfl & __mask;
496 return __r;
497}
498
499// precision
500
501inline _LIBCPP_INLINE_VISIBILITY
502streamsize
503ios_base::precision() const
504{
505 return __precision_;
506}
507
508inline _LIBCPP_INLINE_VISIBILITY
509streamsize
510ios_base::precision(streamsize __prec)
511{
512 streamsize __r = __precision_;
513 __precision_ = __prec;
514 return __r;
515}
516
517// width
518
519inline _LIBCPP_INLINE_VISIBILITY
520streamsize
521ios_base::width() const
522{
523 return __width_;
524}
525
526inline _LIBCPP_INLINE_VISIBILITY
527streamsize
528ios_base::width(streamsize __wide)
529{
530 streamsize __r = __width_;
531 __width_ = __wide;
532 return __r;
533}
534
535// iostate
536
537inline _LIBCPP_INLINE_VISIBILITY
538ios_base::iostate
539ios_base::rdstate() const
540{
541 return __rdstate_;
542}
543
544inline _LIBCPP_INLINE_VISIBILITY
545void
546ios_base::setstate(iostate __state)
547{
548 clear(__rdstate_ | __state);
549}
550
551inline _LIBCPP_INLINE_VISIBILITY
552bool
553ios_base::good() const
554{
555 return __rdstate_ == 0;
556}
557
558inline _LIBCPP_INLINE_VISIBILITY
559bool
560ios_base::eof() const
561{
Marshall Clow11de4872013-10-21 14:41:05 +0000562 return (__rdstate_ & eofbit) != 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000563}
564
565inline _LIBCPP_INLINE_VISIBILITY
566bool
567ios_base::fail() const
568{
Marshall Clow11de4872013-10-21 14:41:05 +0000569 return (__rdstate_ & (failbit | badbit)) != 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000570}
571
572inline _LIBCPP_INLINE_VISIBILITY
573bool
574ios_base::bad() const
575{
Marshall Clow11de4872013-10-21 14:41:05 +0000576 return (__rdstate_ & badbit) != 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000577}
578
579inline _LIBCPP_INLINE_VISIBILITY
580ios_base::iostate
581ios_base::exceptions() const
582{
583 return __exceptions_;
584}
585
586inline _LIBCPP_INLINE_VISIBILITY
587void
Howard Hinnantf8850802013-10-06 21:00:29 +0000588ios_base::exceptions(iostate __iostate)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000589{
Howard Hinnantf8850802013-10-06 21:00:29 +0000590 __exceptions_ = __iostate;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000591 clear(__rdstate_);
592}
593
594template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000595class _LIBCPP_TEMPLATE_VIS basic_ios
Howard Hinnantc51e1022010-05-11 19:42:16 +0000596 : public ios_base
597{
598public:
599 // types:
600 typedef _CharT char_type;
601 typedef _Traits traits_type;
602
603 typedef typename traits_type::int_type int_type;
604 typedef typename traits_type::pos_type pos_type;
605 typedef typename traits_type::off_type off_type;
606
Marshall Clowd151d182018-02-01 03:55:27 +0000607 static_assert((is_same<_CharT, typename traits_type::char_type>::value),
608 "traits_type::char_type must be the same type as CharT");
609
Louis Dionne16fe2952018-07-11 23:14:33 +0000610 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer6c9c9a72021-06-15 12:57:54 -0400611 explicit operator bool() const {return !fail();}
Eric Fiselier6a5f6f92016-12-30 14:05:52 +0000612
Louis Dionne16fe2952018-07-11 23:14:33 +0000613 _LIBCPP_INLINE_VISIBILITY bool operator!() const {return fail();}
614 _LIBCPP_INLINE_VISIBILITY iostate rdstate() const {return ios_base::rdstate();}
615 _LIBCPP_INLINE_VISIBILITY void clear(iostate __state = goodbit) {ios_base::clear(__state);}
616 _LIBCPP_INLINE_VISIBILITY void setstate(iostate __state) {ios_base::setstate(__state);}
617 _LIBCPP_INLINE_VISIBILITY bool good() const {return ios_base::good();}
618 _LIBCPP_INLINE_VISIBILITY bool eof() const {return ios_base::eof();}
619 _LIBCPP_INLINE_VISIBILITY bool fail() const {return ios_base::fail();}
620 _LIBCPP_INLINE_VISIBILITY bool bad() const {return ios_base::bad();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000621
Louis Dionne16fe2952018-07-11 23:14:33 +0000622 _LIBCPP_INLINE_VISIBILITY iostate exceptions() const {return ios_base::exceptions();}
623 _LIBCPP_INLINE_VISIBILITY void exceptions(iostate __iostate) {ios_base::exceptions(__iostate);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000624
625 // 27.5.4.1 Constructor/destructor:
Howard Hinnantcf823322010-12-17 14:46:43 +0000626 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000627 explicit basic_ios(basic_streambuf<char_type,traits_type>* __sb);
628 virtual ~basic_ios();
629
630 // 27.5.4.2 Members:
Louis Dionne173f29e2019-05-29 16:01:36 +0000631 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000632 basic_ostream<char_type, traits_type>* tie() const;
Louis Dionne173f29e2019-05-29 16:01:36 +0000633 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000634 basic_ostream<char_type, traits_type>* tie(basic_ostream<char_type, traits_type>* __tiestr);
635
Louis Dionne173f29e2019-05-29 16:01:36 +0000636 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000637 basic_streambuf<char_type, traits_type>* rdbuf() const;
Louis Dionne173f29e2019-05-29 16:01:36 +0000638 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000639 basic_streambuf<char_type, traits_type>* rdbuf(basic_streambuf<char_type, traits_type>* __sb);
640
641 basic_ios& copyfmt(const basic_ios& __rhs);
642
Louis Dionne173f29e2019-05-29 16:01:36 +0000643 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000644 char_type fill() const;
Louis Dionne173f29e2019-05-29 16:01:36 +0000645 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000646 char_type fill(char_type __ch);
647
Louis Dionne173f29e2019-05-29 16:01:36 +0000648 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000649 locale imbue(const locale& __loc);
650
Louis Dionne173f29e2019-05-29 16:01:36 +0000651 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000652 char narrow(char_type __c, char __dfault) const;
Louis Dionne173f29e2019-05-29 16:01:36 +0000653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000654 char_type widen(char __c) const;
655
656protected:
Louis Dionne16fe2952018-07-11 23:14:33 +0000657 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000658 basic_ios() {// purposefully does no initialization
659 }
Louis Dionne173f29e2019-05-29 16:01:36 +0000660 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000661 void init(basic_streambuf<char_type, traits_type>* __sb);
662
Louis Dionne173f29e2019-05-29 16:01:36 +0000663 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000664 void move(basic_ios& __rhs);
Eric Fiselierf07d88c2017-04-19 01:34:08 +0000665#ifndef _LIBCPP_CXX03_LANG
Louis Dionne16fe2952018-07-11 23:14:33 +0000666 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000667 void move(basic_ios&& __rhs) {move(__rhs);}
668#endif
Louis Dionne173f29e2019-05-29 16:01:36 +0000669 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant770a3662012-07-21 01:03:40 +0000670 void swap(basic_ios& __rhs) _NOEXCEPT;
Louis Dionne173f29e2019-05-29 16:01:36 +0000671 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000672 void set_rdbuf(basic_streambuf<char_type, traits_type>* __sb);
673private:
674 basic_ostream<char_type, traits_type>* __tie_;
Bruce Mitchenerca134c52018-02-14 00:29:38 +0000675 mutable int_type __fill_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000676};
677
678template <class _CharT, class _Traits>
679inline _LIBCPP_INLINE_VISIBILITY
680basic_ios<_CharT, _Traits>::basic_ios(basic_streambuf<char_type,traits_type>* __sb)
681{
682 init(__sb);
683}
684
685template <class _CharT, class _Traits>
686basic_ios<_CharT, _Traits>::~basic_ios()
687{
688}
689
690template <class _CharT, class _Traits>
691inline _LIBCPP_INLINE_VISIBILITY
692void
693basic_ios<_CharT, _Traits>::init(basic_streambuf<char_type, traits_type>* __sb)
694{
695 ios_base::init(__sb);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500696 __tie_ = nullptr;
Howard Hinnant2efe10d2012-08-26 18:05:35 +0000697 __fill_ = traits_type::eof();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000698}
699
700template <class _CharT, class _Traits>
701inline _LIBCPP_INLINE_VISIBILITY
702basic_ostream<_CharT, _Traits>*
703basic_ios<_CharT, _Traits>::tie() const
704{
705 return __tie_;
706}
707
708template <class _CharT, class _Traits>
709inline _LIBCPP_INLINE_VISIBILITY
710basic_ostream<_CharT, _Traits>*
711basic_ios<_CharT, _Traits>::tie(basic_ostream<char_type, traits_type>* __tiestr)
712{
713 basic_ostream<char_type, traits_type>* __r = __tie_;
714 __tie_ = __tiestr;
715 return __r;
716}
717
718template <class _CharT, class _Traits>
719inline _LIBCPP_INLINE_VISIBILITY
720basic_streambuf<_CharT, _Traits>*
721basic_ios<_CharT, _Traits>::rdbuf() const
722{
723 return static_cast<basic_streambuf<char_type, traits_type>*>(ios_base::rdbuf());
724}
725
726template <class _CharT, class _Traits>
727inline _LIBCPP_INLINE_VISIBILITY
728basic_streambuf<_CharT, _Traits>*
729basic_ios<_CharT, _Traits>::rdbuf(basic_streambuf<char_type, traits_type>* __sb)
730{
731 basic_streambuf<char_type, traits_type>* __r = rdbuf();
732 ios_base::rdbuf(__sb);
733 return __r;
734}
735
736template <class _CharT, class _Traits>
737inline _LIBCPP_INLINE_VISIBILITY
738locale
739basic_ios<_CharT, _Traits>::imbue(const locale& __loc)
740{
741 locale __r = getloc();
742 ios_base::imbue(__loc);
743 if (rdbuf())
744 rdbuf()->pubimbue(__loc);
745 return __r;
746}
747
748template <class _CharT, class _Traits>
749inline _LIBCPP_INLINE_VISIBILITY
750char
751basic_ios<_CharT, _Traits>::narrow(char_type __c, char __dfault) const
752{
753 return use_facet<ctype<char_type> >(getloc()).narrow(__c, __dfault);
754}
755
756template <class _CharT, class _Traits>
757inline _LIBCPP_INLINE_VISIBILITY
758_CharT
759basic_ios<_CharT, _Traits>::widen(char __c) const
760{
761 return use_facet<ctype<char_type> >(getloc()).widen(__c);
762}
763
764template <class _CharT, class _Traits>
765inline _LIBCPP_INLINE_VISIBILITY
766_CharT
767basic_ios<_CharT, _Traits>::fill() const
768{
Howard Hinnant2efe10d2012-08-26 18:05:35 +0000769 if (traits_type::eq_int_type(traits_type::eof(), __fill_))
770 __fill_ = widen(' ');
Howard Hinnantc51e1022010-05-11 19:42:16 +0000771 return __fill_;
772}
773
774template <class _CharT, class _Traits>
775inline _LIBCPP_INLINE_VISIBILITY
776_CharT
777basic_ios<_CharT, _Traits>::fill(char_type __ch)
778{
779 char_type __r = __fill_;
780 __fill_ = __ch;
781 return __r;
782}
783
784template <class _CharT, class _Traits>
785basic_ios<_CharT, _Traits>&
786basic_ios<_CharT, _Traits>::copyfmt(const basic_ios& __rhs)
787{
788 if (this != &__rhs)
789 {
790 __call_callbacks(erase_event);
791 ios_base::copyfmt(__rhs);
792 __tie_ = __rhs.__tie_;
793 __fill_ = __rhs.__fill_;
794 __call_callbacks(copyfmt_event);
795 exceptions(__rhs.exceptions());
796 }
797 return *this;
798}
799
800template <class _CharT, class _Traits>
801inline _LIBCPP_INLINE_VISIBILITY
802void
803basic_ios<_CharT, _Traits>::move(basic_ios& __rhs)
804{
805 ios_base::move(__rhs);
806 __tie_ = __rhs.__tie_;
Bruce Mitchener170d8972020-11-24 12:53:53 -0500807 __rhs.__tie_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000808 __fill_ = __rhs.__fill_;
809}
810
811template <class _CharT, class _Traits>
812inline _LIBCPP_INLINE_VISIBILITY
813void
Howard Hinnant770a3662012-07-21 01:03:40 +0000814basic_ios<_CharT, _Traits>::swap(basic_ios& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000815{
816 ios_base::swap(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000817 _VSTD::swap(__tie_, __rhs.__tie_);
818 _VSTD::swap(__fill_, __rhs.__fill_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000819}
820
821template <class _CharT, class _Traits>
822inline _LIBCPP_INLINE_VISIBILITY
823void
824basic_ios<_CharT, _Traits>::set_rdbuf(basic_streambuf<char_type, traits_type>* __sb)
825{
826 ios_base::set_rdbuf(__sb);
827}
828
Louis Dionnee29136f2020-07-13 11:53:48 -0400829inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000830ios_base&
831boolalpha(ios_base& __str)
832{
833 __str.setf(ios_base::boolalpha);
834 return __str;
835}
836
Louis Dionnee29136f2020-07-13 11:53:48 -0400837inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000838ios_base&
839noboolalpha(ios_base& __str)
840{
841 __str.unsetf(ios_base::boolalpha);
842 return __str;
843}
844
Louis Dionnee29136f2020-07-13 11:53:48 -0400845inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000846ios_base&
847showbase(ios_base& __str)
848{
849 __str.setf(ios_base::showbase);
850 return __str;
851}
852
Louis Dionnee29136f2020-07-13 11:53:48 -0400853inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000854ios_base&
855noshowbase(ios_base& __str)
856{
857 __str.unsetf(ios_base::showbase);
858 return __str;
859}
860
Louis Dionnee29136f2020-07-13 11:53:48 -0400861inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000862ios_base&
863showpoint(ios_base& __str)
864{
865 __str.setf(ios_base::showpoint);
866 return __str;
867}
868
Louis Dionnee29136f2020-07-13 11:53:48 -0400869inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000870ios_base&
871noshowpoint(ios_base& __str)
872{
873 __str.unsetf(ios_base::showpoint);
874 return __str;
875}
876
Louis Dionnee29136f2020-07-13 11:53:48 -0400877inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000878ios_base&
879showpos(ios_base& __str)
880{
881 __str.setf(ios_base::showpos);
882 return __str;
883}
884
Louis Dionnee29136f2020-07-13 11:53:48 -0400885inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000886ios_base&
887noshowpos(ios_base& __str)
888{
889 __str.unsetf(ios_base::showpos);
890 return __str;
891}
892
Louis Dionnee29136f2020-07-13 11:53:48 -0400893inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000894ios_base&
895skipws(ios_base& __str)
896{
897 __str.setf(ios_base::skipws);
898 return __str;
899}
900
Louis Dionnee29136f2020-07-13 11:53:48 -0400901inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000902ios_base&
903noskipws(ios_base& __str)
904{
905 __str.unsetf(ios_base::skipws);
906 return __str;
907}
908
Louis Dionnee29136f2020-07-13 11:53:48 -0400909inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000910ios_base&
911uppercase(ios_base& __str)
912{
913 __str.setf(ios_base::uppercase);
914 return __str;
915}
916
Louis Dionnee29136f2020-07-13 11:53:48 -0400917inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000918ios_base&
919nouppercase(ios_base& __str)
920{
921 __str.unsetf(ios_base::uppercase);
922 return __str;
923}
924
Louis Dionnee29136f2020-07-13 11:53:48 -0400925inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000926ios_base&
927unitbuf(ios_base& __str)
928{
929 __str.setf(ios_base::unitbuf);
930 return __str;
931}
932
Louis Dionnee29136f2020-07-13 11:53:48 -0400933inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000934ios_base&
935nounitbuf(ios_base& __str)
936{
937 __str.unsetf(ios_base::unitbuf);
938 return __str;
939}
940
Louis Dionnee29136f2020-07-13 11:53:48 -0400941inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000942ios_base&
943internal(ios_base& __str)
944{
945 __str.setf(ios_base::internal, ios_base::adjustfield);
946 return __str;
947}
948
Louis Dionnee29136f2020-07-13 11:53:48 -0400949inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000950ios_base&
951left(ios_base& __str)
952{
953 __str.setf(ios_base::left, ios_base::adjustfield);
954 return __str;
955}
956
Louis Dionnee29136f2020-07-13 11:53:48 -0400957inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000958ios_base&
959right(ios_base& __str)
960{
961 __str.setf(ios_base::right, ios_base::adjustfield);
962 return __str;
963}
964
Louis Dionnee29136f2020-07-13 11:53:48 -0400965inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000966ios_base&
967dec(ios_base& __str)
968{
969 __str.setf(ios_base::dec, ios_base::basefield);
970 return __str;
971}
972
Louis Dionnee29136f2020-07-13 11:53:48 -0400973inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000974ios_base&
975hex(ios_base& __str)
976{
977 __str.setf(ios_base::hex, ios_base::basefield);
978 return __str;
979}
980
Louis Dionnee29136f2020-07-13 11:53:48 -0400981inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000982ios_base&
983oct(ios_base& __str)
984{
985 __str.setf(ios_base::oct, ios_base::basefield);
986 return __str;
987}
988
Louis Dionnee29136f2020-07-13 11:53:48 -0400989inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000990ios_base&
991fixed(ios_base& __str)
992{
993 __str.setf(ios_base::fixed, ios_base::floatfield);
994 return __str;
995}
996
Louis Dionnee29136f2020-07-13 11:53:48 -0400997inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000998ios_base&
999scientific(ios_base& __str)
1000{
1001 __str.setf(ios_base::scientific, ios_base::floatfield);
1002 return __str;
1003}
1004
Louis Dionnee29136f2020-07-13 11:53:48 -04001005inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006ios_base&
1007hexfloat(ios_base& __str)
1008{
1009 __str.setf(ios_base::fixed | ios_base::scientific, ios_base::floatfield);
1010 return __str;
1011}
1012
Louis Dionnee29136f2020-07-13 11:53:48 -04001013inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001014ios_base&
1015defaultfloat(ios_base& __str)
1016{
1017 __str.unsetf(ios_base::floatfield);
1018 return __str;
1019}
1020
Howard Hinnantc51e1022010-05-11 19:42:16 +00001021_LIBCPP_END_NAMESPACE_STD
1022
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001023#endif // _LIBCPP_IOS