Skip to main content
Pigweed's logo Pigweed
code_xml
  1. Home
  2. Reference
  3. C/C++
  4. Memory & storage
  5. pw_buf
  6. 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 was created with ConstBuf::ZeroLength() or 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 = Buf::const_iterator
 
using const_pointer = const std::byte *
 
using const_reference = const std::byte &
 
using const_iterator = Buf::const_iterator
 

Public Member Functions

constexpr ConstBuf ()=default
 Constructs a null ConstBuf.
 
constexpr ConstBuf (std::nullptr_t) noexcept
 Constructs a null ConstBuf.
 
template<typename T , typename = std::enable_if_t<std::is_integral_v<T>>>
 ConstBuf (T)=delete
 
constexpr ConstBuf (ConstBuf &&other) noexcept
 
constexpr ConstBuf (Buf &&other) noexcept
 
 ConstBuf (const ConstBuf &)=delete
 
 ~ConstBuf ()=default
 Destructor. Releases the owned memory (if any) back to the allocator.
 
ConstBuf & operator= (ConstBuf &&other) noexcept=default
 
ConstBuf & operator= (Buf &&other) noexcept
 
ConstBuf & operator= (const ConstBuf &)=delete
 
ConstBuf & operator= (std::nullptr_t) noexcept
 
template<typename T , typename = std::enable_if_t<std::is_integral_v<T>>>
ConstBuf & operator= (T)=delete
 Integers cannot be assigned to a ConstBuf, not even the literal 0.
 
 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 bool owned () const
 
constexpr Deallocator * deallocator () const
 
void reset ()
 Frees the owned memory (if any) and sets the buffer to null.
 
void swap (ConstBuf &other) noexcept
 Exchanges the contents of this ConstBuf with other.
 

Static Public Member Functions

static ConstBuf ZeroLength () noexcept
 
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 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
 
void swap (ConstBuf &lhs, ConstBuf &rhs) noexcept
 Exchanges the contents of two ConstBufs.
 
ConstBuf Slice (ConstBuf &&const_buf, size_t offset, size_t length)
 

Constructor & Destructor Documentation

◆ ConstBuf() [1/3]

template<typename T , typename = std::enable_if_t<std::is_integral_v<T>>>
pw::ConstBuf::ConstBuf ( T  )
delete

Integers do not convert to ConstBuf. Without this, the literal 0 would convert through the nullptr_t constructor, so passing 0 where a ConstBuf is expected would silently produce a null buffer.

◆ ConstBuf() [2/3]

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

Move constructor.

The moved-from ConstBuf is left null.

◆ ConstBuf() [3/3]

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

Move-constructs a ConstBuf from a mutable Buf (move conversion).

The moved-from Buf 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)
inlinenoexcept

Move conversion assignment operator from Buf.

The moved-from Buf is left null.

◆ operator=() [2/3]

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

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().

◆ owned()

constexpr bool pw::ConstBuf::owned ( ) const
inlineconstexpr

Returns true if this buffer owns the underlying memory allocation (deallocator() != nullptr), or false if the buffer is unowned or null.

◆ ZeroLength()

static ConstBuf pw::ConstBuf::ZeroLength ( )
inlinestaticnoexcept

Returns an empty, non-null, unowned ConstBuf (empty() == true, buf != nullptr, owned() == false).

Friends And Related Function Documentation

◆ 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: