Home | History | Annotate | Download | only in src
      1 //===------------------------ iostream.cpp --------------------------------===//
      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 #include "__std_stream"
     11 #include "string"
     12 #include "new"
     13 
     14 _LIBCPP_BEGIN_NAMESPACE_STD
     15 
     16 #ifndef _LIBCPP_HAS_NO_STDIN
     17 _ALIGNAS_TYPE (istream)  _LIBCPP_FUNC_VIS char cin [sizeof(istream)];
     18 _ALIGNAS_TYPE (__stdinbuf<char> ) static char __cin [sizeof(__stdinbuf <char>)];
     19 static mbstate_t mb_cin;
     20 _ALIGNAS_TYPE (wistream) _LIBCPP_FUNC_VIS char wcin [sizeof(wistream)];
     21 _ALIGNAS_TYPE (__stdinbuf<wchar_t> ) static char __wcin [sizeof(__stdinbuf <wchar_t>)];
     22 static mbstate_t mb_wcin;
     23 #endif
     24 
     25 #ifndef _LIBCPP_HAS_NO_STDOUT
     26 _ALIGNAS_TYPE (ostream)  _LIBCPP_FUNC_VIS char cout[sizeof(ostream)];
     27 _ALIGNAS_TYPE (__stdoutbuf<char>) static char __cout[sizeof(__stdoutbuf<char>)];
     28 static mbstate_t mb_cout;
     29 _ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wcout[sizeof(wostream)];
     30 _ALIGNAS_TYPE (__stdoutbuf<wchar_t>) static char __wcout[sizeof(__stdoutbuf<wchar_t>)];
     31 static mbstate_t mb_wcout;
     32 #endif
     33 
     34 _ALIGNAS_TYPE (ostream)  _LIBCPP_FUNC_VIS char cerr[sizeof(ostream)];
     35 _ALIGNAS_TYPE (__stdoutbuf<char>) static char __cerr[sizeof(__stdoutbuf<char>)];
     36 static mbstate_t mb_cerr;
     37 _ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wcerr[sizeof(wostream)];
     38 _ALIGNAS_TYPE (__stdoutbuf<wchar_t>) static char __wcerr[sizeof(__stdoutbuf<wchar_t>)];
     39 static mbstate_t mb_wcerr;
     40 
     41 _ALIGNAS_TYPE (ostream)  _LIBCPP_FUNC_VIS char clog[sizeof(ostream)];
     42 _ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wclog[sizeof(wostream)];
     43 
     44 ios_base::Init __start_std_streams;
     45 
     46 ios_base::Init::Init()
     47 {
     48 #ifndef _LIBCPP_HAS_NO_STDIN
     49     istream* cin_ptr  = ::new(cin)  istream(::new(__cin)  __stdinbuf <char>(stdin, &mb_cin));
     50     wistream* wcin_ptr  = ::new(wcin)  wistream(::new(__wcin)  __stdinbuf <wchar_t>(stdin, &mb_wcin));
     51 #endif
     52 #ifndef _LIBCPP_HAS_NO_STDOUT
     53     ostream* cout_ptr = ::new(cout) ostream(::new(__cout) __stdoutbuf<char>(stdout, &mb_cout));
     54     wostream* wcout_ptr = ::new(wcout) wostream(::new(__wcout) __stdoutbuf<wchar_t>(stdout, &mb_wcout));
     55 #endif
     56     ostream* cerr_ptr = ::new(cerr) ostream(::new(__cerr) __stdoutbuf<char>(stderr, &mb_cerr));
     57                         ::new(clog) ostream(cerr_ptr->rdbuf());
     58     wostream* wcerr_ptr = ::new(wcerr) wostream(::new(__wcerr) __stdoutbuf<wchar_t>(stderr, &mb_wcerr));
     59                           ::new(wclog) wostream(wcerr_ptr->rdbuf());
     60 
     61 #if !defined(_LIBCPP_HAS_NO_STDIN) && !defined(_LIBCPP_HAS_NO_STDOUT)
     62     cin_ptr->tie(cout_ptr);
     63     wcin_ptr->tie(wcout_ptr);
     64 #endif
     65     _VSTD::unitbuf(*cerr_ptr);
     66     _VSTD::unitbuf(*wcerr_ptr);
     67 #ifndef _LIBCPP_HAS_NO_STDOUT
     68     cerr_ptr->tie(cout_ptr);
     69     wcerr_ptr->tie(wcout_ptr);
     70 #endif
     71 }
     72 
     73 ios_base::Init::~Init()
     74 {
     75 #ifndef _LIBCPP_HAS_NO_STDOUT
     76     ostream* cout_ptr = reinterpret_cast<ostream*>(cout);
     77     wostream* wcout_ptr = reinterpret_cast<wostream*>(wcout);
     78     cout_ptr->flush();
     79     wcout_ptr->flush();
     80 #endif
     81 
     82     ostream* clog_ptr = reinterpret_cast<ostream*>(clog);
     83     wostream* wclog_ptr = reinterpret_cast<wostream*>(wclog);
     84     clog_ptr->flush();
     85     wclog_ptr->flush();
     86 }
     87 
     88 _LIBCPP_END_NAMESPACE_STD
     89