buffer_traits.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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_BUFFER_TRAITS_HPP
  10. #define BOOST_BEAST_BUFFER_TRAITS_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/core/detail/buffer_traits.hpp>
  13. #include <boost/beast/core/detail/static_const.hpp>
  14. #include <boost/asio/buffer.hpp>
  15. #include <boost/config/workaround.hpp>
  16. #include <boost/mp11/function.hpp>
  17. #include <type_traits>
  18. namespace boost {
  19. namespace beast {
  20. /** Determine if a list of types satisfy the <em>ConstBufferSequence</em> requirements.
  21. This metafunction is used to determine if all of the specified types
  22. meet the requirements for constant buffer sequences. This type alias
  23. will be `std::true_type` if each specified type meets the requirements,
  24. otherwise, this type alias will be `std::false_type`.
  25. @tparam BufferSequence A list of zero or more types to check. If this
  26. list is empty, the resulting type alias will be `std::true_type`.
  27. */
  28. template<class... BufferSequence>
  29. #if BOOST_BEAST_DOXYGEN
  30. using is_const_buffer_sequence = __see_below__;
  31. #else
  32. using is_const_buffer_sequence = mp11::mp_all<
  33. net::is_const_buffer_sequence<
  34. typename std::decay<BufferSequence>::type>...>;
  35. #endif
  36. /** Determine if a list of types satisfy the <em>MutableBufferSequence</em> requirements.
  37. This metafunction is used to determine if all of the specified types
  38. meet the requirements for mutable buffer sequences. This type alias
  39. will be `std::true_type` if each specified type meets the requirements,
  40. otherwise, this type alias will be `std::false_type`.
  41. @tparam BufferSequence A list of zero or more types to check. If this
  42. list is empty, the resulting type alias will be `std::true_type`.
  43. */
  44. template<class... BufferSequence>
  45. #if BOOST_BEAST_DOXYGEN
  46. using is_mutable_buffer_sequence = __see_below__;
  47. #else
  48. using is_mutable_buffer_sequence = mp11::mp_all<
  49. net::is_mutable_buffer_sequence<
  50. typename std::decay<BufferSequence>::type>...>;
  51. #endif
  52. /** Type alias for the underlying buffer type of a list of buffer sequence types.
  53. This metafunction is used to determine the underlying buffer type for
  54. a list of buffer sequence. The equivalent type of the alias will vary
  55. depending on the template type argument:
  56. @li If every type in the list is a <em>MutableBufferSequence</em>,
  57. the resulting type alias will be `net::mutable_buffer`, otherwise
  58. @li The resulting type alias will be `net::const_buffer`.
  59. @par Example
  60. The following code returns the first buffer in a buffer sequence,
  61. or generates a compilation error if the argument is not a buffer
  62. sequence:
  63. @code
  64. template <class BufferSequence>
  65. buffers_type <BufferSequence>
  66. buffers_front (BufferSequence const& buffers)
  67. {
  68. static_assert(
  69. net::is_const_buffer_sequence<BufferSequence>::value,
  70. "BufferSequence type requirements not met");
  71. auto const first = net::buffer_sequence_begin (buffers);
  72. if (first == net::buffer_sequence_end (buffers))
  73. return {};
  74. return *first;
  75. }
  76. @endcode
  77. @tparam BufferSequence A list of zero or more types to check. If this
  78. list is empty, the resulting type alias will be `net::mutable_buffer`.
  79. */
  80. template<class... BufferSequence>
  81. #if BOOST_BEAST_DOXYGEN
  82. using buffers_type = __see_below__;
  83. #else
  84. using buffers_type = typename std::conditional<
  85. is_mutable_buffer_sequence<BufferSequence...>::value,
  86. net::mutable_buffer, net::const_buffer>::type;
  87. #endif
  88. /** Type alias for the iterator type of a buffer sequence type.
  89. This metafunction is used to determine the type of iterator
  90. used by a particular buffer sequence.
  91. @tparam T The buffer sequence type to use. The resulting
  92. type alias will be equal to the iterator type used by
  93. the buffer sequence.
  94. */
  95. template <class BufferSequence>
  96. #if BOOST_BEAST_DOXYGEN
  97. using buffers_iterator_type = __see_below__;
  98. #elif BOOST_WORKAROUND(BOOST_MSVC, < 1910)
  99. using buffers_iterator_type = typename
  100. detail::buffers_iterator_type_helper<
  101. typename std::decay<BufferSequence>::type>::type;
  102. #else
  103. using buffers_iterator_type =
  104. decltype(net::buffer_sequence_begin(
  105. std::declval<BufferSequence const&>()));
  106. #endif
  107. /** Return the total number of bytes in a buffer or buffer sequence
  108. This function returns the total number of bytes in a buffer,
  109. buffer sequence, or object convertible to a buffer. Specifically
  110. it may be passed:
  111. @li A <em>ConstBufferSequence</em> or <em>MutableBufferSequence</em>
  112. @li A `net::const_buffer` or `net::mutable_buffer`
  113. @li An object convertible to `net::const_buffer`
  114. This function is designed as an easier-to-use replacement for
  115. `net::buffer_size`. It recognizes customization points found through
  116. argument-dependent lookup. The call `beast::buffer_bytes(b)` is
  117. equivalent to performing:
  118. @code
  119. using namespace net;
  120. buffer_bytes(b);
  121. @endcode
  122. In addition this handles types which are convertible to
  123. `net::const_buffer`; these are not handled by `net::buffer_size`.
  124. @param buffers The buffer or buffer sequence to calculate the size of.
  125. @return The total number of bytes in the buffer or sequence.
  126. */
  127. #if BOOST_BEAST_DOXYGEN
  128. template<class BufferSequence>
  129. std::size_t
  130. buffer_bytes(BufferSequence const& buffers);
  131. #else
  132. BOOST_BEAST_INLINE_VARIABLE(buffer_bytes, detail::buffer_bytes_impl)
  133. #endif
  134. } // beast
  135. } // boost
  136. #endif