Skip to main content
Pigweed's logo Pigweed
  1. Home
  2. Reference
  3. C/C++
  4. pw_buf
  5. pw::Buf Class Reference
Loading...
Searching...
No Matches
pw::Buf Class Reference

Overview

Represents a mutable view of a contiguous block of bytes.

An empty() Buf has no bytes (size() == 0). A null Buf has no bytes, and its base() and data() are nullptr. A Buf is null when default-constructed, moved-from, reset, or when allocation fails.

An empty Buf is not necessarily null (for example, if it has been sliced or truncated to length 0), in which case base() and data() return non-null pointers.

Nullness can be checked via comparison to nullptr (buf == nullptr, buf != nullptr). operator bool is not provided to prevent confusion between nullness and emptiness.

Public Types

using value_type = std::byte
 
using size_type = size_t
 
using difference_type = ptrdiff_t
 
using pointer = std::byte *
 
using reference = std::byte &
 
using const_pointer = const std::byte *
 
using const_reference = const std::byte &
 

Public Member Functions

constexpr Buf ()=default
 Constructs a null Buf.
 
constexpr Buf (std::nullptr_t) noexcept
 Constructs a null Buf.
 
constexpr Buf (Buf &&other) noexcept
 
 Buf (UniquePtr< std::byte[]> &&buffer)
 
 Buf (UniquePtr< std::byte[]> &&buffer, size_t offset)
 
 Buf (UniquePtr< std::byte[]> &&buffer, size_t offset, size_t size)
 Constructs an owned Buf from a UniquePtr, offset, and size.
 
constexpr Buf (std::byte *allocation, size_t size, Deallocator &deallocator)
 Constructs an owned Buf from a raw buffer, size, and deallocator.
 
constexpr Buf (std::byte *allocation, size_t offset, size_t size, Deallocator &deallocator)
 
 Buf (const Buf &)=delete
 
Bufoperator= (Buf &&other) noexcept
 
Bufoperator= (const Buf &)=delete
 
Bufoperator= (std::nullptr_t) noexcept
 
 operator bool () const =delete
 
 operator const ConstBuf & () const &
 
 operator const ConstBuf && () const &&
 
const_reference operator[] (size_t index) const
 Accesses the byte at the specified index as read-only.
 
reference operator[] (size_t index)
 Accesses the byte at the specified index as mutable.
 
constexpr const_pointer data () const
 
constexpr pointer data ()
 
constexpr size_t size () const
 Returns the size of the buffer.
 
constexpr bool empty () const
 
constexpr const std::byte * base () const
 
constexpr Deallocatordeallocator () const
 
const_iterator begin () const
 Returns a read-only iterator pointing to the beginning of the data.
 
const_iterator end () const
 Returns a read-only iterator pointing past the end of the data.
 
const_iterator cbegin () const
 Returns a read-only iterator pointing to the beginning of the data.
 
const_iterator cend () const
 Returns a read-only iterator pointing past the end of the data.
 
iterator begin ()
 Returns a mutable iterator pointing to the beginning of the data.
 
iterator end ()
 Returns a mutable iterator pointing past the end of the data.
 
void reset ()
 Frees the owned memory (if any) and sets the buffer to null.
 

Static Public Member Functions

static Buf Unowned (ByteSpan span, size_t offset, size_t size)
 Creates an unowned Buf from a ByteSpan, offset, and size.
 
static Buf Unowned (ByteSpan span, size_t offset=0)
 
static Buf Unowned (std::byte *base, size_t offset, size_t size)
 Creates an unowned Buf from a pointer, offset, and size.
 
static Buf Unowned (std::byte *base, size_t size)
 Creates an unowned Buf from a pointer and size.
 
static Buf Allocate (Allocator &allocator, size_t size)
 
static Buf Allocate (Allocator &allocator, size_t offset, size_t size)
 
static Buf TryAllocate (Allocator &allocator, size_t size)
 
static Buf TryAllocate (Allocator &allocator, size_t offset, size_t size)
 

Friends

class ConstBuf
 
class multibuf::v2::internal::GenericMultiBuf
 
constexpr bool operator== (const Buf &lhs, std::nullptr_t) noexcept
 
constexpr bool operator== (std::nullptr_t, const Buf &rhs) noexcept
 
constexpr bool operator!= (const Buf &lhs, std::nullptr_t) noexcept
 
constexpr bool operator!= (std::nullptr_t, const Buf &rhs) noexcept
 
Buf Slice (Buf &&buf, size_t offset, size_t length)
 Slices a mutable Buf by shifting its start address and setting its size.
 
Buf Reclaim (Buf &&buf, size_t prefix_count, size_t suffix_count)
 

Constructor & Destructor Documentation

◆ Buf() [1/3]

constexpr pw::Buf::Buf ( Buf &&  other)
inlineconstexprnoexcept

Move constructor.

The moved-from Buf is left null.

◆ Buf() [2/3]

pw::Buf::Buf ( UniquePtr< std::byte[]> &&  buffer)
inlineexplicit

Constructs an owned Buf from a UniquePtr.

If buffer is empty/null, constructs a null Buf.

◆ Buf() [3/3]

constexpr pw::Buf::Buf ( std::byte *  allocation,
size_t  offset,
size_t  size,
Deallocator deallocator 
)
inlineexplicitconstexpr

Constructs an owned Buf from a raw buffer, offset, size, and deallocator.

Member Function Documentation

◆ Allocate() [1/2]

static Buf pw::Buf::Allocate ( Allocator allocator,
size_t  offset,
size_t  size 
)
static

Allocates a new owned Buf of the specified allocation size, shifted to offset, with the specified size.

Asserts if allocation fails.

◆ Allocate() [2/2]

static Buf pw::Buf::Allocate ( Allocator allocator,
size_t  size 
)
inlinestatic

Allocates a new owned Buf of the specified size.

Asserts if allocation fails.

◆ base()

constexpr const std::byte * pw::Buf::base ( ) const
inlineconstexpr

Returns a pointer to the base address of the underlying memory buffer, or nullptr if the Buf is null.

Unlike data(), which points to the start of the current view, base() always points to the beginning of the underlying memory allocation or span from which this buffer was created.

◆ data() [1/2]

constexpr pointer pw::Buf::data ( )
inlineconstexpr

Returns a pointer to the mutable data, or nullptr if the Buf is null.

Note
An empty Buf that is not null (e.g. sliced or truncated to length 0) returns a non-null pointer. data() is only nullptr when the buffer is null.

◆ data() [2/2]

constexpr const_pointer pw::Buf::data ( ) const
inlineconstexpr

Returns a pointer to the read-only data, or nullptr if the Buf is null.

Note
An empty Buf that is not null (e.g. sliced or truncated to length 0) returns a non-null pointer. data() is only nullptr when the buffer is null.

◆ deallocator()

constexpr Deallocator * pw::Buf::deallocator ( ) const
inlineconstexpr

Returns a pointer to the deallocator if this buffer owns the underlying memory allocation, or nullptr if the buffer is unowned or null.

◆ empty()

constexpr bool pw::Buf::empty ( ) const
inlineconstexpr

Returns true if the buffer is empty (has no bytes).

empty() returns true for both null buffers and non-null buffers of size zero (e.g. after being sliced or truncated to length 0).

◆ operator bool()

pw::Buf::operator bool ( ) const
explicitdelete

operator bool is not provided to prevent confusion between nullness and emptiness. Use buf == nullptr to check for null, or buf.empty() for size 0.

◆ operator const ConstBuf &()

pw::Buf::operator const ConstBuf & ( ) const &
inline

Implicit conversion to const ConstBuf& and const ConstBuf&&. Conversion to ConstBuf& is not supported, since truncating the ConstBuf& would nullify this Buf.

Note
Prefer accepting pw::ConstByteSpan (or pw::span<const std::byte>) by value instead of const ConstBuf& for functions that only require read-only access to the buffer's contents.

◆ operator=() [1/2]

Buf & pw::Buf::operator= ( Buf &&  other)
inlinenoexcept

Move assignment operator.

The moved-from Buf is left null.

◆ operator=() [2/2]

Buf & pw::Buf::operator= ( std::nullptr_t  )
inlinenoexcept

Frees the owned memory (if any) and sets the buffer to null. Same as reset().

◆ TryAllocate() [1/2]

static Buf pw::Buf::TryAllocate ( Allocator allocator,
size_t  offset,
size_t  size 
)
static

Allocates a new owned Buf of the specified allocation size, shifted to offset, with the specified size.

Returns a null Buf if allocation fails.

◆ TryAllocate() [2/2]

static Buf pw::Buf::TryAllocate ( Allocator allocator,
size_t  size 
)
inlinestatic

Allocates a new owned Buf of the specified size.

Returns a null Buf if allocation fails.

Friends And Related Function Documentation

◆ Reclaim

Buf Reclaim ( Buf &&  buf,
size_t  prefix_count,
size_t  suffix_count 
)
friend

Reclaims up to prefix_count bytes at the beginning and suffix_count bytes at the end of the buffer.


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