make_printable.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_MAKE_PRINTABLE_HPP
  10. #define BOOST_BEAST_MAKE_PRINTABLE_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/core/buffer_traits.hpp>
  13. #include <boost/asio/buffer.hpp>
  14. #include <ostream>
  15. namespace boost {
  16. namespace beast {
  17. namespace detail {
  18. template<class Buffers>
  19. class make_printable_adaptor
  20. {
  21. Buffers b_;
  22. public:
  23. explicit
  24. make_printable_adaptor(Buffers const& b)
  25. : b_(b)
  26. {
  27. }
  28. template<class B>
  29. friend
  30. std::ostream&
  31. operator<<(std::ostream& os,
  32. make_printable_adaptor<B> const& v);
  33. };
  34. template<class Buffers>
  35. std::ostream&
  36. operator<<(std::ostream& os,
  37. make_printable_adaptor<Buffers> const& v)
  38. {
  39. for(
  40. auto it = net::buffer_sequence_begin(v.b_),
  41. end = net::buffer_sequence_end(v.b_);
  42. it != end;
  43. ++it)
  44. {
  45. net::const_buffer cb = *it;
  46. os.write(static_cast<char const*>(
  47. cb.data()), cb.size());
  48. }
  49. return os;
  50. }
  51. } // detail
  52. /** Helper to permit a buffer sequence to be printed to a std::ostream
  53. This function is used to wrap a buffer sequence to allow it to
  54. be interpreted as characters and written to a `std::ostream` such
  55. as `std::cout`. No character translation is performed; unprintable
  56. and null characters will be transferred as-is to the output stream.
  57. @par Example
  58. This function prints the size and contents of a buffer sequence
  59. to standard output:
  60. @code
  61. template <class ConstBufferSequence>
  62. void
  63. print (ConstBufferSequence const& buffers)
  64. {
  65. std::cout <<
  66. "Buffer size: " << buffer_bytes(buffers) << " bytes\n"
  67. "Buffer data: '" << make_printable(buffers) << "'\n";
  68. }
  69. @endcode
  70. @param buffers An object meeting the requirements of
  71. <em>ConstBufferSequence</em> to be streamed. The implementation
  72. will make a copy of this object. Ownership of the underlying
  73. memory is not transferred, the application is still responsible
  74. for managing its lifetime.
  75. */
  76. template<class ConstBufferSequence>
  77. #if BOOST_BEAST_DOXYGEN
  78. __implementation_defined__
  79. #else
  80. detail::make_printable_adaptor<ConstBufferSequence>
  81. #endif
  82. make_printable(ConstBufferSequence const& buffers)
  83. {
  84. static_assert(net::is_const_buffer_sequence<
  85. ConstBufferSequence>::value,
  86. "ConstBufferSequence type requirements not met");
  87. return detail::make_printable_adaptor<
  88. ConstBufferSequence>{buffers};
  89. }
  90. } // beast
  91. } // boost
  92. #endif