blob: 3efce132e495b1a28bf2d7003055ca25e718402d [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- iomanip ----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_IOMANIP
12#define _LIBCPP_IOMANIP
13
14/*
15 iomanip synopsis
16
17// types T1, T2, ... are unspecified implementation types
18T1 resetiosflags(ios_base::fmtflags mask);
19T2 setiosflags (ios_base::fmtflags mask);
20T3 setbase(int base);
21template<charT> T4 setfill(charT c);
22T5 setprecision(int n);
23T6 setw(int n);
24template <class moneyT> T7 get_money(moneyT& mon, bool intl = false);
25template <class charT, class moneyT> T8 put_money(const moneyT& mon, bool intl = false);
26template <class charT> T9 get_time(struct tm* tmb, const charT* fmt);
27template <class charT> T10 put_time(const struct tm* tmb, const charT* fmt);
28
29} // std
30
31*/
32
33#include <__config>
34#include <istream>
35
36#pragma GCC system_header
37
38_LIBCPP_BEGIN_NAMESPACE_STD
39
40// resetiosflags
41
42class __iom_t1
43{
44 ios_base::fmtflags __mask_;
45public:
46 explicit __iom_t1(ios_base::fmtflags __m) : __mask_(__m) {}
47
48 template <class _CharT, class _Traits>
49 friend
50 basic_istream<_CharT, _Traits>&
51 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t1& __x)
52 {
53 __is.unsetf(__x.__mask_);
54 return __is;
55 }
56
57 template <class _CharT, class _Traits>
58 friend
59 basic_ostream<_CharT, _Traits>&
60 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t1& __x)
61 {
62 __os.unsetf(__x.__mask_);
63 return __os;
64 }
65};
66
67inline _LIBCPP_INLINE_VISIBILITY
68__iom_t1
69resetiosflags(ios_base::fmtflags __mask)
70{
71 return __iom_t1(__mask);
72}
73
74// setiosflags
75
76class __iom_t2
77{
78 ios_base::fmtflags __mask_;
79public:
80 explicit __iom_t2(ios_base::fmtflags __m) : __mask_(__m) {}
81
82 template <class _CharT, class _Traits>
83 friend
84 basic_istream<_CharT, _Traits>&
85 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t2& __x)
86 {
87 __is.setf(__x.__mask_);
88 return __is;
89 }
90
91 template <class _CharT, class _Traits>
92 friend
93 basic_ostream<_CharT, _Traits>&
94 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t2& __x)
95 {
96 __os.setf(__x.__mask_);
97 return __os;
98 }
99};
100
101inline _LIBCPP_INLINE_VISIBILITY
102__iom_t2
103setiosflags(ios_base::fmtflags __mask)
104{
105 return __iom_t2(__mask);
106}
107
108// setbase
109
110class __iom_t3
111{
112 int __base_;
113public:
114 explicit __iom_t3(int __b) : __base_(__b) {}
115
116 template <class _CharT, class _Traits>
117 friend
118 basic_istream<_CharT, _Traits>&
119 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t3& __x)
120 {
121 __is.setf(__x.__base_ == 8 ? ios_base::oct :
122 __x.__base_ == 10 ? ios_base::dec :
123 __x.__base_ == 16 ? ios_base::hex :
124 ios_base::fmtflags(0), ios_base::basefield);
125 return __is;
126 }
127
128 template <class _CharT, class _Traits>
129 friend
130 basic_ostream<_CharT, _Traits>&
131 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t3& __x)
132 {
133 __os.setf(__x.__base_ == 8 ? ios_base::oct :
134 __x.__base_ == 10 ? ios_base::dec :
135 __x.__base_ == 16 ? ios_base::hex :
136 ios_base::fmtflags(0), ios_base::basefield);
137 return __os;
138 }
139};
140
141inline _LIBCPP_INLINE_VISIBILITY
142__iom_t3
143setbase(int __base)
144{
145 return __iom_t3(__base);
146}
147
148// setfill
149
150template<class _CharT>
151class __iom_t4
152{
153 _CharT __fill_;
154public:
155 explicit __iom_t4(_CharT __c) : __fill_(__c) {}
156
157 template <class _Traits>
158 friend
159 basic_ostream<_CharT, _Traits>&
160 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t4& __x)
161 {
162 __os.fill(__x.__fill_);
163 return __os;
164 }
165};
166
167template<class _CharT>
168inline _LIBCPP_INLINE_VISIBILITY
169__iom_t4<_CharT>
170setfill(_CharT __c)
171{
172 return __iom_t4<_CharT>(__c);
173}
174
175// setprecision
176
177class __iom_t5
178{
179 int __n_;
180public:
181 explicit __iom_t5(int __n) : __n_(__n) {}
182
183 template <class _CharT, class _Traits>
184 friend
185 basic_istream<_CharT, _Traits>&
186 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t5& __x)
187 {
188 __is.precision(__x.__n_);
189 return __is;
190 }
191
192 template <class _CharT, class _Traits>
193 friend
194 basic_ostream<_CharT, _Traits>&
195 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t5& __x)
196 {
197 __os.precision(__x.__n_);
198 return __os;
199 }
200};
201
202inline _LIBCPP_INLINE_VISIBILITY
203__iom_t5
204setprecision(int __n)
205{
206 return __iom_t5(__n);
207}
208
209// setw
210
211class __iom_t6
212{
213 int __n_;
214public:
215 explicit __iom_t6(int __n) : __n_(__n) {}
216
217 template <class _CharT, class _Traits>
218 friend
219 basic_istream<_CharT, _Traits>&
220 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t6& __x)
221 {
222 __is.width(__x.__n_);
223 return __is;
224 }
225
226 template <class _CharT, class _Traits>
227 friend
228 basic_ostream<_CharT, _Traits>&
229 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t6& __x)
230 {
231 __os.width(__x.__n_);
232 return __os;
233 }
234};
235
236inline _LIBCPP_INLINE_VISIBILITY
237__iom_t6
238setw(int __n)
239{
240 return __iom_t6(__n);
241}
242
243// get_money
244
245template <class _MoneyT> class __iom_t7;
246
247template <class _CharT, class _Traits, class _MoneyT>
248basic_istream<_CharT, _Traits>&
249operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x);
250
251template <class _MoneyT>
252class __iom_t7
253{
254 _MoneyT& __mon_;
255 bool __intl_;
256public:
257 __iom_t7(_MoneyT& __mon, bool __intl)
258 : __mon_(__mon), __intl_(__intl) {}
259
260 template <class _CharT, class _Traits, class _M>
261 friend
262 basic_istream<_CharT, _Traits>&
263 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_M>& __x);
264};
265
266template <class _CharT, class _Traits, class _MoneyT>
267basic_istream<_CharT, _Traits>&
268operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x)
269{
270#ifndef _LIBCPP_NO_EXCEPTIONS
271 try
272 {
Howard Hinnant324bb032010-08-22 00:02:43 +0000273#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000274 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
275 if (__s)
276 {
277 typedef istreambuf_iterator<_CharT, _Traits> _I;
278 typedef money_get<_CharT, _I> _F;
279 ios_base::iostate __err = ios_base::goodbit;
280 const _F& __mf = use_facet<_F>(__is.getloc());
281 __mf.get(_I(__is), _I(), __x.__intl_, __is, __err, __x.__mon_);
282 __is.setstate(__err);
283 }
284#ifndef _LIBCPP_NO_EXCEPTIONS
285 }
286 catch (...)
287 {
288 __is.__set_badbit_and_consider_rethrow();
289 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000290#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000291 return __is;
292}
293
294template <class _MoneyT>
295inline _LIBCPP_INLINE_VISIBILITY
296__iom_t7<_MoneyT>
297get_money(_MoneyT& __mon, bool __intl = false)
298{
299 return __iom_t7<_MoneyT>(__mon, __intl);
300}
301
302// put_money
303
304template <class _MoneyT> class __iom_t8;
305
306template <class _CharT, class _Traits, class _MoneyT>
307basic_ostream<_CharT, _Traits>&
308operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x);
309
310template <class _MoneyT>
311class __iom_t8
312{
313 const _MoneyT& __mon_;
314 bool __intl_;
315public:
316 __iom_t8(const _MoneyT& __mon, bool __intl)
317 : __mon_(__mon), __intl_(__intl) {}
318
319 template <class _CharT, class _Traits, class _M>
320 friend
321 basic_ostream<_CharT, _Traits>&
322 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_M>& __x);
323};
324
325template <class _CharT, class _Traits, class _MoneyT>
326basic_ostream<_CharT, _Traits>&
327operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x)
328{
329#ifndef _LIBCPP_NO_EXCEPTIONS
330 try
331 {
Howard Hinnant324bb032010-08-22 00:02:43 +0000332#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000333 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
334 if (__s)
335 {
336 typedef ostreambuf_iterator<_CharT, _Traits> _O;
337 typedef money_put<_CharT, _O> _F;
338 const _F& __mf = use_facet<_F>(__os.getloc());
339 if (__mf.put(_O(__os), __x.__intl_, __os, __os.fill(), __x.__mon_).failed())
340 __os.setstate(ios_base::badbit);
341 }
342#ifndef _LIBCPP_NO_EXCEPTIONS
343 }
344 catch (...)
345 {
346 __os.__set_badbit_and_consider_rethrow();
347 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000348#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000349 return __os;
350}
351
352template <class _MoneyT>
353inline _LIBCPP_INLINE_VISIBILITY
354__iom_t8<_MoneyT>
355put_money(const _MoneyT& __mon, bool __intl = false)
356{
357 return __iom_t8<_MoneyT>(__mon, __intl);
358}
359
360// get_time
361
362template <class _CharT> class __iom_t9;
363
364template <class _CharT, class _Traits>
365basic_istream<_CharT, _Traits>&
366operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x);
367
368template <class _CharT>
369class __iom_t9
370{
371 tm* __tm_;
372 const _CharT* __fmt_;
373public:
374 __iom_t9(tm* __tm, const _CharT* __fmt)
375 : __tm_(__tm), __fmt_(__fmt) {}
376
377 template <class _C, class _Traits>
378 friend
379 basic_istream<_C, _Traits>&
380 operator>>(basic_istream<_C, _Traits>& __is, const __iom_t9<_C>& __x);
381};
382
383template <class _CharT, class _Traits>
384basic_istream<_CharT, _Traits>&
385operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x)
386{
387#ifndef _LIBCPP_NO_EXCEPTIONS
388 try
389 {
Howard Hinnant324bb032010-08-22 00:02:43 +0000390#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000391 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
392 if (__s)
393 {
394 typedef istreambuf_iterator<_CharT, _Traits> _I;
395 typedef time_get<_CharT, _I> _F;
396 ios_base::iostate __err = ios_base::goodbit;
397 const _F& __tf = use_facet<_F>(__is.getloc());
398 __tf.get(_I(__is), _I(), __is, __err, __x.__tm_,
399 __x.__fmt_, __x.__fmt_ + _Traits::length(__x.__fmt_));
400 __is.setstate(__err);
401 }
402#ifndef _LIBCPP_NO_EXCEPTIONS
403 }
404 catch (...)
405 {
406 __is.__set_badbit_and_consider_rethrow();
407 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000408#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000409 return __is;
410}
411
412template <class _CharT>
413inline _LIBCPP_INLINE_VISIBILITY
414__iom_t9<_CharT>
415get_time(tm* __tm, const _CharT* __fmt)
416{
417 return __iom_t9<_CharT>(__tm, __fmt);
418}
419
420// put_time
421
422template <class _CharT> class __iom_t10;
423
424template <class _CharT, class _Traits>
425basic_ostream<_CharT, _Traits>&
426operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x);
427
428template <class _CharT>
429class __iom_t10
430{
431 const tm* __tm_;
432 const _CharT* __fmt_;
433public:
434 __iom_t10(const tm* __tm, const _CharT* __fmt)
435 : __tm_(__tm), __fmt_(__fmt) {}
436
437 template <class _C, class _Traits>
438 friend
439 basic_ostream<_C, _Traits>&
440 operator<<(basic_ostream<_C, _Traits>& __os, const __iom_t10<_C>& __x);
441};
442
443template <class _CharT, class _Traits>
444basic_ostream<_CharT, _Traits>&
445operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x)
446{
447#ifndef _LIBCPP_NO_EXCEPTIONS
448 try
449 {
Howard Hinnant324bb032010-08-22 00:02:43 +0000450#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000451 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
452 if (__s)
453 {
454 typedef ostreambuf_iterator<_CharT, _Traits> _O;
455 typedef time_put<_CharT, _O> _F;
456 const _F& __tf = use_facet<_F>(__os.getloc());
457 if (__tf.put(_O(__os), __os, __os.fill(), __x.__tm_,
458 __x.__fmt_, __x.__fmt_ + _Traits::length(__x.__fmt_)).failed())
459 __os.setstate(ios_base::badbit);
460 }
461#ifndef _LIBCPP_NO_EXCEPTIONS
462 }
463 catch (...)
464 {
465 __os.__set_badbit_and_consider_rethrow();
466 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000467#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000468 return __os;
469}
470
471template <class _CharT>
472inline _LIBCPP_INLINE_VISIBILITY
473__iom_t10<_CharT>
474put_time(const tm* __tm, const _CharT* __fmt)
475{
476 return __iom_t10<_CharT>(__tm, __fmt);
477}
478
479_LIBCPP_END_NAMESPACE_STD
480
481#endif // _LIBCPP_IOMANIP