35#ifndef NEKTAR_LIBUTILITIES_PYTHON_BASICUTILS_NEKFACTORY_HPP
36#define NEKTAR_LIBUTILITIES_PYTHON_BASICUTILS_NEKFACTORY_HPP
60template <std::
size_t N>
62 : integral_constant<std::size_t, N + 1>
78 py::incref(obj.ptr());
83 py::decref(
m_obj.ptr());
91 template <
class... Args> std::shared_ptr<T>
create(Args... args)
94 py::object inst =
m_obj(args...);
97 return py::extract<std::shared_ptr<T>>(inst);
109#if PY_MAJOR_VERSION == 2
150template <
template <
typename,
typename,
typename...>
typename tFac,
151 typename tBase,
typename tKey,
typename... tParam>
169 DoRegister(
name, helper, std::index_sequence_for<tParam...>{});
174 std::string key =
"_" +
name;
178#if PY_MAJOR_VERSION == 2
179 py::object capsule(py::handle<>(
180 PyCObject_FromVoidPtr(helper, NekFactoryCapsuleDestructor<tBase>)));
182 py::object capsule(py::handle<>(PyCapsule_New(
183 helper,
nullptr, NekFactoryCapsuleDestructor<tBase>)));
187 py::import(
"__main__").attr(key.c_str()) = capsule;
195 template <std::size_t... Is>
198 std::integer_sequence<std::size_t, Is...>)
200 m_fac.RegisterCreatorFunction(
208 tFac<tKey, tBase, tParam...> &
m_fac;
void NekFactoryCapsuleDestructor(PyObject *ptr)
C-style callback to destroy the NekFactoryRegisterHelper class to avoid memory leaks.
NekFactory_Register(tFac< tKey, tBase, tParam... > &fac)
void DoRegister(std::string const &name, NekFactoryRegisterHelper< tBase > *helper, std::integer_sequence< std::size_t, Is... >)
Helper function to unpack parameter arguments into the factory's register creation function using the...
void operator()(std::string const &name, py::object &obj)
tFac< tKey, tBase, tParam... > & m_fac
Reference to the NekFactory.
Helper class to be used in NekFactory_Register. Stores the Python function used to call the creator f...
~NekFactoryRegisterHelper()
std::shared_ptr< T > create(Args... args)
Callback to Python instantiator function (typically a class constructor). Assumes that all arguments ...
NekFactoryRegisterHelper(py::object obj)
py::object m_obj
Python function that is used to construct objects.