Skip to main content
Pigweed's logo Pigweed
code_xml
  1. Home
  2. Reference
  3. C/C++
  4. Encoding & serialization
  5. pw_bytes
  6. Byte spans

Overview

Aliases for pw::span of bytes.

Typedefs

using pw::ByteSpan = span< std::byte >
 Alias for a mutable pw::span of bytes.
 
using pw::ConstByteSpan = span< const std::byte >
 Alias for a read-only pw::span of bytes.
 

Functions

template<typename VoidPtr >
ConstByteSpan pw::AsBytes (VoidPtr data, size_t size_bytes) noexcept
 
template<typename VoidPtr >
ByteSpan pw::AsWritableBytes (VoidPtr data, size_t size_bytes) noexcept
 
template<typename T >
span< const std::byte, sizeof(T)> pw::ObjectAsBytes (const T &obj)
 
template<typename T >
span< std::byte, sizeof(T)> pw::ObjectAsWritableBytes (T &obj)
 

Function Documentation

◆ AsBytes()

template<typename VoidPtr >
ConstByteSpan pw::AsBytes ( VoidPtr  data,
size_t  size_bytes 
)
noexcept

Gets a read-only pw::ConstByteSpan (pw::span<const std::byte>) view of a type-erased memory buffer (const void* or void*) of a given size in bytes.

This function is constrained strictly to const void* and void* so that typed pointers (T*) cannot implicitly convert to const void* and accidentally interpret an element count as a byte count. For typed pointers, construct a pw::span first: pw::as_bytes(pw::span(ptr, count)).

◆ AsWritableBytes()

template<typename VoidPtr >
ByteSpan pw::AsWritableBytes ( VoidPtr  data,
size_t  size_bytes 
)
noexcept

Gets a writable pw::ByteSpan (pw::span<std::byte>) view of a mutable type-erased memory buffer (void*) of a given size in bytes.

This function is constrained strictly to void* so that typed pointers (T*) cannot implicitly convert to void* and accidentally interpret an element count as a byte count. For typed pointers, construct a pw::span first: pw::as_writable_bytes(pw::span(ptr, count)).

◆ ObjectAsBytes()

template<typename T >
span< const std::byte, sizeof(T)> pw::ObjectAsBytes ( const T &  obj)

Gets a read-only pw::span<const std::byte> (ConstByteSpan) view of an object.

This function is only available for types where it always safe to rely on the underlying bytes of the object, i.e. serializable objects designed to be sent over the wire. It cannot be used with, for example, types that include padding bytes, since those are indeterminate and may leak information.

For types that do not meet these criteria, it is still possible to represent them as bytes using pw::as_bytes, albeit in a manner that is unsafe for serialization.

The returned span has a static extent.

◆ ObjectAsWritableBytes()

template<typename T >
span< std::byte, sizeof(T)> pw::ObjectAsWritableBytes ( T &  obj)

Gets a writable pw::span<std::byte> (ByteSpan) view of an object.

This function is only available for types where it always safe to rely on the underlying bytes of the object, i.e. serializable objects designed to be sent over the wire. It cannot be used with, for example, types that include padding bytes, since those are indeterminate and may leak information.

For types that do not meet these criteria, it is still possible to represent them as bytes using pw::as_writable_bytes, albeit in a manner that is unsafe for serialization.

The returned span has a static extent.