Skip to content

Commit

Permalink
Add holdExternalHandle to Delegator
Browse files Browse the repository at this point in the history
  • Loading branch information
speth authored and ischoegl committed Apr 18, 2023
1 parent 6c45457 commit ae7b158
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 15 deletions.
24 changes: 20 additions & 4 deletions include/cantera/base/Delegator.h
Expand Up @@ -7,6 +7,7 @@
#define CT_DELEGATOR_H

#include "cantera/base/global.h"
#include "cantera/base/Units.h"
#include "cantera/base/ctexceptions.h"
#include "cantera/base/ExtensionManager.h"
#include <array>
Expand Down Expand Up @@ -254,8 +255,21 @@ class Delegator
*m_funcs_sz_csr[name] = makeDelegate(name, func, when, m_base_sz_csr[name]);
}

void holdExternalHandle(const shared_ptr<ExternalHandle>& handle) {
m_handles.push_back(handle);
//! Store a handle to a wrapper for the delegate from an external language interface
void holdExternalHandle(const string& name,
const shared_ptr<ExternalHandle>& handle) {
m_handles[name] = handle;
}

//! Get the handle for a wrapper for the delegate from the external language
//! interface specified by *name*.
//! Returns a null pointer if the requested handle does not exist.
shared_ptr<ExternalHandle> getExternalHandle(const string& name) const {
if (m_handles.count(name)) {
return m_handles.at(name);
} else {
return shared_ptr<ExternalHandle>();
}
}

protected:
Expand Down Expand Up @@ -496,8 +510,10 @@ class Delegator
std::function<size_t(const std::string&)>*> m_funcs_sz_csr;
//! @}

//! Cleanup functions to be called from the destructor
std::list<shared_ptr<ExternalHandle>> m_handles;
//! Handles to wrappers for the delegated object in external language interfaces.
//! Used to provide access to these wrappers as well as managing cleanup functions
//! called from the destructor of the derived ExternalHandle class.
map<string, shared_ptr<ExternalHandle>> m_handles;

//! Name of the class in the extension language
std::string m_delegatorName;
Expand Down
2 changes: 1 addition & 1 deletion include/cantera/base/Solution.h
Expand Up @@ -137,7 +137,7 @@ class Solution : public std::enable_shared_from_this<Solution>

AnyMap m_header; //!< Additional input fields; usually from a YAML input file

//! Wrappers for this Kinetics object in extension languages, for evaluation
//! Wrappers for this Solution object in extension languages, for evaluation
//! of user-defined reaction rates
std::map<std::string, shared_ptr<ExternalHandle>> m_externalHandles;

Expand Down
11 changes: 11 additions & 0 deletions interfaces/cython/cantera/delegator.pxd
Expand Up @@ -22,11 +22,22 @@ cdef extern from "<array>" namespace "std" nogil:
size_t& operator[](size_t)


cdef extern from "cantera/extensions/PythonHandle.h" namespace "Cantera":
cdef cppclass CxxExternalHandle "Cantera::ExternalHandle":
pass

cdef cppclass CxxPythonHandle "Cantera::PythonHandle" (CxxExternalHandle):
CxxPythonHandle(PyObject*, cbool)
PyObject* get()


cdef extern from "cantera/base/Delegator.h" namespace "Cantera":
cdef cppclass CxxDelegator "Cantera::Delegator":
Delegator()

void setDelegatorName(string&)
void holdExternalHandle(string&, shared_ptr[CxxExternalHandle]&)
shared_ptr[CxxExternalHandle] getExternalHandle(string&)

void setDelegate(string&, function[void()], string&) except +translate_exception
void setDelegate(string&, function[void(cbool)], string&) except +translate_exception
Expand Down
10 changes: 2 additions & 8 deletions interfaces/cython/cantera/solutionbase.pxd
Expand Up @@ -8,6 +8,8 @@ import numpy as np
cimport numpy as np

from .ctcxx cimport *
from .delegator cimport CxxExternalHandle


cdef extern from "cantera/thermo/ThermoFactory.h" namespace "Cantera":
cdef cppclass CxxThermoPhase "Cantera::ThermoPhase"
Expand All @@ -29,14 +31,6 @@ cdef extern from "cantera/base/Interface.h" namespace "Cantera":
string, string, vector[string]) except +translate_exception


cdef extern from "cantera/extensions/PythonHandle.h" namespace "Cantera":
cdef cppclass CxxExternalHandle "Cantera::ExternalHandle":
pass

cdef cppclass CxxPythonHandle "Cantera::PythonHandle" (CxxExternalHandle):
CxxPythonHandle(PyObject*, cbool)


cdef extern from "cantera/base/Solution.h" namespace "Cantera":
cdef cppclass CxxKinetics "Cantera::Kinetics"
cdef cppclass CxxTransport "Cantera::Transport"
Expand Down
2 changes: 1 addition & 1 deletion interfaces/cython/cantera/solutionbase.pyx
Expand Up @@ -12,7 +12,7 @@ from .kinetics cimport *
from .transport cimport *
from .reaction cimport *
from ._utils cimport *
from .delegator cimport pyOverride, callback_v
from .delegator cimport pyOverride, callback_v, CxxPythonHandle
from .yamlwriter cimport YamlWriter

ctypedef CxxSurfPhase* CxxSurfPhasePtr
Expand Down
3 changes: 2 additions & 1 deletion src/extensions/PythonExtensionManager.cpp
Expand Up @@ -99,7 +99,8 @@ void PythonExtensionManager::registerRateBuilder(
delegator->setParameters(params, units);

// The delegator is responsible for eventually deleting the Python object
delegator->holdExternalHandle(make_shared<PythonHandle>(extRate, false));
delegator->holdExternalHandle("python",
make_shared<PythonHandle>(extRate, false));
return delegator.release();
};
ReactionRateFactory::factory()->reg(rateName, builder);
Expand Down

0 comments on commit ae7b158

Please sign in to comment.