Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-19 09:25:00

0001 //
0002 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // Official repository: https://github.com/boostorg/beast
0008 //
0009 
0010 #ifndef BOOST_BEAST_UNIT_TEST_DSTREAM_HPP
0011 #define BOOST_BEAST_UNIT_TEST_DSTREAM_HPP
0012 
0013 #include <boost/config.hpp>
0014 #include <ios>
0015 #include <memory>
0016 #include <ostream>
0017 #include <sstream>
0018 #include <streambuf>
0019 #include <string>
0020 
0021 #ifdef BOOST_WINDOWS
0022 #include <boost/winapi/basic_types.hpp>
0023 #include <boost/winapi/debugapi.hpp>
0024 #endif
0025 
0026 namespace boost {
0027 namespace beast {
0028 namespace unit_test {
0029 
0030 #ifdef BOOST_WINDOWS
0031 
0032 namespace detail {
0033 
0034 template<class CharT, class Traits, class Allocator>
0035 class dstream_buf
0036     : public std::basic_stringbuf<CharT, Traits, Allocator>
0037 {
0038     using ostream = std::basic_ostream<CharT, Traits>;
0039 
0040     ostream& os_;
0041     bool dbg_;
0042 
0043     template<class T>
0044     void write(T const*) = delete;
0045 
0046     void write(char const* s)
0047     {
0048         if(dbg_)
0049             boost::winapi::OutputDebugStringA(s);
0050         os_ << s;
0051     }
0052 
0053     void write(wchar_t const* s)
0054     {
0055         if(dbg_)
0056             boost::winapi::OutputDebugStringW(s);
0057         os_ << s;
0058     }
0059 
0060 public:
0061     explicit
0062     dstream_buf(ostream& os)
0063         : os_(os)
0064         , dbg_(boost::winapi::IsDebuggerPresent() != 0)
0065     {
0066     }
0067 
0068     ~dstream_buf()
0069     {
0070         sync();
0071     }
0072 
0073     int
0074     sync() override
0075     {
0076         write(this->str().c_str());
0077         this->str("");
0078         return 0;
0079     }
0080 };
0081 
0082 } // detail
0083 
0084 /** std::ostream with Visual Studio IDE redirection.
0085 
0086     Instances of this stream wrap a specified `std::ostream`
0087     (such as `std::cout` or `std::cerr`). If the IDE debugger
0088     is attached when the stream is created, output will be
0089     additionally copied to the Visual Studio Output window.
0090 */
0091 template<
0092     class CharT,
0093     class Traits = std::char_traits<CharT>,
0094     class Allocator = std::allocator<CharT>
0095 >
0096 class basic_dstream
0097     : public std::basic_ostream<CharT, Traits>
0098 {
0099     detail::dstream_buf<
0100         CharT, Traits, Allocator> buf_;
0101 
0102 public:
0103     /** Construct a stream.
0104 
0105         @param os The output stream to wrap.
0106     */
0107     explicit
0108     basic_dstream(std::ostream& os)
0109         : std::basic_ostream<CharT, Traits>(&buf_)
0110         , buf_(os)
0111     {
0112         if(os.flags() & std::ios::unitbuf)
0113             std::unitbuf(*this);
0114     }
0115 };
0116 
0117 using dstream = basic_dstream<char>;
0118 using dwstream = basic_dstream<wchar_t>;
0119 
0120 #else
0121 
0122 using dstream = std::ostream&;
0123 using dwstream = std::wostream&;
0124 
0125 #endif
0126 
0127 } // unit_test
0128 } // beast
0129 } // boost
0130 
0131 #endif