Skip to content

Commit

Permalink
Introduce basic registration capabilities for factories
Browse files Browse the repository at this point in the history
  • Loading branch information
speth committed Jul 4, 2016
1 parent 64bb049 commit f985169
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 192 deletions.
32 changes: 32 additions & 0 deletions include/cantera/base/FactoryBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <vector>
#include <mutex>
#include <unordered_map>
#include "cantera/base/ctexceptions.h"

namespace Cantera
{
Expand Down Expand Up @@ -53,6 +55,36 @@ class FactoryBase
//! statically held list of Factories.
static std::vector<FactoryBase*> s_vFactoryRegistry;
};

//! Factory class that supports registering functions to create objects
//!
//! Template arguments for the class are the base type created by the factory,
//! followed by the types of any arguments which need to be passed to the
//! functions used to create objects, e.g. arguments to the constructor.
template <class T, typename ... Args>
class Factory : public FactoryBase {
public:
virtual ~Factory() {}

//! Create an object using the object construction function corresponding to
//! "name" and the provided constructor arguments
T* create(const std::string& name, Args... args) {
try {
return m_creators.at(name)(args...);
} catch (std::out_of_range&) {
throw CanteraError("Factory::create", "No such type: '{}'", name);
}
}

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

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

}

#endif
4 changes: 2 additions & 2 deletions include/cantera/kinetics/FalloffFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace Cantera
*
* @ingroup falloffGroup
*/
class FalloffFactory : public FactoryBase
class FalloffFactory : public Factory<Falloff>
{
public:
/**
Expand Down Expand Up @@ -64,7 +64,7 @@ class FalloffFactory : public FactoryBase
static FalloffFactory* s_factory;

//! default constructor, which is defined as private
FalloffFactory() {}
FalloffFactory();

//! Mutex for use when calling the factory
static std::mutex falloff_mutex;
Expand Down
4 changes: 2 additions & 2 deletions include/cantera/kinetics/KineticsFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class UnknownKineticsModel : public CanteraError
/**
* Factory for kinetics managers.
*/
class KineticsFactory : public FactoryBase
class KineticsFactory : public Factory<Kinetics>
{
public:
static KineticsFactory* factory() {
Expand Down Expand Up @@ -70,7 +70,7 @@ class KineticsFactory : public FactoryBase

private:
static KineticsFactory* s_factory;
KineticsFactory() {}
KineticsFactory();
static std::mutex kinetics_mutex;
};

Expand Down
6 changes: 3 additions & 3 deletions include/cantera/thermo/ThermoFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class UnknownThermoPhaseModel : public CanteraError
* This class keeps a list of the known ThermoPhase classes, and is
* used to create new instances of these classes.
*/
class ThermoFactory : public FactoryBase
class ThermoFactory : public Factory<ThermoPhase>
{
public:
//! Static function that creates a static instance of the factory.
Expand Down Expand Up @@ -82,7 +82,7 @@ class ThermoFactory : public FactoryBase
static ThermoFactory* s_factory;

//! Private constructors prevents usage
ThermoFactory() {};
ThermoFactory();

//! Decl for locking mutex for thermo factory singleton
static std::mutex thermo_mutex;
Expand All @@ -102,7 +102,7 @@ inline ThermoPhase* newThermoPhase(const std::string& model,
if (f == 0) {
f = ThermoFactory::factory();
}
return f->newThermoPhase(model);
return f->create(model);
}

//! Translate the eosType id into a string
Expand Down
4 changes: 1 addition & 3 deletions include/cantera/transport/TransportBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -709,9 +709,7 @@ class Transport
* specification of the collision integrals. defaults to no.
* @param log_level Defaults to zero, no logging
*/
virtual void init(thermo_t* thermo, int mode=0, int log_level=0) {
throw NotImplementedError("Transport::init");
}
virtual void init(thermo_t* thermo, int mode=0, int log_level=0) {}

//! Called by TransportFactory to set parameters.
/*!
Expand Down
8 changes: 5 additions & 3 deletions include/cantera/transport/TransportFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Cantera
*
* @ingroup tranprops
*/
class TransportFactory : public FactoryBase
class TransportFactory : public Factory<Transport>
{
public:
//! Return a pointer to a TransportFactory instance.
Expand Down Expand Up @@ -104,8 +104,7 @@ class TransportFactory : public FactoryBase
* @param thermo ThermoPhase object
* @param log_level log level
*/
virtual Transport*
newTransport(thermo_t* thermo, int log_level=0);
virtual Transport* newTransport(thermo_t* thermo, int log_level=0);

//! Initialize an existing transport manager for liquid phase
/*!
Expand Down Expand Up @@ -242,6 +241,9 @@ class TransportFactory : public FactoryBase
//! Mapping between between the string name for a
//! liquid mixture transport property model and the integer name.
std::map<std::string, LiquidTranMixingModel> m_LTImodelMap;

//! Models included in this map are initialized in CK compatibility mode
std::map<std::string, bool> m_CK_mode;
};

//! Create a new transport manager instance.
Expand Down
4 changes: 2 additions & 2 deletions include/cantera/zeroD/ReactorFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace Cantera
{

class ReactorFactory : FactoryBase
class ReactorFactory : public Factory<ReactorBase>
{
public:
static ReactorFactory* factory() {
Expand All @@ -38,7 +38,7 @@ class ReactorFactory : FactoryBase
private:
static ReactorFactory* s_factory;
static std::mutex reactor_mutex;
ReactorFactory() {}
ReactorFactory();
};

inline ReactorBase* newReactor(const std::string& model,
Expand Down
28 changes: 14 additions & 14 deletions src/kinetics/FalloffFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ namespace Cantera
FalloffFactory* FalloffFactory::s_factory = 0;
std::mutex FalloffFactory::falloff_mutex;

FalloffFactory::FalloffFactory()
{
reg("Simple", []() { return new Falloff(); });
reg("Troe", []() { return new Troe(); });
reg("SRI", []() { return new SRI(); });
}

Falloff* FalloffFactory::newFalloff(int type, const vector_fp& c)
{
Falloff* f;
switch (type) {
case SIMPLE_FALLOFF:
f = new Falloff();
break;
case TROE_FALLOFF:
f = new Troe();
break;
case SRI_FALLOFF:
f = new SRI();
break;
default:
return 0;
}
static const std::unordered_map<int, std::string> types {
{SIMPLE_FALLOFF, "Simple"},
{TROE_FALLOFF, "Troe"},
{SRI_FALLOFF, "SRI"}
};

Falloff* f = create(types.at(type));
f->init(c);
return f;
}
Expand Down
23 changes: 9 additions & 14 deletions src/kinetics/KineticsFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,17 @@ Kinetics* KineticsFactory::newKinetics(XML_Node& phaseData,
return k;
}

KineticsFactory::KineticsFactory() {
reg("none", []() { return new Kinetics(); });
reg("gaskinetics", []() { return new GasKinetics(); });
reg("interface", []() { return new InterfaceKinetics(); });
reg("edge", []() { return new EdgeKinetics(); });
reg("aqueouskinetics", []() { return new AqueousKinetics(); });
}

Kinetics* KineticsFactory::newKinetics(const string& model)
{
string lcmodel = lowercase(model);
if (lcmodel == "none") {
return new Kinetics();
} else if (lcmodel == "gaskinetics") {
return new GasKinetics();
} else if (lcmodel == "interface") {
return new InterfaceKinetics();
} else if (lcmodel == "edge") {
return new EdgeKinetics();
} else if (lcmodel == "aqueouskinetics") {
return new AqueousKinetics();
} else {
throw UnknownKineticsModel("KineticsFactory::newKinetics", model);
}
return create(lowercase(model));
}

}
94 changes: 31 additions & 63 deletions src/thermo/ThermoFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,71 +77,39 @@ static int _itypes[] = {cIdealGas, cIncompressible,
cRedlichKwongMFTP, cRedlichKwongMFTP, cMaskellSolidSolnPhase
};

ThermoPhase* ThermoFactory::newThermoPhase(const std::string& model)
ThermoFactory::ThermoFactory()
{
int ieos=-1;

for (int n = 0; n < ntypes; n++) {
if (model == _types[n]) {
ieos = _itypes[n];
break;
}
}
reg("IdealGas", []() { return new IdealGasPhase(); });
reg("Incompressible", []() { return new ConstDensityThermo(); });
reg("Surface", []() { return new SurfPhase(); });
reg("Edge", []() { return new EdgePhase(); });
reg("Metal", []() { return new MetalPhase(); });
reg("StoichSubstance", []() { return new StoichSubstance(); });
reg("PureFluid", []() { return new PureFluidPhase(); });
reg("LatticeSolid", []() { return new LatticeSolidPhase(); });
reg("Lattice", []() { return new LatticePhase(); });
reg("HMW", []() { return new HMWSoln(); });
reg("IdealSolidSolution", []() { return new IdealSolidSolnPhase(); });
reg("DebyeHuckel", []() { return new DebyeHuckel(); });
reg("IdealMolalSolution", []() { return new IdealMolalSoln(); });
reg("IdealGasVPSS", []() { return new IdealSolnGasVPSS(); });
reg("IdealSolnVPSS", []() { return new IdealSolnGasVPSS(); });
reg("MineralEQ3", []() { return new MineralEQ3(); });
reg("MetalSHEelectrons", []() { return new MetalSHEelectrons(); });
reg("Margules", []() { return new MargulesVPSSTP(); });
reg("PhaseCombo_Interaction", []() { return new PhaseCombo_Interaction(); });
reg("IonsFromNeutralMolecule", []() { return new IonsFromNeutralVPSSTP(); });
reg("FixedChemPot", []() { return new FixedChemPotSSTP(); });
reg("MolarityIonicVPSSTP", []() { return new MolarityIonicVPSSTP(); });
reg("Redlich-Kister", []() { return new RedlichKisterVPSSTP(); });
reg("RedlichKwong", []() { return new RedlichKwongMFTP(); });
reg("RedlichKwongMFTP", []() { return new RedlichKwongMFTP(); });
reg("MaskellSolidSolnPhase", []() { return new MaskellSolidSolnPhase(); });
}

switch (ieos) {
case cIdealGas:
return new IdealGasPhase;
case cIncompressible:
return new ConstDensityThermo;
case cSurf:
return new SurfPhase;
case cEdge:
return new EdgePhase;
case cIdealSolidSolnPhase:
return new IdealSolidSolnPhase();
case cMargulesVPSSTP:
return new MargulesVPSSTP();
case cRedlichKisterVPSSTP:
return new RedlichKisterVPSSTP();
case cMolarityIonicVPSSTP:
return new MolarityIonicVPSSTP();
case cPhaseCombo_Interaction:
return new PhaseCombo_Interaction();
case cIonsFromNeutral:
return new IonsFromNeutralVPSSTP();
case cMetal:
return new MetalPhase;
case cStoichSubstance:
return new StoichSubstance;
case cFixedChemPot:
return new FixedChemPotSSTP;
case cMineralEQ3:
return new MineralEQ3();
case cMetalSHEelectrons:
return new MetalSHEelectrons();
case cLatticeSolid:
return new LatticeSolidPhase;
case cLattice:
return new LatticePhase;
case cPureFluid:
return new PureFluidPhase;
case cRedlichKwongMFTP:
return new RedlichKwongMFTP;
case cHMW:
return new HMWSoln;
case cDebyeHuckel:
return new DebyeHuckel;
case cIdealMolalSoln:
return new IdealMolalSoln;
case cVPSS_IdealGas:
return new IdealSolnGasVPSS;
case cIdealSolnGasVPSS_iscv:
return new IdealSolnGasVPSS;
case cMaskellSolidSolnPhase:
return new MaskellSolidSolnPhase;
default:
throw UnknownThermoPhaseModel("ThermoFactory::newThermoPhase", model);
}
ThermoPhase* ThermoFactory::newThermoPhase(const std::string& model)
{
return create(model);
}

std::string eosTypeString(int ieos, int length)
Expand Down
Loading

0 comments on commit f985169

Please sign in to comment.