Skip to main content
Pigweed's logo Pigweed
code_xml
  1. Home
  2. Reference
  3. C/C++
  4. pw_status
  5. pw::StatusBase< Derived, kOkCode > Class Template Reference
Loading...
Searching...
No Matches
pw::StatusBase< Derived, kOkCode > Class Template Reference

Overview

template<typename Derived, auto kOkCode>
class pw::StatusBase< Derived, kOkCode >

Base class providing standard status operations for status types using CRTP.

StatusBase provides common functionality for status types that wrap an enum or integer status code, including checking for success (ok()), retrieving the code (code()), string formatting (str()), updating, and equality comparisons.

Custom status types should derive from StatusBase<Derived, kOkCode> or use the PW_STATUS_TYPE macro. Derived classes should be declared [[nodiscard]] unless created using PW_STATUS_TYPE (which applies [[nodiscard]] automatically).

Users should not interact directly StatusBase, except to instantiate a custom status class. Do not use StatusBase for variables or function parameters.

Template Parameters
DerivedThe derived status class (CRTP parameter).
kOkCodeCode value that represents success (ok() == true).

Public Types

using Code = decltype(kOkCode)
 The underlying status code type.
 

Public Member Functions

constexpr bool ok () const
 Returns true if the status is the OK code.
 
constexpr Code code () const
 Returns the underlying status code.
 
constexpr void Update (Derived other)
 
constexpr void IgnoreError () const
 Ignores any errors, suppressing unused-result warnings.
 
constexpr const char * str () const
 

Protected Member Functions

constexpr StatusBase ()
 Defaults to kOkCode (ok() == true).
 
constexpr StatusBase (Code code)
 Explicitly construct from the underlying code.
 
constexpr StatusBase (const StatusBase &)=default
 
constexpr StatusBase & operator= (const StatusBase &)=default
 
constexpr StatusBase (StatusBase &&)=default
 
constexpr StatusBase & operator= (StatusBase &&)=default
 

Protected Attributes

Code code_
 

Friends

constexpr bool operator== (Derived lhs, Derived rhs)
 
constexpr bool operator!= (Derived lhs, Derived rhs)
 
constexpr bool operator== (Derived lhs, Code rhs)
 
constexpr bool operator== (Code lhs, Derived rhs)
 
constexpr bool operator!= (Derived lhs, Code rhs)
 
constexpr bool operator!= (Code lhs, Derived rhs)
 

Member Function Documentation

◆ str()

template<typename Derived , auto kOkCode>
constexpr const char * pw::StatusBase< Derived, kOkCode >::str ( ) const
inlineconstexpr

Returns a string representation of the status. Only supported for status types with enum codes.

◆ Update()

template<typename Derived , auto kOkCode>
constexpr void pw::StatusBase< Derived, kOkCode >::Update ( Derived  other)
inlineconstexpr

Updates this status to other if this status is currently ok().

This is useful for tracking the first encountered error, as calls to this helper will not change one error status to another error status.


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