Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
pybindaliases.hpp File Reference

Pybind11 include and definitions. More...

#include "shambase/call_lambda.hpp"
#include "shambase/unique_name_macro.hpp"
#include <pybind11/pybind11.h>
Include dependency graph for pybindaliases.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define _internal_register_pybind_init(funcname, lambda_name, varname)
 Internal helper that creates static symbols to register a Python init function via a static initializer. It declares funcname, creates a call_lambda object that calls register_pybind_init_func(funcname) at startup, and defines funcname.
#define ON_PYTHON_INIT
 Register a Python module init function using static initialization.

Typedefs

using fct_sig = std::function<void(py::module &)>
 alias to pybind11 namespace

Functions

void register_pybind_init_func (fct_sig)
 Register a python module init function to be ran on init.

Detailed Description

Pybind11 include and definitions.

Author
Timothée David–Cléris (tim.s.nosp@m.hamr.nosp@m.ock@p.nosp@m.roto.nosp@m.n.me)

If we build shamrock executable we embed python in Shamrock hence the include pybind11/embed.h. If we build shamrock as python lib we import pybind11/pybind11.h. Both options defines a similar syntax for the python module definition, we can then wrap them conveniently in a single macro call.

Definition in file pybindaliases.hpp.

Macro Definition Documentation

◆ _internal_register_pybind_init

#define _internal_register_pybind_init ( funcname,
lambda_name,
varname )
Value:
static void funcname(py::module &varname); \
static shambase::call_lambda lambda_name([]() { \
register_pybind_init_func(funcname); \
}); \
static void funcname(py::module &varname)
Execute a lambda when a call_lambda object is constructed.

Internal helper that creates static symbols to register a Python init function via a static initializer. It declares funcname, creates a call_lambda object that calls register_pybind_init_func(funcname) at startup, and defines funcname.

Here the objects/func are static in order to avoid conflicting name in linking. This is similar to anonymous namespaces

Definition at line 45 of file pybindaliases.hpp.

◆ ON_PYTHON_INIT

#define ON_PYTHON_INIT
Value:
__shamrock_unique_name(pybind_), __shamrock_unique_name(pybind_class_obj_), root_module)
#define _internal_register_pybind_init(funcname, lambda_name, varname)
Internal helper that creates static symbols to register a Python init function via a static initializ...

Register a Python module init function using static initialization.

Generates unique symbols automatically, making it convenient for one-shot initializations in .cpp files.

Usage (in a .cpp file) :

// Define things in the python module object `root_module` like so :
root_module.def("hello", []() { return "Hello from SHAMROCK!"; });
}
#define ON_PYTHON_INIT
Register a Python module init function using static initialization.

Definition at line 69 of file pybindaliases.hpp.

Typedef Documentation

◆ fct_sig

using fct_sig = std::function<void(py::module &)>

alias to pybind11 namespace

function signature used to register python modules

Definition at line 32 of file pybindaliases.hpp.

Function Documentation

◆ register_pybind_init_func()

void register_pybind_init_func ( fct_sig fct)

Register a python module init function to be ran on init.

Register a python module init function to be ran on init.

Definition at line 59 of file pybindings.cpp.