You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.5KB

  1. #pragma once
  2. #include <neo/mutable_buffer.hpp>
  3. #include <cassert>
  4. #include <cstddef>
  5. #include <string_view>
  6. namespace neo {
  7. /**
  8. * A type that represents a view to a readonly segment of contiguous memory.
  9. */
  10. class const_buffer {
  11. public:
  12. using pointer = const std::byte*;
  13. using size_type = std::size_t;
  14. private:
  15. pointer _data = nullptr;
  16. size_type _size = 0;
  17. public:
  18. constexpr const_buffer() noexcept = default;
  19. constexpr const_buffer(pointer ptr, size_type size) noexcept
  20. : _data(ptr)
  21. , _size(size) {}
  22. constexpr const_buffer(mutable_buffer buf)
  23. : _data(buf.data())
  24. , _size(buf.size()) {}
  25. explicit constexpr const_buffer(std::string_view sv)
  26. : _data(byte_pointer(sv.data()))
  27. , _size(sv.size()) {}
  28. constexpr pointer data() const noexcept { return _data; }
  29. constexpr size_type size() const noexcept { return _size; }
  30. constexpr const_buffer& operator+=(size_type s) noexcept {
  31. assert(s <= size() && "Advanced neo::const_buffer past-the-end");
  32. _data += s;
  33. _size -= s;
  34. return *this;
  35. }
  36. };
  37. inline constexpr const_buffer operator+(const_buffer buf, const_buffer::size_type s) noexcept {
  38. auto copy = buf;
  39. copy += s;
  40. return copy;
  41. }
  42. inline constexpr auto get_buffer_sequence_begin(const_buffer buf) noexcept {
  43. return detail::single_buffer_iter{buf};
  44. }
  45. inline constexpr auto get_buffer_sequence_end(const_buffer) noexcept {
  46. return detail::single_buffer_iter_sentinel();
  47. }
  48. } // namespace neo