From e332fcbba21c304d5412b8f802b09df14656811e Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Sun, 11 Sep 2016 23:48:09 -0400 Subject: [PATCH] [clib] Make clib actually usable as a C (not C++) interface - Cannot include any C++ headers or use C++ types (e.g. 'bool') - Functions arguments cannot have default values - "extern C" needs to be wrapped by "#ifdef __cplusplus" --- src/clib/Cabinet.h | 2 +- src/clib/clib_defs.h | 44 +++--------------------------- src/clib/clib_utils.h | 51 +++++++++++++++++++++++++++++++++++ src/clib/ct.h | 15 +++++++---- src/clib/ctbdry.h | 7 +++++ src/clib/ctfunc.h | 6 +++++ src/clib/ctmultiphase.h | 7 +++++ src/clib/ctonedim.h | 11 +++++--- src/clib/ctreactor.cpp | 2 +- src/clib/ctreactor.h | 8 +++++- src/clib/ctrpath.h | 6 +++++ src/clib/ctsurf.h | 7 ++++- src/clib/ctxml.h | 14 +++++----- src/fortran/fct.cpp | 1 + src/fortran/fctxml.cpp | 2 +- src/matlab/mixturemethods.cpp | 1 + src/matlab/onedimmethods.cpp | 4 +-- src/matlab/phasemethods.cpp | 2 ++ src/matlab/surfmethods.cpp | 2 +- src/matlab/thermomethods.cpp | 5 ++-- src/matlab/xmlmethods.cpp | 2 +- 21 files changed, 132 insertions(+), 67 deletions(-) create mode 100644 src/clib/clib_utils.h diff --git a/src/clib/Cabinet.h b/src/clib/Cabinet.h index 4a25f5f90..99cd1668b 100644 --- a/src/clib/Cabinet.h +++ b/src/clib/Cabinet.h @@ -7,7 +7,7 @@ #include "cantera/base/stringUtils.h" #include "cantera/base/ctexceptions.h" #include "cantera/base/global.h" -#include "clib_defs.h" +#include "clib_utils.h" /** * Template for classes to hold pointers to objects. The Cabinet diff --git a/src/clib/clib_defs.h b/src/clib/clib_defs.h index 37f4c3174..a3f6f9798 100644 --- a/src/clib/clib_defs.h +++ b/src/clib/clib_defs.h @@ -1,13 +1,12 @@ /** * @file clib_defs.h */ + #ifndef CTC_DEFS_H #define CTC_DEFS_H -#include "cantera/base/global.h" -#include "cantera/base/ctexceptions.h" -#include "../base/application.h" -#include +#include "cantera/base/config.h" +#include #ifdef _WIN32 // Windows (MSVC or MinGW) @@ -34,41 +33,4 @@ # define DERR -999.999 #endif -namespace Cantera -{ - -//! Exception handler used at language interface boundaries. -/*! - * When called from a "catch (...)" block, this function will attempt to save - * an error message in global error stack and return a value indicating the - * type of exception caught. - * - * @param ctErrorCode Value to return if a CanteraError is caught - * @param otherErrorCode Value to return if a different exception is caught - */ -template -T handleAllExceptions(T ctErrorCode, T otherErrorCode) -{ - // Rethrow the previous exception, then catch a more - // specific exception type if possible. - try { - throw; - } catch (CanteraError& cterr) { - Application::Instance()->addError(cterr.what()); - return ctErrorCode; - } catch (std::exception& err) { - std::cerr << "Cantera: caught an instance of " - << err.what() << std::endl; - Application::Instance()->addError(err.what()); - return otherErrorCode; - } catch (...) { - std::cerr << "Cantera: caught an instance of " - "an unknown exception type" << std::endl; - Application::Instance()->addError("unknown C++ exception"); - return otherErrorCode; - } -} - -} - #endif diff --git a/src/clib/clib_utils.h b/src/clib/clib_utils.h new file mode 100644 index 000000000..bdb92a15e --- /dev/null +++ b/src/clib/clib_utils.h @@ -0,0 +1,51 @@ +/** + * @file clib_utils.h + */ + +#ifndef CT_CLIB_UTILS_H +#define CT_CLIB_UTILS_H + +#include "cantera/base/global.h" +#include "cantera/base/ctexceptions.h" +#include "../base/application.h" +#include "clib_defs.h" +#include + +namespace Cantera +{ + +//! Exception handler used at language interface boundaries. +/*! + * When called from a "catch (...)" block, this function will attempt to save + * an error message in global error stack and return a value indicating the + * type of exception caught. + * + * @param ctErrorCode Value to return if a CanteraError is caught + * @param otherErrorCode Value to return if a different exception is caught + */ +template +T handleAllExceptions(T ctErrorCode, T otherErrorCode) +{ + // Rethrow the previous exception, then catch a more + // specific exception type if possible. + try { + throw; + } catch (CanteraError& cterr) { + Application::Instance()->addError(cterr.what()); + return ctErrorCode; + } catch (std::exception& err) { + std::cerr << "Cantera: caught an instance of " + << err.what() << std::endl; + Application::Instance()->addError(err.what()); + return otherErrorCode; + } catch (...) { + std::cerr << "Cantera: caught an instance of " + "an unknown exception type" << std::endl; + Application::Instance()->addError("unknown C++ exception"); + return otherErrorCode; + } +} + +} + +#endif diff --git a/src/clib/ct.h b/src/clib/ct.h index d0c21423b..a078fb743 100644 --- a/src/clib/ct.h +++ b/src/clib/ct.h @@ -5,9 +5,11 @@ #define CTC_CT_H #include "clib_defs.h" -#include "cantera/base/config.h" +#ifdef __cplusplus extern "C" { +#endif + CANTERA_CAPI int ct_appdelete(); CANTERA_CAPI size_t phase_nElements(int n); CANTERA_CAPI size_t phase_nSpecies(int n); @@ -48,8 +50,8 @@ extern "C" { CANTERA_CAPI size_t th_nSpecies(size_t n); CANTERA_CAPI int th_eosType(int n); CANTERA_CAPI double th_refPressure(int n); - CANTERA_CAPI double th_minTemp(int n, int k=-1); - CANTERA_CAPI double th_maxTemp(int n, int k=-1); + CANTERA_CAPI double th_minTemp(int n, int k); + CANTERA_CAPI double th_maxTemp(int n, int k); CANTERA_CAPI double th_enthalpy_mole(int n); CANTERA_CAPI double th_intEnergy_mole(int n); CANTERA_CAPI double th_entropy_mole(int n); @@ -98,8 +100,8 @@ extern "C" { CANTERA_CAPI int th_setState_Tsat(int n, double t, double x); CANTERA_CAPI size_t newKineticsFromXML(int mxml, int iphase, - int neighbor1=-1, int neighbor2=-1, int neighbor3=-1, - int neighbor4=-1); + int neighbor1, int neighbor2, int neighbor3, + int neighbor4); CANTERA_CAPI int installRxnArrays(int pxml, int ikin, char* default_phase); CANTERA_CAPI size_t kin_nSpecies(int n); @@ -165,6 +167,9 @@ extern "C" { CANTERA_CAPI int ck_to_cti(char* in_file, char* db_file, char* tr_file, char* id_tag, int debug, int validate); + +#ifdef __cplusplus } +#endif #endif diff --git a/src/clib/ctbdry.h b/src/clib/ctbdry.h index 73036bc77..01ccf5bbc 100644 --- a/src/clib/ctbdry.h +++ b/src/clib/ctbdry.h @@ -6,7 +6,10 @@ #include "clib_defs.h" +#ifdef __cplusplus extern "C" { +#endif + CANTERA_CAPI int bndry_new(int itype); CANTERA_CAPI int bndry_del(int i); CANTERA_CAPI double bndry_temperature(int i); @@ -17,5 +20,9 @@ extern "C" { CANTERA_CAPI double bndry_mdot(int i); CANTERA_CAPI int bndry_setxin(int i, double* xin); CANTERA_CAPI int bndry_setxinbyname(int i, char* xin); + +#ifdef __cplusplus } #endif + +#endif diff --git a/src/clib/ctfunc.h b/src/clib/ctfunc.h index f5c01f7ae..f434efe1b 100644 --- a/src/clib/ctfunc.h +++ b/src/clib/ctfunc.h @@ -6,7 +6,10 @@ #include "clib_defs.h" +#ifdef __cplusplus extern "C" { +#endif + CANTERA_CAPI int func_new(int type, size_t n, size_t lenp, double* p); CANTERA_CAPI int func_del(int i); CANTERA_CAPI int func_clear(); @@ -15,6 +18,9 @@ extern "C" { CANTERA_CAPI int func_derivative(int i); CANTERA_CAPI int func_duplicate(int i); CANTERA_CAPI int func_write(int i, size_t lennm, const char* arg, char* nm); + +#ifdef __cplusplus } +#endif #endif diff --git a/src/clib/ctmultiphase.h b/src/clib/ctmultiphase.h index 34f805cf2..97894aafb 100644 --- a/src/clib/ctmultiphase.h +++ b/src/clib/ctmultiphase.h @@ -6,7 +6,10 @@ #include "clib_defs.h" +#ifdef __cplusplus extern "C" { +#endif + CANTERA_CAPI int mix_new(); CANTERA_CAPI int mix_del(int i); CANTERA_CAPI int mix_clear(); @@ -52,5 +55,9 @@ extern "C" { CANTERA_CAPI size_t mix_speciesPhaseIndex(int i, int k); CANTERA_CAPI double mix_moleFraction(int i, int k); + +#ifdef __cplusplus } #endif + +#endif diff --git a/src/clib/ctonedim.h b/src/clib/ctonedim.h index 2d93c5143..3d4b36961 100644 --- a/src/clib/ctonedim.h +++ b/src/clib/ctonedim.h @@ -5,9 +5,11 @@ #define CTC_ONEDIM_H #include "clib_defs.h" -#include "cantera/base/config.h" +#ifdef __cplusplus extern "C" { +#endif + CANTERA_CAPI int domain_clear(); CANTERA_CAPI int domain_del(int i); CANTERA_CAPI int domain_type(int i); @@ -50,7 +52,7 @@ extern "C" { CANTERA_CAPI int inlet_setSpreadRate(int i, double v); - CANTERA_CAPI int stflow_new(int iph, int ikin, int itr, int itype=1); + CANTERA_CAPI int stflow_new(int iph, int ikin, int itr, int itype); CANTERA_CAPI int stflow_setTransport(int i, int itr, int iSoret); CANTERA_CAPI int stflow_enableSoret(int i, int iSoret); CANTERA_CAPI int stflow_setPressure(int i, double p); @@ -77,7 +79,7 @@ extern "C" { CANTERA_CAPI int sim1D_save(int i, char* fname, char* id, char* desc); CANTERA_CAPI int sim1D_restore(int i, char* fname, char* id); - CANTERA_CAPI int sim1D_writeStats(int i, int printTime = 1); + CANTERA_CAPI int sim1D_writeStats(int i, int printTime); CANTERA_CAPI int sim1D_domainIndex(int i, char* name); CANTERA_CAPI double sim1D_value(int i, int idom, int icomp, int localPoint); CANTERA_CAPI double sim1D_workValue(int i, int idom, @@ -90,6 +92,9 @@ extern "C" { CANTERA_CAPI int sim1D_evalSSJacobian(int i); CANTERA_CAPI double sim1D_jacobian(int i, int m, int n); CANTERA_CAPI size_t sim1D_size(int i); + +#ifdef __cplusplus } +#endif #endif diff --git a/src/clib/ctreactor.cpp b/src/clib/ctreactor.cpp index f97f92392..f16276e1b 100644 --- a/src/clib/ctreactor.cpp +++ b/src/clib/ctreactor.cpp @@ -169,7 +169,7 @@ extern "C" { } } - int reactor_setChemistry(int i, bool cflag) + int reactor_setChemistry(int i, int cflag) { try { // @todo This should not fail silently diff --git a/src/clib/ctreactor.h b/src/clib/ctreactor.h index 06eb9178b..a77569b6a 100644 --- a/src/clib/ctreactor.h +++ b/src/clib/ctreactor.h @@ -6,12 +6,15 @@ #include "clib_defs.h" +#ifdef __cplusplus extern "C" { +#endif + CANTERA_CAPI int reactor_new(int type); CANTERA_CAPI int reactor_del(int i); CANTERA_CAPI int reactor_copy(int i); CANTERA_CAPI int reactor_setInitialVolume(int i, double v); - CANTERA_CAPI int reactor_setChemistry(int i, bool cflag); + CANTERA_CAPI int reactor_setChemistry(int i, int cflag); CANTERA_CAPI int reactor_setEnergy(int i, int eflag); CANTERA_CAPI int reactor_setThermoMgr(int i, int n); CANTERA_CAPI int reactor_setKineticsMgr(int i, int n); @@ -79,6 +82,9 @@ extern "C" { CANTERA_CAPI int reactorsurface_addSensitivityReaction(int i, int rxn); CANTERA_CAPI int clear_reactors(); + +#ifdef __cplusplus } +#endif #endif diff --git a/src/clib/ctrpath.h b/src/clib/ctrpath.h index 95be4bbba..986065e39 100644 --- a/src/clib/ctrpath.h +++ b/src/clib/ctrpath.h @@ -6,7 +6,10 @@ #include "clib_defs.h" +#ifdef __cplusplus extern "C" { +#endif + CANTERA_CAPI int rdiag_new(); CANTERA_CAPI int rdiag_del(int i); CANTERA_CAPI int rdiag_detailed(int i); @@ -36,6 +39,9 @@ extern "C" { int idiag, int iquiet); CANTERA_CAPI int clear_rxnpath(); + +#ifdef __cplusplus } +#endif #endif diff --git a/src/clib/ctsurf.h b/src/clib/ctsurf.h index bb1298b80..5df458f5c 100644 --- a/src/clib/ctsurf.h +++ b/src/clib/ctsurf.h @@ -5,9 +5,11 @@ #define CTC_SURF_H #include "clib_defs.h" -#include "cantera/base/config.h" +#ifdef __cplusplus extern "C" { +#endif + CANTERA_CAPI int surf_setcoverages(int i, double* c, int norm); CANTERA_CAPI int surf_getcoverages(int i, double* c); CANTERA_CAPI int surf_setconcentrations(int i, double* c); @@ -15,6 +17,9 @@ extern "C" { CANTERA_CAPI int surf_setsitedensity(int i, double s0); CANTERA_CAPI double surf_sitedensity(int i); CANTERA_CAPI int surf_setcoveragesbyname(int i, char* c); + +#ifdef __cplusplus } +#endif #endif diff --git a/src/clib/ctxml.h b/src/clib/ctxml.h index b6370e1c6..b3b5e9398 100644 --- a/src/clib/ctxml.h +++ b/src/clib/ctxml.h @@ -6,15 +6,12 @@ #include "clib_defs.h" -#ifdef CANTERA_USE_INTERNAL -#include "cantera/base/config.h" -#else -#include "cantera/base/config.h" +#ifdef __cplusplus +extern "C" { #endif -extern "C" { CANTERA_CAPI int xml_new(const char* name); - CANTERA_CAPI int xml_get_XML_File(const char* file, int debug = 0); + CANTERA_CAPI int xml_get_XML_File(const char* file, int debug); CANTERA_CAPI int xml_del(int i); CANTERA_CAPI int xml_clear(); CANTERA_CAPI int xml_copy(int i); @@ -34,7 +31,10 @@ extern "C" { CANTERA_CAPI int xml_addChildNode(int i, int j); CANTERA_CAPI int xml_write(int i, const char* file); CANTERA_CAPI int xml_removeChild(int i, int j); - CANTERA_CAPI int ctml_getFloatArray(int i, size_t n, double* data, int iconvert=0); + CANTERA_CAPI int ctml_getFloatArray(int i, size_t n, double* data, int iconvert); + +#ifdef __cplusplus } +#endif #endif diff --git a/src/fortran/fct.cpp b/src/fortran/fct.cpp index 396d8c53e..7004aed16 100644 --- a/src/fortran/fct.cpp +++ b/src/fortran/fct.cpp @@ -16,6 +16,7 @@ #include "cantera/kinetics/InterfaceKinetics.h" #include "clib/clib_defs.h" +#include "clib/ct.h" using namespace Cantera; diff --git a/src/fortran/fctxml.cpp b/src/fortran/fctxml.cpp index 41c1ffd4c..a25c47032 100644 --- a/src/fortran/fctxml.cpp +++ b/src/fortran/fctxml.cpp @@ -5,7 +5,7 @@ // Copyright 2001 California Institute of Technology -#include "clib/clib_defs.h" +#include "clib/clib_utils.h" #include "cantera/base/ctml.h" #include diff --git a/src/matlab/mixturemethods.cpp b/src/matlab/mixturemethods.cpp index 6654d5f9d..9d0be444e 100644 --- a/src/matlab/mixturemethods.cpp +++ b/src/matlab/mixturemethods.cpp @@ -2,6 +2,7 @@ * @file mixturemethods.cpp */ #include +#include #include "clib/ctmultiphase.h" #include "clib/ct.h" diff --git a/src/matlab/onedimmethods.cpp b/src/matlab/onedimmethods.cpp index 45c405cc7..308165b24 100644 --- a/src/matlab/onedimmethods.cpp +++ b/src/matlab/onedimmethods.cpp @@ -1,11 +1,11 @@ #include #include +#include #include "ctmatutils.h" #include "clib/ctonedim.h" using namespace std; -using namespace Cantera; void onedimmethods(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) @@ -329,7 +329,7 @@ void onedimmethods(int nlhs, mxArray* plhs[], break; case 108: checkNArgs(3, nrhs); - iok = sim1D_writeStats(dom); + iok = sim1D_writeStats(dom, 1); break; case 109: checkNArgs(4, nrhs); diff --git a/src/matlab/phasemethods.cpp b/src/matlab/phasemethods.cpp index 4f9635d5e..052f99d7d 100644 --- a/src/matlab/phasemethods.cpp +++ b/src/matlab/phasemethods.cpp @@ -5,6 +5,8 @@ #include "ctmatutils.h" #include "clib/ct.h" +#include + void phasemethods(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) { diff --git a/src/matlab/surfmethods.cpp b/src/matlab/surfmethods.cpp index e4e71c393..3a3713e82 100644 --- a/src/matlab/surfmethods.cpp +++ b/src/matlab/surfmethods.cpp @@ -1,12 +1,12 @@ #include #include +#include #include "ctmatutils.h" #include "clib/ctsurf.h" #include "clib/ct.h" using namespace std; -using namespace Cantera; void surfmethods(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) diff --git a/src/matlab/thermomethods.cpp b/src/matlab/thermomethods.cpp index 398f1589c..e3fe6aa68 100644 --- a/src/matlab/thermomethods.cpp +++ b/src/matlab/thermomethods.cpp @@ -4,6 +4,7 @@ #include "clib/ct.h" #include "ctmatutils.h" +#include static void thermoset(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) @@ -164,10 +165,10 @@ static void thermoget(int nlhs, mxArray* plhs[], vv = th_refPressure(n); break; case 16: - vv = th_minTemp(n); + vv = th_minTemp(n, -1); break; case 17: - vv = th_maxTemp(n); + vv = th_maxTemp(n, -1); break; case 18: vv = double(th_eosType(n)); diff --git a/src/matlab/xmlmethods.cpp b/src/matlab/xmlmethods.cpp index cc805bde8..ea11b6ae2 100644 --- a/src/matlab/xmlmethods.cpp +++ b/src/matlab/xmlmethods.cpp @@ -113,7 +113,7 @@ void xmlmethods(int nlhs, mxArray* plhs[], break; case 15: file = getString(prhs[3]); - iok = xml_get_XML_File(file); + iok = xml_get_XML_File(file, 0); break; default: mexErrMsgTxt("unknown job parameter");