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 <stdexcept>
9 #include <string>
10 #include <string_view>
11 #include <sys/uio.h>
12 #include <vector>
13 
15 class Buffer {
16  private:
18  size_t _starting_offset{};
19 
20  public:
21  Buffer() = default;
22 
24  Buffer(std::string &&str) noexcept : _storage(std::make_shared<std::string>(std::move(str))) {}
25 
28  std::string_view str() const {
29  if (not _storage) {
30  return {};
31  }
32  return {_storage->data() + _starting_offset, _storage->size() - _starting_offset};
33  }
34 
35  operator std::string_view() const { return str(); }
37 
39  uint8_t at(const size_t n) const { return str().at(n); }
40 
42  size_t size() const { return str().size(); }
43 
45  std::string copy() const { return std::string(str()); }
46 
49  void remove_prefix(const size_t n);
50 };
51 
57 class BufferList {
58  private:
60 
61  public:
64 
65  BufferList() = default;
66 
69 
71  BufferList(std::string &&str) noexcept {
72  Buffer buf{std::move(str)};
73  append(buf);
74  }
76 
78  const std::deque<Buffer> &buffers() const { return _buffers; }
79 
81  void append(const BufferList &other);
82 
85  operator Buffer() const;
86 
88  void remove_prefix(size_t n);
89 
91  size_t size() const;
92 
94  std::string concatenate() const;
95 };
96 
100 
101  public:
104 
106  BufferViewList(const std::string &str) : BufferViewList(std::string_view(str)) {}
107 
109  BufferViewList(const char *s) : BufferViewList(std::string_view(s)) {}
110 
112  BufferViewList(const BufferList &buffers);
113 
115  BufferViewList(std::string_view str) { _views.push_back({const_cast<char *>(str.data()), str.size()}); }
117 
119  void remove_prefix(size_t n);
120 
122  size_t size() const;
123 
128 };
129 
130 #endif // SPONGE_LIBSPONGE_BUFFER_HH
A reference-counted read-only string that can discard bytes from the front.
Definition: buffer.hh:15
size_t size() const
Size of the string.
Definition: buffer.hh:42
size_t _starting_offset
Definition: buffer.hh:18
std::shared_ptr< std::string > _storage
Definition: buffer.hh:17
Buffer(std::string &&str) noexcept
Construct by taking ownership of a string.
Definition: buffer.hh:24
uint8_t at(const size_t n) const
Get character at location n
Definition: buffer.hh:39
Buffer()=default
std::string copy() const
Make a copy to a new std::string.
Definition: buffer.hh:45
std::string_view str() const
Definition: buffer.hh:28
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
A reference-counted discontiguous string that can discard bytes from the front.
Definition: buffer.hh:57
const std::deque< Buffer > & buffers() const
Access the underlying queue of Buffers.
Definition: buffer.hh:78
void append(const BufferList &other)
Append a BufferList.
Definition: buffer.cc:15
std::deque< Buffer > _buffers
Definition: buffer.hh:59
BufferList(std::string &&str) noexcept
Construct by taking ownership of a std::string.
Definition: buffer.hh:71
BufferList()=default
std::string concatenate() const
Make a copy to a new std::string.
Definition: buffer.cc:34
BufferList(Buffer buffer)
Construct from a Buffer.
Definition: buffer.hh:68
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
size_t size() const
Size of the string.
Definition: buffer.cc:43
A non-owning temporary view (similar to std::string_view) of a discontiguous string.
Definition: buffer.hh:98
std::deque< std::string_view > _views
Definition: buffer.hh:99
BufferViewList(const char *s)
Construct from a C string (must be NULL-terminated)
Definition: buffer.hh:109
BufferViewList(std::string_view str)
Construct from a std::string_view.
Definition: buffer.hh:115
BufferViewList(const std::string &str)
Construct from a std::string.
Definition: buffer.hh:106
size_t size() const
Size of the string.
Definition: buffer.cc:89
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
std::vector< iovec > as_iovecs() const
Convert to a vector of iovec structures.
Definition: buffer.cc:97
T move(T... args)
std::string buffer
T push_back(T... args)