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

Overview

Represents a read-only view of a contiguous block of bytes.

ConstBuf is intended for passing ownership of read-only data, or providing an unowned view, optionally slicing the view between layers. Unlike Buf, ConstBuf does not support Reclaim() or expose its base pointer. Once a ConstBuf is sliced or truncated, the excluded bytes cannot be recovered.

An empty() ConstBuf has no bytes (size() == 0). A null ConstBuf has no bytes, and its data() pointer is nullptr. A ConstBuf is null when default-constructed, moved-from, or reset.

An empty ConstBuf is not necessarily null (for example, if it has been sliced or truncated to length 0), in which case data() returns a non-null pointer.

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 = const std::byte *
 
using reference = const std::byte &
 
using iterator = containers::ConstPtrIterator< ConstBuf >
 
using const_pointer = const std::byte *
 
using const_reference = const std::byte &
 
using const_iterator = containers::ConstPtrIterator< ConstBuf >
 

Public Member Functions

constexpr ConstBuf ()=default
 Constructs a null ConstBuf.
 
constexpr ConstBuf (std::nullptr_t) noexcept
 Constructs a null ConstBuf.
 
constexpr ConstBuf (ConstBuf &&other) noexcept
 
constexpr ConstBuf (Buf &&other) noexcept
 
 ConstBuf (const ConstBuf &)=delete
 
 ~ConstBuf ()
 Destructor. Releases the owned memory (if any) back to the allocator.
 
ConstBufoperator= (ConstBuf &&other) noexcept
 
ConstBufoperator= (Buf &&other) noexcept
 
ConstBufoperator= (const ConstBuf &)=delete
 
ConstBufoperator= (std::nullptr_t) noexcept
 
 operator bool () const =delete
 
reference operator[] (size_t index) const
 Accesses the byte at the specified index.
 
constexpr pointer data () const
 
constexpr size_t size () const
 Returns the number of bytes in the buffer view.
 
constexpr bool empty () const
 
iterator begin () const
 Returns a read-only iterator pointing to the beginning of the data.
 
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.
 
constexpr Deallocatordeallocator () const
 
void reset ()
 Frees the owned memory (if any) and sets the buffer to null.
 

Static Public Member Functions

static ConstBuf Unowned (ConstByteSpan span)
 Creates an unowned ConstBuf from a ConstByteSpan.
 
static ConstBuf Unowned (const std::byte *data, size_t size_bytes)
 Creates an unowned ConstBuf from a pointer and size in bytes.
 

Friends

class Buf
 
class multibuf::v2::internal::GenericMultiBuf
 
constexpr bool operator== (const ConstBuf &lhs, std::nullptr_t) noexcept
 
constexpr bool operator== (std::nullptr_t, const ConstBuf &rhs) noexcept
 
constexpr bool operator!= (const ConstBuf &lhs, std::nullptr_t) noexcept
 
constexpr bool operator!= (std::nullptr_t, const ConstBuf &rhs) noexcept
 
ConstBuf Slice (ConstBuf &&const_buf, size_t offset, size_t length)
 
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

◆ ConstBuf()

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

Move constructor.

The moved-from ConstBuf is left null.

Member Function Documentation

◆ data()

constexpr pointer pw::ConstBuf::data ( ) const
inlineconstexpr

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

Note
An empty ConstBuf 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::ConstBuf::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::ConstBuf::empty ( ) const
inlineconstexpr

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

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

◆ operator bool()

pw::ConstBuf::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=() [1/3]

ConstBuf & pw::ConstBuf::operator= ( Buf &&  other)
noexcept

Move conversion assignment operator from Buf.

The moved-from Buf is left null.

◆ operator=() [2/3]

ConstBuf & pw::ConstBuf::operator= ( ConstBuf &&  other)
noexcept

Move assignment operator.

The moved-from ConstBuf is left null.

◆ operator=() [3/3]

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

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

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.

◆ Slice

ConstBuf Slice ( ConstBuf &&  const_buf,
size_t  offset,
size_t  length 
)
friend

Slices a read-only ConstBuf by shifting its start address and setting its size.


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