Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===--------------------------- iomanip ----------------------------------===// |
| 3 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // |
| 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 |
| 18 | T1 resetiosflags(ios_base::fmtflags mask); |
| 19 | T2 setiosflags (ios_base::fmtflags mask); |
| 20 | T3 setbase(int base); |
| 21 | template<charT> T4 setfill(charT c); |
| 22 | T5 setprecision(int n); |
| 23 | T6 setw(int n); |
| 24 | template <class moneyT> T7 get_money(moneyT& mon, bool intl = false); |
| 25 | template <class charT, class moneyT> T8 put_money(const moneyT& mon, bool intl = false); |
| 26 | template <class charT> T9 get_time(struct tm* tmb, const charT* fmt); |
| 27 | template <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 | |
| 42 | class __iom_t1 |
| 43 | { |
| 44 | ios_base::fmtflags __mask_; |
| 45 | public: |
| 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 | |
| 67 | inline _LIBCPP_INLINE_VISIBILITY |
| 68 | __iom_t1 |
| 69 | resetiosflags(ios_base::fmtflags __mask) |
| 70 | { |
| 71 | return __iom_t1(__mask); |
| 72 | } |
| 73 | |
| 74 | // setiosflags |
| 75 | |
| 76 | class __iom_t2 |
| 77 | { |
| 78 | ios_base::fmtflags __mask_; |
| 79 | public: |
| 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 | |
| 101 | inline _LIBCPP_INLINE_VISIBILITY |
| 102 | __iom_t2 |
| 103 | setiosflags(ios_base::fmtflags __mask) |
| 104 | { |
| 105 | return __iom_t2(__mask); |
| 106 | } |
| 107 | |
| 108 | // setbase |
| 109 | |
| 110 | class __iom_t3 |
| 111 | { |
| 112 | int __base_; |
| 113 | public: |
| 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 | |
| 141 | inline _LIBCPP_INLINE_VISIBILITY |
| 142 | __iom_t3 |
| 143 | setbase(int __base) |
| 144 | { |
| 145 | return __iom_t3(__base); |
| 146 | } |
| 147 | |
| 148 | // setfill |
| 149 | |
| 150 | template<class _CharT> |
| 151 | class __iom_t4 |
| 152 | { |
| 153 | _CharT __fill_; |
| 154 | public: |
| 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 | |
| 167 | template<class _CharT> |
| 168 | inline _LIBCPP_INLINE_VISIBILITY |
| 169 | __iom_t4<_CharT> |
| 170 | setfill(_CharT __c) |
| 171 | { |
| 172 | return __iom_t4<_CharT>(__c); |
| 173 | } |
| 174 | |
| 175 | // setprecision |
| 176 | |
| 177 | class __iom_t5 |
| 178 | { |
| 179 | int __n_; |
| 180 | public: |
| 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 | |
| 202 | inline _LIBCPP_INLINE_VISIBILITY |
| 203 | __iom_t5 |
| 204 | setprecision(int __n) |
| 205 | { |
| 206 | return __iom_t5(__n); |
| 207 | } |
| 208 | |
| 209 | // setw |
| 210 | |
| 211 | class __iom_t6 |
| 212 | { |
| 213 | int __n_; |
| 214 | public: |
| 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 | |
| 236 | inline _LIBCPP_INLINE_VISIBILITY |
| 237 | __iom_t6 |
| 238 | setw(int __n) |
| 239 | { |
| 240 | return __iom_t6(__n); |
| 241 | } |
| 242 | |
| 243 | // get_money |
| 244 | |
| 245 | template <class _MoneyT> class __iom_t7; |
| 246 | |
| 247 | template <class _CharT, class _Traits, class _MoneyT> |
| 248 | basic_istream<_CharT, _Traits>& |
| 249 | operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x); |
| 250 | |
| 251 | template <class _MoneyT> |
| 252 | class __iom_t7 |
| 253 | { |
| 254 | _MoneyT& __mon_; |
| 255 | bool __intl_; |
| 256 | public: |
| 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 | |
| 266 | template <class _CharT, class _Traits, class _MoneyT> |
| 267 | basic_istream<_CharT, _Traits>& |
| 268 | operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x) |
| 269 | { |
| 270 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 271 | try |
| 272 | { |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame^] | 273 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 274 | 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 Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame^] | 290 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 291 | return __is; |
| 292 | } |
| 293 | |
| 294 | template <class _MoneyT> |
| 295 | inline _LIBCPP_INLINE_VISIBILITY |
| 296 | __iom_t7<_MoneyT> |
| 297 | get_money(_MoneyT& __mon, bool __intl = false) |
| 298 | { |
| 299 | return __iom_t7<_MoneyT>(__mon, __intl); |
| 300 | } |
| 301 | |
| 302 | // put_money |
| 303 | |
| 304 | template <class _MoneyT> class __iom_t8; |
| 305 | |
| 306 | template <class _CharT, class _Traits, class _MoneyT> |
| 307 | basic_ostream<_CharT, _Traits>& |
| 308 | operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x); |
| 309 | |
| 310 | template <class _MoneyT> |
| 311 | class __iom_t8 |
| 312 | { |
| 313 | const _MoneyT& __mon_; |
| 314 | bool __intl_; |
| 315 | public: |
| 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 | |
| 325 | template <class _CharT, class _Traits, class _MoneyT> |
| 326 | basic_ostream<_CharT, _Traits>& |
| 327 | operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x) |
| 328 | { |
| 329 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 330 | try |
| 331 | { |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame^] | 332 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 333 | 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 Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame^] | 348 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 349 | return __os; |
| 350 | } |
| 351 | |
| 352 | template <class _MoneyT> |
| 353 | inline _LIBCPP_INLINE_VISIBILITY |
| 354 | __iom_t8<_MoneyT> |
| 355 | put_money(const _MoneyT& __mon, bool __intl = false) |
| 356 | { |
| 357 | return __iom_t8<_MoneyT>(__mon, __intl); |
| 358 | } |
| 359 | |
| 360 | // get_time |
| 361 | |
| 362 | template <class _CharT> class __iom_t9; |
| 363 | |
| 364 | template <class _CharT, class _Traits> |
| 365 | basic_istream<_CharT, _Traits>& |
| 366 | operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x); |
| 367 | |
| 368 | template <class _CharT> |
| 369 | class __iom_t9 |
| 370 | { |
| 371 | tm* __tm_; |
| 372 | const _CharT* __fmt_; |
| 373 | public: |
| 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 | |
| 383 | template <class _CharT, class _Traits> |
| 384 | basic_istream<_CharT, _Traits>& |
| 385 | operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x) |
| 386 | { |
| 387 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 388 | try |
| 389 | { |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame^] | 390 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 391 | 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 Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame^] | 408 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 409 | return __is; |
| 410 | } |
| 411 | |
| 412 | template <class _CharT> |
| 413 | inline _LIBCPP_INLINE_VISIBILITY |
| 414 | __iom_t9<_CharT> |
| 415 | get_time(tm* __tm, const _CharT* __fmt) |
| 416 | { |
| 417 | return __iom_t9<_CharT>(__tm, __fmt); |
| 418 | } |
| 419 | |
| 420 | // put_time |
| 421 | |
| 422 | template <class _CharT> class __iom_t10; |
| 423 | |
| 424 | template <class _CharT, class _Traits> |
| 425 | basic_ostream<_CharT, _Traits>& |
| 426 | operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x); |
| 427 | |
| 428 | template <class _CharT> |
| 429 | class __iom_t10 |
| 430 | { |
| 431 | const tm* __tm_; |
| 432 | const _CharT* __fmt_; |
| 433 | public: |
| 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 | |
| 443 | template <class _CharT, class _Traits> |
| 444 | basic_ostream<_CharT, _Traits>& |
| 445 | operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x) |
| 446 | { |
| 447 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 448 | try |
| 449 | { |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame^] | 450 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 451 | 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 Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame^] | 467 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 468 | return __os; |
| 469 | } |
| 470 | |
| 471 | template <class _CharT> |
| 472 | inline _LIBCPP_INLINE_VISIBILITY |
| 473 | __iom_t10<_CharT> |
| 474 | put_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 |