xref: /minix3/external/bsd/libc++/dist/libcxx/src/iostream.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
14684ddb6SLionel Sambuc //===------------------------ iostream.cpp --------------------------------===//
24684ddb6SLionel Sambuc //
34684ddb6SLionel Sambuc //                     The LLVM Compiler Infrastructure
44684ddb6SLionel Sambuc //
54684ddb6SLionel Sambuc // This file is dual licensed under the MIT and the University of Illinois Open
64684ddb6SLionel Sambuc // Source Licenses. See LICENSE.TXT for details.
74684ddb6SLionel Sambuc //
84684ddb6SLionel Sambuc //===----------------------------------------------------------------------===//
94684ddb6SLionel Sambuc 
104684ddb6SLionel Sambuc #include "__std_stream"
114684ddb6SLionel Sambuc #include "string"
124684ddb6SLionel Sambuc #include "new"
134684ddb6SLionel Sambuc 
144684ddb6SLionel Sambuc _LIBCPP_BEGIN_NAMESPACE_STD
154684ddb6SLionel Sambuc 
16*0a6a1f1dSLionel Sambuc #ifndef _LIBCPP_HAS_NO_STDIN
174684ddb6SLionel Sambuc _ALIGNAS_TYPE (istream)  _LIBCPP_FUNC_VIS char cin [sizeof(istream)];
18*0a6a1f1dSLionel Sambuc _ALIGNAS_TYPE (__stdinbuf<char> ) static char __cin [sizeof(__stdinbuf <char>)];
19*0a6a1f1dSLionel Sambuc static mbstate_t mb_cin;
204684ddb6SLionel Sambuc _ALIGNAS_TYPE (wistream) _LIBCPP_FUNC_VIS char wcin [sizeof(wistream)];
21*0a6a1f1dSLionel Sambuc _ALIGNAS_TYPE (__stdinbuf<wchar_t> ) static char __wcin [sizeof(__stdinbuf <wchar_t>)];
22*0a6a1f1dSLionel Sambuc static mbstate_t mb_wcin;
23*0a6a1f1dSLionel Sambuc #endif
24*0a6a1f1dSLionel Sambuc 
25*0a6a1f1dSLionel Sambuc #ifndef _LIBCPP_HAS_NO_STDOUT
26*0a6a1f1dSLionel Sambuc _ALIGNAS_TYPE (ostream)  _LIBCPP_FUNC_VIS char cout[sizeof(ostream)];
27*0a6a1f1dSLionel Sambuc _ALIGNAS_TYPE (__stdoutbuf<char>) static char __cout[sizeof(__stdoutbuf<char>)];
28*0a6a1f1dSLionel Sambuc static mbstate_t mb_cout;
294684ddb6SLionel Sambuc _ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wcout[sizeof(wostream)];
30*0a6a1f1dSLionel Sambuc _ALIGNAS_TYPE (__stdoutbuf<wchar_t>) static char __wcout[sizeof(__stdoutbuf<wchar_t>)];
31*0a6a1f1dSLionel Sambuc static mbstate_t mb_wcout;
32*0a6a1f1dSLionel Sambuc #endif
33*0a6a1f1dSLionel Sambuc 
34*0a6a1f1dSLionel Sambuc _ALIGNAS_TYPE (ostream)  _LIBCPP_FUNC_VIS char cerr[sizeof(ostream)];
35*0a6a1f1dSLionel Sambuc _ALIGNAS_TYPE (__stdoutbuf<char>) static char __cerr[sizeof(__stdoutbuf<char>)];
36*0a6a1f1dSLionel Sambuc static mbstate_t mb_cerr;
374684ddb6SLionel Sambuc _ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wcerr[sizeof(wostream)];
38*0a6a1f1dSLionel Sambuc _ALIGNAS_TYPE (__stdoutbuf<wchar_t>) static char __wcerr[sizeof(__stdoutbuf<wchar_t>)];
39*0a6a1f1dSLionel Sambuc static mbstate_t mb_wcerr;
40*0a6a1f1dSLionel Sambuc 
41*0a6a1f1dSLionel Sambuc _ALIGNAS_TYPE (ostream)  _LIBCPP_FUNC_VIS char clog[sizeof(ostream)];
424684ddb6SLionel Sambuc _ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wclog[sizeof(wostream)];
434684ddb6SLionel Sambuc 
444684ddb6SLionel Sambuc ios_base::Init __start_std_streams;
454684ddb6SLionel Sambuc 
Init()464684ddb6SLionel Sambuc ios_base::Init::Init()
474684ddb6SLionel Sambuc {
48*0a6a1f1dSLionel Sambuc #ifndef _LIBCPP_HAS_NO_STDIN
49*0a6a1f1dSLionel Sambuc     istream* cin_ptr  = ::new(cin)  istream(::new(__cin)  __stdinbuf <char>(stdin, &mb_cin));
50*0a6a1f1dSLionel Sambuc     wistream* wcin_ptr  = ::new(wcin)  wistream(::new(__wcin)  __stdinbuf <wchar_t>(stdin, &mb_wcin));
51*0a6a1f1dSLionel Sambuc #endif
52*0a6a1f1dSLionel Sambuc #ifndef _LIBCPP_HAS_NO_STDOUT
53*0a6a1f1dSLionel Sambuc     ostream* cout_ptr = ::new(cout) ostream(::new(__cout) __stdoutbuf<char>(stdout, &mb_cout));
54*0a6a1f1dSLionel Sambuc     wostream* wcout_ptr = ::new(wcout) wostream(::new(__wcout) __stdoutbuf<wchar_t>(stdout, &mb_wcout));
55*0a6a1f1dSLionel Sambuc #endif
56*0a6a1f1dSLionel Sambuc     ostream* cerr_ptr = ::new(cerr) ostream(::new(__cerr) __stdoutbuf<char>(stderr, &mb_cerr));
574684ddb6SLionel Sambuc                         ::new(clog) ostream(cerr_ptr->rdbuf());
58*0a6a1f1dSLionel Sambuc     wostream* wcerr_ptr = ::new(wcerr) wostream(::new(__wcerr) __stdoutbuf<wchar_t>(stderr, &mb_wcerr));
594684ddb6SLionel Sambuc                           ::new(wclog) wostream(wcerr_ptr->rdbuf());
60*0a6a1f1dSLionel Sambuc 
61*0a6a1f1dSLionel Sambuc #if !defined(_LIBCPP_HAS_NO_STDIN) && !defined(_LIBCPP_HAS_NO_STDOUT)
62*0a6a1f1dSLionel Sambuc     cin_ptr->tie(cout_ptr);
634684ddb6SLionel Sambuc     wcin_ptr->tie(wcout_ptr);
64*0a6a1f1dSLionel Sambuc #endif
65*0a6a1f1dSLionel Sambuc     _VSTD::unitbuf(*cerr_ptr);
664684ddb6SLionel Sambuc     _VSTD::unitbuf(*wcerr_ptr);
67*0a6a1f1dSLionel Sambuc #ifndef _LIBCPP_HAS_NO_STDOUT
68*0a6a1f1dSLionel Sambuc     cerr_ptr->tie(cout_ptr);
694684ddb6SLionel Sambuc     wcerr_ptr->tie(wcout_ptr);
70*0a6a1f1dSLionel Sambuc #endif
714684ddb6SLionel Sambuc }
724684ddb6SLionel Sambuc 
~Init()734684ddb6SLionel Sambuc ios_base::Init::~Init()
744684ddb6SLionel Sambuc {
75*0a6a1f1dSLionel Sambuc #ifndef _LIBCPP_HAS_NO_STDOUT
764684ddb6SLionel Sambuc     ostream* cout_ptr = reinterpret_cast<ostream*>(cout);
774684ddb6SLionel Sambuc     wostream* wcout_ptr = reinterpret_cast<wostream*>(wcout);
78*0a6a1f1dSLionel Sambuc     cout_ptr->flush();
794684ddb6SLionel Sambuc     wcout_ptr->flush();
80*0a6a1f1dSLionel Sambuc #endif
81*0a6a1f1dSLionel Sambuc 
82*0a6a1f1dSLionel Sambuc     ostream* clog_ptr = reinterpret_cast<ostream*>(clog);
83*0a6a1f1dSLionel Sambuc     wostream* wclog_ptr = reinterpret_cast<wostream*>(wclog);
84*0a6a1f1dSLionel Sambuc     clog_ptr->flush();
854684ddb6SLionel Sambuc     wclog_ptr->flush();
864684ddb6SLionel Sambuc }
874684ddb6SLionel Sambuc 
884684ddb6SLionel Sambuc _LIBCPP_END_NAMESPACE_STD
89