Library of Assembled Shared Sources
|
Definition of an enum.StrEnum
-derived enum type in Python.
More...
#include <enum_definition.h>
Public Member Functions | |
void | addEnumerator (std::string name, TEnum enumerator, TValue value) |
Add an enumerator to the definition. | |
PyObject * | build (TEnum value) const |
Creates a Python enum instance from a C++ enum value. | |
int | get (PyObject *obj, TEnum &value) const |
Extracts a C++ enum value from a Python object. | |
const char * | name () const |
The name of the Python enum type. | |
const char * | doc () const |
Optional docstring for the Python enum type. | |
PyObject * | type () const |
The Python enum type object, after freezeDefinition() has been called. | |
PyObject * | freezeDefinition (const char *moduleName, const char *scopeName) |
Freeze the enum definition and create the Python enum type. | |
Protected Member Functions | |
TPyObjPtr | doFreezeDefinition (TPyObjPtr &&kwargs) override |
Creates an enum.StrEnum -derived type. | |
TPyObjPtr | doValueObject (PyObject *obj) const override |
Extracts string values from enum instances or string objects. | |
TPyObjPtr | freezeEnumerators () |
Converts stored enumerators to Python tuple format and builds lookup tables. | |
TPyObjPtr | valueObject (PyObject *obj) const |
Returns the value of an enum instances as a Python object. | |
Definition of an enum.StrEnum
-derived enum type in Python.
C++ enums exported using StrEnumDefinition will generate a new enum type derived from enum.StrEnum
(or enum.Enum
+ str for Python < 3.11). The values of the Python enum will be strings, and how they map on the C++ enum values needs to be defined at export.
As enum.StrEnum
is used, the Python enum instances will also be valid str instances, and str instances are accepted when converting from Python to C++.
Because this defines a new type, it must be added to a module or other class.
This class is typically not used directly. Use PY_SHADOW_STR_ENUM and PY_DECLARE_STR_ENUM_* macros instead.
Definition at line 788 of file enum_definition.h.
|
inlineoverrideprotectedvirtual |
Creates an enum.StrEnum
-derived type.
Processes enumerators and creates a Python string enum type.
Reimplemented from lass::python::EnumDefinition< EnumType, std::string >.
Definition at line 797 of file enum_definition.h.
References lass::python::EnumDefinition< EnumType, std::string >::freezeEnumerators(), lass::python::impl::makeStrEnumType(), and lass::python::EnumDefinition< EnumType, std::string >::name().
|
inlineoverrideprotectedvirtual |
Extracts string values from enum instances or string objects.
Handles both string enum instances and raw Python strings.
Reimplemented from lass::python::EnumDefinitionBase.
Definition at line 805 of file enum_definition.h.
References lass::python::EnumDefinition< EnumType, std::string >::type().
|
inlineinherited |
Add an enumerator to the definition.
name | Python name for the enumerator |
enumerator | C++ enum value |
value | Python value for the enumerator |
Definition at line 636 of file enum_definition.h.
|
inlineinherited |
Creates a Python enum instance from a C++ enum value.
Looks up the Python value corresponding to the C++ enum and creates an enum instance.
Definition at line 644 of file enum_definition.h.
|
inlineinherited |
Extracts a C++ enum value from a Python object.
Accepts enum instances and compatible value types. Returns 0 on success, 1 on failure.
Definition at line 661 of file enum_definition.h.
|
inlineprotectedinherited |
Converts stored enumerators to Python tuple format and builds lookup tables.
Creates bidirectional mappings between C++ enum values and Python values.
Definition at line 711 of file enum_definition.h.
Referenced by lass::python::StrEnumDefinition< EnumType, ValueType >::doFreezeDefinition().
|
inherited |
Freeze the enum definition and create the Python enum type.
PY_MODULE_ENUM()
or PY_CLASS_ENUM()
to add the enum to a module or class, which will call this method at the appropriate time. Definition at line 234 of file enum_definition.cpp.
References type(), and lass::python::EnumDefinitionBase::type().
|
protectedinherited |
Returns the value of an enum instances as a Python object.
If the object is not an instance of this enum type, a Python exception will be set and a null pointer will be returned.
Definition at line 253 of file enum_definition.cpp.