Skip to content

Commit

Permalink
Make functions with signature void(string&, void*) delegatable
Browse files Browse the repository at this point in the history
  • Loading branch information
speth authored and ischoegl committed Apr 18, 2023
1 parent 3e07f34 commit 7537d15
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
23 changes: 23 additions & 0 deletions include/cantera/base/Delegator.h
Expand Up @@ -172,6 +172,19 @@ class Delegator
*m_funcs_v_cAMr_cUSr[name] = makeDelegate(func, when, *m_funcs_v_cAMr_cUSr[name]);
}

//! set delegates for member functions with the signature
//! `void(const string&, void*)`
void setDelegate(const string& name,
const function<void(const string&, void*)>& func,
const string& when)
{
if (!m_funcs_v_csr_vp.count(name)) {
throw NotImplementedError("Delegator::setDelegate",
"for function named '{}' with signature 'void(const string&, void*)'.");
}
*m_funcs_v_csr_vp[name] = makeDelegate(func, when, *m_funcs_v_csr_vp[name]);
}

//! Set delegates for member functions with the signature `void(double*)`
void setDelegate(const std::string& name,
const std::function<void(std::array<size_t, 1>, double*)>& func,
Expand Down Expand Up @@ -326,6 +339,15 @@ class Delegator
m_funcs_v_cAMr_cUSr[name] = &target;
}

//! Install a function with the signature `void(const string&, void*) as being
//! delegatable
void install(const string& name, function<void(const string&, void*)>& target,
const function<void(const string&, void*)>& func)
{
target = func;
m_funcs_v_csr_vp[name] = &target;
}

//! Install a function with the signature `void(double*)` as being delegatable
void install(const std::string& name,
std::function<void(std::array<size_t, 1>, double*)>& target,
Expand Down Expand Up @@ -504,6 +526,7 @@ class Delegator
map<string, function<void(AnyMap&)>*> m_funcs_v_AMr;
std::map<std::string,
std::function<void(const AnyMap&, const UnitStack&)>*> m_funcs_v_cAMr_cUSr;
map<string, function<void(const string&, void*)>*> m_funcs_v_csr_vp;
std::map<std::string,
std::function<void(std::array<size_t, 1>, double*)>*> m_funcs_v_dp;
std::map<std::string,
Expand Down
3 changes: 3 additions & 0 deletions interfaces/cython/cantera/delegator.pxd
Expand Up @@ -44,6 +44,7 @@ cdef extern from "cantera/base/Delegator.h" namespace "Cantera":
void setDelegate(string&, function[void(double)], string&) except +translate_exception
void setDelegate(string&, function[void(CxxAnyMap&)], string&) except +translate_exception
void setDelegate(string&, function[void(const CxxAnyMap&, const CxxUnitStack&)], string&) except +translate_exception
void setDelegate(string&, function[void(const string&, void*)], string&) except +translate_exception
void setDelegate(string&, function[void(size_array1, double*)], string&) except +translate_exception
void setDelegate(string&, function[void(size_array1, double, double*)], string&) except +translate_exception
void setDelegate(string&, function[void(size_array2, double, double*, double*)], string&) except +translate_exception
Expand All @@ -62,6 +63,8 @@ cdef extern from "cantera/cython/funcWrapper.h":
cdef function[void(CxxAnyMap&)] pyOverride(PyObject*, void(PyFuncInfo&, CxxAnyMap&))
cdef function[void(const CxxAnyMap&, const CxxUnitStack&)] pyOverride(
PyObject*, void(PyFuncInfo&, const CxxAnyMap&, const CxxUnitStack&))
cdef function[void(const string&, void*)] pyOverride(
PyObject*, void(PyFuncInfo&, const string&, void*))
cdef function[void(size_array1, double*)] pyOverride(
PyObject*, void(PyFuncInfo&, size_array1, double*))
cdef function[void(size_array1, double, double*)] pyOverride(
Expand Down
13 changes: 13 additions & 0 deletions interfaces/cython/cantera/delegator.pyx
Expand Up @@ -139,6 +139,16 @@ cdef void callback_v_cAMr_cUSr(PyFuncInfo& funcInfo, const CxxAnyMap& arg1,
funcInfo.setExceptionType(<PyObject*>exc_type)
funcInfo.setExceptionValue(<PyObject*>exc_value)

# Wrapper for functions of type void(const string&, void*)
cdef void callback_v_csr_vp(PyFuncInfo& funcInfo,
const string& arg1, void* obj) noexcept:
try:
(<object>funcInfo.func())(pystr(arg1), <object>obj)
except BaseException as e:
exc_type, exc_value = sys.exc_info()[:2]
funcInfo.setExceptionType(<PyObject*>exc_type)
funcInfo.setExceptionValue(<PyObject*>exc_value)

# Wrapper for functions of type void(double*)
cdef void callback_v_dp(PyFuncInfo& funcInfo, size_array1 sizes, double* arg) noexcept:
cdef double[:] view = <double[:sizes[0]]>arg if sizes[0] else None
Expand Down Expand Up @@ -329,6 +339,9 @@ cdef int assign_delegates(obj, CxxDelegator* delegator) except -1:
elif callback == 'void(AnyMap&,UnitStack&)':
delegator.setDelegate(cxx_name,
pyOverride(<PyObject*>method, callback_v_cAMr_cUSr), cxx_when)
elif callback == 'void(string,void*)':
delegator.setDelegate(cxx_name,
pyOverride(<PyObject*>method, callback_v_csr_vp), cxx_when)
elif callback == 'void(double*)':
delegator.setDelegate(cxx_name,
pyOverride(<PyObject*>method, callback_v_dp), cxx_when)
Expand Down

0 comments on commit 7537d15

Please sign in to comment.