Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:28

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_MAKE_PRINTABLE_HPP
0011 #define BOOST_BEAST_MAKE_PRINTABLE_HPP
0012 
0013 #include <boost/beast/core/detail/config.hpp>
0014 #include <boost/beast/core/buffer_traits.hpp>
0015 #include <boost/asio/buffer.hpp>
0016 #include <ostream>
0017 
0018 namespace boost {
0019 namespace beast {
0020 
0021 namespace detail {
0022 
0023 template<class Buffers>
0024 class make_printable_adaptor
0025 {
0026     Buffers b_;
0027 
0028 public:
0029     explicit
0030     make_printable_adaptor(Buffers const& b)
0031         : b_(b)
0032     {
0033     }
0034 
0035     template<class B>
0036     friend
0037     std::ostream&
0038     operator<<(std::ostream& os,
0039         make_printable_adaptor<B> const& v);
0040 };
0041 
0042 template<class Buffers>
0043 std::ostream&
0044 operator<<(std::ostream& os,
0045     make_printable_adaptor<Buffers> const& v)
0046 {
0047     for(
0048         auto it = net::buffer_sequence_begin(v.b_),
0049         end = net::buffer_sequence_end(v.b_);
0050         it != end;
0051         ++it)
0052     {
0053         net::const_buffer cb = *it;
0054         os.write(static_cast<char const*>(
0055             cb.data()), cb.size());
0056     }
0057     return os;
0058 }
0059 
0060 } // detail
0061 
0062 /** Helper to permit a buffer sequence to be printed to a std::ostream
0063 
0064     This function is used to wrap a buffer sequence to allow it to
0065     be interpreted as characters and written to a `std::ostream` such
0066     as `std::cout`. No character translation is performed; unprintable
0067     and null characters will be transferred as-is to the output stream.
0068 
0069     @par Example
0070     This function prints the size and contents of a buffer sequence
0071     to standard output:
0072     @code
0073     template <class ConstBufferSequence>
0074     void
0075     print (ConstBufferSequence const& buffers)
0076     {
0077         std::cout <<
0078             "Buffer size: " << buffer_bytes(buffers) << " bytes\n"
0079             "Buffer data: '" << make_printable(buffers) << "'\n";
0080     }
0081     @endcode
0082 
0083     @param buffers An object meeting the requirements of
0084     <em>ConstBufferSequence</em> to be streamed. The implementation
0085     will make a copy of this object. Ownership of the underlying
0086     memory is not transferred, the application is still responsible
0087     for managing its lifetime.
0088 */
0089 template<class ConstBufferSequence>
0090 #if BOOST_BEAST_DOXYGEN
0091 __implementation_defined__
0092 #else
0093 detail::make_printable_adaptor<ConstBufferSequence>
0094 #endif
0095 make_printable(ConstBufferSequence const& buffers)
0096 {
0097     static_assert(net::is_const_buffer_sequence<
0098         ConstBufferSequence>::value,
0099             "ConstBufferSequence type requirements not met");
0100     return detail::make_printable_adaptor<
0101         ConstBufferSequence>{buffers};
0102 }
0103 
0104 } // beast
0105 } // boost
0106 
0107 #endif