Skip to content

Commit

Permalink
Using std::function
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl authored and speth committed Aug 5, 2023
1 parent 8f320c2 commit ddd803c
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 74 deletions.
2 changes: 1 addition & 1 deletion include/cantera/base/AnyMap.h
Expand Up @@ -189,7 +189,7 @@ class AnyValue : public AnyBase
//! Assign a vector where all the values have the same units
void setQuantity(const vector<double>& values, const std::string& units);

typedef std::function<void(AnyValue&, const UnitSystem&)> unitConverter;
typedef function<void(AnyValue&, const UnitSystem&)> unitConverter;

//! Assign a value of any type where the unit conversion requires a
//! different behavior besides scaling all values by the same factor
Expand Down
104 changes: 52 additions & 52 deletions include/cantera/base/Delegator.h
Expand Up @@ -114,7 +114,7 @@ class Delegator
}

//! Set delegates for member functions with the signature `void()`.
void setDelegate(const std::string& name, const std::function<void()>& func,
void setDelegate(const std::string& name, const function<void()>& func,
const std::string& when)
{
if (!m_funcs_v.count(name)) {
Expand All @@ -125,7 +125,7 @@ class Delegator
}

//! set delegates for member functions with the signature `void(bool)`
void setDelegate(const std::string& name, const std::function<void(bool)>& func,
void setDelegate(const std::string& name, const function<void(bool)>& func,
const std::string& when)
{
if (!m_funcs_v_b.count(name)) {
Expand All @@ -136,7 +136,7 @@ class Delegator
}

//! set delegates for member functions with the signature `void(double)`
void setDelegate(const std::string& name, const std::function<void(double)>& func,
void setDelegate(const std::string& name, const function<void(double)>& func,
const std::string& when)
{
if (!m_funcs_v_d.count(name)) {
Expand All @@ -160,7 +160,7 @@ class Delegator
//! set delegates for member functions with the signature
//! `void(AnyMap&, UnitStack&)`
void setDelegate(const std::string& name,
const std::function<void(const AnyMap&, const UnitStack&)>& func,
const function<void(const AnyMap&, const UnitStack&)>& func,
const std::string& when)
{
if (!m_funcs_v_cAMr_cUSr.count(name)) {
Expand All @@ -187,7 +187,7 @@ class Delegator

//! 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,
const function<void(std::array<size_t, 1>, double*)>& func,
const std::string& when)
{
if (!m_funcs_v_dp.count(name)) {
Expand All @@ -200,7 +200,7 @@ class Delegator
//! Set delegates for member functions with the signature `void(double, double*)`
void setDelegate(
const std::string& name,
const std::function<void(std::array<size_t, 1>, double, double*)>& func,
const function<void(std::array<size_t, 1>, double, double*)>& func,
const std::string& when)
{
if (!m_funcs_v_d_dp.count(name)) {
Expand All @@ -215,7 +215,7 @@ class Delegator
//! `void(double, double*, double*)`
void setDelegate(
const std::string& name,
const std::function<void(std::array <size_t, 2>, double, double*, double*)>& func,
const function<void(std::array <size_t, 2>, double, double*, double*)>& func,
const std::string& when)
{
if (!m_funcs_v_d_dp_dp.count(name)) {
Expand All @@ -230,7 +230,7 @@ class Delegator
//! `void(double*, double*, double*)`
void setDelegate(
const std::string& name,
const std::function<void(std::array<size_t, 3>, double*, double*, double*)>& func,
const function<void(std::array<size_t, 3>, double*, double*, double*)>& func,
const std::string& when)
{
if (!m_funcs_v_dp_dp_dp.count(name)) {
Expand All @@ -243,7 +243,7 @@ class Delegator

//! set delegates for member functions with the signature `double(void*)`
void setDelegate(const std::string& name,
const std::function<int(double&, void*)>& func,
const function<int(double&, void*)>& func,
const std::string& when)
{
if (!m_funcs_d_vp.count(name)) {
Expand All @@ -255,7 +255,7 @@ class Delegator

//! Set delegates for member functions with the signature `string(size_t)`
void setDelegate(const std::string& name,
const std::function<int(std::string&, size_t)>& func,
const function<int(std::string&, size_t)>& func,
const std::string& when)
{
if (!m_funcs_s_sz.count(name)) {
Expand All @@ -268,7 +268,7 @@ class Delegator

//! Set delegates for member functions with the signature `size_t(string)`
void setDelegate(const std::string& name,
const std::function<int(size_t&, const std::string&)>& func,
const function<int(size_t&, const std::string&)>& func,
const std::string& when)
{
if (!m_funcs_sz_csr.count(name)) {
Expand Down Expand Up @@ -298,24 +298,24 @@ class Delegator

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

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

//! Install a function with the signature `void(double)` as being delegatable
void install(const std::string& name, std::function<void(double)>& target,
const std::function<void(double)>& func)
void install(const std::string& name, function<void(double)>& target,
const function<void(double)>& func)
{
target = func;
m_funcs_v_d[name] = &target;
Expand All @@ -332,8 +332,8 @@ class Delegator
//! Install a function with the signature `void(const AnyMap&, const UnitStack&)`
//! as being delegatable
void install(const std::string& name,
std::function<void(const AnyMap&, const UnitStack&)>& target,
const std::function<void(const AnyMap&, const UnitStack&)>& func)
function<void(const AnyMap&, const UnitStack&)>& target,
const function<void(const AnyMap&, const UnitStack&)>& func)
{
target = func;
m_funcs_v_cAMr_cUSr[name] = &target;
Expand All @@ -350,17 +350,17 @@ class Delegator

//! 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,
const std::function<void(std::array<size_t, 1>, double*)>& func)
function<void(std::array<size_t, 1>, double*)>& target,
const function<void(std::array<size_t, 1>, double*)>& func)
{
target = func;
m_funcs_v_dp[name] = &target;
}

//! Install a function with the signature `void(double, double*)` as being delegatable
void install(const std::string& name,
std::function<void(std::array<size_t, 1>, double, double*)>& target,
const std::function<void(std::array<size_t, 1>, double, double*)>& func)
function<void(std::array<size_t, 1>, double, double*)>& target,
const function<void(std::array<size_t, 1>, double, double*)>& func)
{
target = func;
m_funcs_v_d_dp[name] = &target;
Expand All @@ -369,8 +369,8 @@ class Delegator
//! Install a function with the signature `void(double, double*, double*)` as being
//! delegatable
void install(const std::string& name,
std::function<void(std::array<size_t, 2>, double, double*, double*)>& target,
const std::function<void(std::array<size_t, 2>, double, double*, double*)>& func)
function<void(std::array<size_t, 2>, double, double*, double*)>& target,
const function<void(std::array<size_t, 2>, double, double*, double*)>& func)
{
target = func;
m_funcs_v_d_dp_dp[name] = &target;
Expand All @@ -379,25 +379,25 @@ class Delegator
//! Install a function with the signature
//! `void(double*, double*, double*)` as being delegatable
void install(const std::string& name,
std::function<void(std::array<size_t, 3>, double*, double*, double*)>& target,
const std::function<void(std::array<size_t, 3>, double*, double*, double*)>& base)
function<void(std::array<size_t, 3>, double*, double*, double*)>& target,
const function<void(std::array<size_t, 3>, double*, double*, double*)>& base)
{
target = base;
m_funcs_v_dp_dp_dp[name] = &target;
}

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

//! Install a function with the signature `string(size_t)` as being delegatable
void install(const std::string& name,
std::function<std::string(size_t)>& target,
const std::function<std::string(size_t)>& base)
function<std::string(size_t)>& target,
const function<std::string(size_t)>& base)
{
target = base;
m_funcs_s_sz[name] = &target;
Expand All @@ -406,8 +406,8 @@ class Delegator

//! Install a function with the signature `size_t(string)` as being delegatable
void install(const std::string& name,
std::function<size_t(const std::string&)>& target,
const std::function<size_t(const std::string&)>& base)
function<size_t(const std::string&)>& target,
const function<size_t(const std::string&)>& base)
{
target = base;
m_funcs_sz_csr[name] = &target;
Expand All @@ -416,8 +416,8 @@ class Delegator

//! Create a delegate for a function with no return value
template <typename BaseFunc, class ... Args>
std::function<void(Args ...)> makeDelegate(
const std::function<void(Args ...)>& func,
function<void(Args ...)> makeDelegate(
const function<void(Args ...)>& func,
const std::string& when,
BaseFunc base)
{
Expand All @@ -444,11 +444,11 @@ class Delegator

//! Create a delegate for a function with a return value
template <typename ReturnType, class ... Args>
std::function<ReturnType(Args ...)> makeDelegate(
function<ReturnType(Args ...)> makeDelegate(
const std::string& name,
const std::function<int(ReturnType&, Args ...)>& func,
const function<int(ReturnType&, Args ...)>& func,
const std::string& when,
const std::function<ReturnType(Args ...)>& base)
const function<ReturnType(Args ...)>& base)
{
if (when == "before") {
return [base, func](Args ... args) {
Expand Down Expand Up @@ -520,35 +520,35 @@ class Delegator
//! @{

// Delegates with no return value
std::map<std::string, std::function<void()>*> m_funcs_v;
std::map<std::string, std::function<void(bool)>*> m_funcs_v_b;
std::map<std::string, std::function<void(double)>*> m_funcs_v_d;
std::map<std::string, function<void()>*> m_funcs_v;
std::map<std::string, function<void(bool)>*> m_funcs_v_b;
std::map<std::string, function<void(double)>*> m_funcs_v_d;
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;
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;
function<void(std::array<size_t, 1>, double*)>*> m_funcs_v_dp;
std::map<std::string,
std::function<void(std::array<size_t, 1>, double, double*)>*> m_funcs_v_d_dp;
function<void(std::array<size_t, 1>, double, double*)>*> m_funcs_v_d_dp;
std::map<std::string,
std::function<void(std::array<size_t, 2>, double, double*, double*)>*> m_funcs_v_d_dp_dp;
function<void(std::array<size_t, 2>, double, double*, double*)>*> m_funcs_v_d_dp_dp;
std::map<std::string,
std::function<void(std::array<size_t, 3>, double*, double*, double*)>*> m_funcs_v_dp_dp_dp;
function<void(std::array<size_t, 3>, double*, double*, double*)>*> m_funcs_v_dp_dp_dp;

// Delegates with a return value
std::map<std::string, std::function<double(void*)>> m_base_d_vp;
std::map<std::string, std::function<double(void*)>*> m_funcs_d_vp;
std::map<std::string, function<double(void*)>> m_base_d_vp;
std::map<std::string, function<double(void*)>*> m_funcs_d_vp;

std::map<std::string,
std::function<std::string(size_t)>> m_base_s_sz;
function<std::string(size_t)>> m_base_s_sz;
std::map<std::string,
std::function<std::string(size_t)>*> m_funcs_s_sz;
function<std::string(size_t)>*> m_funcs_s_sz;

std::map<std::string,
std::function<size_t(const std::string&)>> m_base_sz_csr;
function<size_t(const std::string&)>> m_base_sz_csr;
std::map<std::string,
std::function<size_t(const std::string&)>*> m_funcs_sz_csr;
function<size_t(const std::string&)>*> m_funcs_sz_csr;
//! @}

//! Handles to wrappers for the delegated object in external language interfaces.
Expand Down
6 changes: 3 additions & 3 deletions include/cantera/base/ExtensionManager.h
Expand Up @@ -100,7 +100,7 @@ class ExtensionManager
//! Register a function that can be used to create wrappers for Solution objects in
//! an external language and link it to the corresponding C++ objects
static void registerSolutionLinker(const std::string& wrapperName,
std::function<shared_ptr<ExternalHandle>(shared_ptr<Solution>)> link);
function<shared_ptr<ExternalHandle>(shared_ptr<Solution>)> link);

//! Get the Solution wrapper type corresponding to the specified user-defined
//! reaction rate type.
Expand All @@ -109,11 +109,11 @@ class ExtensionManager
protected:
//! Functions for wrapping and linking ReactionData objects
static std::map<std::string,
std::function<void(ReactionDataDelegator&)>> s_ReactionData_linkers;
function<void(ReactionDataDelegator&)>> s_ReactionData_linkers;

//! Functions for wrapping and linking Solution objects
static std::map<std::string,
std::function<shared_ptr<ExternalHandle>(shared_ptr<Solution>)>> s_Solution_linkers;
function<shared_ptr<ExternalHandle>(shared_ptr<Solution>)>> s_Solution_linkers;

//! Mapping from user-defined rate types to Solution wrapper types
static map<string, string> s_userTypeToWrapperType;
Expand Down
4 changes: 2 additions & 2 deletions include/cantera/base/FactoryBase.h
Expand Up @@ -77,7 +77,7 @@ class Factory : public FactoryBase {
}

//! Register a new object construction function
void reg(const std::string& name, std::function<T*(Args...)> f) {
void reg(const std::string& name, function<T*(Args...)> f) {
m_creators[name] = f;
}

Expand Down Expand Up @@ -123,7 +123,7 @@ class Factory : public FactoryBase {
}

private:
std::unordered_map<std::string, std::function<T*(Args...)>> m_creators;
std::unordered_map<std::string, function<T*(Args...)>> m_creators;

//! Map of synonyms to canonical names
std::unordered_map<std::string, std::string> m_synonyms;
Expand Down
2 changes: 1 addition & 1 deletion include/cantera/equil/ChemEquil.h
Expand Up @@ -256,7 +256,7 @@ class ChemEquil
//! it is computed. It's initialized to #m_mm.
size_t m_nComponents;

std::function<double(ThermoPhase&)> m_p1, m_p2;
function<double(ThermoPhase&)> m_p1, m_p2;

//! Current value of the mole fractions in the single phase. length = #m_kk.
vector<double> m_molefractions;
Expand Down
2 changes: 1 addition & 1 deletion include/cantera/kinetics/ReactionRateDelegator.h
Expand Up @@ -58,7 +58,7 @@ class ReactionDataDelegator : public Delegator, public ReactionData
shared_ptr<ExternalHandle> m_wrappedData;

//! Delegated `update` method taking the Solution wrapper as its argument
std::function<double(void*)> m_update;
function<double(void*)> m_update;
};

//! Delegate methods of the ReactionRate class to external functions
Expand Down
26 changes: 13 additions & 13 deletions include/cantera/zeroD/ReactorDelegator.h
Expand Up @@ -186,19 +186,19 @@ class ReactorDelegator : public Delegator, public R, public ReactorAccessor
}

private:
std::function<void(double)> m_initialize;
std::function<void()> m_syncState;
std::function<void(std::array<size_t, 1>, double*)> m_getState;
std::function<void(std::array<size_t, 1>, double*)> m_updateState;
std::function<void(std::array<size_t, 1>, double*)> m_updateSurfaceState;
std::function<void(std::array<size_t, 1>, double*)> m_getSurfaceInitialConditions;
std::function<void(bool)> m_updateConnected;
std::function<void(std::array<size_t, 2>, double, double*, double*)> m_eval;
std::function<void(double)> m_evalWalls;
std::function<void(std::array<size_t, 3>, double*, double*, double*)> m_evalSurfaces;
std::function<std::string(size_t)> m_componentName;
std::function<size_t(const std::string&)> m_componentIndex;
std::function<size_t(const std::string&)> m_speciesIndex;
function<void(double)> m_initialize;
function<void()> m_syncState;
function<void(std::array<size_t, 1>, double*)> m_getState;
function<void(std::array<size_t, 1>, double*)> m_updateState;
function<void(std::array<size_t, 1>, double*)> m_updateSurfaceState;
function<void(std::array<size_t, 1>, double*)> m_getSurfaceInitialConditions;
function<void(bool)> m_updateConnected;
function<void(std::array<size_t, 2>, double, double*, double*)> m_eval;
function<void(double)> m_evalWalls;
function<void(std::array<size_t, 3>, double*, double*, double*)> m_evalSurfaces;
function<std::string(size_t)> m_componentName;
function<size_t(const std::string&)> m_componentIndex;
function<size_t(const std::string&)> m_speciesIndex;
};

}
Expand Down

0 comments on commit ddd803c

Please sign in to comment.