Library of Assembled Shared Sources
|
Definition of an enum.IntEnum
-derived enum type in Python.
More...
#include <enum_definition.h>
Data Structures | |
struct | Enumerator |
Represents a single enumerator member with name and C++ enum value. More... | |
Public Member Functions | |
IntEnumDefinition (const char *name) | |
Construct with enum name only. | |
IntEnumDefinition (const char *name, const char *doc) | |
Construct with name and documentation. | |
IntEnumDefinition (const char *name, std::initializer_list< Enumerator > enumerators) | |
Construct with name and enumerators list. | |
IntEnumDefinition (const char *name, const char *doc, std::initializer_list< Enumerator > enumerators) | |
Construct with name, documentation, and enumerators list. | |
void | addEnumerator (std::string name, TEnum value) |
Add an enumerator member 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.IntEnum -derived type. | |
TPyObjPtr | doValueObject (PyObject *obj) const override |
Extracts integer values from enum instances or integer objects. | |
virtual TPyObjPtr | doMakeEnumType (TPyObjPtr &&enumerators, TPyObjPtr &&kwargs) |
Creates the actual enum.IntEnum type object. | |
TPyObjPtr | valueObject (PyObject *obj) const |
Returns the value of an enum instances as a Python object. | |
Definition of an enum.IntEnum
-derived enum type in Python.
C++ enums exported using IntEnumDefinition will generate a new enum type derived from enum.IntEnum
. The values of the Python enum will map directly on the C++ enum values, but then names need to be defined at export.
As enum.IntEnum
is used, the Python enum instances will also be valid int instances, and int 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_INT_ENUM and PY_DECLARE_INT_ENUM_* macros instead.
Definition at line 312 of file enum_definition.h.
|
inline |
Construct with enum name only.
name | Python class name for the enum type (must have static storage duration) |
Definition at line 328 of file enum_definition.h.
References lass::python::EnumDefinitionBase::EnumDefinitionBase(), and lass::python::EnumDefinitionBase::name().
Referenced by lass::python::IntFlagDefinition< EnumType >::IntFlagDefinition().
|
inline |
Construct with name and documentation.
name | Python class name for the enum type (must have static storage duration) |
doc | Optional docstring for the Python enum type (must have static storage duration, or nullptr) |
Definition at line 337 of file enum_definition.h.
References lass::python::EnumDefinitionBase::doc(), lass::python::EnumDefinitionBase::EnumDefinitionBase(), and lass::python::EnumDefinitionBase::name().
|
inline |
Construct with name and enumerators list.
name | Python class name for the enum type (must have static storage duration) |
enumerators | List of name-value pairs defining the enum members |
Definition at line 346 of file enum_definition.h.
References lass::python::EnumDefinitionBase::EnumDefinitionBase(), and lass::python::EnumDefinitionBase::name().
|
inline |
Construct with name, documentation, and enumerators list.
name | Python class name for the enum type (must have static storage duration) |
doc | Optional docstring for the Python enum type (must have static storage duration, or nullptr) |
enumerators | List of name-value pairs defining the enum members |
Definition at line 357 of file enum_definition.h.
References lass::python::EnumDefinitionBase::doc(), lass::python::EnumDefinitionBase::EnumDefinitionBase(), and lass::python::EnumDefinitionBase::name().
|
inline |
Add an enumerator member to the definition.
name | Python name for the enumerator member |
value | C++ enum value |
Definition at line 367 of file enum_definition.h.
References lass::python::EnumDefinitionBase::name().
|
inline |
Creates a Python enum instance from a C++ enum value.
Converts the C++ enum to its corresponding Python enum instance.
Definition at line 375 of file enum_definition.h.
References lass::python::EnumDefinitionBase::type().
|
inline |
Extracts a C++ enum value from a Python object.
Accepts both enum instances and raw integers. Returns 0 on success, 1 on failure.
Definition at line 385 of file enum_definition.h.
References lass::python::EnumDefinitionBase::valueObject().
|
inlineoverrideprotectedvirtual |
Creates an enum.IntEnum
-derived type.
Converts enumerators to Python tuple format and creates the integer enum type.
Implements lass::python::EnumDefinitionBase.
Definition at line 407 of file enum_definition.h.
References doMakeEnumType(), lass::python::IntEnumDefinition< EnumType >::Enumerator::enumerator, lass::python::fromSharedPtrToNakedCast(), and lass::python::IntEnumDefinition< EnumType >::Enumerator::name.
|
inlineoverrideprotectedvirtual |
Extracts integer values from enum instances or integer objects.
Handles both integer enum instances and raw Python integers.
Reimplemented from lass::python::EnumDefinitionBase.
Definition at line 431 of file enum_definition.h.
References lass::python::EnumDefinitionBase::type().
|
inlineprotectedvirtual |
Creates the actual enum.IntEnum
type object.
Can be overridden by derived classes to create different enum types (e.g., IntFlag).
Reimplemented in lass::python::IntFlagDefinition< EnumType >.
Definition at line 456 of file enum_definition.h.
References lass::python::impl::makeIntEnumType(), and lass::python::EnumDefinitionBase::name().
Referenced by 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 247 of file enum_definition.cpp.
|
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 230 of file enum_definition.cpp.
Referenced by lass::python::EnumDefinition< EnumType, ValueType >::get(), and lass::python::IntEnumDefinition< EnumType >::get().