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