Skip to main content
Pigweed's logo Pigweed
code_xml
  1. Home
  2. Reference
  3. C/C++
  4. pw_enum
Loading...
Searching...
No Matches
pw_enum

Overview

Automatic versioned tokenized enum support.

Main docs: Home

Namespaces

namespace  pw
 The Pigweed namespace.
 

Macros

#define PW_ENUM(enum_name, ...)
 
#define PW_ENUM_STATUS(status_name, ok_code, ...)
 

Macro Definition Documentation

◆ PW_ENUM

#define PW_ENUM (   enum_name,
  ... 
)
Value:
static_assert(false, \
"PW_ENUM(" #enum_name \
") must be compiled with a pw_cc_enum target!"); \
_PW_ENUM_MUST_BE_COMPILED_BY_pw_cc_enum( \
#enum_name, \
PW_APPLY(_PW_ENUM_STRINGIFY_ARG, _PW_ENUM_COMMA, unused, __VA_ARGS__)) \
_PW_ENUM_MACRO_END
#define PW_APPLY(macro, separator, forwarded_arg,...)
Definition: apply.h:37

Registers a C++ enum for automatic stringification and tokenization.

This macro must be called at the global scope (outside of any namespaces, classes, or functions).

Parameters
enum_nameThe fully-qualified C++ enum name (e.g., my_ns::MyEnum).
...The enumerator names to register. By default, Google-style kCamelCase names are converted to UPPER_SNAKE_CASE without the k prefix. A custom display name can be specified with kEnum = "custom". If multiple enumerators share the same value (aliases), they are grouped and their names are joined (e.g., "A|B").

◆ PW_ENUM_STATUS

#define PW_ENUM_STATUS (   status_name,
  ok_code,
  ... 
)
Value:
static_assert(false, \
"PW_ENUM_STATUS(" #status_name \
") must be compiled with a pw_cc_enum target!"); \
_PW_ENUM_STATUS_MUST_BE_COMPILED_BY_pw_cc_enum( \
#status_name, \
#ok_code, \
PW_APPLY(_PW_ENUM_STRINGIFY_ARG, _PW_ENUM_COMMA, unused, __VA_ARGS__)) \
_PW_ENUM_MACRO_END

Registers a C++ enum and generates a pw::StatusBase-derived status class wrapping it, with automatic stringification, tokenization, static factory functions (e.g. MyStatus::NotFound()), and Is*() query functions (e.g. status.IsNotFound()). IsOk() is not generated for codes named OK; use ok(). A code named OK that is not the OK code is an error.

This macro must be called at the global scope (outside of any namespaces, classes, or functions) in place of PW_ENUM. The status class is generated at the location of the PW_ENUM_STATUS invocation, so place it at the bottom of the header, after any declarations that reference the status class. Otherwise, those declarations require a forward declaration of the status class (e.g. namespace my_ns { class MyStatus; }).

Example
#include <cstdint>
#include "pw_enum/generate.h"
namespace my_driver {
enum class I2cStatusCode : uint8_t {
kOk,
kNack,
kBusError,
kTimeout,
};
} // namespace my_driver
// Generates the my_driver::I2cStatus class. The second argument is the OK
// enumerator (its namespace is inferred from the status class).
my_driver::I2cStatus, I2cStatusCode::kOk, kOk, kNack, kBusError, kTimeout);
#define PW_ENUM_STATUS(status_name, ok_code,...)
Definition: generate.h:76
Source: pw_enum/examples/private/enum_example/i2c_status.h
Parameters
status_nameThe fully-qualified name of the status class to generate (e.g., my_ns::MyStatus).
ok_codeThe enumerator that represents success, including its enum type (e.g., MyStatusCode::kOk or my_ns::MyStatusCode::kOk). If only the enum type and enumerator are given, the enum is assumed to be in the namespace of status_name. Otherwise, the name is treated as fully qualified, with or without a leading ::. Enums in other namespaces or nested in classes must be fully qualified (e.g., my_ns::MyClass::Code::kOk). The enum type is deduced from this argument.
...The enumerator names to register (same syntax as PW_ENUM). Must include the OK enumerator.