www.mooseframework.org
Factory.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 #include <set>
13 #include <vector>
14 #include <ctime>
15 
16 // MOOSE includes
17 #include "MooseObject.h"
18 #include "MooseTypes.h"
19 #include "FileLineInfo.h"
20 
21 // Forward declarations
22 class InputParameters;
23 
27 #define stringifyName(name) #name
28 #define registerObject(name) factory.reg<name>(stringifyName(name), __FILE__, __LINE__)
29 #define registerNamedObject(obj, name) \
30  do \
31  { \
32  factory.reg<obj>(name, __FILE__, __LINE__); \
33  factory.associateNameToClass(name, stringifyName(obj)); \
34  } while (0)
35 
36 #define registerDeprecatedObject(name, time) \
37  factory.regDeprecated<name>(stringifyName(name), time, __FILE__, __LINE__)
38 
39 #define registerDeprecatedObjectWithReplacement(dep_obj, replacement_name, time) \
40  factory.regReplaced<dep_obj>(stringifyName(dep_obj), replacement_name, time, __FILE__, __LINE__)
41 
42 #define registerRenamedObject(orig_name, new_obj, time) \
43  factory.renameObject<new_obj>(orig_name, stringifyName(new_obj), time, __FILE__, __LINE__)
44 
45 // for backward compatibility
46 #define registerKernel(name) registerObject(name)
47 #define registerNodalKernel(name) registerObject(name)
48 #define registerBoundaryCondition(name) registerObject(name)
49 #define registerAux(name) registerObject(name)
50 #define registerAuxKernel(name) registerObject(name)
51 #define registerMaterial(name) registerObject(name)
52 #define registerPostprocessor(name) registerObject(name)
53 #define registerVectorPostprocessor(name) registerObject(name)
54 #define registerInitialCondition(name) registerObject(name)
55 #define registerDamper(name) registerObject(name)
56 #define registerDiracKernel(name) registerObject(name)
57 #define registerDGKernel(name) registerObject(name)
58 #define registerInterfaceKernel(name) registerObject(name)
59 #define registerExecutioner(name) registerObject(name)
60 #define registerFunction(name) registerObject(name)
61 #define registerDistribution(name) registerObject(name)
62 #define registerSampler(name) registerObject(name)
63 #define registerMesh(name) registerObject(name)
64 #define registerMeshModifier(name) registerObject(name)
65 #define registerConstraint(name) registerObject(name)
66 #define registerScalarKernel(name) registerObject(name)
67 #define registerUserObject(name) registerObject(name)
68 #define registerPreconditioner(name) registerObject(name)
69 #define registerIndicator(name) registerObject(name)
70 #define registerMarker(name) registerObject(name)
71 #define registerProblem(name) registerObject(name)
72 #define registerMultiApp(name) registerObject(name)
73 #define registerTransfer(name) registerObject(name)
74 #define registerTimeStepper(name) registerObject(name)
75 #define registerTimeIntegrator(name) registerObject(name)
76 #define registerPredictor(name) registerObject(name)
77 #define registerSplit(name) registerObject(name)
78 #define registerOutput(name) registerObject(name)
79 #define registerControl(name) registerObject(name)
80 #define registerPartitioner(name) registerObject(name)
81 #define registerRelationshipManager(name) registerObject(name)
82 
83 #define registerNamedKernel(obj, name) registerNamedObject(obj, name)
84 #define registerNamedNodalKernel(obj, name) registerNamedObject(obj, name)
85 #define registerNamedBoundaryCondition(obj, name) registerNamedObject(obj, name)
86 #define registerNamedAux(obj, name) registerNamedObject(obj, name)
87 #define registerNamedAuxKernel(name) registerNamedObject(obj, name)
88 #define registerNamedMaterial(obj, name) registerNamedObject(obj, name)
89 #define registerNamedPostprocessor(obj, name) registerNamedObject(obj, name)
90 #define registerNamedVectorPostprocessor(obj, name) registerNamedObject(obj, name)
91 #define registerNamedInitialCondition(obj, name) registerNamedObject(obj, name)
92 #define registerNamedDamper(obj, name) registerNamedObject(obj, name)
93 #define registerNamedDiracKernel(obj, name) registerNamedObject(obj, name)
94 #define registerNamedDGKernel(obj, name) registerNamedObject(obj, name)
95 #define registerNamedExecutioner(obj, name) registerNamedObject(obj, name)
96 #define registerNamedFunction(obj, name) registerNamedObject(obj, name)
97 #define registerNamedDistribution(obj, name) registerNamedObject(obj, name)
98 #define registerNamedSampler(obj, name) registerNamedObject(obj, name)
99 #define registerNamedMesh(obj, name) registerNamedObject(obj, name)
100 #define registerNamedMeshModifier(name) registerNamedObject(obj, name)
101 #define registerNamedConstraint(obj, name) registerNamedObject(obj, name)
102 #define registerNamedUserObject(obj, name) registerNamedObject(obj, name)
103 #define registerNamedPreconditioner(obj, name) registerNamedObject(obj, name)
104 #define registerNamedIndicator(obj, name) registerNamedObject(obj, name)
105 #define registerNamedMarker(obj, name) registerNamedObject(obj, name)
106 #define registerNamedProblem(obj, name) registerNamedObject(obj, name)
107 #define registerNamedMultiApp(obj, name) registerNamedObject(obj, name)
108 #define registerNamedTransfer(obj, name) registerNamedObject(obj, name)
109 #define registerNamedTimeStepper(obj, name) registerNamedObject(obj, name)
110 #define registerNamedTimeIntegrator(obj, name) registerNamedObject(obj, name)
111 #define registerNamedPredictor(obj, name) registerNamedObject(obj, name)
112 #define registerNamedSplit(obj, name) registerNamedObject(obj, name)
113 #define registerNamedOutput(obj, name) registerNamedObject(obj, name)
114 #define registerNamedControl(obj, name) registerNamedObject(obj, name)
115 #define registerNamedPartitioner(obj, name) registerNamedObject(obj, name)
116 
117 // Execute on flag registration
118 #define registerExecFlag(flag) factory.regExecFlag(flag)
119 
123 using MooseObjectPtr = std::shared_ptr<MooseObject>;
124 
129 
133 using buildPtr = MooseObjectPtr (*)(const InputParameters & parameters);
134 
138 using registeredMooseObjectIterator = std::map<std::string, paramsPtr>::iterator;
139 
143 template <class T>
145 buildObject(const InputParameters & parameters)
146 {
147  return std::make_shared<T>(parameters);
148 }
149 
153 class Factory
154 {
155 public:
156  Factory(MooseApp & app);
157  virtual ~Factory();
158 
163  template <typename T>
164  void reg(const std::string & obj_name, const std::string & file = "", int line = -1)
165  {
166  reg("", obj_name, &buildObject<T>, &validParams<T>, "", "", file, line);
167  }
168 
169  void reg(const std::string & label,
170  const std::string & obj_name,
171  const buildPtr & build_ptr,
172  const paramsPtr & params_ptr,
173  const std::string & deprecated_time = "",
174  const std::string & replacement_name = "",
175  const std::string & file = "",
176  int line = -1);
177 
183  FileLineInfo getLineInfo(const std::string & name) const;
184 
190  void associateNameToClass(const std::string & name, const std::string & class_name);
191 
197  std::string associatedClassName(const std::string & name) const;
198 
207  template <typename T>
208  void regDeprecated(const std::string & obj_name,
209  const std::string t_str,
210  const std::string & file,
211  int line)
212  {
213  reg("", obj_name, &buildObject<T>, &validParams<T>, t_str, "", file, line);
214  }
215 
225  template <typename T>
226  void regReplaced(const std::string & dep_obj,
227  const std::string & replacement_name,
228  const std::string time_str,
229  const std::string & file,
230  int line)
231  {
232  reg("", dep_obj, &buildObject<T>, &validParams<T>, time_str, replacement_name, file, line);
233  }
234 
246  template <typename T>
247  void renameObject(const std::string & orig_name,
248  const std::string & new_name,
249  const std::string time_str,
250  const std::string & file,
251  int line)
252  {
253  // Deprecate the old name
254  // Store the time
255  _deprecated_time[orig_name] = parseTime(time_str);
256 
257  // Store the new name
258  _deprecated_name[orig_name] = new_name;
259 
260  // Register the new object with the old name
261  reg<T>(orig_name, __FILE__, __LINE__);
262  associateNameToClass(orig_name, new_name);
263 
264  // Register the new object with the new name
265  reg<T>(new_name, file, line);
266  }
267 
273  InputParameters getValidParams(const std::string & name);
274 
284  std::shared_ptr<MooseObject> create(const std::string & obj_name,
285  const std::string & name,
286  InputParameters parameters,
287  THREAD_ID tid = 0,
288  bool print_deprecated = true);
289 
298  template <typename T>
299  std::shared_ptr<T> create(const std::string & obj_name,
300  const std::string & name,
301  InputParameters parameters,
302  THREAD_ID tid = 0)
303  {
304  std::shared_ptr<T> new_object =
305  std::dynamic_pointer_cast<T>(create(obj_name, name, parameters, tid, false));
306  if (!new_object)
307  mooseError("We expected to create an object of type '" + demangle(typeid(T).name()) +
308  "'.\nInstead we received a parameters object for type '" + obj_name +
309  "'.\nDid you call the wrong \"add\" method in your Action?");
310 
311  return new_object;
312  }
313 
320  void releaseSharedObjects(const MooseObject & moose_object, THREAD_ID tid = 0);
321 
328  void restrictRegisterableObjects(const std::vector<std::string> & names);
329 
334 
339 
343  std::vector<std::string> getConstructedObjects() const;
344 
349  void regExecFlag(const ExecFlagType & flag);
350 
351  MooseApp & app() { return _app; }
352 
353 protected:
359  std::time_t parseTime(std::string);
360 
365  void deprecatedMessage(const std::string obj_name);
366 
370  void reportUnregisteredError(const std::string & obj_name) const;
371 
374 
376  std::map<std::string, buildPtr> _name_to_build_pointer;
377 
379  std::map<std::string, paramsPtr> _name_to_params_pointer;
380 
382 
384  std::map<std::string, std::string> _name_to_class;
385 
387  std::map<std::string, std::time_t> _deprecated_time;
388 
390  std::map<std::string, std::string> _deprecated_name;
391 
393  std::set<std::string> _registerable_objects;
394 
397 
399  std::set<std::string> _constructed_types;
400 
404  std::set<std::pair<std::string, std::string>> _objects_by_label;
405 };
406 
std::set< std::pair< std::string, std::string > > _objects_by_label
set<label/appname, objectname> used to track if an object previously added is being added again - whi...
Definition: Factory.h:404
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:172
void reportUnregisteredError(const std::string &obj_name) const
Prints error information when an object is not registered.
Definition: Factory.C:251
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:87
MooseObjectID _object_count
Object id count.
Definition: Factory.h:396
MooseApp & _app
Reference to the application.
Definition: Factory.h:373
registeredMooseObjectIterator registeredObjectsEnd()
Access to registered object iterator (end)
Definition: Factory.h:338
Generic factory class for build all sorts of objects.
Definition: Factory.h:153
Factory(MooseApp &app)
Definition: Factory.C:16
std::time_t parseTime(std::string)
Parse time string (mm/dd/yyyy HH:MM)
Definition: Factory.C:178
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
MooseApp & app()
Definition: Factory.h:351
InputParameters getValidParams(const std::string &name)
Get valid parameters for the object.
Definition: Factory.C:67
void regDeprecated(const std::string &obj_name, const std::string t_str, const std::string &file, int line)
Register a deprecated object that expires.
Definition: Factory.h:208
std::string associatedClassName(const std::string &name) const
Get the associated class name for an object name.
Definition: Factory.C:298
FileLineInfoMap _name_to_line
Definition: Factory.h:381
virtual ~Factory()
Definition: Factory.C:18
Base class for MOOSE-based applications.
Definition: MooseApp.h:59
void regExecFlag(const ExecFlagType &flag)
Add a new flag to the app.
Definition: Factory.C:308
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::unique_ptr< T_DEST, T_DELETER > dynamic_pointer_cast(std::unique_ptr< T_SRC, T_DELETER > &src)
These are reworked from https://stackoverflow.com/a/11003103.
MooseObjectPtr(*)(const InputParameters &parameters) buildPtr
alias for method to build objects
Definition: Factory.h:133
std::map< std::string, paramsPtr >::iterator registeredMooseObjectIterator
alias for registered Object iterator
Definition: Factory.h:138
FileLineInfo getLineInfo(const std::string &name) const
Gets file and line information where an object was initially registered.
Definition: Factory.C:286
void renameObject(const std::string &orig_name, const std::string &new_name, const std::string time_str, const std::string &file, int line)
Used when an existing object&#39;s name changes.
Definition: Factory.h:247
std::map< std::string, std::time_t > _deprecated_time
Storage for deprecated object experiation dates.
Definition: Factory.h:387
void associateNameToClass(const std::string &name, const std::string &class_name)
Associates an object name with a class name.
Definition: Factory.C:292
std::set< std::string > _registerable_objects
The list of objects that may be registered.
Definition: Factory.h:393
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:42
void deprecatedMessage(const std::string obj_name)
Show the appropriate message for deprecated objects.
Definition: Factory.C:200
void regReplaced(const std::string &dep_obj, const std::string &replacement_name, const std::string time_str, const std::string &file, int line)
Registers an object as deprecated and associates it with the replacement name.
Definition: Factory.h:226
void reg(const std::string &obj_name, const std::string &file="", int line=-1)
Register a new object.
Definition: Factory.h:164
std::map< std::string, std::string > _deprecated_name
Storage for the deprecated objects that have replacements.
Definition: Factory.h:390
Holds file and line information.
Definition: FileLineInfo.h:18
registeredMooseObjectIterator registeredObjectsBegin()
Access to registered object iterator (begin)
Definition: Factory.h:333
std::map< std::string, paramsPtr > _name_to_params_pointer
Storage for pointers to the parameters objects.
Definition: Factory.h:379
std::map< std::string, std::string > _name_to_class
Object name to class name association.
Definition: Factory.h:384
void releaseSharedObjects(const MooseObject &moose_object, THREAD_ID tid=0)
Releases any shared resources created as a side effect of creating an object through the Factory::cre...
Definition: Factory.C:166
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:21
std::vector< std::string > getConstructedObjects() const
Get a list of all constructed Moose Object types.
Definition: Factory.C:277
std::set< std::string > _constructed_types
Constructed Moose Object types.
Definition: Factory.h:399
std::map< std::string, buildPtr > _name_to_build_pointer
Storage for pointers to the object.
Definition: Factory.h:376
std::shared_ptr< MooseObject > MooseObjectPtr
alias to wrap shared pointer type
Definition: Factory.h:123
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:299
InputParameters(*)() paramsPtr
alias for validParams function
Definition: AppFactory.h:32
A mapping between a series of keys to a FileLineInfo.
Definition: FileLineInfo.h:40
unsigned int MooseObjectID
Definition: MooseTypes.h:160
MooseObjectPtr buildObject(const InputParameters &parameters)
Build an object of type T.
Definition: Factory.h:145
unsigned int THREAD_ID
Definition: MooseTypes.h:161