www.mooseframework.org
Factory.h
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #ifndef FACTORY_H
16 #define FACTORY_H
17 
18 #include <set>
19 #include <vector>
20 #include <time.h>
21 
22 // MOOSE includes
23 #include "MooseObject.h"
24 #include "MooseTypes.h"
25 #include "FileLineInfo.h"
26 
27 // Forward declarations
28 class InputParameters;
29 
33 #define stringifyName(name) #name
34 #define registerObject(name) factory.reg<name>(stringifyName(name), __FILE__, __LINE__)
35 #define registerNamedObject(obj, name) \
36  do \
37  { \
38  factory.reg<obj>(name, __FILE__, __LINE__); \
39  factory.associateNameToClass(name, stringifyName(obj)); \
40  } while (0)
41 
42 #define registerDeprecatedObject(name) \
43  factory.regDeprecated<name>(stringifyName(name), __FILE__, __LINE__)
44 
45 #define registerDeprecatedObjectWithReplacement(dep_obj, replacement_name) \
46  factory.regReplaced<dep_obj>(stringifyName(dep_obj), replacement_name, __FILE__, __LINE__)
47 
48 // for backward compatibility
49 #define registerKernel(name) registerObject(name)
50 #define registerNodalKernel(name) registerObject(name)
51 #define registerBoundaryCondition(name) registerObject(name)
52 #define registerAux(name) registerObject(name)
53 #define registerAuxKernel(name) registerObject(name)
54 #define registerMaterial(name) registerObject(name)
55 #define registerPostprocessor(name) registerObject(name)
56 #define registerVectorPostprocessor(name) registerObject(name)
57 #define registerInitialCondition(name) registerObject(name)
58 #define registerDamper(name) registerObject(name)
59 #define registerDiracKernel(name) registerObject(name)
60 #define registerDGKernel(name) registerObject(name)
61 #define registerInterfaceKernel(name) registerObject(name)
62 #define registerExecutioner(name) registerObject(name)
63 #define registerFunction(name) registerObject(name)
64 #define registerDistribution(name) registerObject(name)
65 #define registerSampler(name) registerObject(name)
66 #define registerMesh(name) registerObject(name)
67 #define registerMeshModifier(name) registerObject(name)
68 #define registerConstraint(name) registerObject(name)
69 #define registerScalarKernel(name) registerObject(name)
70 #define registerUserObject(name) registerObject(name)
71 #define registerPreconditioner(name) registerObject(name)
72 #define registerIndicator(name) registerObject(name)
73 #define registerMarker(name) registerObject(name)
74 #define registerProblem(name) registerObject(name)
75 #define registerMultiApp(name) registerObject(name)
76 #define registerTransfer(name) registerObject(name)
77 #define registerTimeStepper(name) registerObject(name)
78 #define registerTimeIntegrator(name) registerObject(name)
79 #define registerPredictor(name) registerObject(name)
80 #define registerSplit(name) registerObject(name)
81 #define registerOutput(name) registerObject(name)
82 #define registerControl(name) registerObject(name)
83 #define registerPartitioner(name) registerObject(name)
84 
85 #define registerNamedKernel(obj, name) registerNamedObject(obj, name)
86 #define registerNamedNodalKernel(obj, name) registerNamedObject(obj, name)
87 #define registerNamedBoundaryCondition(obj, name) registerNamedObject(obj, name)
88 #define registerNamedAux(obj, name) registerNamedObject(obj, name)
89 #define registerNamedAuxKernel(name) registerNamedObject(obj, name)
90 #define registerNamedMaterial(obj, name) registerNamedObject(obj, name)
91 #define registerNamedPostprocessor(obj, name) registerNamedObject(obj, name)
92 #define registerNamedVectorPostprocessor(obj, name) registerNamedObject(obj, name)
93 #define registerNamedInitialCondition(obj, name) registerNamedObject(obj, name)
94 #define registerNamedDamper(obj, name) registerNamedObject(obj, name)
95 #define registerNamedDiracKernel(obj, name) registerNamedObject(obj, name)
96 #define registerNamedDGKernel(obj, name) registerNamedObject(obj, name)
97 #define registerNamedExecutioner(obj, name) registerNamedObject(obj, name)
98 #define registerNamedFunction(obj, name) registerNamedObject(obj, name)
99 #define registerNamedDistribution(obj, name) registerNamedObject(obj, name)
100 #define registerNamedSampler(obj, name) registerNamedObject(obj, name)
101 #define registerNamedMesh(obj, name) registerNamedObject(obj, name)
102 #define registerNamedMeshModifier(name) registerNamedObject(obj, name)
103 #define registerNamedConstraint(obj, name) registerNamedObject(obj, name)
104 #define registerNamedUserObject(obj, name) registerNamedObject(obj, name)
105 #define registerNamedPreconditioner(obj, name) registerNamedObject(obj, name)
106 #define registerNamedIndicator(obj, name) registerNamedObject(obj, name)
107 #define registerNamedMarker(obj, name) registerNamedObject(obj, name)
108 #define registerNamedProblem(obj, name) registerNamedObject(obj, name)
109 #define registerNamedMultiApp(obj, name) registerNamedObject(obj, name)
110 #define registerNamedTransfer(obj, name) registerNamedObject(obj, name)
111 #define registerNamedTimeStepper(obj, name) registerNamedObject(obj, name)
112 #define registerNamedTimeIntegrator(obj, name) registerNamedObject(obj, name)
113 #define registerNamedPredictor(obj, name) registerNamedObject(obj, name)
114 #define registerNamedSplit(obj, name) registerNamedObject(obj, name)
115 #define registerNamedOutput(obj, name) registerNamedObject(obj, name)
116 #define registerNamedControl(obj, name) registerNamedObject(obj, name)
117 #define registerNamedPartitioner(obj, name) registerNamedObject(obj, name)
118 
122 using MooseObjectPtr = std::shared_ptr<MooseObject>;
123 
128 
132 using buildPtr = MooseObjectPtr (*)(const InputParameters & parameters);
133 
137 using registeredMooseObjectIterator = std::map<std::string, paramsPtr>::iterator;
138 
142 template <class T>
144 buildObject(const InputParameters & parameters)
145 {
146  return MooseObjectPtr(new T(parameters));
147 }
148 
152 class Factory
153 {
154 public:
155  Factory(MooseApp & app);
156  virtual ~Factory();
157 
162  template <typename T>
163  void reg(const std::string & obj_name, const std::string & file = "", int line = -1)
164  {
165 
166  /*
167  * If _registerable_objects has been set the user has requested that we only register some
168  * subset
169  * of the objects for a dynamically loaded application. The objects listed in *this*
170  * application's
171  * registerObjects() method will have already been registered before that member was set.
172  *
173  * If _registerable_objects is empty, the factory is unrestricted
174  */
175  if (_registerable_objects.empty() ||
176  _registerable_objects.find(obj_name) != _registerable_objects.end())
177  {
178  if (_name_to_build_pointer.find(obj_name) == _name_to_build_pointer.end())
179  {
180  _name_to_build_pointer[obj_name] = &buildObject<T>;
181  _name_to_params_pointer[obj_name] = &validParams<T>;
182  }
183  else
184  mooseError("Object '" + obj_name + "' already registered.");
185  }
186  _name_to_line.addInfo(obj_name, file, line);
187  // TODO: Possibly store and print information about objects that are skipped here?
188  }
194  FileLineInfo getLineInfo(const std::string & name) const;
195 
201  void associateNameToClass(const std::string & name, const std::string & class_name);
202 
208  std::string associatedClassName(const std::string & name) const;
209 
217  template <typename T>
218  void regDeprecated(const std::string & obj_name,
219  const std::string & file,
220  int line)
221  {
222  // Register the name
223  reg<T>(obj_name, file, line);
224  deprecateObject(obj_name);
225  }
226 
235  template <typename T>
236  void regReplaced(const std::string & dep_obj,
237  const std::string & replacement_name,
238  const std::string & file,
239  int line)
240  {
241  // Register the name
242  regDeprecated<T>(dep_obj, file, line);
243 
244  // Store the new name
245  deprecateObject(dep_obj, replacement_name);
246  }
247 
253  InputParameters getValidParams(const std::string & name);
254 
264  std::shared_ptr<MooseObject> create(const std::string & obj_name,
265  const std::string & name,
266  InputParameters parameters,
267  THREAD_ID tid = 0,
268  bool print_deprecated = true);
269 
278  template <typename T>
279  std::shared_ptr<T> create(const std::string & obj_name,
280  const std::string & name,
281  InputParameters parameters,
282  THREAD_ID tid = 0)
283  {
284  std::shared_ptr<T> new_object =
285  std::dynamic_pointer_cast<T>(create(obj_name, name, parameters, tid, false));
286  if (!new_object)
287  mooseError("We expected to create an object of type '" + demangle(typeid(T).name()) +
288  "'.\nInstead we received a parameters object for type '" + obj_name +
289  "'.\nDid you call the wrong \"add\" method in your Action?");
290 
291  return new_object;
292  }
293 
300  void restrictRegisterableObjects(const std::vector<std::string> & names);
301 
306 
311 
315  std::vector<std::string> getConstructedObjects() const;
316 
318 
321  void deprecateObject(const std::string & name);
322  void deprecateObject(const std::string & name, const std::string & replacement);
324 
325 protected:
330  void deprecatedMessage(const std::string obj_name);
331 
335  void reportUnregisteredError(const std::string & obj_name) const;
336 
339 
341  std::map<std::string, buildPtr> _name_to_build_pointer;
342 
344  std::map<std::string, paramsPtr> _name_to_params_pointer;
345 
347 
349  std::map<std::string, std::string> _name_to_class;
350 
352  std::set<std::string> _deprecated;
353 
355  std::map<std::string, std::string> _deprecated_with_replace;
356 
358  std::set<std::string> _registerable_objects;
359 
362 
364  std::set<std::string> _constructed_types;
365 };
366 
367 #endif /* FACTORY_H */
FileLineInfo getLineInfo(const std::string &name) const
Gets file and line information where an object was initially registered.
Definition: Factory.C:136
void restrictRegisterableObjects(const std::vector< std::string > &names)
Calling this object with a non-empty vector will cause this factory to ignore registrations from any ...
Definition: Factory.C:82
std::shared_ptr< MooseObject > create(const std::string &obj_name, const std::string &name, InputParameters parameters, THREAD_ID tid=0, bool print_deprecated=true)
Build an object (must be registered) - THIS METHOD IS DEPRECATED (Use create<T>()) ...
Definition: Factory.C:46
MooseObjectID _object_count
Object id count.
Definition: Factory.h:361
MooseApp & _app
Reference to the application.
Definition: Factory.h:338
registeredMooseObjectIterator registeredObjectsEnd()
Access to registered object iterator (end)
Definition: Factory.h:310
Generic factory class for build all sorts of objects.
Definition: Factory.h:152
Factory(MooseApp &app)
Definition: Factory.C:21
InputParameters getValidParams(const std::string &name)
Get valid parameters for the object.
Definition: Factory.C:26
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
FileLineInfoMap _name_to_line
Definition: Factory.h:346
InputParameters(*)( paramsPtr)
alias for validParams function
Definition: Factory.h:127
virtual ~Factory()
Definition: Factory.C:23
std::string associatedClassName(const std::string &name) const
Get the associated class name for an object name.
Definition: Factory.C:148
Base class for MOOSE-based applications.
Definition: MooseApp.h:58
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void reportUnregisteredError(const std::string &obj_name) const
Prints error information when an object is not registered.
Definition: Factory.C:109
MooseObjectPtr(*)(const InputParameters &parameters) buildPtr
alias for method to build objects
Definition: Factory.h:132
std::map< std::string, paramsPtr >::iterator registeredMooseObjectIterator
alias for registered Object iterator
Definition: Factory.h:137
void addInfo(const std::string &key0, const std::string &file, int line)
Associate a key with file/line info.
Definition: FileLineInfo.C:40
std::map< std::string, std::string > _deprecated_with_replace
Storage for the deprecated objects that have replacements.
Definition: Factory.h:355
void associateNameToClass(const std::string &name, const std::string &class_name)
Associates an object name with a class name.
Definition: Factory.C:142
std::set< std::string > _registerable_objects
The list of objects that may be registered.
Definition: Factory.h:358
std::vector< std::string > getConstructedObjects() const
Get a list of all constructed Moose Object types.
Definition: Factory.C:127
void deprecatedMessage(const std::string obj_name)
Show the appropriate message for deprecated objects.
Definition: Factory.C:88
void deprecateObject(const std::string &name)
Allow objects to be deprecated via function call.
Definition: Factory.C:158
void reg(const std::string &obj_name, const std::string &file="", int line=-1)
Register a new object.
Definition: Factory.h:163
Holds file and line information.
Definition: FileLineInfo.h:24
registeredMooseObjectIterator registeredObjectsBegin()
Access to registered object iterator (begin)
Definition: Factory.h:305
std::map< std::string, paramsPtr > _name_to_params_pointer
Storage for pointers to the parameters objects.
Definition: Factory.h:344
std::map< std::string, std::string > _name_to_class
Object name to class name association.
Definition: Factory.h:349
std::set< std::string > _constructed_types
Constructed Moose Object types.
Definition: Factory.h:364
std::map< std::string, buildPtr > _name_to_build_pointer
Storage for pointers to the object.
Definition: Factory.h:341
void regDeprecated(const std::string &obj_name, const std::string &file, int line)
Register a deprecated object that expires.
Definition: Factory.h:218
std::set< std::string > _deprecated
Storage for deprecated objects.
Definition: Factory.h:352
void regReplaced(const std::string &dep_obj, const std::string &replacement_name, const std::string &file, int line)
Registers an object as deprecated and associates it with the replacement name.
Definition: Factory.h:236
std::shared_ptr< MooseObject > MooseObjectPtr
alias to wrap shared pointer type
Definition: Factory.h:122
std::shared_ptr< T > create(const std::string &obj_name, const std::string &name, InputParameters parameters, THREAD_ID tid=0)
Build an object (must be registered)
Definition: Factory.h:279
A mapping between a series of keys to a FileLineInfo.
Definition: FileLineInfo.h:46
unsigned int MooseObjectID
Definition: MooseTypes.h:78
MooseObjectPtr buildObject(const InputParameters &parameters)
Build an object of type T.
Definition: Factory.h:144
unsigned int THREAD_ID
Definition: MooseTypes.h:79