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_IMPL_STRING_PARAM_HPP
11 #define BOOST_BEAST_IMPL_STRING_PARAM_HPP
12 
13 namespace boost {
14 namespace beast {
15 
16 template<class T>
17 typename std::enable_if<
18     std::is_integral<T>::value>::type
19 string_param::
print(T const & t)20 print(T const& t)
21 {
22     auto const last = buf_ + sizeof(buf_);
23     auto const it = detail::raw_to_string<
24         char, T, std::char_traits<char>>(
25             last, sizeof(buf_), t);
26     sv_ = {it, static_cast<std::size_t>(
27         last - it)};
28 }
29 
30 template<class T>
31 typename std::enable_if<
32     ! std::is_integral<T>::value &&
33     ! std::is_convertible<T, string_view>::value
34 >::type
35 string_param::
print(T const & t)36 print(T const& t)
37 {
38     os_.emplace(buf_, sizeof(buf_));
39     *os_ << t;
40     os_->flush();
41     sv_ = os_->str();
42 }
43 
44 inline
45 void
46 string_param::
print(string_view sv)47 print(string_view sv)
48 {
49     sv_ = sv;
50 }
51 
52 template<class T>
53 typename std::enable_if<
54     std::is_integral<T>::value>::type
55 string_param::
print_1(T const & t)56 print_1(T const& t)
57 {
58     char buf[detail::max_digits(sizeof(T))];
59     auto const last = buf + sizeof(buf);
60     auto const it = detail::raw_to_string<
61         char, T, std::char_traits<char>>(
62             last, sizeof(buf), t);
63     *os_ << string_view{it,
64         static_cast<std::size_t>(last - it)};
65 }
66 
67 template<class T>
68 typename std::enable_if<
69     ! std::is_integral<T>::value>::type
70 string_param::
print_1(T const & t)71 print_1(T const& t)
72 {
73     *os_ << t;
74 }
75 
76 template<class T0, class... TN>
77 void
78 string_param::
print_n(T0 const & t0,TN const &...tn)79 print_n(T0 const& t0, TN const&... tn)
80 {
81     print_1(t0);
82     print_n(tn...);
83 }
84 
85 template<class T0, class T1, class... TN>
86 void
87 string_param::
print(T0 const & t0,T1 const & t1,TN const &...tn)88 print(T0 const& t0, T1 const& t1, TN const&... tn)
89 {
90     os_.emplace(buf_, sizeof(buf_));
91     print_1(t0);
92     print_1(t1);
93     print_n(tn...);
94     os_->flush();
95     sv_ = os_->str();
96 }
97 
98 template<class... Args>
99 string_param::
string_param(Args const &...args)100 string_param(Args const&... args)
101 {
102     print(args...);
103 }
104 
105 } // beast
106 } // boost
107 
108 #endif
109