Library of Assembled Shared Sources
lass::python::impl::ClassDefinition Class Reference

Definition of a Python class. More...

#include <class_definition.h>

Public Types

typedef int TSlotID
 Function to call during registration of the class (optional).
 

Public Member Functions

 ClassDefinition (const char *name, const char *doc, Py_ssize_t typeSize, richcmpfunc richcmp, ClassDefinition *parent, TClassRegisterHook registerHook)
 Construct a class definition.
 
 ~ClassDefinition ()
 Destructor.
 
const char * name () const
 Get the class name.
 
const char * doc () const
 Get the class docstring.
 
void setDoc (const char *doc)
 Set the class docstring.
 
void setDocIfNotNull (const char *doc)
 Set the class docstring if non-null (keeps existing one if nullptr).
 
void addConstructor (newfunc dispatcher, newfunc &overloadChain)
 Add a constructor overload (__init__ dispatcher).
 
PyObject * freezeDefinition (PyObject *module=nullptr)
 Finalize the definition and create the Python type.
 
PyObject * callRichCompare (PyObject *self, PyObject *other, int op)
 Dispatch rich-compare for this class (used by operator slots).
 
PyTypeObject * type ()
 Get the Python type object (available after freezeDefinition() has been called).
 
const PyTypeObject * type () const
 Get the Python type object (available after freezeDefinition() has been called).
 
void addMethod (const char *name, const char *doc, PyCFunction dispatcher, OverloadLink &overloadChain)
 Add a named method.
 
void addMethod (const ComparatorSlot &slot, const char *doc, PyCFunction dispatcher, OverloadLink &overloadChain)
 Add a comparator-slot method (rich compare operator).
 
void addMethod (const UnarySlot &slot, const char *doc, unaryfunc dispatcher, OverloadLink &overloadChain)
 Add a unary-slot method (e.g., __neg__, __pos__, ...).
 
void addMethod (const BinarySlot &slot, const char *doc, binaryfunc dispatcher, OverloadLink &overloadChain)
 Add a binary-slot method (e.g., arithmetic, comparisons).
 
void addMethod (const TernarySlot &slot, const char *doc, ternaryfunc dispatcher, OverloadLink &overloadChain)
 Add a ternary-slot method.
 
void addMethod (const LenSlot &slot, const char *doc, lenfunc dispatcher, OverloadLink &overloadChain)
 Add a length-slot method (__len__).
 
void addMethod (const SsizeArgSlot &slot, const char *doc, ssizeargfunc dispatcher, OverloadLink &overloadChain)
 Add an ssize-arg-slot method.
 
void addMethod (const SsizeObjArgSlot &slot, const char *doc, ssizeobjargproc dispatcher, OverloadLink &overloadChain)
 Add an ssize-obj-arg-slot method.
 
void addMethod (const ObjObjSlot &slot, const char *doc, objobjproc dispatcher, OverloadLink &overloadChain)
 Add an obj-obj-slot method.
 
void addMethod (const ObjObjArgSlot &slot, const char *doc, objobjargproc dispatcher, OverloadLink &overloadChain)
 Add an obj-obj-arg-slot method.
 
void addMethod (const IterSlot &slot, const char *doc, getiterfunc dispatcher, OverloadLink &overloadChain)
 Add an iterator-slot method (__iter__).
 
void addMethod (const IterNextSlot &slot, const char *doc, iternextfunc dispatcher, OverloadLink &overloadChain)
 Add an iternext-slot method (__next__).
 
void addMethod (const ArgKwSlot &slot, const char *doc, ternaryfunc dispatcher, OverloadLink &overloadChain)
 Add an arg+kw-slot method (callable semantics).
 
void addMethod (const InquirySlot &slot, const char *doc, inquiry dispatcher, OverloadLink &overloadChain)
 Add an inquiry-slot method.
 
void addGetSetter (const char *name, const char *doc, getter get, setter set)
 Add a property with optional getter/setter.
 
void addStaticMethod (const char *name, const char *doc, PyCFunction dispatcher, PyCFunction &overloadChain)
 Add a static method with overload support.
 
void addInnerClass (ClassDefinition &innerClass)
 Add a nested class definition (inner class).
 
void addInnerEnum (EnumDefinitionBase *enumDefinition)
 Add a nested enum definition (inner enum).
 
void * getSlot (TSlotID slotId)
 Get raw pointer from a given slot id.
 
void * setSlot (TSlotID slotId, void *value)
 Set raw pointer for a given slot id.
 
template<typename Ptr>
Ptr setSlot (TSlotID slotId, Ptr value)
 Get raw pointer from a given slot id.
 

Friends

LASS_PYTHON_DLL PyObject *LASS_CALL establishMagicalBackLinks (PyObject *result, PyObject *self)
 Here, we try to fix some lifetime issues to guarantee some lifetime requirements on self.
 

Detailed Description

Definition of a Python class.

Collects all information required to define a Python type that wraps a C++ class. The definition accumulates constructors, methods (including Python slot methods), properties, static members, and nested types. When ready, call freezeDefinition() to create the underlying PyTypeObject and (optionally) inject it into a module or as a nested type.

The ClassDefinition class is typically not used directly by user code. Instead, use the provided macros in pyobject_macros.h that work with ClassDefinition instances.

Definition at line 273 of file class_definition.h.

Member Typedef Documentation

◆ TSlotID

Function to call during registration of the class (optional).

Identifier type for indexing into the internal Python slot table.

Definition at line 277 of file class_definition.h.

Constructor & Destructor Documentation

◆ ClassDefinition()

lass::python::impl::ClassDefinition::ClassDefinition ( const char * name,
const char * doc,
Py_ssize_t typeSize,
richcmpfunc richcmp,
ClassDefinition * parent,
TClassRegisterHook registerHook )

Construct a class definition.

Parameters
namePython class name
docClass docstring (must outlive the definition unless changed via setDoc())
typeSizeSize of the Python instance struct (derived from PyObject)
richcmpOptional rich-compare function (may be nullptr)
parentOptional parent to nest this class into (may be nullptr)
registerHookOptional hook called when the class is registered

Definition at line 107 of file class_definition.cpp.

References ClassDefinition(), doc(), and name().

Referenced by addInnerClass(), and ClassDefinition().

Member Function Documentation

◆ setDoc()

void lass::python::impl::ClassDefinition::setDoc ( const char * doc)

Set the class docstring.

Note: the provided string must remain valid until another docstring is set.

Definition at line 174 of file class_definition.cpp.

References doc().

Referenced by setDocIfNotNull().

◆ addConstructor()

void lass::python::impl::ClassDefinition::addConstructor ( newfunc dispatcher,
newfunc & overloadChain )

Add a constructor overload (__init__ dispatcher).

Parameters
dispatcherNew-function dispatcher
overloadChainReference to overload chain

Definition at line 224 of file class_definition.cpp.

References setSlot().

◆ addMethod()

void lass::python::impl::ClassDefinition::addMethod ( const char * name,
const char * doc,
PyCFunction dispatcher,
OverloadLink & overloadChain )

Add a named method.

Parameters
name,slotPython method name or special slot
docMethod docstring
dispatcherDispatcher implementing the method
overloadChainOverload chain for this name

Definition at line 230 of file class_definition.cpp.

References doc(), and name().

◆ freezeDefinition()

PyObject * lass::python::impl::ClassDefinition::freezeDefinition ( PyObject * module = nullptr)

Finalize the definition and create the Python type.

If a module is provided, the type is added to that module.

Definition at line 352 of file class_definition.cpp.

References freezeDefinition().

Referenced by freezeDefinition(), and lass::python::ModuleDefinition::injectClass().


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