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_STRING_PARAM_HPP
11 #define BOOST_BEAST_STRING_PARAM_HPP
12 
13 #if defined(BOOST_BEAST_ALLOW_DEPRECATED) && !defined(BOOST_BEAST_DOXYGEN)
14 
15 
16 #include <boost/beast/core/detail/config.hpp>
17 #include <boost/beast/core/string.hpp>
18 #include <boost/beast/core/static_string.hpp>
19 #include <boost/beast/core/detail/static_ostream.hpp>
20 #include <boost/optional.hpp>
21 
22 namespace boost {
23 namespace beast {
24 
25 /** A function parameter which efficiently converts to string.
26 
27     This is used as a function parameter type to allow callers
28     notational convenience: objects other than strings may be
29     passed in contexts where a string is expected. The conversion
30     to string is made using `operator<<` to a non-dynamically
31     allocated static buffer if possible, else to a `std::string`
32     on overflow.
33 
34     To use it, modify your function signature to accept
35     `string_param` and then extract the string inside the
36     function:
37     @code
38     void print(string_param s)
39     {
40         std::cout << s.str();
41     }
42     @endcode
43 */
44 class string_param
45 {
46     string_view sv_;
47     char buf_[128];
48     boost::optional<detail::static_ostream> os_;
49 
50     template<class T>
51     typename std::enable_if<
52         std::is_integral<T>::value>::type
53     print(T const&);
54 
55     template<class T>
56     typename std::enable_if<
57         ! std::is_integral<T>::value &&
58         ! std::is_convertible<T, string_view>::value
59     >::type
60     print(T const&);
61 
62     void
63     print(string_view);
64 
65     template<class T>
66     typename std::enable_if<
67         std::is_integral<T>::value>::type
68     print_1(T const&);
69 
70     template<class T>
71     typename std::enable_if<
72         ! std::is_integral<T>::value>::type
73     print_1(T const&);
74 
75     void
print_n()76     print_n()
77     {
78     }
79 
80     template<class T0, class... TN>
81     void
82     print_n(T0 const&, TN const&...);
83 
84     template<class T0, class T1, class... TN>
85     void
86     print(T0 const&, T1 const&, TN const&...);
87 
88 public:
89     /// Copy constructor (disallowed)
90     string_param(string_param const&) = delete;
91 
92     /// Copy assignment (disallowed)
93     string_param& operator=(string_param const&) = delete;
94 
95     /** Constructor
96 
97         This function constructs a string as if by concatenating
98         the result of streaming each argument in order into an
99         output stream. It is used as a notational convenience
100         at call sites which expect a parameter with the semantics
101         of a @ref string_view.
102 
103         The implementation uses a small, internal static buffer
104         to avoid memory allocations especially for the case where
105         the list of arguments to be converted consists of a single
106         integral type.
107 
108         @param args One or more arguments to convert
109     */
110     template<class... Args>
111     string_param(Args const&... args);
112 
113     /// Returns the contained string
114     string_view
str() const115     str() const
116     {
117         return sv_;
118     }
119 
120     /// Implicit conversion to @ref string_view
operator string_view const() const121     operator string_view const() const
122     {
123         return sv_;
124     }
125 };
126 
127 } // beast
128 } // boost
129 
130 #include <boost/beast/core/impl/string_param.hpp>
131 
132 #endif // defined(BOOST_BEAST_ALLOW_DEPRECATED) && !BOOST_BEAST_DOXYGEN
133 
134 #endif
135