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 // <ostream> 11 12 // template <class charT, class traits = char_traits<charT> > 13 // class basic_ostream; 14 15 // basic_ostream<charT,traits>& put(char_type c); 16 17 #include <ostream> 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: testbuf()27 testbuf() 28 { 29 } 30 str() const31 std::basic_string<CharT> str() const 32 {return std::basic_string<CharT>(base::pbase(), base::pptr());} 33 34 protected: 35 36 virtual typename base::int_type overflow(typename base::int_type __c=base::traits_type::eof ())37 overflow(typename base::int_type __c = base::traits_type::eof()) 38 { 39 if (__c != base::traits_type::eof()) 40 { 41 int n = str_.size(); 42 str_.push_back(__c); 43 str_.resize(str_.capacity()); 44 base::setp(const_cast<CharT*>(str_.data()), 45 const_cast<CharT*>(str_.data() + str_.size())); 46 base::pbump(n+1); 47 } 48 return __c; 49 } 50 }; 51 main()52int main() 53 { 54 { 55 std::wostream os((std::wstreambuf*)0); 56 wchar_t c = L'a'; 57 os.put(c); 58 assert(os.bad()); 59 } 60 { 61 testbuf<wchar_t> sb; 62 std::wostream os(&sb); 63 wchar_t c = L'a'; 64 os.put(c); 65 assert(sb.str() == L"a"); 66 assert(os.good()); 67 } 68 { 69 testbuf<char> sb; 70 std::ostream os(&sb); 71 char c = 'a'; 72 os.put(c); 73 assert(sb.str() == "a"); 74 assert(os.good()); 75 } 76 } 77