Files
boost_beast/include/beast/core/string_param.hpp
T

127 lines
3.1 KiB
C++
Raw Normal View History

2017-06-10 15:56:48 -07:00
//
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BEAST_STRING_PARAM_HPP
#define BEAST_STRING_PARAM_HPP
#include <beast/config.hpp>
2017-06-16 09:38:53 -07:00
#include <beast/core/string.hpp>
2017-06-22 16:49:46 -07:00
#include <beast/core/static_string.hpp>
2017-06-10 15:56:48 -07:00
#include <beast/core/detail/static_ostream.hpp>
#include <beast/core/detail/type_traits.hpp>
2017-06-22 16:49:46 -07:00
#include <boost/optional.hpp>
2017-06-10 15:56:48 -07:00
namespace beast {
/** A function parameter which efficiently converts to string.
This is used as a function parameter type to allow callers
notational convenience: objects other than strings may be
passed in contexts where a string is expected. The conversion
to string is made using `operator<<` to a non-dynamically
allocated static buffer if possible, else to a `std::string`
2017-07-14 08:06:24 -07:00
on overflow.
To use it, modify your function signature to accept
`string_param` and then extract the string inside the
function:
@code
void print(string_param s)
{
std::cout << s.str();
}
@endcode
2017-06-10 15:56:48 -07:00
*/
class string_param
{
string_view sv_;
2017-06-22 16:49:46 -07:00
char buf_[128];
boost::optional<detail::static_ostream> os_;
template<class T>
typename std::enable_if<
std::is_integral<T>::value>::type
print(T const&);
template<class T>
typename std::enable_if<
! std::is_integral<T>::value &&
! std::is_convertible<T, string_view>::value
>::type
print(T const&);
void
2017-07-12 16:35:52 -07:00
print(string_view);
2017-06-22 16:49:46 -07:00
template<class T>
typename std::enable_if<
std::is_integral<T>::value>::type
print_1(T const&);
template<class T>
typename std::enable_if<
! std::is_integral<T>::value>::type
print_1(T const&);
void
print_n()
{
}
template<class T0, class... TN>
void
print_n(T0 const&, TN const&...);
template<class T0, class T1, class... TN>
void
print(T0 const&, T1 const&, TN const&...);
2017-06-10 15:56:48 -07:00
public:
/// Copy constructor (disallowed)
string_param(string_param const&) = delete;
/// Copy assignment (disallowed)
string_param& operator=(string_param const&) = delete;
/** Constructor
2017-06-22 16:49:46 -07:00
This function constructs a string as if by concatenating
the result of streaming each argument in order into an
output stream. It is used as a notational convenience
at call sites which expect a parameter with the semantics
of a @ref string_view.
2017-06-10 15:56:48 -07:00
2017-06-22 16:49:46 -07:00
The implementation uses a small, internal static buffer
to avoid memory allocations especially for the case where
the list of arguments to be converted consists of a single
integral type.
2017-06-10 15:56:48 -07:00
2017-06-22 16:49:46 -07:00
@param args One or more arguments to convert
2017-06-10 15:56:48 -07:00
*/
2017-06-22 16:49:46 -07:00
template<class... Args>
string_param(Args const&... args);
2017-06-10 15:56:48 -07:00
2017-07-14 08:06:24 -07:00
/// Returns the contained string
string_view
str() const
{
return sv_;
}
2017-06-22 16:49:46 -07:00
/// Implicit conversion to @ref string_view
2017-06-10 15:56:48 -07:00
operator string_view const() const
{
return sv_;
}
};
} // beast
2017-06-22 16:49:46 -07:00
#include <beast/core/impl/string_param.ipp>
2017-06-10 15:56:48 -07:00
#endif