blob: 15704046227f91ec3a98a7692fe31c95da7b7ad4 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- iomanip ----------------------------------===//
3//
Howard Hinnant5b08a8a2010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:16 +00005//
Howard Hinnant412dbeb2010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant3e519522010-05-11 19:42:16 +00008//
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
Howard Hinnant073458b2011-10-17 20:05:10 +000036#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant3e519522010-05-11 19:42:16 +000037#pragma GCC system_header
Howard Hinnant073458b2011-10-17 20:05:10 +000038#endif
Howard Hinnant3e519522010-05-11 19:42:16 +000039
40_LIBCPP_BEGIN_NAMESPACE_STD
41
42// resetiosflags
43
44class __iom_t1
45{
46 ios_base::fmtflags __mask_;
47public:
Howard Hinnant3030b2f2010-09-22 15:29:08 +000048 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +000049 explicit __iom_t1(ios_base::fmtflags __m) : __mask_(__m) {}
50
51 template <class _CharT, class _Traits>
52 friend
Howard Hinnant3030b2f2010-09-22 15:29:08 +000053 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +000054 basic_istream<_CharT, _Traits>&
55 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t1& __x)
56 {
57 __is.unsetf(__x.__mask_);
58 return __is;
59 }
60
61 template <class _CharT, class _Traits>
62 friend
Howard Hinnant3030b2f2010-09-22 15:29:08 +000063 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +000064 basic_ostream<_CharT, _Traits>&
65 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t1& __x)
66 {
67 __os.unsetf(__x.__mask_);
68 return __os;
69 }
70};
71
72inline _LIBCPP_INLINE_VISIBILITY
73__iom_t1
74resetiosflags(ios_base::fmtflags __mask)
75{
76 return __iom_t1(__mask);
77}
78
79// setiosflags
80
81class __iom_t2
82{
83 ios_base::fmtflags __mask_;
84public:
Howard Hinnant3030b2f2010-09-22 15:29:08 +000085 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +000086 explicit __iom_t2(ios_base::fmtflags __m) : __mask_(__m) {}
87
88 template <class _CharT, class _Traits>
89 friend
Howard Hinnant3030b2f2010-09-22 15:29:08 +000090 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +000091 basic_istream<_CharT, _Traits>&
92 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t2& __x)
93 {
94 __is.setf(__x.__mask_);
95 return __is;
96 }
97
98 template <class _CharT, class _Traits>
99 friend
Howard Hinnant3030b2f2010-09-22 15:29:08 +0000100 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000101 basic_ostream<_CharT, _Traits>&
102 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t2& __x)
103 {
104 __os.setf(__x.__mask_);
105 return __os;
106 }
107};
108
109inline _LIBCPP_INLINE_VISIBILITY
110__iom_t2
111setiosflags(ios_base::fmtflags __mask)
112{
113 return __iom_t2(__mask);
114}
115
116// setbase
117
118class __iom_t3
119{
120 int __base_;
121public:
Howard Hinnant3030b2f2010-09-22 15:29:08 +0000122 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000123 explicit __iom_t3(int __b) : __base_(__b) {}
124
125 template <class _CharT, class _Traits>
126 friend
Howard Hinnant3030b2f2010-09-22 15:29:08 +0000127 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000128 basic_istream<_CharT, _Traits>&
129 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t3& __x)
130 {
131 __is.setf(__x.__base_ == 8 ? ios_base::oct :
132 __x.__base_ == 10 ? ios_base::dec :
133 __x.__base_ == 16 ? ios_base::hex :
134 ios_base::fmtflags(0), ios_base::basefield);
135 return __is;
136 }
137
138 template <class _CharT, class _Traits>
139 friend
Howard Hinnant3030b2f2010-09-22 15:29:08 +0000140 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000141 basic_ostream<_CharT, _Traits>&
142 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t3& __x)
143 {
144 __os.setf(__x.__base_ == 8 ? ios_base::oct :
145 __x.__base_ == 10 ? ios_base::dec :
146 __x.__base_ == 16 ? ios_base::hex :
147 ios_base::fmtflags(0), ios_base::basefield);
148 return __os;
149 }
150};
151
152inline _LIBCPP_INLINE_VISIBILITY
153__iom_t3
154setbase(int __base)
155{
156 return __iom_t3(__base);
157}
158
159// setfill
160
161template<class _CharT>
162class __iom_t4
163{
164 _CharT __fill_;
165public:
Howard Hinnant3030b2f2010-09-22 15:29:08 +0000166 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000167 explicit __iom_t4(_CharT __c) : __fill_(__c) {}
168
169 template <class _Traits>
170 friend
Howard Hinnant3030b2f2010-09-22 15:29:08 +0000171 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000172 basic_ostream<_CharT, _Traits>&
173 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t4& __x)
174 {
175 __os.fill(__x.__fill_);
176 return __os;
177 }
178};
179
180template<class _CharT>
181inline _LIBCPP_INLINE_VISIBILITY
182__iom_t4<_CharT>
183setfill(_CharT __c)
184{
185 return __iom_t4<_CharT>(__c);
186}
187
188// setprecision
189
190class __iom_t5
191{
192 int __n_;
193public:
Howard Hinnant3030b2f2010-09-22 15:29:08 +0000194 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000195 explicit __iom_t5(int __n) : __n_(__n) {}
196
197 template <class _CharT, class _Traits>
198 friend
Howard Hinnant3030b2f2010-09-22 15:29:08 +0000199 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000200 basic_istream<_CharT, _Traits>&
201 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t5& __x)
202 {
203 __is.precision(__x.__n_);
204 return __is;
205 }
206
207 template <class _CharT, class _Traits>
208 friend
Howard Hinnant3030b2f2010-09-22 15:29:08 +0000209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000210 basic_ostream<_CharT, _Traits>&
211 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t5& __x)
212 {
213 __os.precision(__x.__n_);
214 return __os;
215 }
216};
217
218inline _LIBCPP_INLINE_VISIBILITY
219__iom_t5
220setprecision(int __n)
221{
222 return __iom_t5(__n);
223}
224
225// setw
226
227class __iom_t6
228{
229 int __n_;
230public:
Howard Hinnant3030b2f2010-09-22 15:29:08 +0000231 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000232 explicit __iom_t6(int __n) : __n_(__n) {}
233
234 template <class _CharT, class _Traits>
235 friend
Howard Hinnant3030b2f2010-09-22 15:29:08 +0000236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000237 basic_istream<_CharT, _Traits>&
238 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t6& __x)
239 {
240 __is.width(__x.__n_);
241 return __is;
242 }
243
244 template <class _CharT, class _Traits>
245 friend
Howard Hinnant3030b2f2010-09-22 15:29:08 +0000246 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000247 basic_ostream<_CharT, _Traits>&
248 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t6& __x)
249 {
250 __os.width(__x.__n_);
251 return __os;
252 }
253};
254
255inline _LIBCPP_INLINE_VISIBILITY
256__iom_t6
257setw(int __n)
258{
259 return __iom_t6(__n);
260}
261
262// get_money
263
264template <class _MoneyT> class __iom_t7;
265
266template <class _CharT, class _Traits, class _MoneyT>
267basic_istream<_CharT, _Traits>&
268operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x);
269
270template <class _MoneyT>
271class __iom_t7
272{
273 _MoneyT& __mon_;
274 bool __intl_;
275public:
Howard Hinnant3030b2f2010-09-22 15:29:08 +0000276 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000277 __iom_t7(_MoneyT& __mon, bool __intl)
278 : __mon_(__mon), __intl_(__intl) {}
279
280 template <class _CharT, class _Traits, class _M>
281 friend
282 basic_istream<_CharT, _Traits>&
283 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_M>& __x);
284};
285
286template <class _CharT, class _Traits, class _MoneyT>
287basic_istream<_CharT, _Traits>&
288operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x)
289{
290#ifndef _LIBCPP_NO_EXCEPTIONS
291 try
292 {
Howard Hinnantb3371f62010-08-22 00:02:43 +0000293#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +0000294 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
295 if (__s)
296 {
297 typedef istreambuf_iterator<_CharT, _Traits> _I;
298 typedef money_get<_CharT, _I> _F;
299 ios_base::iostate __err = ios_base::goodbit;
300 const _F& __mf = use_facet<_F>(__is.getloc());
301 __mf.get(_I(__is), _I(), __x.__intl_, __is, __err, __x.__mon_);
302 __is.setstate(__err);
303 }
304#ifndef _LIBCPP_NO_EXCEPTIONS
305 }
306 catch (...)
307 {
308 __is.__set_badbit_and_consider_rethrow();
309 }
Howard Hinnantb3371f62010-08-22 00:02:43 +0000310#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +0000311 return __is;
312}
313
314template <class _MoneyT>
315inline _LIBCPP_INLINE_VISIBILITY
316__iom_t7<_MoneyT>
317get_money(_MoneyT& __mon, bool __intl = false)
318{
319 return __iom_t7<_MoneyT>(__mon, __intl);
320}
321
322// put_money
323
324template <class _MoneyT> class __iom_t8;
325
326template <class _CharT, class _Traits, class _MoneyT>
327basic_ostream<_CharT, _Traits>&
328operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x);
329
330template <class _MoneyT>
331class __iom_t8
332{
333 const _MoneyT& __mon_;
334 bool __intl_;
335public:
Howard Hinnant3030b2f2010-09-22 15:29:08 +0000336 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000337 __iom_t8(const _MoneyT& __mon, bool __intl)
338 : __mon_(__mon), __intl_(__intl) {}
339
340 template <class _CharT, class _Traits, class _M>
341 friend
342 basic_ostream<_CharT, _Traits>&
343 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_M>& __x);
344};
345
346template <class _CharT, class _Traits, class _MoneyT>
347basic_ostream<_CharT, _Traits>&
348operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x)
349{
350#ifndef _LIBCPP_NO_EXCEPTIONS
351 try
352 {
Howard Hinnantb3371f62010-08-22 00:02:43 +0000353#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +0000354 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
355 if (__s)
356 {
357 typedef ostreambuf_iterator<_CharT, _Traits> _O;
358 typedef money_put<_CharT, _O> _F;
359 const _F& __mf = use_facet<_F>(__os.getloc());
360 if (__mf.put(_O(__os), __x.__intl_, __os, __os.fill(), __x.__mon_).failed())
361 __os.setstate(ios_base::badbit);
362 }
363#ifndef _LIBCPP_NO_EXCEPTIONS
364 }
365 catch (...)
366 {
367 __os.__set_badbit_and_consider_rethrow();
368 }
Howard Hinnantb3371f62010-08-22 00:02:43 +0000369#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +0000370 return __os;
371}
372
373template <class _MoneyT>
374inline _LIBCPP_INLINE_VISIBILITY
375__iom_t8<_MoneyT>
376put_money(const _MoneyT& __mon, bool __intl = false)
377{
378 return __iom_t8<_MoneyT>(__mon, __intl);
379}
380
381// get_time
382
383template <class _CharT> class __iom_t9;
384
385template <class _CharT, class _Traits>
386basic_istream<_CharT, _Traits>&
387operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x);
388
389template <class _CharT>
390class __iom_t9
391{
392 tm* __tm_;
393 const _CharT* __fmt_;
394public:
Howard Hinnant3030b2f2010-09-22 15:29:08 +0000395 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000396 __iom_t9(tm* __tm, const _CharT* __fmt)
397 : __tm_(__tm), __fmt_(__fmt) {}
398
399 template <class _C, class _Traits>
400 friend
401 basic_istream<_C, _Traits>&
402 operator>>(basic_istream<_C, _Traits>& __is, const __iom_t9<_C>& __x);
403};
404
405template <class _CharT, class _Traits>
406basic_istream<_CharT, _Traits>&
407operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x)
408{
409#ifndef _LIBCPP_NO_EXCEPTIONS
410 try
411 {
Howard Hinnantb3371f62010-08-22 00:02:43 +0000412#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +0000413 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
414 if (__s)
415 {
416 typedef istreambuf_iterator<_CharT, _Traits> _I;
417 typedef time_get<_CharT, _I> _F;
418 ios_base::iostate __err = ios_base::goodbit;
419 const _F& __tf = use_facet<_F>(__is.getloc());
420 __tf.get(_I(__is), _I(), __is, __err, __x.__tm_,
421 __x.__fmt_, __x.__fmt_ + _Traits::length(__x.__fmt_));
422 __is.setstate(__err);
423 }
424#ifndef _LIBCPP_NO_EXCEPTIONS
425 }
426 catch (...)
427 {
428 __is.__set_badbit_and_consider_rethrow();
429 }
Howard Hinnantb3371f62010-08-22 00:02:43 +0000430#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +0000431 return __is;
432}
433
434template <class _CharT>
435inline _LIBCPP_INLINE_VISIBILITY
436__iom_t9<_CharT>
437get_time(tm* __tm, const _CharT* __fmt)
438{
439 return __iom_t9<_CharT>(__tm, __fmt);
440}
441
442// put_time
443
444template <class _CharT> class __iom_t10;
445
446template <class _CharT, class _Traits>
447basic_ostream<_CharT, _Traits>&
448operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x);
449
450template <class _CharT>
451class __iom_t10
452{
453 const tm* __tm_;
454 const _CharT* __fmt_;
455public:
Howard Hinnant3030b2f2010-09-22 15:29:08 +0000456 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000457 __iom_t10(const tm* __tm, const _CharT* __fmt)
458 : __tm_(__tm), __fmt_(__fmt) {}
459
460 template <class _C, class _Traits>
461 friend
462 basic_ostream<_C, _Traits>&
463 operator<<(basic_ostream<_C, _Traits>& __os, const __iom_t10<_C>& __x);
464};
465
466template <class _CharT, class _Traits>
467basic_ostream<_CharT, _Traits>&
468operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x)
469{
470#ifndef _LIBCPP_NO_EXCEPTIONS
471 try
472 {
Howard Hinnantb3371f62010-08-22 00:02:43 +0000473#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +0000474 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
475 if (__s)
476 {
477 typedef ostreambuf_iterator<_CharT, _Traits> _O;
478 typedef time_put<_CharT, _O> _F;
479 const _F& __tf = use_facet<_F>(__os.getloc());
480 if (__tf.put(_O(__os), __os, __os.fill(), __x.__tm_,
481 __x.__fmt_, __x.__fmt_ + _Traits::length(__x.__fmt_)).failed())
482 __os.setstate(ios_base::badbit);
483 }
484#ifndef _LIBCPP_NO_EXCEPTIONS
485 }
486 catch (...)
487 {
488 __os.__set_badbit_and_consider_rethrow();
489 }
Howard Hinnantb3371f62010-08-22 00:02:43 +0000490#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +0000491 return __os;
492}
493
494template <class _CharT>
495inline _LIBCPP_INLINE_VISIBILITY
496__iom_t10<_CharT>
497put_time(const tm* __tm, const _CharT* __fmt)
498{
499 return __iom_t10<_CharT>(__tm, __fmt);
500}
501
502_LIBCPP_END_NAMESPACE_STD
503
504#endif // _LIBCPP_IOMANIP