ostream.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_WRITE_OSTREAM_HPP
  10. #define BOOST_BEAST_WRITE_OSTREAM_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/core/detail/ostream.hpp>
  13. #include <type_traits>
  14. #include <streambuf>
  15. #include <utility>
  16. #ifdef BOOST_BEAST_ALLOW_DEPRECATED
  17. #include <boost/beast/core/make_printable.hpp>
  18. #endif
  19. namespace boost {
  20. namespace beast {
  21. /** Return an output stream that formats values into a <em>DynamicBuffer</em>.
  22. This function wraps the caller provided <em>DynamicBuffer</em> into
  23. a `std::ostream` derived class, to allow `operator<<` stream style
  24. formatting operations.
  25. @par Example
  26. @code
  27. ostream(buffer) << "Hello, world!" << std::endl;
  28. @endcode
  29. @note Calling members of the underlying buffer before the output
  30. stream is destroyed results in undefined behavior.
  31. @param buffer An object meeting the requirements of <em>DynamicBuffer</em>
  32. into which the formatted output will be placed.
  33. @return An object derived from `std::ostream` which redirects output
  34. The wrapped dynamic buffer is not modified, a copy is made instead.
  35. Ownership of the underlying memory is not transferred, the application
  36. is still responsible for managing its lifetime. The caller is
  37. responsible for ensuring the dynamic buffer is not destroyed for the
  38. lifetime of the output stream.
  39. */
  40. template<class DynamicBuffer>
  41. #if BOOST_BEAST_DOXYGEN
  42. __implementation_defined__
  43. #else
  44. detail::ostream_helper<
  45. DynamicBuffer, char, std::char_traits<char>,
  46. detail::basic_streambuf_movable::value>
  47. #endif
  48. ostream(DynamicBuffer& buffer)
  49. {
  50. static_assert(
  51. net::is_dynamic_buffer<DynamicBuffer>::value,
  52. "DynamicBuffer type requirements not met");
  53. return detail::ostream_helper<
  54. DynamicBuffer, char, std::char_traits<char>,
  55. detail::basic_streambuf_movable::value>{buffer};
  56. }
  57. //------------------------------------------------------------------------------
  58. #ifdef BOOST_BEAST_ALLOW_DEPRECATED
  59. template<class T>
  60. detail::make_printable_adaptor<T>
  61. buffers(T const& t)
  62. {
  63. return make_printable(t);
  64. }
  65. #else
  66. template<class T>
  67. void buffers(T const&)
  68. {
  69. static_assert(sizeof(T) == 0,
  70. "The function buffers() is deprecated, use make_printable() instead, "
  71. "or define BOOST_BEAST_ALLOW_DEPRECATED to silence this error.");
  72. }
  73. #endif
  74. } // beast
  75. } // boost
  76. #endif