1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <istream> 11 12 // template <class charT, class traits = char_traits<charT> > 13 // class basic_istream; 14 15 // basic_istream<charT,traits>& operator<<(basic_streambuf<charT,traits>* sb); 16 17 #include <istream> 18 #include <cassert> 19 20 template <class CharT> 21 class testbuf 22 : public std::basic_streambuf<CharT> 23 { 24 typedef std::basic_streambuf<CharT> base; 25 std::basic_string<CharT> str_; 26 public: 27 testbuf() 28 { 29 } 30 testbuf(const std::basic_string<CharT>& str) 31 : str_(str) 32 { 33 base::setg(const_cast<CharT*>(str_.data()), 34 const_cast<CharT*>(str_.data()), 35 const_cast<CharT*>(str_.data() + str_.size())); 36 } 37 38 std::basic_string<CharT> str() const 39 {return std::basic_string<CharT>(base::pbase(), base::pptr());} 40 41 protected: 42 43 virtual typename base::int_type 44 overflow(typename base::int_type __c = base::traits_type::eof()) 45 { 46 if (__c != base::traits_type::eof()) 47 { 48 int n = str_.size(); 49 str_.push_back(__c); 50 str_.resize(str_.capacity()); 51 base::setp(const_cast<CharT*>(str_.data()), 52 const_cast<CharT*>(str_.data() + str_.size())); 53 base::pbump(n+1); 54 } 55 return __c; 56 } 57 }; 58 59 int main() 60 { 61 { 62 testbuf<char> sb("testing..."); 63 std::istream is(&sb); 64 testbuf<char> sb2; 65 is >> &sb2; 66 assert(sb2.str() == "testing..."); 67 assert(is.gcount() == 10); 68 } 69 } 70