Library of Assembled Shared Sources
 
Loading...
Searching...
No Matches
Python Macro Name Conventions

Every export macro is composed as:

PY_<scope>_[FREE_]<kind>[_R|_RW][_QUALIFIED|_CAST][_NAME][_DOC][_<N>]
PY_<scope>_[FREE_]<kind>[_R|_RW][_QUALIFIED|_CAST]_EX[_<N>]

where <scope> is MODULE or CLASS, and <kind> is FUNCTION, METHOD, STATIC_METHOD MEMBER, PUBLIC_MEMBER, CONSTRUCTOR, INNER_CLASS, ...

Every element is optional and they always appear in this order. So PY_CLASS_FREE_MEMBER_RW_NAME_DOC parses as class scope, free function form, property, read/write, custom name, with docstring..

Macro Parameter Prefixes

The macro parameters use Hungarian prefixes to tell you what is expected of the argument:

Name, Docstring, and Dispatcher Name

Nearly all export macros take common suffixes to add an optional custom Python name or docstring. They all forward to the _EX variant that provides full control over the parameters and the dispatcher name.

At the class scope, the _EX form is unique in that it allows you to pass fully qualified class names, which the other forms cannot since they build the dispatcher name from it.

Normally, you will not be using this _EX variant, but one of the top four:

Suffix Adds parameters Use for ... Example
- - Python name = C++ name PY_MODULE_FUNCTION
_NAME s_name Custom Python name PY_MODULE_FUNCTION_NAME
_DOC s_doc With docstring PY_MODULE_FUNCTION_DOC
_NAME_DOC s_name, s_doc Custom Python name + Docstring PY_MODULE_FUNCTION_NAME_DOC
_EX s_name, s_doc, i_dispatcher Custom dispatcher / Qualified class PY_MODULE_FUNCTION_EX

Examples:

void spam(int a);
PY_MODULE_FUNCTION (foo, spam) // foo.spam
PY_MODULE_FUNCTION_DOC (foo, spam, "eat it") // foo.spam, documented
PY_MODULE_FUNCTION_NAME (foo, spam, "eggs") // foo.eggs
PY_MODULE_FUNCTION_NAME_DOC(foo, spam, "eggs", "eat it") // foo.eggs, documented
PY_MODULE_FUNCTION_EX (foo, spam, "eggs", "eat it", dsp) // ... + explicit dispatcher
#define PY_MODULE_FUNCTION_NAME_DOC(i_module, f_cppFunction, s_name, s_doc)
Export a C++ function to Python, with custom name and docstring.
#define PY_MODULE_FUNCTION(i_module, f_cppFunction)
Export a C++ function to Python.
#define PY_MODULE_FUNCTION_NAME(i_module, f_cppFunction, s_name)
Export a C++ function to Python, with custom Python name.
#define PY_MODULE_FUNCTION_EX(i_module, f_cppFunction, s_functionName, s_doc, i_dispatcher)
Export a C++ function to Python, with full control.
#define PY_MODULE_FUNCTION_DOC(i_module, f_cppFunction, s_doc)
Export a C++ function to Python, with docstring.
Qualifying Overloaded Signatures

Macros for exporting functions also come in variants to fully qualify the function signature to disambiguate overloaded functions, by adding the return type (except for constructors) and all parameter types.

The list of parameter types can be passed as a single lass::meta::TypeTuple, or as individual arguments. For the latter, the _<N> tells the number of arguments.

The _<N> form is the most often used one, and simply packs its types into a TypeTuple

Form Adds parameters Example
_QUALIFIED one lass::meta::TypeTuple PY_MODULE_FUNCTION_QUALIFIED
_QUALIFIED_<N> N loose types, N = 0…15 PY_MODULE_FUNCTION_QUALIFIED_2

Examples:

double spam(int a, float b);
void spam(const std::string& c);
PY_MODULE_FUNCTION_QUALIFIED_2(foo, spam, double, int, float)
PY_MODULE_FUNCTION_QUALIFIED_1(foo, spam, void, const std::string&)
#define PY_MODULE_FUNCTION_QUALIFIED_1(i_module, f_cppFunction, t_return, t_P1)
Export an overloaded 1-ary C++ function to Python.
#define PY_MODULE_FUNCTION_QUALIFIED_2(i_module, f_cppFunction, t_return, t_P1, t_P2)
Export an overloaded 2-ary C++ function to Python.

They combine with the _NAME and _DOC suffixes, with the s_name, s_doc, i_dispatcher arguments following the function return and parameter types:

Suffix Use for ... Example
_QUALIFIED_<N> Python name = C++ name PY_MODULE_FUNCTION_QUALIFIED_2
_QUALIFIED_NAME_<N> Custom Python name PY_MODULE_FUNCTION_QUALIFIED_NAME_2
_QUALIFIED_DOC_<N> With docstring PY_MODULE_FUNCTION_QUALIFIED_DOC_2
_QUALIFIED_NAME_DOC_<N> Custom Python name + Docstring PY_MODULE_FUNCTION_QUALIFIED_NAME_DOC_2
_QUALIFIED_EX_<N> Full control PY_MODULE_FUNCTION_QUALIFIED_EX_2