Cantera  3.1.0a1
DomainFactory.h
Go to the documentation of this file.
1 //! @file DomainFactory.h
2 
3 // This file is part of Cantera. See License.txt in the top-level directory or
4 // at https://cantera.org/license.txt for license and copyright information.
5 
6 #ifndef DOMAIN_FACTORY_H
7 #define DOMAIN_FACTORY_H
8 
11 
12 namespace Cantera
13 {
14 
15 //! Factory class to create domain objects
16 //!
17 //! This class is mainly used via the newDomain() function, for example:
18 //!
19 //! ```cpp
20 //! shared_ptr<Domain1D> d1 = newDomain("Inlet", sol, "reactants");
21 //! ```
22 class DomainFactory : public Factory<Domain1D, shared_ptr<Solution>, const string&>
23 {
24 public:
25  /**
26  * Return a pointer to the factory. On the first call, a new instance is
27  * created. Since there is no need to instantiate more than one factory,
28  * on all subsequent calls, a pointer to the existing factory is returned.
29  */
30  static DomainFactory* factory();
31 
32  void deleteFactory() override;
33 
34 private:
35  //! Pointer to the single instance of the factory
37 
38  //! default constructor, which is defined as private
39  DomainFactory();
40 
41  //! Mutex for use when calling the factory
42  static std::mutex domain_mutex;
43 };
44 
45 //! Create a Domain object of the specified type. An optional template argument will
46 //! dynamically cast Domain1D to the desired specialization.
47 //! @param domainType string identifying domain type.
48 //! @param solution Solution holding ThermoPhase, Kinetics and Transport objects.
49 //! @param id string identifier describing domain. If omitted, id defaults to the
50 //! domain type identifier.
51 //! @ingroup onedGroup
52 template <class T=Domain1D>
53 shared_ptr<T> newDomain(
54  const string& domainType, shared_ptr<Solution> solution, const string& id="")
55 {
56  string id_ = id;
57  if (id_ == "") {
58  id_ = domainType;
59  }
60  auto ret = std::dynamic_pointer_cast<T>(
61  shared_ptr<Domain1D>(
62  DomainFactory::factory()->create(domainType, solution, id_)));
63  if (!ret) {
64  throw CanteraError("newDomain",
65  "Invalid cast: unable to access 'Domain1D' as '{}'.", demangle(typeid(T)));
66  }
67  return ret;
68 }
69 
70 }
71 
72 #endif
File contains the FactoryBase class declarations.
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:66
Factory class to create domain objects.
Definition: DomainFactory.h:23
static std::mutex domain_mutex
Mutex for use when calling the factory.
Definition: DomainFactory.h:42
DomainFactory()
default constructor, which is defined as private
void deleteFactory() override
Virtual abstract function that deletes the factory.
static DomainFactory * s_factory
Pointer to the single instance of the factory.
Definition: DomainFactory.h:36
static DomainFactory * factory()
Return a pointer to the factory.
Factory class that supports registering functions to create objects.
Definition: FactoryBase.h:69
string demangle(const std::type_info &type)
Convert a type name to a human readable string, using boost::core::demangle if available.
Definition: global.cpp:213
shared_ptr< T > newDomain(const string &domainType, shared_ptr< Solution > solution, const string &id="")
Create a Domain object of the specified type.
Definition: DomainFactory.h:53
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:564