buffers_ref.hpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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_BUFFERS_REF_HPP
  10. #define BOOST_BEAST_DETAIL_BUFFERS_REF_HPP
  11. #include <boost/beast/core/buffer_traits.hpp>
  12. #include <iterator>
  13. #include <memory>
  14. namespace boost {
  15. namespace beast {
  16. namespace detail {
  17. // A very lightweight reference to a buffer sequence
  18. template<class BufferSequence>
  19. class buffers_ref
  20. {
  21. BufferSequence const* buffers_;
  22. public:
  23. using const_iterator =
  24. buffers_iterator_type<BufferSequence>;
  25. using value_type = typename
  26. std::iterator_traits<const_iterator>::value_type;
  27. buffers_ref(buffers_ref const&) = default;
  28. buffers_ref& operator=(buffers_ref const&) = default;
  29. explicit
  30. buffers_ref(BufferSequence const& buffers)
  31. : buffers_(std::addressof(buffers))
  32. {
  33. }
  34. const_iterator
  35. begin() const
  36. {
  37. return net::buffer_sequence_begin(*buffers_);
  38. }
  39. const_iterator
  40. end() const
  41. {
  42. return net::buffer_sequence_end(*buffers_);
  43. }
  44. };
  45. // Return a reference to a buffer sequence
  46. template<class BufferSequence>
  47. buffers_ref<BufferSequence>
  48. make_buffers_ref(BufferSequence const& buffers)
  49. {
  50. static_assert(
  51. is_const_buffer_sequence<BufferSequence>::value,
  52. "BufferSequence type requirements not met");
  53. return buffers_ref<BufferSequence>(buffers);
  54. }
  55. } // detail
  56. } // beast
  57. } // boost
  58. #endif