[zeroD] Implement ConnectorFactory

This commit is contained in:
Ingmar Schoegl 2024-08-22 09:16:25 -05:00
parent f9578bbedd
commit 9a9f378334
2 changed files with 122 additions and 0 deletions

View File

@ -0,0 +1,68 @@
//! @file ConnectorFactory.h
// This file is part of Cantera. See License.txt in the top-level directory or
// at https://cantera.org/license.txt for license and copyright information.
#ifndef CONNECTOR_FACTORY_H
#define CONNECTOR_FACTORY_H
#include "cantera/base/FactoryBase.h"
#include "cantera/zeroD/ConnectorNode.h"
namespace Cantera
{
//! Factory class to create ConnectorNode objects.
//!
//! This class is mainly used via the newConnectorNode() function, for example:
//!
//! ```cpp
//! shared_ptr<ConnectorNode> valve = newConnectorNode("Valve", r0, r1, "my_valve");
//! ```
//!
//! where `r0` and `r1` are reactor objects.
class ConnectorFactory :
public Factory<ConnectorNode, shared_ptr<ReactorBase>, shared_ptr<ReactorBase>, const string&>
{
public:
static ConnectorFactory* factory();
void deleteFactory() override;
private:
static ConnectorFactory* s_factory;
static std::mutex connector_mutex;
ConnectorFactory();
};
//! @defgroup connectorGroup Connectors
//! %ConnectorNode objects connect zero-dimensional reactors.
//! ConnectorNode objects should be instantiated via the newConnectorNode() function,
//! for example:
//!
//! ```cpp
//! shared_ptr<ConnectorNode> valve = newConnectorNode("Valve", r0, r1, "my_valve");
//! ```
//!
//! where `r0` and `r1` are reactor objects.
//!
//! @since New in %Cantera 3.1.
//!
//! @ingroup zerodGroup
//! @{
//! Create a %ConnectorNode object of the specified type
//! @param model String specifying reactor type.
//! @param r0 First reactor.
//! @param r1 Second reactor.
//! @param name Name of the connector.
//! @since New in %Cantera 3.1.
shared_ptr<ConnectorNode> newConnectorNode(const string& model,
shared_ptr<ReactorBase> r0,
shared_ptr<ReactorBase> r1,
const string& name="(none)");
//! @}
}
#endif

View File

@ -0,0 +1,54 @@
//! @file ConnectorFactory.cpp
// This file is part of Cantera. See License.txt in the top-level directory or
// at https://cantera.org/license.txt for license and copyright information.
#include "cantera/zeroD/ConnectorFactory.h"
#include "cantera/zeroD/flowControllers.h"
#include "cantera/zeroD/Wall.h"
namespace Cantera
{
ConnectorFactory* ConnectorFactory::s_factory = 0;
std::mutex ConnectorFactory::connector_mutex;
ConnectorFactory::ConnectorFactory()
{
reg("MassFlowController",
[](shared_ptr<ReactorBase> r0, shared_ptr<ReactorBase> r1, const string& name)
{ return new MassFlowController(r0, r1, name); });
reg("PressureController",
[](shared_ptr<ReactorBase> r0, shared_ptr<ReactorBase> r1, const string& name)
{ return new PressureController(r0, r1, name); });
reg("Valve",
[](shared_ptr<ReactorBase> r0, shared_ptr<ReactorBase> r1, const string& name)
{ return new Valve(r0, r1, name); });
reg("Wall",
[](shared_ptr<ReactorBase> r0, shared_ptr<ReactorBase> r1, const string& name)
{ return new Wall(r0, r1, name); });
}
ConnectorFactory* ConnectorFactory::factory() {
std::unique_lock<std::mutex> lock(connector_mutex);
if (!s_factory) {
s_factory = new ConnectorFactory;
}
return s_factory;
}
void ConnectorFactory::deleteFactory() {
std::unique_lock<std::mutex> lock(connector_mutex);
delete s_factory;
s_factory = 0;
}
shared_ptr<ConnectorNode> newConnectorNode(
const string& model,
shared_ptr<ReactorBase> r0, shared_ptr<ReactorBase> r1, const string& name)
{
return shared_ptr<ConnectorNode>(
ConnectorFactory::factory()->create(model, r0, r1, name));
}
}