[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"
This commit is contained in:
Ray Speth 2016-09-11 23:48:09 -04:00
parent 09629e27ee
commit e332fcbba2
21 changed files with 132 additions and 67 deletions

View file

@ -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<M>

View file

@ -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 <iostream>
#include "cantera/base/config.h"
#include <stdlib.h>
#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 <typename T>
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

51
src/clib/clib_utils.h Normal file
View file

@ -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 <iostream>
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 <typename T>
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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -16,6 +16,7 @@
#include "cantera/kinetics/InterfaceKinetics.h"
#include "clib/clib_defs.h"
#include "clib/ct.h"
using namespace Cantera;

View file

@ -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 <cstring>

View file

@ -2,6 +2,7 @@
* @file mixturemethods.cpp
*/
#include <iostream>
#include <vector>
#include "clib/ctmultiphase.h"
#include "clib/ct.h"

View file

@ -1,11 +1,11 @@
#include <iostream>
#include <string>
#include <vector>
#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);

View file

@ -5,6 +5,8 @@
#include "ctmatutils.h"
#include "clib/ct.h"
#include <vector>
void phasemethods(int nlhs, mxArray* plhs[],
int nrhs, const mxArray* prhs[])
{

View file

@ -1,12 +1,12 @@
#include <iostream>
#include <string>
#include <vector>
#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[])

View file

@ -4,6 +4,7 @@
#include "clib/ct.h"
#include "ctmatutils.h"
#include <vector>
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));

View file

@ -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");