Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | List of all members
pw::StatusWithSize Class Reference

Public Member Functions

constexpr StatusWithSize ()
 Creates a StatusWithSize with status OK and a size of 0.
 
template<typename T , typename = std::enable_if_t<std::is_integral<T>::value>>
constexpr StatusWithSize (T size)
 
constexpr StatusWithSize (Status status, size_t size)
 Creates a StatusWithSize with the provided status and size.
 
constexpr StatusWithSize (const StatusWithSize &)=default
 
constexpr StatusWithSizeoperator= (const StatusWithSize &)=default
 
constexpr void UpdateAndAdd (StatusWithSize new_status_with_size)
 
constexpr void ZeroIfNotOk ()
 Zeroes the size if the status is not OK.
 
constexpr size_t size () const
 
constexpr bool ok () const
 True if status() == OkStatus().
 
constexpr void IgnoreError () const
 
constexpr Status status () const
 
constexpr bool IsCancelled () const
 
constexpr bool IsUnknown () const
 
constexpr bool IsInvalidArgument () const
 
constexpr bool IsDeadlineExceeded () const
 
constexpr bool IsNotFound () const
 
constexpr bool IsAlreadyExists () const
 
constexpr bool IsPermissionDenied () const
 
constexpr bool IsResourceExhausted () const
 
constexpr bool IsFailedPrecondition () const
 
constexpr bool IsAborted () const
 
constexpr bool IsOutOfRange () const
 
constexpr bool IsUnimplemented () const
 
constexpr bool IsInternal () const
 
constexpr bool IsUnavailable () const
 
constexpr bool IsDataLoss () const
 
constexpr bool IsUnauthenticated () const
 

Static Public Member Functions

static constexpr StatusWithSize Cancelled (size_t size=0)
 
static constexpr StatusWithSize Unknown (size_t size=0)
 
static constexpr StatusWithSize InvalidArgument (size_t size=0)
 
static constexpr StatusWithSize DeadlineExceeded (size_t size=0)
 
static constexpr StatusWithSize NotFound (size_t size=0)
 
static constexpr StatusWithSize AlreadyExists (size_t size=0)
 
static constexpr StatusWithSize PermissionDenied (size_t size=0)
 
static constexpr StatusWithSize Unauthenticated (size_t size=0)
 
static constexpr StatusWithSize ResourceExhausted (size_t size=0)
 
static constexpr StatusWithSize FailedPrecondition (size_t size=0)
 
static constexpr StatusWithSize Aborted (size_t size=0)
 
static constexpr StatusWithSize OutOfRange (size_t size=0)
 
static constexpr StatusWithSize Unimplemented (size_t size=0)
 
static constexpr StatusWithSize Internal (size_t size=0)
 
static constexpr StatusWithSize Unavailable (size_t size=0)
 
static constexpr StatusWithSize DataLoss (size_t size=0)
 
static constexpr size_t max_size ()
 The maximum valid value for size.
 

Detailed Description

StatusWithSize stores a status and an unsigned integer. The integer must not exceed StatusWithSize::max_size(), which is 134,217,727 (2**27 - 1) on 32-bit systems.

StatusWithSize is useful for reporting the number of bytes read or written in an operation along with the status. For example, a function that writes a formatted string may want to report both the number of characters written and whether it ran out of space.

StatusWithSize is more efficient than its alternatives. It packs a status and size into a single word, which can be returned from a function in a register. Because they are packed together, the size is limited to max_size().

StatusWithSize's alternatives result in larger code size. For example:

  1. Return status, pass size output as a pointer argument.

    Requires an additional argument and forces the output argument to the stack in order to pass an address, increasing code size.

  2. Return an object with Status and size members.

    At least for ARMv7-M, the returned struct is created on the stack, which increases code size.

Constructor & Destructor Documentation

◆ StatusWithSize()

template<typename T , typename = std::enable_if_t<std::is_integral<T>::value>>
constexpr pw::StatusWithSize::StatusWithSize ( size)
inlineexplicitconstexpr

Creates a StatusWithSize with status OK and the provided size. std::enable_if is used to prevent enum types (e.g. Status::Code) from being used.

Member Function Documentation

◆ IgnoreError()

constexpr void pw::StatusWithSize::IgnoreError ( ) const
inlineconstexpr

Ignores any errors. This method does nothing except potentially suppress complaints from any tools that are checking that errors are not dropped on the floor.

◆ size()

constexpr size_t pw::StatusWithSize::size ( ) const
inlineconstexpr

Returns the size. The size is always present, even if status() is an error.

◆ UpdateAndAdd()

constexpr void pw::StatusWithSize::UpdateAndAdd ( StatusWithSize  new_status_with_size)
inlineconstexpr

Update s this status and adds to size.

The resulting StatusWithSize will have a size of this->size() + new_status_with_size.size()

The resulting status will be OK if both statuses are OK, otherwise it will take on the earliest non-OK status.


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