string_param.hpp 2.1 KB

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