From 8c1ec6a5f567d48e89917ae1abc99c2455388586 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Thu, 2 Aug 2012 15:47:56 +0000 Subject: [PATCH] Made casting operations in ctonedim safer --- src/clib/Cabinet.h | 13 +++++++++ src/clib/ctonedim.cpp | 67 ++++++++++++++----------------------------- 2 files changed, 35 insertions(+), 45 deletions(-) diff --git a/src/clib/Cabinet.h b/src/clib/Cabinet.h index 0ac6a8157..463256e75 100644 --- a/src/clib/Cabinet.h +++ b/src/clib/Cabinet.h @@ -165,6 +165,19 @@ public: } } + /** + * Return a reference to object n, cast to a reference of the specified type. + */ + template + static T& get(size_t n) { + T* x = dynamic_cast(&item(n)); + if (x == 0) { + throw Cantera::CanteraError("Cabinet::get", + "Item is not of the correct type."); + } + return *x; + } + /** * Return the index in the Cabinet to the specified object, or -1 * if the object is not in the cabinet. diff --git a/src/clib/ctonedim.cpp b/src/clib/ctonedim.cpp index 65420d869..7636d904d 100644 --- a/src/clib/ctonedim.cpp +++ b/src/clib/ctonedim.cpp @@ -25,26 +25,6 @@ typedef Cabinet ThermoCabinet; typedef Cabinet KineticsCabinet; typedef Cabinet TransportCabinet; -static StFlow* _stflow(int i) -{ - Domain1D* d = &DomainCabinet::item(i); - if (d->domainType() == cFlowType) { - return dynamic_cast(d); - } else { - throw CanteraError("_stflow","wrong domain type"); - } - return 0; -} - -static Bdry1D* _bdry(int i) -{ - Domain1D* d = &DomainCabinet::item(i); - if (! d->isConnector()) { - throw CanteraError("_bdry","wrong domain type: " +int2str(d->domainType())); - } - return dynamic_cast(d); -} - extern "C" { int domain_clear() @@ -304,7 +284,7 @@ extern "C" { int bdry_setMdot(int i, double mdot) { try { - _bdry(i)->setMdot(mdot); + DomainCabinet::get(i).setMdot(mdot); return 0; } catch (...) { return handleAllExceptions(-1, ERR); @@ -314,7 +294,7 @@ extern "C" { int bdry_setTemperature(int i, double t) { try { - _bdry(i)->setTemperature(t); + DomainCabinet::get(i).setTemperature(t); return 0; } catch (...) { return handleAllExceptions(-1, ERR); @@ -324,7 +304,7 @@ extern "C" { int bdry_setMoleFractions(int i, char* x) { try { - _bdry(i)->setMoleFractions(string(x)); + DomainCabinet::get(i).setMoleFractions(string(x)); return 0; } catch (...) { return handleAllExceptions(-1, ERR); @@ -334,7 +314,7 @@ extern "C" { double bdry_temperature(int i) { try { - return _bdry(i)->temperature(); + return DomainCabinet::get(i).temperature(); } catch (...) { return handleAllExceptions(DERR, DERR); } @@ -343,7 +323,7 @@ extern "C" { double bdry_massFraction(int i, int k) { try { - return _bdry(i)->massFraction(k); + return DomainCabinet::get(i).massFraction(k); } catch (...) { return handleAllExceptions(DERR, DERR); } @@ -352,7 +332,7 @@ extern "C" { double bdry_mdot(int i) { try { - return _bdry(i)->mdot(); + return DomainCabinet::get(i).mdot(); } catch (...) { return handleAllExceptions(DERR, DERR); } @@ -361,10 +341,9 @@ extern "C" { int reactingsurf_setkineticsmgr(int i, int j) { try { - ReactingSurf1D* srf = (ReactingSurf1D*)_bdry(i); - InterfaceKinetics* k = - dynamic_cast(&Cabinet::item(j)); - srf->setKineticsMgr(k); + InterfaceKinetics& k = + dynamic_cast(Cabinet::item(j)); + DomainCabinet::get(i).setKineticsMgr(&k); return 0; } catch (...) { return handleAllExceptions(-1, ERR); @@ -374,8 +353,7 @@ extern "C" { int reactingsurf_enableCoverageEqs(int i, int onoff) { try { - ReactingSurf1D* srf = (ReactingSurf1D*)_bdry(i); - srf->enableCoverageEquations(onoff != 0); + DomainCabinet::get(i).enableCoverageEquations(onoff != 0); return 0; } catch (...) { return handleAllExceptions(-1, ERR); @@ -385,8 +363,7 @@ extern "C" { int inlet_setSpreadRate(int i, double v) { try { - Inlet1D* inlt = (Inlet1D*)_bdry(i); - inlt->setSpreadRate(v); + DomainCabinet::get(i).setSpreadRate(v); return 0; } catch (...) { return handleAllExceptions(-1, ERR); @@ -398,14 +375,14 @@ extern "C" { int stflow_new(int iph, int ikin, int itr, int itype) { try { - IdealGasPhase* ph = dynamic_cast(&ThermoCabinet::item(iph)); + IdealGasPhase& ph = dynamic_cast(ThermoCabinet::item(iph)); if (itype == 1) { - AxiStagnFlow* x = new AxiStagnFlow(ph, ph->nSpecies(), 2); + AxiStagnFlow* x = new AxiStagnFlow(&ph, ph.nSpecies(), 2); x->setKinetics(KineticsCabinet::item(ikin)); x->setTransport(TransportCabinet::item(itr)); return DomainCabinet::add(x); } else if (itype == 2) { - FreeFlame* x = new FreeFlame(ph, ph->nSpecies(), 2); + FreeFlame* x = new FreeFlame(&ph, ph.nSpecies(), 2); x->setKinetics(KineticsCabinet::item(ikin)); x->setTransport(TransportCabinet::item(itr)); return DomainCabinet::add(x); @@ -425,7 +402,7 @@ extern "C" { if (iSoret > 0) { withSoret = true; } - _stflow(i)->setTransport(TransportCabinet::item(itr), withSoret); + DomainCabinet::get(i).setTransport(TransportCabinet::item(itr), withSoret); return 0; } catch (...) { return handleAllExceptions(-1, ERR); @@ -439,7 +416,7 @@ extern "C" { if (iSoret > 0) { withSoret = true; } - _stflow(i)->enableSoret(withSoret); + DomainCabinet::get(i).enableSoret(withSoret); return 0; } catch (...) { return handleAllExceptions(-1, ERR); @@ -449,7 +426,7 @@ extern "C" { int stflow_setPressure(int i, double p) { try { - _stflow(i)->setPressure(p); + DomainCabinet::get(i).setPressure(p); return 0; } catch (...) { return handleAllExceptions(-1, ERR); @@ -465,7 +442,7 @@ extern "C" { vpos[j] = pos[j]; vtemp[j] = temp[j]; } - _stflow(i)->setFixedTempProfile(vpos, vtemp); + DomainCabinet::get(i).setFixedTempProfile(vpos, vtemp); return 0; } catch (...) { return handleAllExceptions(-1, ERR); @@ -477,9 +454,9 @@ extern "C" { { try { if (flag > 0) { - _stflow(i)->solveSpecies(npos); + DomainCabinet::get(i).solveSpecies(npos); } else { - _stflow(i)->fixSpecies(npos); + DomainCabinet::get(i).fixSpecies(npos); } return 0; } catch (...) { @@ -492,9 +469,9 @@ extern "C" { { try { if (flag > 0) { - _stflow(i)->solveEnergyEqn(npos); + DomainCabinet::get(i).solveEnergyEqn(npos); } else { - _stflow(i)->fixTemperature(npos); + DomainCabinet::get(i).fixTemperature(npos); } return 0; } catch (...) {