ostream.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // Copyright (c) 2016-2017 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_WRITE_OSTREAM_HPP
  10. #define BOOST_BEAST_WRITE_OSTREAM_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/core/type_traits.hpp>
  13. #include <boost/beast/core/detail/ostream.hpp>
  14. #include <type_traits>
  15. #include <streambuf>
  16. #include <utility>
  17. namespace boost {
  18. namespace beast {
  19. /** Return an object representing a @b ConstBufferSequence.
  20. This function wraps a reference to a buffer sequence and permits
  21. the following operation:
  22. @li `operator<<` to `std::ostream`. No character translation is
  23. performed; unprintable and null characters will be transferred
  24. as-is to the output stream.
  25. @par Example
  26. @code
  27. multi_buffer b;
  28. ...
  29. std::cout << buffers(b.data()) << std::endl;
  30. @endcode
  31. @param b An object meeting the requirements of @b ConstBufferSequence
  32. to be streamed. The implementation will make a copy of this object.
  33. Ownership of the underlying memory is not transferred, the application
  34. is still responsible for managing its lifetime.
  35. */
  36. template<class ConstBufferSequence>
  37. #if BOOST_BEAST_DOXYGEN
  38. implementation_defined
  39. #else
  40. detail::buffers_helper<ConstBufferSequence>
  41. #endif
  42. buffers(ConstBufferSequence const& b)
  43. {
  44. static_assert(boost::asio::is_const_buffer_sequence<
  45. ConstBufferSequence>::value,
  46. "ConstBufferSequence requirements not met");
  47. return detail::buffers_helper<
  48. ConstBufferSequence>{b};
  49. }
  50. /** Return an output stream that formats values into a @b DynamicBuffer.
  51. This function wraps the caller provided @b DynamicBuffer into
  52. a `std::ostream` derived class, to allow `operator<<` stream style
  53. formatting operations.
  54. @par Example
  55. @code
  56. ostream(buffer) << "Hello, world!" << std::endl;
  57. @endcode
  58. @note Calling members of the underlying buffer before the output
  59. stream is destroyed results in undefined behavior.
  60. @param buffer An object meeting the requirements of @b DynamicBuffer
  61. into which the formatted output will be placed.
  62. @return An object derived from `std::ostream` which redirects output
  63. The wrapped dynamic buffer is not modified, a copy is made instead.
  64. Ownership of the underlying memory is not transferred, the application
  65. is still responsible for managing its lifetime. The caller is
  66. responsible for ensuring the dynamic buffer is not destroyed for the
  67. lifetime of the output stream.
  68. */
  69. template<class DynamicBuffer>
  70. #if BOOST_BEAST_DOXYGEN
  71. implementation_defined
  72. #else
  73. detail::ostream_helper<
  74. DynamicBuffer, char, std::char_traits<char>,
  75. detail::basic_streambuf_movable::value>
  76. #endif
  77. ostream(DynamicBuffer& buffer)
  78. {
  79. static_assert(
  80. boost::asio::is_dynamic_buffer<DynamicBuffer>::value,
  81. "DynamicBuffer requirements not met");
  82. return detail::ostream_helper<
  83. DynamicBuffer, char, std::char_traits<char>,
  84. detail::basic_streambuf_movable::value>{buffer};
  85. }
  86. } // beast
  87. } // boost
  88. #endif