string_param.hpp 3.4 KB

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