Library of Assembled Shared Sources
lass::python::ModuleDefinition Class Reference

Definition of a Python module. More...

#include <module_definition.h>

Inheritance diagram for lass::python::ModuleDefinition:
Collaboration diagram for lass::python::ModuleDefinition:

Public Types

typedef util::Callback0 TPreInject
 Callback type for pre-injection hooks (called before module creation).
 
typedef util::Callback1< PyObject * > TPostInject
 Callback type for post-injection hooks (called after module creation with module object).
 

Public Member Functions

 ModuleDefinition (const char *name, const char *doc=0)
 Construct module definition with name and optional documentation.
 
const char * name () const
 Get the module name.
 
void setName (const char *name)
 Set the module name.
 
const char * doc () const
 Get the module documentation string.
 
void setDoc (const char *doc)
 Set the module documentation string.
 
PyObject * module () const
 Get the Python module object (available after inject() has been called).
 
void setPreInject (const TPreInject &callback)
 Set callback to be executed before module injection.
 
void setPostInject (const TPostInject &callback)
 Set callback to be executed after module injection.
 
void addFunctionDispatcher (PyCFunction dispatcher, const char *name, const char *doc, PyCFunction &overloadChain)
 Add a function dispatcher to the module.
 
void addClass (impl::ClassDefinition &classDef)
 Add a class definition to the module.
 
void addEnum (EnumDefinitionBase *enumDef)
 Add an enum definition to the module.
 
void addObject (PyObject *object, const char *name)
 Add an arbitrary Python object to the module.
 
void addLong (long object, const char *name)
 Add a long integer constant to the module.
 
void addString (const char *object, const char *name)
 Add a string constant to the module.
 
void injectLong (const char *name, long value)
 Inject a long integer directly into an already created module.
 
void injectString (const char *name, const char *value)
 Inject a string directly into an already created module.
 
template<typename T>
void injectObject (T &&object, const char *name)
 Inject an arbitrary object directly into an already created module.
 
bool injectClass (impl::ClassDefinition &classDef)
 Inject a class definition directly into an already created module.
 
PyObject * inject ()
 Create and inject the Python module with all accumulated definitions.
 

Detailed Description

Definition of a Python module.

Holds the definition of a Python module and can create the module by calling inject(). This class manages all components that make up a Python module: functions, classes, enums, and other objects.

The module definition accumulates all components during the program initialization phase, then creates the actual Python module when inject() is called.

This class is typically not used directly by user code. Use the provided macros in pyobject_macros.h that work with ModuleDefinition instances.

Definition at line 117 of file module_definition.h.

Constructor & Destructor Documentation

◆ ModuleDefinition()

lass::python::ModuleDefinition::ModuleDefinition ( const char * name,
const char * doc = 0 )

Construct module definition with name and optional documentation.

Parameters
namePython module name (will be copied and stored internally)
docOptional module docstring (will be copied and stored internally, or nullptr)

Definition at line 73 of file module_definition.cpp.

References doc(), name(), setDoc(), and setName().

Member Function Documentation

◆ setName()

void lass::python::ModuleDefinition::setName ( const char * name)

Set the module name.

Parameters
nameNew module name (will be copied and stored internally)

Definition at line 113 of file module_definition.cpp.

References name().

Referenced by ModuleDefinition().

◆ setDoc()

void lass::python::ModuleDefinition::setDoc ( const char * doc)

Set the module documentation string.

Parameters
docNew docstring (will be copied and stored internally, or nullptr)

Definition at line 118 of file module_definition.cpp.

References doc().

Referenced by ModuleDefinition().

◆ setPreInject()

void lass::python::ModuleDefinition::setPreInject ( const TPreInject & callback)

Set callback to be executed before module injection.

callback called at start of module injection

Useful for performing setup tasks before the module is created.

Parameters
callbackFunction to call before injection

Definition at line 125 of file module_definition.cpp.

◆ setPostInject()

void lass::python::ModuleDefinition::setPostInject ( const TPostInject & callback)

Set callback to be executed after module injection.

callback called at end of module injection, with module as parameter

Useful for performing additional setup on the created module.

Parameters
callbackFunction to call with the module object after injection

Definition at line 132 of file module_definition.cpp.

◆ addFunctionDispatcher()

void lass::python::ModuleDefinition::addFunctionDispatcher ( PyCFunction dispatcher,
const char * name,
const char * doc,
PyCFunction & overloadChain )

Add a function dispatcher to the module.

Used internally by function export macros to register C++ functions.

Parameters
dispatcherFunction dispatcher for overload resolution
namePython function name
docFunction documentation string
overloadChainReference to overload chain for this function name

Definition at line 181 of file module_definition.cpp.

References doc(), and name().

◆ addClass()

void lass::python::ModuleDefinition::addClass ( impl::ClassDefinition & classDef)

Add a class definition to the module.

new-style class addition.

The class will be created and added when the module is injected.

Parameters
classDefClass definition containing class export information

This function adds a class definition to a table that will be injected when the module is.

Definition at line 141 of file module_definition.cpp.

◆ addEnum()

void lass::python::ModuleDefinition::addEnum ( EnumDefinitionBase * enumDef)

Add an enum definition to the module.

The enum type will be created and added when the module is injected.

Parameters
enumDefEnum definition containing enum export information

Definition at line 147 of file module_definition.cpp.

◆ addObject()

void lass::python::ModuleDefinition::addObject ( PyObject * object,
const char * name )

Add an arbitrary Python object to the module.

The object will be added directly to the module namespace.

Parameters
objectPython object to add
nameName for the object in the module namespace

Definition at line 153 of file module_definition.cpp.

References name().

◆ addLong()

void lass::python::ModuleDefinition::addLong ( long object,
const char * name )

Add a long integer constant to the module.

Parameters
objectLong value to add as module constant
nameName for the constant in the module namespace

Definition at line 162 of file module_definition.cpp.

References name().

◆ addString()

void lass::python::ModuleDefinition::addString ( const char * object,
const char * name )

Add a string constant to the module.

Parameters
objectString value to add as module constant
nameName for the string in the module namespace

Definition at line 171 of file module_definition.cpp.

References name().

◆ injectLong()

void lass::python::ModuleDefinition::injectLong ( const char * name,
long value )

Inject a long integer directly into an already created module.

This is for immediate injection, unlike addLong() which defers until inject().

Parameters
nameName for the constant in the module namespace
valueLong value to inject

Definition at line 198 of file module_definition.cpp.

References name().

◆ injectString()

void lass::python::ModuleDefinition::injectString ( const char * name,
const char * value )

Inject a string directly into an already created module.

This is for immediate injection, unlike addString() which defers until inject().

Parameters
nameName for the string in the module namespace
valueString value to inject

Definition at line 205 of file module_definition.cpp.

References name().

◆ injectObject()

template<typename T>
void lass::python::ModuleDefinition::injectObject ( T && object,
const char * name )
inline

Inject an arbitrary object directly into an already created module.

This is for immediate injection, unlike addObject() which defers until inject().

Parameters
objectObject to inject (will be converted to Python object)
nameName for the object in the module namespace

Definition at line 222 of file module_definition.h.

References name().

◆ injectClass()

bool lass::python::ModuleDefinition::injectClass ( impl::ClassDefinition & classDef)

Inject a class definition directly into an already created module.

old-style class injection.

Parameters
classDefClass definition to inject
Returns
true on success, false on failure

This function can injects a class in an already-injected module

Deprecated

Definition at line 217 of file module_definition.cpp.

References lass::python::impl::ClassDefinition::freezeDefinition(), and lass::python::impl::ClassDefinition::name().

◆ inject()

PyObject * lass::python::ModuleDefinition::inject ( )

Create and inject the Python module with all accumulated definitions.

This method should typically not be called directly. Use module registration macros which call this at the appropriate time during Python initialization.

Returns
The created Python module object

Definition at line 227 of file module_definition.cpp.

References lass::python::chainErrFormat().


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