buffer_traits.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_DETAIL_BUFFER_TRAITS_HPP
  10. #define BOOST_BEAST_DETAIL_BUFFER_TRAITS_HPP
  11. #include <boost/asio/buffer.hpp>
  12. #include <boost/config/workaround.hpp>
  13. #include <boost/type_traits/make_void.hpp>
  14. #include <cstdint>
  15. #include <type_traits>
  16. namespace boost {
  17. namespace beast {
  18. namespace detail {
  19. #if BOOST_WORKAROUND(BOOST_MSVC, < 1910)
  20. template<class T>
  21. struct buffers_iterator_type_helper
  22. {
  23. using type = decltype(
  24. net::buffer_sequence_begin(
  25. std::declval<T const&>()));
  26. };
  27. template<>
  28. struct buffers_iterator_type_helper<
  29. net::const_buffer>
  30. {
  31. using type = net::const_buffer const*;
  32. };
  33. template<>
  34. struct buffers_iterator_type_helper<
  35. net::mutable_buffer>
  36. {
  37. using type = net::mutable_buffer const*;
  38. };
  39. #endif
  40. struct buffer_bytes_impl
  41. {
  42. std::size_t
  43. operator()(net::const_buffer b) const noexcept
  44. {
  45. return net::const_buffer(b).size();
  46. }
  47. std::size_t
  48. operator()(net::mutable_buffer b) const noexcept
  49. {
  50. return net::mutable_buffer(b).size();
  51. }
  52. template<
  53. class B,
  54. class = typename std::enable_if<
  55. net::is_const_buffer_sequence<B>::value>::type>
  56. std::size_t
  57. operator()(B const& b) const noexcept
  58. {
  59. using net::buffer_size;
  60. return buffer_size(b);
  61. }
  62. };
  63. /** Return `true` if a buffer sequence is empty
  64. This is sometimes faster than using @ref buffer_bytes
  65. */
  66. template<class ConstBufferSequence>
  67. bool
  68. buffers_empty(ConstBufferSequence const& buffers)
  69. {
  70. auto it = net::buffer_sequence_begin(buffers);
  71. auto end = net::buffer_sequence_end(buffers);
  72. while(it != end)
  73. {
  74. if(net::const_buffer(*it).size() > 0)
  75. return false;
  76. ++it;
  77. }
  78. return true;
  79. }
  80. } // detail
  81. } // beast
  82. } // boost
  83. #endif