Home | History | Annotate | Download | only in ostream_sentry
      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::sentry;
     14 
     15 // ~sentry();
     16 
     17 #include <ostream>
     18 #include <cassert>
     19 
     20 int sync_called = 0;
     21 
     22 template <class CharT>
     23 struct testbuf1
     24     : public std::basic_streambuf<CharT>
     25 {
     26     testbuf1() {}
     27 
     28 protected:
     29 
     30     int virtual sync()
     31     {
     32         ++sync_called;
     33         return 1;
     34     }
     35 };
     36 
     37 int main()
     38 {
     39     {
     40         std::ostream os((std::streambuf*)0);
     41         std::ostream::sentry s(os);
     42         assert(!bool(s));
     43     }
     44     assert(sync_called == 0);
     45     {
     46         testbuf1<char> sb;
     47         std::ostream os(&sb);
     48         std::ostream::sentry s(os);
     49         assert(bool(s));
     50     }
     51     assert(sync_called == 0);
     52     {
     53         testbuf1<char> sb;
     54         std::ostream os(&sb);
     55         std::ostream::sentry s(os);
     56         assert(bool(s));
     57         unitbuf(os);
     58     }
     59     assert(sync_called == 1);
     60     {
     61         testbuf1<char> sb;
     62         std::ostream os(&sb);
     63         try
     64         {
     65             std::ostream::sentry s(os);
     66             assert(bool(s));
     67             unitbuf(os);
     68             throw 1;
     69         }
     70         catch (...)
     71         {
     72         }
     73         assert(sync_called == 1);
     74     }
     75 }
     76