blob: 9633639630205fa65ac56434952ea2656cc93fc6 [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> { };
Marshall Clow3f138dc2013-10-12 22:49:56 +0000205error_code make_error_code(io_errc e) noexcept;
206error_condition make_error_condition(io_errc e) noexcept;
207storage-class-specifier const error_category& iostream_category() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000208
209} // std
210
211*/
212
213#include <__config>
214#include <iosfwd>
215#include <__locale>
216#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
333protected:
Howard Hinnant64da2602010-09-22 15:29:08 +0000334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000335 ios_base() {// purposefully does no initialization
336 }
337
338 void init(void* __sb);
Louis Dionne16fe2952018-07-11 23:14:33 +0000339 _LIBCPP_INLINE_VISIBILITY void* rdbuf() const {return __rdbuf_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000340
Louis Dionne16fe2952018-07-11 23:14:33 +0000341 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000342 void rdbuf(void* __sb)
343 {
344 __rdbuf_ = __sb;
345 clear();
346 }
347
348 void __call_callbacks(event);
349 void copyfmt(const ios_base&);
350 void move(ios_base&);
Howard Hinnant770a3662012-07-21 01:03:40 +0000351 void swap(ios_base&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000352
Louis Dionne16fe2952018-07-11 23:14:33 +0000353 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000354 void set_rdbuf(void* __sb)
355 {
356 __rdbuf_ = __sb;
357 }
358
359private:
360 // All data members must be scalars
361 fmtflags __fmtflags_;
362 streamsize __precision_;
363 streamsize __width_;
364 iostate __rdstate_;
365 iostate __exceptions_;
366 void* __rdbuf_;
367 void* __loc_;
368 event_callback* __fn_;
369 int* __index_;
370 size_t __event_size_;
371 size_t __event_cap_;
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000372// TODO(EricWF): Enable this for both Clang and GCC. Currently it is only
373// enabled with clang.
374#if defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
Marshall Clowf9698ab2013-10-12 19:13:52 +0000375 static atomic<int> __xindex_;
376#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000377 static int __xindex_;
Marshall Clowf9698ab2013-10-12 19:13:52 +0000378#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379 long* __iarray_;
380 size_t __iarray_size_;
381 size_t __iarray_cap_;
382 void** __parray_;
383 size_t __parray_size_;
384 size_t __parray_cap_;
385};
386
387//enum class io_errc
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000388_LIBCPP_DECLARE_STRONG_ENUM(io_errc)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000389{
Howard Hinnantc51e1022010-05-11 19:42:16 +0000390 stream = 1
391};
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000392_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(io_errc)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393
Howard Hinnant64da2602010-09-22 15:29:08 +0000394template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000395struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<io_errc> : public true_type { };
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000396
397#ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
Howard Hinnant64da2602010-09-22 15:29:08 +0000398template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000399struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<io_errc::__lx> : public true_type { };
Howard Hinnant09bc0c92011-12-02 19:36:40 +0000400#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000401
Howard Hinnant8331b762013-03-06 23:30:19 +0000402_LIBCPP_FUNC_VIS
Marshall Clow3f138dc2013-10-12 22:49:56 +0000403const error_category& iostream_category() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000404
405inline _LIBCPP_INLINE_VISIBILITY
406error_code
Marshall Clow3f138dc2013-10-12 22:49:56 +0000407make_error_code(io_errc __e) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000408{
409 return error_code(static_cast<int>(__e), iostream_category());
410}
411
412inline _LIBCPP_INLINE_VISIBILITY
413error_condition
Marshall Clow3f138dc2013-10-12 22:49:56 +0000414make_error_condition(io_errc __e) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000415{
416 return error_condition(static_cast<int>(__e), iostream_category());
417}
418
Howard Hinnant64da2602010-09-22 15:29:08 +0000419class _LIBCPP_EXCEPTION_ABI ios_base::failure
Howard Hinnantc51e1022010-05-11 19:42:16 +0000420 : public system_error
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000421{
422public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423 explicit failure(const string& __msg, const error_code& __ec = io_errc::stream);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000424 explicit failure(const char* __msg, const error_code& __ec = io_errc::stream);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000425 virtual ~failure() throw();
426};
427
Howard Hinnant8331b762013-03-06 23:30:19 +0000428class _LIBCPP_TYPE_VIS ios_base::Init
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429{
430public:
431 Init();
432 ~Init();
433};
434
435// fmtflags
436
437inline _LIBCPP_INLINE_VISIBILITY
438ios_base::fmtflags
439ios_base::flags() const
440{
441 return __fmtflags_;
442}
443
444inline _LIBCPP_INLINE_VISIBILITY
445ios_base::fmtflags
446ios_base::flags(fmtflags __fmtfl)
447{
448 fmtflags __r = __fmtflags_;
449 __fmtflags_ = __fmtfl;
450 return __r;
451}
452
453inline _LIBCPP_INLINE_VISIBILITY
454ios_base::fmtflags
455ios_base::setf(fmtflags __fmtfl)
456{
457 fmtflags __r = __fmtflags_;
458 __fmtflags_ |= __fmtfl;
459 return __r;
460}
461
462inline _LIBCPP_INLINE_VISIBILITY
463void
464ios_base::unsetf(fmtflags __mask)
465{
466 __fmtflags_ &= ~__mask;
467}
468
469inline _LIBCPP_INLINE_VISIBILITY
470ios_base::fmtflags
471ios_base::setf(fmtflags __fmtfl, fmtflags __mask)
472{
473 fmtflags __r = __fmtflags_;
474 unsetf(__mask);
475 __fmtflags_ |= __fmtfl & __mask;
476 return __r;
477}
478
479// precision
480
481inline _LIBCPP_INLINE_VISIBILITY
482streamsize
483ios_base::precision() const
484{
485 return __precision_;
486}
487
488inline _LIBCPP_INLINE_VISIBILITY
489streamsize
490ios_base::precision(streamsize __prec)
491{
492 streamsize __r = __precision_;
493 __precision_ = __prec;
494 return __r;
495}
496
497// width
498
499inline _LIBCPP_INLINE_VISIBILITY
500streamsize
501ios_base::width() const
502{
503 return __width_;
504}
505
506inline _LIBCPP_INLINE_VISIBILITY
507streamsize
508ios_base::width(streamsize __wide)
509{
510 streamsize __r = __width_;
511 __width_ = __wide;
512 return __r;
513}
514
515// iostate
516
517inline _LIBCPP_INLINE_VISIBILITY
518ios_base::iostate
519ios_base::rdstate() const
520{
521 return __rdstate_;
522}
523
524inline _LIBCPP_INLINE_VISIBILITY
525void
526ios_base::setstate(iostate __state)
527{
528 clear(__rdstate_ | __state);
529}
530
531inline _LIBCPP_INLINE_VISIBILITY
532bool
533ios_base::good() const
534{
535 return __rdstate_ == 0;
536}
537
538inline _LIBCPP_INLINE_VISIBILITY
539bool
540ios_base::eof() const
541{
Marshall Clow11de4872013-10-21 14:41:05 +0000542 return (__rdstate_ & eofbit) != 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000543}
544
545inline _LIBCPP_INLINE_VISIBILITY
546bool
547ios_base::fail() const
548{
Marshall Clow11de4872013-10-21 14:41:05 +0000549 return (__rdstate_ & (failbit | badbit)) != 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000550}
551
552inline _LIBCPP_INLINE_VISIBILITY
553bool
554ios_base::bad() const
555{
Marshall Clow11de4872013-10-21 14:41:05 +0000556 return (__rdstate_ & badbit) != 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000557}
558
559inline _LIBCPP_INLINE_VISIBILITY
560ios_base::iostate
561ios_base::exceptions() const
562{
563 return __exceptions_;
564}
565
566inline _LIBCPP_INLINE_VISIBILITY
567void
Howard Hinnantf8850802013-10-06 21:00:29 +0000568ios_base::exceptions(iostate __iostate)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000569{
Howard Hinnantf8850802013-10-06 21:00:29 +0000570 __exceptions_ = __iostate;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000571 clear(__rdstate_);
572}
573
Eric Fiselier70d0df32017-01-13 18:03:46 +0000574#if defined(_LIBCPP_CXX03_LANG)
Eric Fiselier31042ef2017-01-13 18:25:13 +0000575struct _LIBCPP_TYPE_VIS __cxx03_bool {
Eric Fiselier70d0df32017-01-13 18:03:46 +0000576 typedef void (__cxx03_bool::*__bool_type)();
577 void __true_value() {}
578};
579#endif
580
Howard Hinnantc51e1022010-05-11 19:42:16 +0000581template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000582class _LIBCPP_TEMPLATE_VIS basic_ios
Howard Hinnantc51e1022010-05-11 19:42:16 +0000583 : public ios_base
584{
585public:
586 // types:
587 typedef _CharT char_type;
588 typedef _Traits traits_type;
589
590 typedef typename traits_type::int_type int_type;
591 typedef typename traits_type::pos_type pos_type;
592 typedef typename traits_type::off_type off_type;
593
Marshall Clowd151d182018-02-01 03:55:27 +0000594 static_assert((is_same<_CharT, typename traits_type::char_type>::value),
595 "traits_type::char_type must be the same type as CharT");
596
Eric Fiselier70d0df32017-01-13 18:03:46 +0000597 // __true_value will generate undefined references when linking unless
598 // we give it internal linkage.
599
600#if defined(_LIBCPP_CXX03_LANG)
Louis Dionne16fe2952018-07-11 23:14:33 +0000601 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier70d0df32017-01-13 18:03:46 +0000602 operator __cxx03_bool::__bool_type() const {
603 return !fail() ? &__cxx03_bool::__true_value : nullptr;
604 }
605#else
Louis Dionne16fe2952018-07-11 23:14:33 +0000606 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6a5f6f92016-12-30 14:05:52 +0000607 _LIBCPP_EXPLICIT operator bool() const {return !fail();}
Eric Fiselier70d0df32017-01-13 18:03:46 +0000608#endif
Eric Fiselier6a5f6f92016-12-30 14:05:52 +0000609
Louis Dionne16fe2952018-07-11 23:14:33 +0000610 _LIBCPP_INLINE_VISIBILITY bool operator!() const {return fail();}
611 _LIBCPP_INLINE_VISIBILITY iostate rdstate() const {return ios_base::rdstate();}
612 _LIBCPP_INLINE_VISIBILITY void clear(iostate __state = goodbit) {ios_base::clear(__state);}
613 _LIBCPP_INLINE_VISIBILITY void setstate(iostate __state) {ios_base::setstate(__state);}
614 _LIBCPP_INLINE_VISIBILITY bool good() const {return ios_base::good();}
615 _LIBCPP_INLINE_VISIBILITY bool eof() const {return ios_base::eof();}
616 _LIBCPP_INLINE_VISIBILITY bool fail() const {return ios_base::fail();}
617 _LIBCPP_INLINE_VISIBILITY bool bad() const {return ios_base::bad();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000618
Louis Dionne16fe2952018-07-11 23:14:33 +0000619 _LIBCPP_INLINE_VISIBILITY iostate exceptions() const {return ios_base::exceptions();}
620 _LIBCPP_INLINE_VISIBILITY void exceptions(iostate __iostate) {ios_base::exceptions(__iostate);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000621
622 // 27.5.4.1 Constructor/destructor:
Howard Hinnantcf823322010-12-17 14:46:43 +0000623 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000624 explicit basic_ios(basic_streambuf<char_type,traits_type>* __sb);
625 virtual ~basic_ios();
626
627 // 27.5.4.2 Members:
Howard Hinnantcf823322010-12-17 14:46:43 +0000628 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000629 basic_ostream<char_type, traits_type>* tie() const;
Howard Hinnantcf823322010-12-17 14:46:43 +0000630 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000631 basic_ostream<char_type, traits_type>* tie(basic_ostream<char_type, traits_type>* __tiestr);
632
Howard Hinnantcf823322010-12-17 14:46:43 +0000633 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000634 basic_streambuf<char_type, traits_type>* rdbuf() const;
Howard Hinnantcf823322010-12-17 14:46:43 +0000635 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000636 basic_streambuf<char_type, traits_type>* rdbuf(basic_streambuf<char_type, traits_type>* __sb);
637
638 basic_ios& copyfmt(const basic_ios& __rhs);
639
Howard Hinnantcf823322010-12-17 14:46:43 +0000640 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000641 char_type fill() const;
Howard Hinnantcf823322010-12-17 14:46:43 +0000642 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000643 char_type fill(char_type __ch);
644
Howard Hinnantcf823322010-12-17 14:46:43 +0000645 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000646 locale imbue(const locale& __loc);
647
Howard Hinnantcf823322010-12-17 14:46:43 +0000648 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000649 char narrow(char_type __c, char __dfault) const;
Howard Hinnantcf823322010-12-17 14:46:43 +0000650 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000651 char_type widen(char __c) const;
652
653protected:
Louis Dionne16fe2952018-07-11 23:14:33 +0000654 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000655 basic_ios() {// purposefully does no initialization
656 }
Howard Hinnantcf823322010-12-17 14:46:43 +0000657 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000658 void init(basic_streambuf<char_type, traits_type>* __sb);
659
Howard Hinnantcf823322010-12-17 14:46:43 +0000660 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000661 void move(basic_ios& __rhs);
Eric Fiselierf07d88c2017-04-19 01:34:08 +0000662#ifndef _LIBCPP_CXX03_LANG
Louis Dionne16fe2952018-07-11 23:14:33 +0000663 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000664 void move(basic_ios&& __rhs) {move(__rhs);}
665#endif
Howard Hinnantcf823322010-12-17 14:46:43 +0000666 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant770a3662012-07-21 01:03:40 +0000667 void swap(basic_ios& __rhs) _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000668 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000669 void set_rdbuf(basic_streambuf<char_type, traits_type>* __sb);
670private:
671 basic_ostream<char_type, traits_type>* __tie_;
Bruce Mitchenerca134c52018-02-14 00:29:38 +0000672 mutable int_type __fill_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000673};
674
675template <class _CharT, class _Traits>
676inline _LIBCPP_INLINE_VISIBILITY
677basic_ios<_CharT, _Traits>::basic_ios(basic_streambuf<char_type,traits_type>* __sb)
678{
679 init(__sb);
680}
681
682template <class _CharT, class _Traits>
683basic_ios<_CharT, _Traits>::~basic_ios()
684{
685}
686
687template <class _CharT, class _Traits>
688inline _LIBCPP_INLINE_VISIBILITY
689void
690basic_ios<_CharT, _Traits>::init(basic_streambuf<char_type, traits_type>* __sb)
691{
692 ios_base::init(__sb);
693 __tie_ = 0;
Howard Hinnant2efe10d2012-08-26 18:05:35 +0000694 __fill_ = traits_type::eof();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000695}
696
697template <class _CharT, class _Traits>
698inline _LIBCPP_INLINE_VISIBILITY
699basic_ostream<_CharT, _Traits>*
700basic_ios<_CharT, _Traits>::tie() const
701{
702 return __tie_;
703}
704
705template <class _CharT, class _Traits>
706inline _LIBCPP_INLINE_VISIBILITY
707basic_ostream<_CharT, _Traits>*
708basic_ios<_CharT, _Traits>::tie(basic_ostream<char_type, traits_type>* __tiestr)
709{
710 basic_ostream<char_type, traits_type>* __r = __tie_;
711 __tie_ = __tiestr;
712 return __r;
713}
714
715template <class _CharT, class _Traits>
716inline _LIBCPP_INLINE_VISIBILITY
717basic_streambuf<_CharT, _Traits>*
718basic_ios<_CharT, _Traits>::rdbuf() const
719{
720 return static_cast<basic_streambuf<char_type, traits_type>*>(ios_base::rdbuf());
721}
722
723template <class _CharT, class _Traits>
724inline _LIBCPP_INLINE_VISIBILITY
725basic_streambuf<_CharT, _Traits>*
726basic_ios<_CharT, _Traits>::rdbuf(basic_streambuf<char_type, traits_type>* __sb)
727{
728 basic_streambuf<char_type, traits_type>* __r = rdbuf();
729 ios_base::rdbuf(__sb);
730 return __r;
731}
732
733template <class _CharT, class _Traits>
734inline _LIBCPP_INLINE_VISIBILITY
735locale
736basic_ios<_CharT, _Traits>::imbue(const locale& __loc)
737{
738 locale __r = getloc();
739 ios_base::imbue(__loc);
740 if (rdbuf())
741 rdbuf()->pubimbue(__loc);
742 return __r;
743}
744
745template <class _CharT, class _Traits>
746inline _LIBCPP_INLINE_VISIBILITY
747char
748basic_ios<_CharT, _Traits>::narrow(char_type __c, char __dfault) const
749{
750 return use_facet<ctype<char_type> >(getloc()).narrow(__c, __dfault);
751}
752
753template <class _CharT, class _Traits>
754inline _LIBCPP_INLINE_VISIBILITY
755_CharT
756basic_ios<_CharT, _Traits>::widen(char __c) const
757{
758 return use_facet<ctype<char_type> >(getloc()).widen(__c);
759}
760
761template <class _CharT, class _Traits>
762inline _LIBCPP_INLINE_VISIBILITY
763_CharT
764basic_ios<_CharT, _Traits>::fill() const
765{
Howard Hinnant2efe10d2012-08-26 18:05:35 +0000766 if (traits_type::eq_int_type(traits_type::eof(), __fill_))
767 __fill_ = widen(' ');
Howard Hinnantc51e1022010-05-11 19:42:16 +0000768 return __fill_;
769}
770
771template <class _CharT, class _Traits>
772inline _LIBCPP_INLINE_VISIBILITY
773_CharT
774basic_ios<_CharT, _Traits>::fill(char_type __ch)
775{
776 char_type __r = __fill_;
777 __fill_ = __ch;
778 return __r;
779}
780
781template <class _CharT, class _Traits>
782basic_ios<_CharT, _Traits>&
783basic_ios<_CharT, _Traits>::copyfmt(const basic_ios& __rhs)
784{
785 if (this != &__rhs)
786 {
787 __call_callbacks(erase_event);
788 ios_base::copyfmt(__rhs);
789 __tie_ = __rhs.__tie_;
790 __fill_ = __rhs.__fill_;
791 __call_callbacks(copyfmt_event);
792 exceptions(__rhs.exceptions());
793 }
794 return *this;
795}
796
797template <class _CharT, class _Traits>
798inline _LIBCPP_INLINE_VISIBILITY
799void
800basic_ios<_CharT, _Traits>::move(basic_ios& __rhs)
801{
802 ios_base::move(__rhs);
803 __tie_ = __rhs.__tie_;
804 __rhs.__tie_ = 0;
805 __fill_ = __rhs.__fill_;
806}
807
808template <class _CharT, class _Traits>
809inline _LIBCPP_INLINE_VISIBILITY
810void
Howard Hinnant770a3662012-07-21 01:03:40 +0000811basic_ios<_CharT, _Traits>::swap(basic_ios& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000812{
813 ios_base::swap(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000814 _VSTD::swap(__tie_, __rhs.__tie_);
815 _VSTD::swap(__fill_, __rhs.__fill_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816}
817
818template <class _CharT, class _Traits>
819inline _LIBCPP_INLINE_VISIBILITY
820void
821basic_ios<_CharT, _Traits>::set_rdbuf(basic_streambuf<char_type, traits_type>* __sb)
822{
823 ios_base::set_rdbuf(__sb);
824}
825
826inline _LIBCPP_INLINE_VISIBILITY
827ios_base&
828boolalpha(ios_base& __str)
829{
830 __str.setf(ios_base::boolalpha);
831 return __str;
832}
833
834inline _LIBCPP_INLINE_VISIBILITY
835ios_base&
836noboolalpha(ios_base& __str)
837{
838 __str.unsetf(ios_base::boolalpha);
839 return __str;
840}
841
842inline _LIBCPP_INLINE_VISIBILITY
843ios_base&
844showbase(ios_base& __str)
845{
846 __str.setf(ios_base::showbase);
847 return __str;
848}
849
850inline _LIBCPP_INLINE_VISIBILITY
851ios_base&
852noshowbase(ios_base& __str)
853{
854 __str.unsetf(ios_base::showbase);
855 return __str;
856}
857
858inline _LIBCPP_INLINE_VISIBILITY
859ios_base&
860showpoint(ios_base& __str)
861{
862 __str.setf(ios_base::showpoint);
863 return __str;
864}
865
866inline _LIBCPP_INLINE_VISIBILITY
867ios_base&
868noshowpoint(ios_base& __str)
869{
870 __str.unsetf(ios_base::showpoint);
871 return __str;
872}
873
874inline _LIBCPP_INLINE_VISIBILITY
875ios_base&
876showpos(ios_base& __str)
877{
878 __str.setf(ios_base::showpos);
879 return __str;
880}
881
882inline _LIBCPP_INLINE_VISIBILITY
883ios_base&
884noshowpos(ios_base& __str)
885{
886 __str.unsetf(ios_base::showpos);
887 return __str;
888}
889
890inline _LIBCPP_INLINE_VISIBILITY
891ios_base&
892skipws(ios_base& __str)
893{
894 __str.setf(ios_base::skipws);
895 return __str;
896}
897
898inline _LIBCPP_INLINE_VISIBILITY
899ios_base&
900noskipws(ios_base& __str)
901{
902 __str.unsetf(ios_base::skipws);
903 return __str;
904}
905
906inline _LIBCPP_INLINE_VISIBILITY
907ios_base&
908uppercase(ios_base& __str)
909{
910 __str.setf(ios_base::uppercase);
911 return __str;
912}
913
914inline _LIBCPP_INLINE_VISIBILITY
915ios_base&
916nouppercase(ios_base& __str)
917{
918 __str.unsetf(ios_base::uppercase);
919 return __str;
920}
921
922inline _LIBCPP_INLINE_VISIBILITY
923ios_base&
924unitbuf(ios_base& __str)
925{
926 __str.setf(ios_base::unitbuf);
927 return __str;
928}
929
930inline _LIBCPP_INLINE_VISIBILITY
931ios_base&
932nounitbuf(ios_base& __str)
933{
934 __str.unsetf(ios_base::unitbuf);
935 return __str;
936}
937
938inline _LIBCPP_INLINE_VISIBILITY
939ios_base&
940internal(ios_base& __str)
941{
942 __str.setf(ios_base::internal, ios_base::adjustfield);
943 return __str;
944}
945
946inline _LIBCPP_INLINE_VISIBILITY
947ios_base&
948left(ios_base& __str)
949{
950 __str.setf(ios_base::left, ios_base::adjustfield);
951 return __str;
952}
953
954inline _LIBCPP_INLINE_VISIBILITY
955ios_base&
956right(ios_base& __str)
957{
958 __str.setf(ios_base::right, ios_base::adjustfield);
959 return __str;
960}
961
962inline _LIBCPP_INLINE_VISIBILITY
963ios_base&
964dec(ios_base& __str)
965{
966 __str.setf(ios_base::dec, ios_base::basefield);
967 return __str;
968}
969
970inline _LIBCPP_INLINE_VISIBILITY
971ios_base&
972hex(ios_base& __str)
973{
974 __str.setf(ios_base::hex, ios_base::basefield);
975 return __str;
976}
977
978inline _LIBCPP_INLINE_VISIBILITY
979ios_base&
980oct(ios_base& __str)
981{
982 __str.setf(ios_base::oct, ios_base::basefield);
983 return __str;
984}
985
986inline _LIBCPP_INLINE_VISIBILITY
987ios_base&
988fixed(ios_base& __str)
989{
990 __str.setf(ios_base::fixed, ios_base::floatfield);
991 return __str;
992}
993
994inline _LIBCPP_INLINE_VISIBILITY
995ios_base&
996scientific(ios_base& __str)
997{
998 __str.setf(ios_base::scientific, ios_base::floatfield);
999 return __str;
1000}
1001
1002inline _LIBCPP_INLINE_VISIBILITY
1003ios_base&
1004hexfloat(ios_base& __str)
1005{
1006 __str.setf(ios_base::fixed | ios_base::scientific, ios_base::floatfield);
1007 return __str;
1008}
1009
1010inline _LIBCPP_INLINE_VISIBILITY
1011ios_base&
1012defaultfloat(ios_base& __str)
1013{
1014 __str.unsetf(ios_base::floatfield);
1015 return __str;
1016}
1017
Marshall Clow8d0942c2013-07-09 20:34:14 +00001018template <class _CharT, class _Traits>
1019class __save_flags
1020{
1021 typedef basic_ios<_CharT, _Traits> __stream_type;
1022 typedef typename __stream_type::fmtflags fmtflags;
1023
1024 __stream_type& __stream_;
1025 fmtflags __fmtflags_;
1026 _CharT __fill_;
1027
1028 __save_flags(const __save_flags&);
1029 __save_flags& operator=(const __save_flags&);
1030public:
1031 _LIBCPP_INLINE_VISIBILITY
1032 explicit __save_flags(__stream_type& __stream)
1033 : __stream_(__stream),
1034 __fmtflags_(__stream.flags()),
1035 __fill_(__stream.fill())
1036 {}
1037 _LIBCPP_INLINE_VISIBILITY
1038 ~__save_flags()
1039 {
1040 __stream_.flags(__fmtflags_);
1041 __stream_.fill(__fill_);
1042 }
1043};
1044
Howard Hinnantc51e1022010-05-11 19:42:16 +00001045_LIBCPP_END_NAMESPACE_STD
1046
1047#endif // _LIBCPP_IOS