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