Changeset bed3d11 in mainline
- Timestamp:
- 2018-07-05T21:41:19Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 25cc4a5
- Parents:
- f5a77a00
- git-author:
- Dzejrou <dzejrou@…> (2017-12-17 14:58:57)
- git-committer:
- Dzejrou <dzejrou@…> (2018-07-05 21:41:19)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/cpp/include/impl/ostream.hpp
rf5a77a00 rbed3d11 135 135 basic_ostream<Char, Traits>& operator<<(bool x) 136 136 { 137 // TODO: sentry etc 138 /* bool failed = use_facet< */ 139 /* num_put<char_type, ostreambuf_iterator<char_type, traits_type>> */ 140 /* >(this->getloc()).put(*this, *this, this->fill(), x).failed(); */ 141 142 /* if (failed) */ 143 /* this->setstate(ios_base::badbit); */ 137 sentry sen{*this}; 138 139 if (sen) 140 { 141 bool failed = use_facet< 142 num_put<char_type, ostreambuf_iterator<char_type, traits_type>> 143 >(this->getloc()).put(*this, *this, this->fill(), x).failed(); 144 145 if (failed) 146 this->setstate(ios_base::badbit); 147 } 144 148 145 149 return *this; … … 148 152 basic_ostream<Char, Traits>& operator<<(short x) 149 153 { 150 // TODO: implement 154 sentry sen{*this}; 155 156 if (sen) 157 { 158 auto basefield = (this->flags() & ios_base::basefield); 159 bool failed = use_facet< 160 num_put<char_type, ostreambuf_iterator<char_type, traits_type>> 161 >(this->getloc()).put(*this, *this, this->fill(), 162 (basefield == ios_base::oct || basefield == ios_base::hex) 163 ? static_cast<long>(static_cast<unsigned short>(x)) 164 : static_cast<long>(x)).failed(); 165 166 if (failed) 167 this->setstate(ios_base::badbit); 168 } 169 151 170 return *this; 152 171 } … … 154 173 basic_ostream<Char, Traits>& operator<<(unsigned short x) 155 174 { 156 // TODO: implement 175 sentry sen{*this}; 176 177 if (sen) 178 { 179 bool failed = use_facet< 180 num_put<char_type, ostreambuf_iterator<char_type, traits_type>> 181 >(this->getloc()).put(*this, *this, this->fill(), 182 static_cast<unsigned long>(x)).failed(); 183 184 if (failed) 185 this->setstate(ios_base::badbit); 186 } 187 157 188 return *this; 158 189 } … … 160 191 basic_ostream<Char, Traits>& operator<<(int x) 161 192 { 162 // TODO: implement 193 sentry sen{*this}; 194 195 if (sen) 196 { 197 auto basefield = (this->flags() & ios_base::basefield); 198 bool failed = use_facet< 199 num_put<char_type, ostreambuf_iterator<char_type, traits_type>> 200 >(this->getloc()).put(*this, *this, this->fill(), 201 (basefield == ios_base::oct || basefield == ios_base::hex) 202 ? static_cast<long>(static_cast<unsigned int>(x)) 203 : static_cast<long>(x)).failed(); 204 205 if (failed) 206 this->setstate(ios_base::badbit); 207 } 208 163 209 return *this; 164 210 } … … 166 212 basic_ostream<Char, Traits>& operator<<(unsigned int x) 167 213 { 168 // TODO: implement 214 sentry sen{*this}; 215 216 if (sen) 217 { 218 bool failed = use_facet< 219 num_put<char_type, ostreambuf_iterator<char_type, traits_type>> 220 >(this->getloc()).put(*this, *this, this->fill(), 221 static_cast<unsigned long>(x)).failed(); 222 223 if (failed) 224 this->setstate(ios_base::badbit); 225 } 226 169 227 return *this; 170 228 } … … 172 230 basic_ostream<Char, Traits>& operator<<(long x) 173 231 { 174 // TODO: implement 232 sentry sen{*this}; 233 234 if (sen) 235 { 236 bool failed = use_facet< 237 num_put<char_type, ostreambuf_iterator<char_type, traits_type>> 238 >(this->getloc()).put(*this, *this, this->fill(), x).failed(); 239 240 if (failed) 241 this->setstate(ios_base::badbit); 242 } 243 175 244 return *this; 176 245 } … … 178 247 basic_ostream<Char, Traits>& operator<<(unsigned long x) 179 248 { 180 // TODO: implement 249 sentry sen{*this}; 250 251 if (sen) 252 { 253 bool failed = use_facet< 254 num_put<char_type, ostreambuf_iterator<char_type, traits_type>> 255 >(this->getloc()).put(*this, *this, this->fill(), x).failed(); 256 257 if (failed) 258 this->setstate(ios_base::badbit); 259 } 260 181 261 return *this; 182 262 } … … 184 264 basic_ostream<Char, Traits>& operator<<(long long x) 185 265 { 186 // TODO: implement 266 sentry sen{*this}; 267 268 if (sen) 269 { 270 bool failed = use_facet< 271 num_put<char_type, ostreambuf_iterator<char_type, traits_type>> 272 >(this->getloc()).put(*this, *this, this->fill(), x).failed(); 273 274 if (failed) 275 this->setstate(ios_base::badbit); 276 } 277 187 278 return *this; 188 279 } … … 190 281 basic_ostream<Char, Traits>& operator<<(unsigned long long x) 191 282 { 192 // TODO: implement 283 sentry sen{*this}; 284 285 if (sen) 286 { 287 bool failed = use_facet< 288 num_put<char_type, ostreambuf_iterator<char_type, traits_type>> 289 >(this->getloc()).put(*this, *this, this->fill(), x).failed(); 290 291 if (failed) 292 this->setstate(ios_base::badbit); 293 } 294 193 295 return *this; 194 296 } … … 196 298 basic_ostream<Char, Traits>& operator<<(float x) 197 299 { 198 // TODO: implement 300 sentry sen{*this}; 301 302 if (sen) 303 { 304 bool failed = use_facet< 305 num_put<char_type, ostreambuf_iterator<char_type, traits_type>> 306 >(this->getloc()).put(*this, *this, this->fill(), static_cast<double>(x)).failed(); 307 308 if (failed) 309 this->setstate(ios_base::badbit); 310 } 311 199 312 return *this; 200 313 } … … 202 315 basic_ostream<Char, Traits>& operator<<(double x) 203 316 { 204 // TODO: implement 317 sentry sen{*this}; 318 319 if (sen) 320 { 321 bool failed = use_facet< 322 num_put<char_type, ostreambuf_iterator<char_type, traits_type>> 323 >(this->getloc()).put(*this, *this, this->fill(), x).failed(); 324 325 if (failed) 326 this->setstate(ios_base::badbit); 327 } 328 205 329 return *this; 206 330 } … … 208 332 basic_ostream<Char, Traits>& operator<<(long double x) 209 333 { 210 // TODO: implement 334 sentry sen{*this}; 335 336 if (sen) 337 { 338 bool failed = use_facet< 339 num_put<char_type, ostreambuf_iterator<char_type, traits_type>> 340 >(this->getloc()).put(*this, *this, this->fill(), x).failed(); 341 342 if (failed) 343 this->setstate(ios_base::badbit); 344 } 345 211 346 return *this; 212 347 } … … 214 349 basic_ostream<Char, Traits>& operator<<(const void* p) 215 350 { 216 // TODO: implement 351 sentry sen{*this}; 352 353 if (sen) 354 { 355 bool failed = use_facet< 356 num_put<char_type, ostreambuf_iterator<char_type, traits_type>> 357 >(this->getloc()).put(*this, *this, this->fill(), p).failed(); 358 359 if (failed) 360 this->setstate(ios_base::badbit); 361 } 362 217 363 return *this; 218 364 } … … 220 366 basic_ostream<Char, Traits>& operator<<(basic_streambuf<Char, Traits>* sb) 221 367 { 222 // TODO: implement 368 if (!sb) 369 return *this; 370 371 sentry sen{*this}; 372 373 if (sen) 374 { 375 size_t count{}; 376 377 int_type c = sb->sgetc(); 378 while (!traits_type::eq_int_type(c, traits_type::eof())) 379 { 380 this->put(traits_type::to_char_type(c)); 381 382 if (!(*this)) 383 break; 384 385 ++count; 386 sb->sbumpc(); 387 c = sb->sgetc(); 388 } 389 390 if (count == 0) 391 this->setstate(ios_base::failbit); 392 } 393 223 394 return *this; 224 395 } … … 332 503 using wostream = basic_ostream<wchar_t>; 333 504 505 /** 506 * 27.7.6.3.4, character inserter function templates: 507 */ 508 509 template<class Char, class Traits> 510 basic_ostream<Char, Traits>& operator<<(basic_ostream<Char, Traits>& os, 511 Char c) 512 { 513 typename basic_ostream<Char, Traits>::sentry sen{os}; 514 515 if (sen) 516 { 517 if (os.width() > 0) 518 { 519 if ((os.flags() & ios_base::adjustfield) != ios_base::left) 520 { 521 for (decltype(os.width()) i = 0; i < os.width(); ++i) 522 os.put(os.fill()); 523 os.put(c); 524 } 525 else 526 { 527 os.put(c); 528 for (decltype(os.width()) i = 0; i < os.width(); ++i) 529 os.put(os.fill()); 530 } 531 } 532 else 533 os.put(c); 534 535 os.width(0); 536 } 537 538 return os; 539 } 540 541 template<class Char, class Traits> 542 basic_ostream<Char, Traits>& operator<<(basic_ostream<Char, Traits>& os, 543 char c) 544 { 545 typename basic_ostream<Char, Traits>::sentry sen{os}; 546 547 if (sen) 548 { 549 if (os.width() > 0) 550 { 551 if ((os.flags() & ios_base::adjustfield) != ios_base::left) 552 { 553 for (decltype(os.width()) i = 0; i < os.width(); ++i) 554 os.put(os.fill()); 555 os.put(os.widen(c)); 556 } 557 else 558 { 559 os.put(os.widen(c)); 560 for (decltype(os.width()) i = 0; i < os.width(); ++i) 561 os.put(os.fill()); 562 } 563 } 564 else 565 os.put(os.widen(c)); 566 567 os.width(0); 568 } 569 return os; 570 } 571 572 template<class Traits> 573 basic_ostream<char, Traits>& operator<<(basic_ostream<char, Traits>& os, 574 char c) 575 { 576 typename basic_ostream<char, Traits>::sentry sen{os}; 577 578 if (sen) 579 { 580 if (os.width() > 0) 581 { 582 if ((os.flags() & ios_base::adjustfield) != ios_base::left) 583 { 584 for (decltype(os.width()) i = 0; i < os.width(); ++i) 585 os.put(os.fill()); 586 os.put(c); 587 } 588 else 589 { 590 os.put(c); 591 for (decltype(os.width()) i = 0; i < os.width(); ++i) 592 os.put(os.fill()); 593 } 594 } 595 else 596 os.put(c); 597 598 os.width(0); 599 } 600 601 return os; 602 } 603 604 template<class Traits> 605 basic_ostream<char, Traits>& operator<<(basic_ostream<char, Traits>& os, 606 signed char c) 607 { 608 typename basic_ostream<char, Traits>::sentry sen{os}; 609 610 if (sen) 611 { 612 if (os.width() > 0) 613 { 614 if ((os.flags() & ios_base::adjustfield) != ios_base::left) 615 { 616 for (decltype(os.width()) i = 0; i < os.width(); ++i) 617 os.put(os.fill()); 618 os.put(c); 619 } 620 else 621 { 622 os.put(c); 623 for (decltype(os.width()) i = 0; i < os.width(); ++i) 624 os.put(os.fill()); 625 } 626 } 627 else 628 os.put(c); 629 630 os.width(0); 631 } 632 633 return os; 634 } 635 636 template<class Traits> 637 basic_ostream<char, Traits>& operator<<(basic_ostream<char, Traits>& os, 638 unsigned char c) 639 { 640 typename basic_ostream<char, Traits>::sentry sen{os}; 641 642 if (sen) 643 { 644 if (os.width() > 0) 645 { 646 if ((os.flags() & ios_base::adjustfield) != ios_base::left) 647 { 648 for (decltype(os.width()) i = 0; i < os.width(); ++i) 649 os.put(os.fill()); 650 os.put(c); 651 } 652 else 653 { 654 os.put(c); 655 for (decltype(os.width()) i = 0; i < os.width(); ++i) 656 os.put(os.fill()); 657 } 658 } 659 else 660 os.put(c); 661 662 os.width(0); 663 } 664 665 return os; 666 } 667 668 namespace aux 669 { 670 template<class Char, class Traits> 671 basic_ostream<Char, Traits>& insert(basic_ostream<Char, Traits>& os, 672 const Char* str, size_t len) 673 { 674 if (os.width() > 0 && static_cast<size_t>(os.width()) > len) 675 { 676 size_t to_pad = (static_cast<size_t>(os.width()) - len); 677 678 if ((os.flags() & ios_base::adjustfield) != ios_base::left) 679 { 680 for (size_t i = 0; i < to_pad; ++i) 681 os.put(os.fill()); 682 for (size_t i = 0; i < len; ++i) 683 os.put(os.widen(str[i])); 684 } 685 else 686 { 687 for (size_t i = 0; i < len; ++i) 688 os.put(os.widen(str[i])); 689 for (size_t i = 0; i < to_pad; ++i) 690 os.put(os.fill()); 691 } 692 } 693 else 694 { 695 for (size_t i = 0; i < len; ++i) 696 os.put(os.widen(str[i])); 697 } 698 699 os.width(0); 700 return os; 701 } 702 } 703 704 template<class Char, class Traits> 705 basic_ostream<Char, Traits>& operator<<(basic_ostream<Char, Traits>& os, 706 const Char* str) 707 { 708 typename basic_ostream<Char, Traits>::sentry sen{os}; 709 710 auto len = Traits::length(str); 711 712 return aux::insert(os, str, len); 713 } 714 715 template<class Char, class Traits> 716 basic_ostream<Char, Traits>& operator<<(basic_ostream<Char, Traits>& os, 717 const char* str) 718 { 719 typename basic_ostream<Char, Traits>::sentry sen{os}; 720 721 auto len = std::char_traits<char>::length(str); 722 723 return aux::insert(os, str, len); 724 } 725 726 template<class Traits> 727 basic_ostream<char, Traits>& operator<<(basic_ostream<char, Traits>& os, 728 const char* str) 729 { 730 typename basic_ostream<char, Traits>::sentry sen{os}; 731 732 if (sen) 733 { 734 auto len = Traits::length(str); 735 736 return aux::insert(os, str, len); 737 } 738 else 739 return os; 740 } 741 742 template<class Traits> 743 basic_ostream<char, Traits>& operator<<(basic_ostream<char, Traits>& os, 744 const signed char* str) 745 { 746 typename basic_ostream<char, Traits>::sentry sen{os}; 747 748 if (sen) 749 { 750 auto len = Traits::length(reinterpret_cast<const char*>(str)); 751 752 return aux::insert(os, str, len); 753 } 754 else 755 return os; 756 } 757 758 template<class Traits> 759 basic_ostream<char, Traits>& operator<<(basic_ostream<char, Traits>& os, 760 const unsigned char* str) 761 { 762 typename basic_ostream<char, Traits>::sentry sen{os}; 763 764 if (sen) 765 { 766 auto len = Traits::length(reinterpret_cast<const char*>(str)); 767 768 return aux::insert(os, str, len); 769 } 770 else 771 return os; 772 } 773 774 /** 775 * 27.7.3.8, standard basic_ostream manipulators: 776 */ 777 334 778 template<class Char, class Traits = char_traits<Char>> 335 779 basic_ostream<Char, Traits>& endl(basic_ostream<Char, Traits>& os) 336 780 { 337 os.put( '\n');781 os.put(os.widen('\n')); 338 782 os.flush(); 339 783 … … 342 786 343 787 template<class Char, class Traits = char_traits<Char>> 344 basic_ostream<Char, Traits>& ends(basic_ostream<Char, Traits>& os); 788 basic_ostream<Char, Traits>& ends(basic_ostream<Char, Traits>& os) 789 { 790 os.put(Char{}); 791 792 return os; 793 } 345 794 346 795 template<class Char, class Traits = char_traits<Char>> 347 basic_ostream<Char, Traits>& flush(basic_ostream<Char, Traits>& os); 796 basic_ostream<Char, Traits>& flush(basic_ostream<Char, Traits>& os) 797 { 798 os.flush(); 799 800 return os; 801 } 348 802 349 803 template<class Char, class Traits = char_traits<Char>, class T> 350 basic_ostream<Char, Traits>& operator<<(basic_ostream<Char, Traits>& os, const T& x); 804 basic_ostream<Char, Traits>& operator<<(basic_ostream<Char, Traits>&& os, const T& x) 805 { 806 os << x; 807 808 return os; 809 } 351 810 } 352 811
Note:
See TracChangeset
for help on using the changeset viewer.