string_param.hpp 3.2 KB

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