Sponge
CS144's user-space TCP library
buffer.hh
Go to the documentation of this file.
1 #ifndef SPONGE_LIBSPONGE_BUFFER_HH
2 #define SPONGE_LIBSPONGE_BUFFER_HH
3 
4 #include <algorithm>
5 #include <deque>
6 #include <memory>
7 #include <numeric>
8 #include <string>
9 #include <string_view>
10 #include <sys/uio.h>
11 #include <vector>
12 
14 class Buffer {
15  private:
17  size_t _starting_offset{};
18 
19  public:
20  Buffer() = default;
21 
23  Buffer(std::string &&str) noexcept : _storage(std::make_shared<std::string>(std::move(str))) {}
24 
27  std::string_view str() const {
28  if (not _storage) {
29  return {};
30  }
31  return {_storage->data() + _starting_offset, _storage->size() - _starting_offset};
32  }
33 
34  operator std::string_view() const { return str(); }
36 
38  uint8_t at(const size_t n) const { return str().at(n); }
39 
41  size_t size() const { return str().size(); }
42 
44  std::string copy() const { return std::string(str()); }
45 
48  void remove_prefix(const size_t n);
49 };
50 
56 class BufferList {
57  private:
59 
60  public:
63 
64  BufferList() = default;
65 
68 
70  BufferList(std::string &&str) noexcept {
71  Buffer buf{std::move(str)};
72  append(buf);
73  }
75 
77  const std::deque<Buffer> &buffers() const { return _buffers; }
78 
80  void append(const BufferList &other);
81 
84  operator Buffer() const;
85 
87  void remove_prefix(size_t n);
88 
90  size_t size() const;
91 
93  std::string concatenate() const;
94 };
95 
99 
100  public:
103 
105  BufferViewList(const std::string &str) : BufferViewList(std::string_view(str)) {}
106 
108  BufferViewList(const char *s) : BufferViewList(std::string_view(s)) {}
109 
111  BufferViewList(const BufferList &buffers);
112 
114  BufferViewList(std::string_view str) { _views.push_back({const_cast<char *>(str.data()), str.size()}); }
116 
118  void remove_prefix(size_t n);
119 
121  size_t size() const;
122 
127 };
128 
129 #endif // SPONGE_LIBSPONGE_BUFFER_HH
BufferList::BufferList
BufferList(Buffer buffer)
Construct from a Buffer.
Definition: buffer.hh:67
Buffer::_storage
std::shared_ptr< std::string > _storage
Definition: buffer.hh:16
std::string
std::shared_ptr< std::string >
Buffer::copy
std::string copy() const
Make a copy to a new std::string.
Definition: buffer.hh:44
std::move
T move(T... args)
BufferViewList::BufferViewList
BufferViewList(const char *s)
Construct from a C string (must be NULL-terminated)
Definition: buffer.hh:108
Buffer::_starting_offset
size_t _starting_offset
Definition: buffer.hh:17
std::vector
BufferViewList
A non-owning temporary view (similar to std::string_view) of a discontiguous string.
Definition: buffer.hh:97
BufferList::append
void append(const BufferList &other)
Append a BufferList.
Definition: buffer.cc:15
BufferViewList::remove_prefix
void remove_prefix(size_t n)
Discard the first n bytes of the string (does not require a copy or move)
Definition: buffer.cc:73
BufferList
A reference-counted discontiguous string that can discard bytes from the front.
Definition: buffer.hh:56
BufferList::BufferList
BufferList(std::string &&str) noexcept
Construct by taking ownership of a std::string.
Definition: buffer.hh:70
std::deque::push_back
T push_back(T... args)
Buffer::Buffer
Buffer(std::string &&str) noexcept
Construct by taking ownership of a string.
Definition: buffer.hh:23
buffer
std::string buffer
Definition: parser_example.cc:7
Buffer
A reference-counted read-only string that can discard bytes from the front.
Definition: buffer.hh:14
std::deque< Buffer >
Buffer::at
uint8_t at(const size_t n) const
Get character at location n
Definition: buffer.hh:38
Buffer::remove_prefix
void remove_prefix(const size_t n)
Discard the first n bytes of the string (does not require a copy or move)
Definition: buffer.cc:5
BufferList::_buffers
std::deque< Buffer > _buffers
Definition: buffer.hh:58
BufferViewList::size
size_t size() const
Size of the string.
Definition: buffer.cc:89
BufferViewList::_views
std::deque< std::string_view > _views
Definition: buffer.hh:98
BufferViewList::as_iovecs
std::vector< iovec > as_iovecs() const
Convert to a vector of iovec structures.
Definition: buffer.cc:97
BufferViewList::BufferViewList
BufferViewList(const std::string &str)
Construct from a std::string.
Definition: buffer.hh:105
std
BufferList::size
size_t size() const
Size of the string.
Definition: buffer.cc:43
Buffer::Buffer
Buffer()=default
BufferList::buffers
const std::deque< Buffer > & buffers() const
Access the underlying queue of Buffers.
Definition: buffer.hh:77
Buffer::size
size_t size() const
Size of the string.
Definition: buffer.hh:41
BufferViewList::BufferViewList
BufferViewList(std::string_view str)
Construct from a std::string_view.
Definition: buffer.hh:114
Buffer::str
std::string_view str() const
Definition: buffer.hh:27
BufferList::remove_prefix
void remove_prefix(size_t n)
Discard the first n bytes of the string (does not require a copy or move)
Definition: buffer.cc:51
BufferList::BufferList
BufferList()=default
BufferList::concatenate
std::string concatenate() const
Make a copy to a new std::string.
Definition: buffer.cc:34