Sponge
CS144's user-space TCP library
Public Member Functions | Private Attributes | List of all members
Buffer Class Reference

A reference-counted read-only string that can discard bytes from the front. More...

#include <buffer.hh>

Public Member Functions

 Buffer ()=default
 
 Buffer (std::string &&str) noexcept
 Construct by taking ownership of a string. More...
 
uint8_t at (const size_t n) const
 Get character at location n More...
 
std::string copy () const
 Make a copy to a new std::string. More...
 
void remove_prefix (const size_t n)
 Discard the first n bytes of the string (does not require a copy or move) More...
 
size_t size () const
 Size of the string. More...
 
Expose contents as a std::string_view
std::string_view str () const
 
 operator std::string_view () const
 

Private Attributes

size_t _starting_offset {}
 
std::shared_ptr< std::string_storage {}
 

Detailed Description

A reference-counted read-only string that can discard bytes from the front.

Definition at line 15 of file buffer.hh.

Constructor & Destructor Documentation

◆ Buffer() [1/2]

Buffer::Buffer ( )
default

◆ Buffer() [2/2]

Buffer::Buffer ( std::string &&  str)
inlinenoexcept

Construct by taking ownership of a string.

Definition at line 24 of file buffer.hh.

Member Function Documentation

◆ at()

uint8_t Buffer::at ( const size_t  n) const
inline

Get character at location n

Definition at line 39 of file buffer.hh.

◆ copy()

std::string Buffer::copy ( ) const
inline

Make a copy to a new std::string.

Definition at line 45 of file buffer.hh.

◆ operator std::string_view()

Buffer::operator std::string_view ( ) const
inline

Definition at line 35 of file buffer.hh.

◆ remove_prefix()

void Buffer::remove_prefix ( const size_t  n)

Discard the first n bytes of the string (does not require a copy or move)

Note
Doesn't free any memory until the whole string has been discarded in all copies of the Buffer.

Definition at line 5 of file buffer.cc.

◆ size()

size_t Buffer::size ( ) const
inline

Size of the string.

Definition at line 42 of file buffer.hh.

◆ str()

std::string_view Buffer::str ( ) const
inline

Definition at line 28 of file buffer.hh.

Member Data Documentation

◆ _starting_offset

size_t Buffer::_starting_offset {}
private

Definition at line 18 of file buffer.hh.

◆ _storage

std::shared_ptr<std::string> Buffer::_storage {}
private

Definition at line 17 of file buffer.hh.


The documentation for this class was generated from the following files: