buffers_range.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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_BUFFERS_RANGE_HPP
  10. #define BOOST_BEAST_BUFFERS_RANGE_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/core/buffer_traits.hpp>
  13. #include <boost/beast/core/detail/buffers_range_adaptor.hpp>
  14. namespace boost {
  15. namespace beast {
  16. /** Returns an iterable range representing a buffer sequence.
  17. This function returns an iterable range representing the
  18. passed buffer sequence. The values obtained when iterating
  19. the range will be `net::const_buffer`, unless the underlying
  20. buffer sequence is a <em>MutableBufferSequence</em>, in which case
  21. the value obtained when iterating will be a `net::mutable_buffer`.
  22. @par Example
  23. The following function returns the total number of bytes in
  24. the specified buffer sequence. A copy of the buffer sequence
  25. is maintained for the lifetime of the range object:
  26. @code
  27. template <class BufferSequence>
  28. std::size_t buffer_sequence_size (BufferSequence const& buffers)
  29. {
  30. std::size_t size = 0;
  31. for (auto const buffer : buffers_range (buffers))
  32. size += buffer.size();
  33. return size;
  34. }
  35. @endcode
  36. @param buffers The buffer sequence to adapt into a range. The
  37. range object returned from this function will contain a copy
  38. of the passed buffer sequence.
  39. @return An object of unspecified type which meets the requirements
  40. of <em>ConstBufferSequence</em>. If `buffers` is a mutable buffer
  41. sequence, the returned object will also meet the requirements of
  42. <em>MutableBufferSequence</em>.
  43. @see buffers_range_ref
  44. */
  45. template<class BufferSequence>
  46. #if BOOST_BEAST_DOXYGEN
  47. __implementation_defined__
  48. #else
  49. detail::buffers_range_adaptor<BufferSequence>
  50. #endif
  51. buffers_range(BufferSequence const& buffers)
  52. {
  53. static_assert(
  54. is_const_buffer_sequence<BufferSequence>::value,
  55. "BufferSequence type requirements not met");
  56. return detail::buffers_range_adaptor<
  57. BufferSequence>(buffers);
  58. }
  59. /** Returns an iterable range representing a buffer sequence.
  60. This function returns an iterable range representing the
  61. passed buffer sequence. The values obtained when iterating
  62. the range will be `net::const_buffer`, unless the underlying
  63. buffer sequence is a <em>MutableBufferSequence</em>, in which case
  64. the value obtained when iterating will be a `net::mutable_buffer`.
  65. @par Example
  66. The following function returns the total number of bytes in
  67. the specified buffer sequence. A reference to the original
  68. buffers is maintained for the lifetime of the range object:
  69. @code
  70. template <class BufferSequence>
  71. std::size_t buffer_sequence_size_ref (BufferSequence const& buffers)
  72. {
  73. std::size_t size = 0;
  74. for (auto const buffer : buffers_range_ref (buffers))
  75. size += buffer.size();
  76. return size;
  77. }
  78. @endcode
  79. @param buffers The buffer sequence to adapt into a range. The
  80. range returned from this function will maintain a reference to
  81. these buffers. The application is responsible for ensuring that
  82. the lifetime of the referenced buffers extends until the range
  83. object is destroyed.
  84. @return An object of unspecified type which meets the requirements
  85. of <em>ConstBufferSequence</em>. If `buffers` is a mutable buffer
  86. sequence, the returned object will also meet the requirements of
  87. <em>MutableBufferSequence</em>.
  88. @see buffers_range
  89. */
  90. template<class BufferSequence>
  91. #if BOOST_BEAST_DOXYGEN
  92. __implementation_defined__
  93. #else
  94. detail::buffers_range_adaptor<BufferSequence const&>
  95. #endif
  96. buffers_range_ref(BufferSequence const& buffers)
  97. {
  98. static_assert(
  99. is_const_buffer_sequence<BufferSequence>::value,
  100. "BufferSequence type requirements not met");
  101. return detail::buffers_range_adaptor<
  102. BufferSequence const&>(buffers);
  103. }
  104. } // beast
  105. } // boost
  106. #endif