Cantera  2.3.0
FactoryBase.h
Go to the documentation of this file.
1 /**
2  * @file FactoryBase.h
3  * File contains the FactoryBase class declarations.
4  */
5 
6 // This file is part of Cantera. See License.txt in the top-level directory or
7 // at http://www.cantera.org/license.txt for license and copyright information.
8 
9 #ifndef CT_FACTORY_BASE
10 #define CT_FACTORY_BASE
11 
12 #include <vector>
13 #include <mutex>
14 #include <unordered_map>
16 
17 namespace Cantera
18 {
19 
20 //! Base class for factories.
21 /*!
22  * This class maintains a registry of all factories that derive from it, and
23  * deletes them all when its static method deleteFactories is invoked.
24  */
26 {
27 public:
28 
29  //! destructor
30  virtual ~FactoryBase() {
31  }
32 
33  //! static function that deletes all factories in the internal registry
34  //! maintained in a static variable
35  static void deleteFactories() {
36  for (const auto& f : s_vFactoryRegistry) {
37  f->deleteFactory();
38  }
39  s_vFactoryRegistry.clear();
40  }
41 
42 protected:
43 
44  //! Constructor.
45  /*!
46  * Adds the current object to the current static list
47  */
49  s_vFactoryRegistry.push_back(this);
50  }
51 
52  //! Virtual abstract function that deletes the factory
53  /*!
54  * This must be properly defined in child objects.
55  */
56  virtual void deleteFactory() = 0;
57 
58 private:
59  //! statically held list of Factories.
60  static std::vector<FactoryBase*> s_vFactoryRegistry;
61 };
62 
63 //! Factory class that supports registering functions to create objects
64 //!
65 //! Template arguments for the class are the base type created by the factory,
66 //! followed by the types of any arguments which need to be passed to the
67 //! functions used to create objects, e.g. arguments to the constructor.
68 template <class T, typename ... Args>
69 class Factory : public FactoryBase {
70 public:
71  virtual ~Factory() {}
72 
73  //! Create an object using the object construction function corresponding to
74  //! "name" and the provided constructor arguments
75  T* create(const std::string& name, Args... args) {
76  try {
77  return m_creators.at(name)(args...);
78  } catch (std::out_of_range&) {
79  throw CanteraError("Factory::create", "No such type: '{}'", name);
80  }
81  }
82 
83  //! Register a new object construction function
84  void reg(const std::string& name, std::function<T*(Args...)> f) {
85  m_creators[name] = f;
86  }
87 
88 protected:
89  std::unordered_map<std::string, std::function<T*(Args...)>> m_creators;
90 };
91 
92 }
93 
94 #endif
virtual ~FactoryBase()
destructor
Definition: FactoryBase.h:30
FactoryBase()
Constructor.
Definition: FactoryBase.h:48
Base class for factories.
Definition: FactoryBase.h:25
virtual void deleteFactory()=0
Virtual abstract function that deletes the factory.
T * create(const std::string &name, Args... args)
Create an object using the object construction function corresponding to "name" and the provided cons...
Definition: FactoryBase.h:75
static void deleteFactories()
static function that deletes all factories in the internal registry maintained in a static variable ...
Definition: FactoryBase.h:35
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:65
Factory class that supports registering functions to create objects.
Definition: FactoryBase.h:69
void reg(const std::string &name, std::function< T *(Args...)> f)
Register a new object construction function.
Definition: FactoryBase.h:84
static std::vector< FactoryBase * > s_vFactoryRegistry
statically held list of Factories.
Definition: FactoryBase.h:60
Namespace for the Cantera kernel.
Definition: application.cpp:29
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...