blob: a407360ff0e9cabb95af01f82431154bdef18731 [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//
Howard Hinnantb64f8b02010-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 Hinnantbc8d3f92010-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
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:
Howard Hinnant68a8e902010-09-22 15:29:08 +000046 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000047 explicit __iom_t1(ios_base::fmtflags __m) : __mask_(__m) {}
48
49 template <class _CharT, class _Traits>
50 friend
Howard Hinnant68a8e902010-09-22 15:29:08 +000051 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000052 basic_istream<_CharT, _Traits>&
53 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t1& __x)
54 {
55 __is.unsetf(__x.__mask_);
56 return __is;
57 }
58
59 template <class _CharT, class _Traits>
60 friend
Howard Hinnant68a8e902010-09-22 15:29:08 +000061 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000062 basic_ostream<_CharT, _Traits>&
63 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t1& __x)
64 {
65 __os.unsetf(__x.__mask_);
66 return __os;
67 }
68};
69
70inline _LIBCPP_INLINE_VISIBILITY
71__iom_t1
72resetiosflags(ios_base::fmtflags __mask)
73{
74 return __iom_t1(__mask);
75}
76
77// setiosflags
78
79class __iom_t2
80{
81 ios_base::fmtflags __mask_;
82public:
Howard Hinnant68a8e902010-09-22 15:29:08 +000083 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000084 explicit __iom_t2(ios_base::fmtflags __m) : __mask_(__m) {}
85
86 template <class _CharT, class _Traits>
87 friend
Howard Hinnant68a8e902010-09-22 15:29:08 +000088 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000089 basic_istream<_CharT, _Traits>&
90 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t2& __x)
91 {
92 __is.setf(__x.__mask_);
93 return __is;
94 }
95
96 template <class _CharT, class _Traits>
97 friend
Howard Hinnant68a8e902010-09-22 15:29:08 +000098 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000099 basic_ostream<_CharT, _Traits>&
100 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t2& __x)
101 {
102 __os.setf(__x.__mask_);
103 return __os;
104 }
105};
106
107inline _LIBCPP_INLINE_VISIBILITY
108__iom_t2
109setiosflags(ios_base::fmtflags __mask)
110{
111 return __iom_t2(__mask);
112}
113
114// setbase
115
116class __iom_t3
117{
118 int __base_;
119public:
Howard Hinnant68a8e902010-09-22 15:29:08 +0000120 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000121 explicit __iom_t3(int __b) : __base_(__b) {}
122
123 template <class _CharT, class _Traits>
124 friend
Howard Hinnant68a8e902010-09-22 15:29:08 +0000125 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000126 basic_istream<_CharT, _Traits>&
127 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t3& __x)
128 {
129 __is.setf(__x.__base_ == 8 ? ios_base::oct :
130 __x.__base_ == 10 ? ios_base::dec :
131 __x.__base_ == 16 ? ios_base::hex :
132 ios_base::fmtflags(0), ios_base::basefield);
133 return __is;
134 }
135
136 template <class _CharT, class _Traits>
137 friend
Howard Hinnant68a8e902010-09-22 15:29:08 +0000138 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000139 basic_ostream<_CharT, _Traits>&
140 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t3& __x)
141 {
142 __os.setf(__x.__base_ == 8 ? ios_base::oct :
143 __x.__base_ == 10 ? ios_base::dec :
144 __x.__base_ == 16 ? ios_base::hex :
145 ios_base::fmtflags(0), ios_base::basefield);
146 return __os;
147 }
148};
149
150inline _LIBCPP_INLINE_VISIBILITY
151__iom_t3
152setbase(int __base)
153{
154 return __iom_t3(__base);
155}
156
157// setfill
158
159template<class _CharT>
160class __iom_t4
161{
162 _CharT __fill_;
163public:
Howard Hinnant68a8e902010-09-22 15:29:08 +0000164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000165 explicit __iom_t4(_CharT __c) : __fill_(__c) {}
166
167 template <class _Traits>
168 friend
Howard Hinnant68a8e902010-09-22 15:29:08 +0000169 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000170 basic_ostream<_CharT, _Traits>&
171 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t4& __x)
172 {
173 __os.fill(__x.__fill_);
174 return __os;
175 }
176};
177
178template<class _CharT>
179inline _LIBCPP_INLINE_VISIBILITY
180__iom_t4<_CharT>
181setfill(_CharT __c)
182{
183 return __iom_t4<_CharT>(__c);
184}
185
186// setprecision
187
188class __iom_t5
189{
190 int __n_;
191public:
Howard Hinnant68a8e902010-09-22 15:29:08 +0000192 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000193 explicit __iom_t5(int __n) : __n_(__n) {}
194
195 template <class _CharT, class _Traits>
196 friend
Howard Hinnant68a8e902010-09-22 15:29:08 +0000197 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000198 basic_istream<_CharT, _Traits>&
199 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t5& __x)
200 {
201 __is.precision(__x.__n_);
202 return __is;
203 }
204
205 template <class _CharT, class _Traits>
206 friend
Howard Hinnant68a8e902010-09-22 15:29:08 +0000207 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000208 basic_ostream<_CharT, _Traits>&
209 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t5& __x)
210 {
211 __os.precision(__x.__n_);
212 return __os;
213 }
214};
215
216inline _LIBCPP_INLINE_VISIBILITY
217__iom_t5
218setprecision(int __n)
219{
220 return __iom_t5(__n);
221}
222
223// setw
224
225class __iom_t6
226{
227 int __n_;
228public:
Howard Hinnant68a8e902010-09-22 15:29:08 +0000229 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000230 explicit __iom_t6(int __n) : __n_(__n) {}
231
232 template <class _CharT, class _Traits>
233 friend
Howard Hinnant68a8e902010-09-22 15:29:08 +0000234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000235 basic_istream<_CharT, _Traits>&
236 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t6& __x)
237 {
238 __is.width(__x.__n_);
239 return __is;
240 }
241
242 template <class _CharT, class _Traits>
243 friend
Howard Hinnant68a8e902010-09-22 15:29:08 +0000244 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000245 basic_ostream<_CharT, _Traits>&
246 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t6& __x)
247 {
248 __os.width(__x.__n_);
249 return __os;
250 }
251};
252
253inline _LIBCPP_INLINE_VISIBILITY
254__iom_t6
255setw(int __n)
256{
257 return __iom_t6(__n);
258}
259
260// get_money
261
262template <class _MoneyT> class __iom_t7;
263
264template <class _CharT, class _Traits, class _MoneyT>
265basic_istream<_CharT, _Traits>&
266operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x);
267
268template <class _MoneyT>
269class __iom_t7
270{
271 _MoneyT& __mon_;
272 bool __intl_;
273public:
Howard Hinnant68a8e902010-09-22 15:29:08 +0000274 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000275 __iom_t7(_MoneyT& __mon, bool __intl)
276 : __mon_(__mon), __intl_(__intl) {}
277
278 template <class _CharT, class _Traits, class _M>
279 friend
280 basic_istream<_CharT, _Traits>&
281 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_M>& __x);
282};
283
284template <class _CharT, class _Traits, class _MoneyT>
285basic_istream<_CharT, _Traits>&
286operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x)
287{
288#ifndef _LIBCPP_NO_EXCEPTIONS
289 try
290 {
Howard Hinnant324bb032010-08-22 00:02:43 +0000291#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000292 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
293 if (__s)
294 {
295 typedef istreambuf_iterator<_CharT, _Traits> _I;
296 typedef money_get<_CharT, _I> _F;
297 ios_base::iostate __err = ios_base::goodbit;
298 const _F& __mf = use_facet<_F>(__is.getloc());
299 __mf.get(_I(__is), _I(), __x.__intl_, __is, __err, __x.__mon_);
300 __is.setstate(__err);
301 }
302#ifndef _LIBCPP_NO_EXCEPTIONS
303 }
304 catch (...)
305 {
306 __is.__set_badbit_and_consider_rethrow();
307 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000308#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000309 return __is;
310}
311
312template <class _MoneyT>
313inline _LIBCPP_INLINE_VISIBILITY
314__iom_t7<_MoneyT>
315get_money(_MoneyT& __mon, bool __intl = false)
316{
317 return __iom_t7<_MoneyT>(__mon, __intl);
318}
319
320// put_money
321
322template <class _MoneyT> class __iom_t8;
323
324template <class _CharT, class _Traits, class _MoneyT>
325basic_ostream<_CharT, _Traits>&
326operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x);
327
328template <class _MoneyT>
329class __iom_t8
330{
331 const _MoneyT& __mon_;
332 bool __intl_;
333public:
Howard Hinnant68a8e902010-09-22 15:29:08 +0000334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000335 __iom_t8(const _MoneyT& __mon, bool __intl)
336 : __mon_(__mon), __intl_(__intl) {}
337
338 template <class _CharT, class _Traits, class _M>
339 friend
340 basic_ostream<_CharT, _Traits>&
341 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_M>& __x);
342};
343
344template <class _CharT, class _Traits, class _MoneyT>
345basic_ostream<_CharT, _Traits>&
346operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x)
347{
348#ifndef _LIBCPP_NO_EXCEPTIONS
349 try
350 {
Howard Hinnant324bb032010-08-22 00:02:43 +0000351#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000352 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
353 if (__s)
354 {
355 typedef ostreambuf_iterator<_CharT, _Traits> _O;
356 typedef money_put<_CharT, _O> _F;
357 const _F& __mf = use_facet<_F>(__os.getloc());
358 if (__mf.put(_O(__os), __x.__intl_, __os, __os.fill(), __x.__mon_).failed())
359 __os.setstate(ios_base::badbit);
360 }
361#ifndef _LIBCPP_NO_EXCEPTIONS
362 }
363 catch (...)
364 {
365 __os.__set_badbit_and_consider_rethrow();
366 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000367#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000368 return __os;
369}
370
371template <class _MoneyT>
372inline _LIBCPP_INLINE_VISIBILITY
373__iom_t8<_MoneyT>
374put_money(const _MoneyT& __mon, bool __intl = false)
375{
376 return __iom_t8<_MoneyT>(__mon, __intl);
377}
378
379// get_time
380
381template <class _CharT> class __iom_t9;
382
383template <class _CharT, class _Traits>
384basic_istream<_CharT, _Traits>&
385operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x);
386
387template <class _CharT>
388class __iom_t9
389{
390 tm* __tm_;
391 const _CharT* __fmt_;
392public:
Howard Hinnant68a8e902010-09-22 15:29:08 +0000393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000394 __iom_t9(tm* __tm, const _CharT* __fmt)
395 : __tm_(__tm), __fmt_(__fmt) {}
396
397 template <class _C, class _Traits>
398 friend
399 basic_istream<_C, _Traits>&
400 operator>>(basic_istream<_C, _Traits>& __is, const __iom_t9<_C>& __x);
401};
402
403template <class _CharT, class _Traits>
404basic_istream<_CharT, _Traits>&
405operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x)
406{
407#ifndef _LIBCPP_NO_EXCEPTIONS
408 try
409 {
Howard Hinnant324bb032010-08-22 00:02:43 +0000410#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000411 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
412 if (__s)
413 {
414 typedef istreambuf_iterator<_CharT, _Traits> _I;
415 typedef time_get<_CharT, _I> _F;
416 ios_base::iostate __err = ios_base::goodbit;
417 const _F& __tf = use_facet<_F>(__is.getloc());
418 __tf.get(_I(__is), _I(), __is, __err, __x.__tm_,
419 __x.__fmt_, __x.__fmt_ + _Traits::length(__x.__fmt_));
420 __is.setstate(__err);
421 }
422#ifndef _LIBCPP_NO_EXCEPTIONS
423 }
424 catch (...)
425 {
426 __is.__set_badbit_and_consider_rethrow();
427 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000428#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000429 return __is;
430}
431
432template <class _CharT>
433inline _LIBCPP_INLINE_VISIBILITY
434__iom_t9<_CharT>
435get_time(tm* __tm, const _CharT* __fmt)
436{
437 return __iom_t9<_CharT>(__tm, __fmt);
438}
439
440// put_time
441
442template <class _CharT> class __iom_t10;
443
444template <class _CharT, class _Traits>
445basic_ostream<_CharT, _Traits>&
446operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x);
447
448template <class _CharT>
449class __iom_t10
450{
451 const tm* __tm_;
452 const _CharT* __fmt_;
453public:
Howard Hinnant68a8e902010-09-22 15:29:08 +0000454 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000455 __iom_t10(const tm* __tm, const _CharT* __fmt)
456 : __tm_(__tm), __fmt_(__fmt) {}
457
458 template <class _C, class _Traits>
459 friend
460 basic_ostream<_C, _Traits>&
461 operator<<(basic_ostream<_C, _Traits>& __os, const __iom_t10<_C>& __x);
462};
463
464template <class _CharT, class _Traits>
465basic_ostream<_CharT, _Traits>&
466operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x)
467{
468#ifndef _LIBCPP_NO_EXCEPTIONS
469 try
470 {
Howard Hinnant324bb032010-08-22 00:02:43 +0000471#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000472 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
473 if (__s)
474 {
475 typedef ostreambuf_iterator<_CharT, _Traits> _O;
476 typedef time_put<_CharT, _O> _F;
477 const _F& __tf = use_facet<_F>(__os.getloc());
478 if (__tf.put(_O(__os), __os, __os.fill(), __x.__tm_,
479 __x.__fmt_, __x.__fmt_ + _Traits::length(__x.__fmt_)).failed())
480 __os.setstate(ios_base::badbit);
481 }
482#ifndef _LIBCPP_NO_EXCEPTIONS
483 }
484 catch (...)
485 {
486 __os.__set_badbit_and_consider_rethrow();
487 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000488#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000489 return __os;
490}
491
492template <class _CharT>
493inline _LIBCPP_INLINE_VISIBILITY
494__iom_t10<_CharT>
495put_time(const tm* __tm, const _CharT* __fmt)
496{
497 return __iom_t10<_CharT>(__tm, __fmt);
498}
499
500_LIBCPP_END_NAMESPACE_STD
501
502#endif // _LIBCPP_IOMANIP