Library of Assembled Shared Sources
|
Defining first-class enum types in Python from C++ enums.
This module provides helper macros and classes to define Python enum types that derive from enum.Enum
, enum.IntEnum
, enum.StrEnum
or enum.IntFlag
.
The following Python enum types can be created:
enum.IntEnum
: Values are integers, allows direct int conversion and comparison (recommended for most C++ enums)enum.StrEnum
: Values are strings, allows direct string operationsenum.IntFlag
: Integer flags supporting bitwise operations (|
, &
, ^
, ~
)enum.Enum
: Generic enums with custom value types (advanced use)To export a C++ enum to Python, you need 3 macros:
PY_SHADOW_*
macro in the header file to specialize PyExportTraits
PY_DECLARE_*
macro in the source file to define the enum mappingOnce defined, the enum can be used as parameters or return types in function signatures, as class members, etc. The header defining PY_SHADOW_*
must be included in source files that export these functions to Python, or add the enum to a module or class.
enum.IntEnum
(most common case):enum.StrEnum
(string-based enums):enum.IntFlag
(bitfield enums):enum.Enum
(generic enums):Data Structures | |
class | lass::python::EnumDefinitionBase |
Base class of all enum definitions. More... | |
class | lass::python::IntEnumDefinition< EnumType > |
Definition of an enum.IntEnum -derived enum type in Python. More... | |
class | lass::python::IntFlagDefinition< EnumType > |
Definition of an enum.IntFlag -derived enum type in Python. More... | |
class | lass::python::EnumDefinition< EnumType, ValueType > |
Definition of a general enum.Enum -derived enum type in Python. More... | |
class | lass::python::StrEnumDefinition< EnumType, ValueType > |
Definition of an enum.StrEnum -derived enum type in Python. More... | |
Enumerations | |
enum class | lass::python::FlagBoundary { lass::python::FlagBoundary::Keep , lass::python::FlagBoundary::Strict , lass::python::FlagBoundary::Conform } |
Defines the boundary behavior for enum.IntFlag -derived enums. More... | |
Functions | |
TPyObjPtr | lass::python::impl::makeEnumType (const char *name, TPyObjPtr &&enumerators, TPyObjPtr &&kwargs) |
Creates a basic enum.Enum type. | |
TPyObjPtr | lass::python::impl::makeIntEnumType (const char *name, TPyObjPtr &&enumerators, TPyObjPtr &&kwargs) |
Creates an enum.IntEnum type. | |
TPyObjPtr | lass::python::impl::makeIntFlagType (const char *name, TPyObjPtr &&enumerators, TPyObjPtr &&kwargs, FlagBoundary boundary=FlagBoundary::Keep) |
Creates an enum.IntFlag type. | |
TPyObjPtr | lass::python::impl::makeStrEnumType (const char *name, TPyObjPtr &&enumerators, TPyObjPtr &&kwargs) |
Creates an enum.StrEnum type (or equivalent for Python < 3.11). | |
Integer Enum Support | |
Macros for exporting C++ integer enums as Python | |
#define | PY_SHADOW_INT_ENUM(dllInterface, t_cppEnum) |
Specializes PyExportTraits for a C++ enum using enum.IntEnum . | |
#define | PY_DECLARE_INT_ENUM_NAME(t_cppEnum, s_name) |
Defines the enumDefinition with name only for enum.IntEnum . | |
#define | PY_DECLARE_INT_ENUM_NAME_DOC(t_cppEnum, s_name, s_doc) |
Defines the enumDefinition with name and docstring for enum.IntEnum . | |
#define | PY_DECLARE_INT_ENUM_EX(t_cppEnum) |
Defines the enumDefinition for initialization with constructor arguments for enum.IntEnum . | |
Integer Flag Support | |
Macros for exporting C++ flag enums as Python | |
#define | PY_SHADOW_INT_FLAG(dllInterface, t_cppEnum) |
Specializes PyExportTraits for a C++ enum using enum.IntFlag . | |
#define | PY_DECLARE_INT_FLAG_NAME(t_cppEnum, s_name) |
Defines the enumDefinition with name only for enum.IntFlag . | |
#define | PY_DECLARE_INT_FLAG_NAME_DOC(t_cppEnum, s_name, s_doc) |
Defines the enumDefinition with name and docstring for enum.IntFlag . | |
#define | PY_DECLARE_INT_FLAG_EX(t_cppEnum) |
Defines the enumDefinition for initialization with constructor arguments for enum.IntFlag . | |
Generic Enum Support | |
Macros for exporting C++ enums as Python | |
#define | PY_SHADOW_ENUM(dllInterface, t_cppEnum, t_valueType) |
Specializes PyExportTraits for a C++ enum using generic enum.Enum . | |
#define | PY_DECLARE_ENUM_NAME(t_cppEnum, t_valueType, s_name) |
Defines the enumDefinition with name only for generic enum.Enum . | |
#define | PY_DECLARE_ENUM_NAME_DOC(t_cppEnum, t_valueType, s_name, s_doc) |
Defines the enumDefinition with name and docstring for generic enum.Enum . | |
#define | PY_DECLARE_ENUM_EX(t_cppEnum, t_valueType) |
Defines the enumDefinition for initialization with constructor arguments for generic enum.Enum . | |
String Enum Support | |
Macros for exporting C++ enums as Python | |
#define | PY_SHADOW_STR_ENUM(dllInterface, t_cppEnum) |
Specializes PyExportTraits for a C++ enum using enum.StrEnum . | |
#define | PY_DECLARE_STR_ENUM_NAME(t_cppEnum, s_name) |
Defines the enumDefinition with name only for enum.StrEnum . | |
#define | PY_DECLARE_STR_ENUM_NAME_DOC(t_cppEnum, s_name, s_doc) |
Defines the enumDefinition with name and docstring for enum.StrEnum . | |
#define | PY_DECLARE_STR_ENUM_EX(t_cppEnum) |
Defines the enumDefinition for initialization with constructor arguments for enum.StrEnum . | |
Common Enum Utilities | |
Utility macros for adding enum definitions to modules and classes. | |
#define | PY_MODULE_ENUM(i_module, t_cppEnum) |
Adds an enum definition to a Python module. | |
#define | PY_CLASS_ENUM(i_cppClass, t_cppEnum) |
Adds an enum definition as a nested enum to a Python class. | |
#define PY_SHADOW_INT_ENUM | ( | dllInterface, | |
t_cppEnum ) |
Specializes PyExportTraits for a C++ enum using enum.IntEnum
.
Use this in header files, then use PY_DECLARE_INT_ENUM_* in source files.
dllInterface | Export specification (e.g., LASS_DLL_EXPORT) |
t_cppEnum | C++ enum type |
Definition at line 846 of file enum_definition.h.
#define PY_DECLARE_INT_ENUM_NAME | ( | t_cppEnum, | |
s_name ) |
Defines the enumDefinition with name only for enum.IntEnum
.
t_cppEnum | C++ enum type |
s_name | Python enum name (const char* string with static storage duration) |
Definition at line 870 of file enum_definition.h.
#define PY_DECLARE_INT_ENUM_NAME_DOC | ( | t_cppEnum, | |
s_name, | |||
s_doc ) |
Defines the enumDefinition with name and docstring for enum.IntEnum
.
t_cppEnum | C++ enum type |
s_name | Python enum name (const char* string with static storage duration) |
s_doc | Documentation string (const char* string with static storage duration, or nullptr) |
Definition at line 880 of file enum_definition.h.
#define PY_DECLARE_INT_ENUM_EX | ( | t_cppEnum | ) |
Defines the enumDefinition for initialization with constructor arguments for enum.IntEnum
.
Use this with initializer list: PY_DECLARE_INT_ENUM_EX(MyEnum)("MyEnum", "docs", { ... });
t_cppEnum | C++ enum type |
Definition at line 889 of file enum_definition.h.
#define PY_SHADOW_INT_FLAG | ( | dllInterface, | |
t_cppEnum ) |
Specializes PyExportTraits for a C++ enum using enum.IntFlag
.
Use this in header files for bitfield enums, then use PY_DECLARE_INT_FLAG_* in source files.
dllInterface | Export specification (e.g., LASS_DLL_EXPORT) |
t_cppEnum | C++ enum type (should be a bitfield enum) |
Definition at line 910 of file enum_definition.h.
#define PY_DECLARE_INT_FLAG_NAME | ( | t_cppEnum, | |
s_name ) |
Defines the enumDefinition with name only for enum.IntFlag
.
t_cppEnum | C++ enum type |
s_name | Python enum name (const char* string with static storage duration) |
Definition at line 934 of file enum_definition.h.
#define PY_DECLARE_INT_FLAG_NAME_DOC | ( | t_cppEnum, | |
s_name, | |||
s_doc ) |
Defines the enumDefinition with name and docstring for enum.IntFlag
.
t_cppEnum | C++ enum type |
s_name | Python enum name (const char* string with static storage duration) |
s_doc | Documentation string (const char* string with static storage duration, or nullptr) |
Definition at line 944 of file enum_definition.h.
#define PY_DECLARE_INT_FLAG_EX | ( | t_cppEnum | ) |
Defines the enumDefinition for initialization with constructor arguments for enum.IntFlag
.
Use with: PY_DECLARE_INT_FLAG_EX(MyEnum)("MyEnum", "docs", FlagBoundary::Keep, { ... });
t_cppEnum | C++ enum type |
Definition at line 954 of file enum_definition.h.
#define PY_SHADOW_ENUM | ( | dllInterface, | |
t_cppEnum, | |||
t_valueType ) |
Specializes PyExportTraits for a C++ enum using generic enum.Enum
.
Use this in header files for enums with custom value types, then use PY_DECLARE_ENUM_* in source files.
dllInterface | Export specification (e.g., LASS_DLL_EXPORT) |
t_cppEnum | C++ enum type |
t_valueType | Python value type for enum values |
Definition at line 975 of file enum_definition.h.
#define PY_DECLARE_ENUM_NAME | ( | t_cppEnum, | |
t_valueType, | |||
s_name ) |
Defines the enumDefinition with name only for generic enum.Enum
.
t_cppEnum | C++ enum type |
t_valueType | Python value type |
s_name | Python enum name (const char* string with static storage duration) |
Definition at line 1000 of file enum_definition.h.
#define PY_DECLARE_ENUM_NAME_DOC | ( | t_cppEnum, | |
t_valueType, | |||
s_name, | |||
s_doc ) |
Defines the enumDefinition with name and docstring for generic enum.Enum
.
t_cppEnum | C++ enum type |
t_valueType | Python value type |
s_name | Python enum name (const char* string with static storage duration) |
s_doc | Documentation string (const char* string with static storage duration, or nullptr) |
Definition at line 1011 of file enum_definition.h.
#define PY_DECLARE_ENUM_EX | ( | t_cppEnum, | |
t_valueType ) |
Defines the enumDefinition for initialization with constructor arguments for generic enum.Enum
.
Use with: PY_DECLARE_ENUM_EX(MyEnum, std::string)("MyEnum", "docs", { ... });
t_cppEnum | C++ enum type |
t_valueType | Python value type |
Definition at line 1021 of file enum_definition.h.
#define PY_SHADOW_STR_ENUM | ( | dllInterface, | |
t_cppEnum ) |
Specializes PyExportTraits for a C++ enum using enum.StrEnum
.
Use this in header files for string-based enums, then use PY_DECLARE_STR_ENUM_* in source files.
dllInterface | Export specification (e.g., LASS_DLL_EXPORT) |
t_cppEnum | C++ enum type |
Definition at line 1041 of file enum_definition.h.
#define PY_DECLARE_STR_ENUM_NAME | ( | t_cppEnum, | |
s_name ) |
Defines the enumDefinition with name only for enum.StrEnum
.
t_cppEnum | C++ enum type |
s_name | Python enum name (const char* string with static storage duration) |
Definition at line 1064 of file enum_definition.h.
#define PY_DECLARE_STR_ENUM_NAME_DOC | ( | t_cppEnum, | |
s_name, | |||
s_doc ) |
Defines the enumDefinition with name and docstring for enum.StrEnum
.
t_cppEnum | C++ enum type |
s_name | Python enum name (const char* string with static storage duration) |
s_doc | Documentation string (const char* string with static storage duration, or nullptr) |
Definition at line 1074 of file enum_definition.h.
#define PY_DECLARE_STR_ENUM_EX | ( | t_cppEnum | ) |
Defines the enumDefinition for initialization with constructor arguments for enum.StrEnum
.
Use with: PY_DECLARE_STR_ENUM_EX(MyEnum)("MyEnum", "docs", { ... });
t_cppEnum | C++ enum type |
Definition at line 1083 of file enum_definition.h.
#define PY_MODULE_ENUM | ( | i_module, | |
t_cppEnum ) |
Adds an enum definition to a Python module.
Use this after declaring the enum with PY_DECLARE_*_ENUM_* macros. The enum will be accessible as module.EnumName in Python.
i_module | Module variable identifier (must be unscoped identifier for token concatenation) |
t_cppEnum | C++ enum type |
PY_DECLARE_MODULE_*
macros. Definition at line 1117 of file enum_definition.h.
#define PY_CLASS_ENUM | ( | i_cppClass, | |
t_cppEnum ) |
Adds an enum definition as a nested enum to a Python class.
Use this after declaring the enum with PY_DECLARE_*_ENUM_* macros. The enum will be accessible as ClassName.EnumName in Python.
i_cppClass | C++ class identifier (must be unscoped identifier for token concatenation) |
t_cppEnum | C++ enum type |
PY_DECLARE_CLASS_*
macros. Definition at line 1144 of file enum_definition.h.
|
strong |
Defines the boundary behavior for enum.IntFlag
-derived enums.
Controls how Python handles flag values that are outside the defined enumerators
Keep
is supported.Enumerator | |
---|---|
Keep | Allow any integer value (most permissive) |
Strict | Only allow exact flag combinations. |
Conform | Mask to defined bits only. |
Definition at line 161 of file enum_definition.h.
LASS_PYTHON_DLL TPyObjPtr lass::python::impl::makeEnumType | ( | const char * | name, |
TPyObjPtr && | enumerators, | ||
TPyObjPtr && | kwargs ) |
Creates a basic enum.Enum
type.
name | Python class name for the enum |
enumerators | Tuple of (name, value) pairs |
kwargs | Additional keyword arguments for enum constructor |
Definition at line 54 of file enum_definition.cpp.
Referenced by lass::python::EnumDefinition< EnumType, ValueType >::doFreezeDefinition(), and makeStrEnumType().
LASS_PYTHON_DLL TPyObjPtr lass::python::impl::makeIntEnumType | ( | const char * | name, |
TPyObjPtr && | enumerators, | ||
TPyObjPtr && | kwargs ) |
Creates an enum.IntEnum
type.
name | Python class name for the enum |
enumerators | Tuple of (name, value) pairs |
kwargs | Additional keyword arguments for enum constructor |
enum.IntEnum
type object Definition at line 115 of file enum_definition.cpp.
Referenced by lass::python::IntEnumDefinition< EnumType >::doMakeEnumType().
LASS_PYTHON_DLL TPyObjPtr lass::python::impl::makeIntFlagType | ( | const char * | name, |
TPyObjPtr && | enumerators, | ||
TPyObjPtr && | kwargs, | ||
FlagBoundary | boundary = FlagBoundary::Keep ) |
Creates an enum.IntFlag
type.
name | Python class name for the flag enum |
enumerators | Tuple of (name, value) pairs |
kwargs | Additional keyword arguments for enum constructor |
boundary | Boundary behavior for invalid flag combinations |
enum.IntFlag
type object Definition at line 120 of file enum_definition.cpp.
References lass::python::Keep, and PY_ENFORCE_POINTER.
Referenced by lass::python::IntFlagDefinition< EnumType >::doMakeEnumType().
LASS_PYTHON_DLL TPyObjPtr lass::python::impl::makeStrEnumType | ( | const char * | name, |
TPyObjPtr && | enumerators, | ||
TPyObjPtr && | kwargs ) |
Creates an enum.StrEnum
type (or equivalent for Python < 3.11).
name | Python class name for the string enum |
enumerators | Tuple of (name, value) pairs where values are strings |
kwargs | Additional keyword arguments for enum constructor |
enum.StrEnum
type object Definition at line 152 of file enum_definition.cpp.
References makeEnumType(), and PY_ENFORCE_POINTER.
Referenced by lass::python::StrEnumDefinition< EnumType, ValueType >::doFreezeDefinition().