Cantera  2.4.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>
15 #include <functional>
17 #include "cantera/base/global.h"
18 
19 namespace Cantera
20 {
21 
22 //! Base class for factories.
23 /*!
24  * This class maintains a registry of all factories that derive from it, and
25  * deletes them all when its static method deleteFactories is invoked.
26  */
28 {
29 public:
30 
31  //! destructor
32  virtual ~FactoryBase() {
33  }
34 
35  //! static function that deletes all factories in the internal registry
36  //! maintained in a static variable
37  static void deleteFactories() {
38  for (const auto& f : s_vFactoryRegistry) {
39  f->deleteFactory();
40  }
41  s_vFactoryRegistry.clear();
42  }
43 
44 protected:
45 
46  //! Constructor.
47  /*!
48  * Adds the current object to the current static list
49  */
51  s_vFactoryRegistry.push_back(this);
52  }
53 
54  //! Virtual abstract function that deletes the factory
55  /*!
56  * This must be properly defined in child objects.
57  */
58  virtual void deleteFactory() = 0;
59 
60 private:
61  //! statically held list of Factories.
62  static std::vector<FactoryBase*> s_vFactoryRegistry;
63 };
64 
65 //! Factory class that supports registering functions to create objects
66 //!
67 //! Template arguments for the class are the base type created by the factory,
68 //! followed by the types of any arguments which need to be passed to the
69 //! functions used to create objects, e.g. arguments to the constructor.
70 template <class T, typename ... Args>
71 class Factory : public FactoryBase {
72 public:
73  virtual ~Factory() {}
74 
75  //! Create an object using the object construction function corresponding to
76  //! "name" and the provided constructor arguments
77  T* create(std::string name, Args... args) {
78  try {
79  return m_creators.at(name)(args...);
80  } catch (std::out_of_range&) {
81  if (m_synonyms.find(name) != m_synonyms.end()) {
82  return m_creators.at(m_synonyms.at(name))(args...);
83  } else if (m_deprecated_names.find(name) != m_deprecated_names.end()) {
84  warn_deprecated(name,
85  fmt::format("Use '{}' instead.", m_deprecated_names.at(name)));
86  return m_creators.at(m_deprecated_names.at(name))(args...);
87  } else {
88  throw CanteraError("Factory::create", "No such type: '{}'", name);
89  }
90  }
91  }
92 
93  //! Register a new object construction function
94  void reg(const std::string& name, std::function<T*(Args...)> f) {
95  m_creators[name] = f;
96  }
97 
98 protected:
99  std::unordered_map<std::string, std::function<T*(Args...)>> m_creators;
100 
101  //! Map of synonyms to canonical names
102  std::unordered_map<std::string, std::string> m_synonyms;
103 
104  //! Map of deprecated synonyms to canonical names. Use of these names will
105  //! show a deprecation warning.
106  std::unordered_map<std::string, std::string> m_deprecated_names;
107 };
108 
109 }
110 
111 #endif
virtual ~FactoryBase()
destructor
Definition: FactoryBase.h:32
FactoryBase()
Constructor.
Definition: FactoryBase.h:50
void warn_deprecated(const std::string &method, const std::string &extra)
Print a warning indicating that method is deprecated.
Definition: global.cpp:54
Base class for factories.
Definition: FactoryBase.h:27
This file contains definitions for utility functions and text for modules, inputfiles, logs, textlogs, (see Input File Handling, Diagnostic Output, and Writing messages to the screen).
virtual void deleteFactory()=0
Virtual abstract function that deletes the factory.
std::unordered_map< std::string, std::string > m_deprecated_names
Map of deprecated synonyms to canonical names.
Definition: FactoryBase.h:106
T * create(std::string name, Args... args)
Create an object using the object construction function corresponding to "name" and the provided cons...
Definition: FactoryBase.h:77
static void deleteFactories()
static function that deletes all factories in the internal registry maintained in a static variable ...
Definition: FactoryBase.h:37
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:65
Factory class that supports registering functions to create objects.
Definition: FactoryBase.h:71
void reg(const std::string &name, std::function< T *(Args...)> f)
Register a new object construction function.
Definition: FactoryBase.h:94
static std::vector< FactoryBase * > s_vFactoryRegistry
statically held list of Factories.
Definition: FactoryBase.h:62
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:8
std::unordered_map< std::string, std::string > m_synonyms
Map of synonyms to canonical names.
Definition: FactoryBase.h:102
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...