1 // -*- C++ -*- 2 //===--------------------------- iomanip ----------------------------------===// 3 // 4 // The LLVM Compiler Infrastructure 5 // 6 // This file is dual licensed under the MIT and the University of Illinois Open 7 // Source Licenses. 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 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 37 #pragma GCC system_header 38 #endif 39 40 _LIBCPP_BEGIN_NAMESPACE_STD 41 42 // resetiosflags 43 44 class __iom_t1 45 { 46 ios_base::fmtflags __mask_; 47 public: 48 _LIBCPP_INLINE_VISIBILITY 49 explicit __iom_t1(ios_base::fmtflags __m) : __mask_(__m) {} 50 51 template <class _CharT, class _Traits> 52 friend 53 _LIBCPP_INLINE_VISIBILITY 54 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 63 _LIBCPP_INLINE_VISIBILITY 64 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 72 inline _LIBCPP_INLINE_VISIBILITY 73 __iom_t1 74 resetiosflags(ios_base::fmtflags __mask) 75 { 76 return __iom_t1(__mask); 77 } 78 79 // setiosflags 80 81 class __iom_t2 82 { 83 ios_base::fmtflags __mask_; 84 public: 85 _LIBCPP_INLINE_VISIBILITY 86 explicit __iom_t2(ios_base::fmtflags __m) : __mask_(__m) {} 87 88 template <class _CharT, class _Traits> 89 friend 90 _LIBCPP_INLINE_VISIBILITY 91 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 100 _LIBCPP_INLINE_VISIBILITY 101 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 109 inline _LIBCPP_INLINE_VISIBILITY 110 __iom_t2 111 setiosflags(ios_base::fmtflags __mask) 112 { 113 return __iom_t2(__mask); 114 } 115 116 // setbase 117 118 class __iom_t3 119 { 120 int __base_; 121 public: 122 _LIBCPP_INLINE_VISIBILITY 123 explicit __iom_t3(int __b) : __base_(__b) {} 124 125 template <class _CharT, class _Traits> 126 friend 127 _LIBCPP_INLINE_VISIBILITY 128 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 140 _LIBCPP_INLINE_VISIBILITY 141 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 152 inline _LIBCPP_INLINE_VISIBILITY 153 __iom_t3 154 setbase(int __base) 155 { 156 return __iom_t3(__base); 157 } 158 159 // setfill 160 161 template<class _CharT> 162 class __iom_t4 163 { 164 _CharT __fill_; 165 public: 166 _LIBCPP_INLINE_VISIBILITY 167 explicit __iom_t4(_CharT __c) : __fill_(__c) {} 168 169 template <class _Traits> 170 friend 171 _LIBCPP_INLINE_VISIBILITY 172 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 180 template<class _CharT> 181 inline _LIBCPP_INLINE_VISIBILITY 182 __iom_t4<_CharT> 183 setfill(_CharT __c) 184 { 185 return __iom_t4<_CharT>(__c); 186 } 187 188 // setprecision 189 190 class __iom_t5 191 { 192 int __n_; 193 public: 194 _LIBCPP_INLINE_VISIBILITY 195 explicit __iom_t5(int __n) : __n_(__n) {} 196 197 template <class _CharT, class _Traits> 198 friend 199 _LIBCPP_INLINE_VISIBILITY 200 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 209 _LIBCPP_INLINE_VISIBILITY 210 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 218 inline _LIBCPP_INLINE_VISIBILITY 219 __iom_t5 220 setprecision(int __n) 221 { 222 return __iom_t5(__n); 223 } 224 225 // setw 226 227 class __iom_t6 228 { 229 int __n_; 230 public: 231 _LIBCPP_INLINE_VISIBILITY 232 explicit __iom_t6(int __n) : __n_(__n) {} 233 234 template <class _CharT, class _Traits> 235 friend 236 _LIBCPP_INLINE_VISIBILITY 237 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 246 _LIBCPP_INLINE_VISIBILITY 247 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 255 inline _LIBCPP_INLINE_VISIBILITY 256 __iom_t6 257 setw(int __n) 258 { 259 return __iom_t6(__n); 260 } 261 262 // get_money 263 264 template <class _MoneyT> class __iom_t7; 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 template <class _MoneyT> 271 class __iom_t7 272 { 273 _MoneyT& __mon_; 274 bool __intl_; 275 public: 276 _LIBCPP_INLINE_VISIBILITY 277 __iom_t7(_MoneyT& __mon, bool __intl) 278 : __mon_(__mon), __intl_(__intl) {} 279 280 template <class _CharT, class _Traits, class _Mp> 281 friend 282 basic_istream<_CharT, _Traits>& 283 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_Mp>& __x); 284 }; 285 286 template <class _CharT, class _Traits, class _MoneyT> 287 basic_istream<_CharT, _Traits>& 288 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x) 289 { 290 #ifndef _LIBCPP_NO_EXCEPTIONS 291 try 292 { 293 #endif // _LIBCPP_NO_EXCEPTIONS 294 typename basic_istream<_CharT, _Traits>::sentry __s(__is); 295 if (__s) 296 { 297 typedef istreambuf_iterator<_CharT, _Traits> _Ip; 298 typedef money_get<_CharT, _Ip> _Fp; 299 ios_base::iostate __err = ios_base::goodbit; 300 const _Fp& __mf = use_facet<_Fp>(__is.getloc()); 301 __mf.get(_Ip(__is), _Ip(), __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 } 310 #endif // _LIBCPP_NO_EXCEPTIONS 311 return __is; 312 } 313 314 template <class _MoneyT> 315 inline _LIBCPP_INLINE_VISIBILITY 316 __iom_t7<_MoneyT> 317 get_money(_MoneyT& __mon, bool __intl = false) 318 { 319 return __iom_t7<_MoneyT>(__mon, __intl); 320 } 321 322 // put_money 323 324 template <class _MoneyT> class __iom_t8; 325 326 template <class _CharT, class _Traits, class _MoneyT> 327 basic_ostream<_CharT, _Traits>& 328 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x); 329 330 template <class _MoneyT> 331 class __iom_t8 332 { 333 const _MoneyT& __mon_; 334 bool __intl_; 335 public: 336 _LIBCPP_INLINE_VISIBILITY 337 __iom_t8(const _MoneyT& __mon, bool __intl) 338 : __mon_(__mon), __intl_(__intl) {} 339 340 template <class _CharT, class _Traits, class _Mp> 341 friend 342 basic_ostream<_CharT, _Traits>& 343 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_Mp>& __x); 344 }; 345 346 template <class _CharT, class _Traits, class _MoneyT> 347 basic_ostream<_CharT, _Traits>& 348 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x) 349 { 350 #ifndef _LIBCPP_NO_EXCEPTIONS 351 try 352 { 353 #endif // _LIBCPP_NO_EXCEPTIONS 354 typename basic_ostream<_CharT, _Traits>::sentry __s(__os); 355 if (__s) 356 { 357 typedef ostreambuf_iterator<_CharT, _Traits> _Op; 358 typedef money_put<_CharT, _Op> _Fp; 359 const _Fp& __mf = use_facet<_Fp>(__os.getloc()); 360 if (__mf.put(_Op(__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 } 369 #endif // _LIBCPP_NO_EXCEPTIONS 370 return __os; 371 } 372 373 template <class _MoneyT> 374 inline _LIBCPP_INLINE_VISIBILITY 375 __iom_t8<_MoneyT> 376 put_money(const _MoneyT& __mon, bool __intl = false) 377 { 378 return __iom_t8<_MoneyT>(__mon, __intl); 379 } 380 381 // get_time 382 383 template <class _CharT> class __iom_t9; 384 385 template <class _CharT, class _Traits> 386 basic_istream<_CharT, _Traits>& 387 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x); 388 389 template <class _CharT> 390 class __iom_t9 391 { 392 tm* __tm_; 393 const _CharT* __fmt_; 394 public: 395 _LIBCPP_INLINE_VISIBILITY 396 __iom_t9(tm* __tm, const _CharT* __fmt) 397 : __tm_(__tm), __fmt_(__fmt) {} 398 399 template <class _Cp, class _Traits> 400 friend 401 basic_istream<_Cp, _Traits>& 402 operator>>(basic_istream<_Cp, _Traits>& __is, const __iom_t9<_Cp>& __x); 403 }; 404 405 template <class _CharT, class _Traits> 406 basic_istream<_CharT, _Traits>& 407 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x) 408 { 409 #ifndef _LIBCPP_NO_EXCEPTIONS 410 try 411 { 412 #endif // _LIBCPP_NO_EXCEPTIONS 413 typename basic_istream<_CharT, _Traits>::sentry __s(__is); 414 if (__s) 415 { 416 typedef istreambuf_iterator<_CharT, _Traits> _Ip; 417 typedef time_get<_CharT, _Ip> _Fp; 418 ios_base::iostate __err = ios_base::goodbit; 419 const _Fp& __tf = use_facet<_Fp>(__is.getloc()); 420 __tf.get(_Ip(__is), _Ip(), __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 } 430 #endif // _LIBCPP_NO_EXCEPTIONS 431 return __is; 432 } 433 434 template <class _CharT> 435 inline _LIBCPP_INLINE_VISIBILITY 436 __iom_t9<_CharT> 437 get_time(tm* __tm, const _CharT* __fmt) 438 { 439 return __iom_t9<_CharT>(__tm, __fmt); 440 } 441 442 // put_time 443 444 template <class _CharT> class __iom_t10; 445 446 template <class _CharT, class _Traits> 447 basic_ostream<_CharT, _Traits>& 448 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x); 449 450 template <class _CharT> 451 class __iom_t10 452 { 453 const tm* __tm_; 454 const _CharT* __fmt_; 455 public: 456 _LIBCPP_INLINE_VISIBILITY 457 __iom_t10(const tm* __tm, const _CharT* __fmt) 458 : __tm_(__tm), __fmt_(__fmt) {} 459 460 template <class _Cp, class _Traits> 461 friend 462 basic_ostream<_Cp, _Traits>& 463 operator<<(basic_ostream<_Cp, _Traits>& __os, const __iom_t10<_Cp>& __x); 464 }; 465 466 template <class _CharT, class _Traits> 467 basic_ostream<_CharT, _Traits>& 468 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x) 469 { 470 #ifndef _LIBCPP_NO_EXCEPTIONS 471 try 472 { 473 #endif // _LIBCPP_NO_EXCEPTIONS 474 typename basic_ostream<_CharT, _Traits>::sentry __s(__os); 475 if (__s) 476 { 477 typedef ostreambuf_iterator<_CharT, _Traits> _Op; 478 typedef time_put<_CharT, _Op> _Fp; 479 const _Fp& __tf = use_facet<_Fp>(__os.getloc()); 480 if (__tf.put(_Op(__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 } 490 #endif // _LIBCPP_NO_EXCEPTIONS 491 return __os; 492 } 493 494 template <class _CharT> 495 inline _LIBCPP_INLINE_VISIBILITY 496 __iom_t10<_CharT> 497 put_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 505