www.mooseframework.org
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Factory Class Reference

Generic factory class for build all sorts of objects. More...

#include <Factory.h>

Public Member Functions

 Factory (MooseApp &app)
 
virtual ~Factory ()
 
template<typename T >
void reg (const std::string &obj_name, const std::string &file="", int line=-1)
 Register a new object. More...
 
FileLineInfo getLineInfo (const std::string &name) const
 Gets file and line information where an object was initially registered. More...
 
void associateNameToClass (const std::string &name, const std::string &class_name)
 Associates an object name with a class name. More...
 
std::string associatedClassName (const std::string &name) const
 Get the associated class name for an object name. More...
 
template<typename T >
void regDeprecated (const std::string &obj_name, const std::string &file, int line)
 Register a deprecated object that expires. More...
 
template<typename T >
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. More...
 
InputParameters getValidParams (const std::string &name)
 Get valid parameters for the object. More...
 
std::shared_ptr< MooseObjectcreate (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>()) More...
 
template<typename T >
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) More...
 
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 object not contained within the list. More...
 
registeredMooseObjectIterator registeredObjectsBegin ()
 Access to registered object iterator (begin) More...
 
registeredMooseObjectIterator registeredObjectsEnd ()
 Access to registered object iterator (end) More...
 
std::vector< std::string > getConstructedObjects () const
 Get a list of all constructed Moose Object types. More...
 
void deprecateObject (const std::string &name)
 Allow objects to be deprecated via function call. More...
 
void deprecateObject (const std::string &name, const std::string &replacement)
 

Protected Member Functions

void deprecatedMessage (const std::string obj_name)
 Show the appropriate message for deprecated objects. More...
 
void reportUnregisteredError (const std::string &obj_name) const
 Prints error information when an object is not registered. More...
 

Protected Attributes

MooseApp_app
 Reference to the application. More...
 
std::map< std::string, buildPtr_name_to_build_pointer
 Storage for pointers to the object. More...
 
std::map< std::string, paramsPtr_name_to_params_pointer
 Storage for pointers to the parameters objects. More...
 
FileLineInfoMap _name_to_line
 
std::map< std::string, std::string > _name_to_class
 Object name to class name association. More...
 
std::set< std::string > _deprecated
 Storage for deprecated objects. More...
 
std::map< std::string, std::string > _deprecated_with_replace
 Storage for the deprecated objects that have replacements. More...
 
std::set< std::string > _registerable_objects
 The list of objects that may be registered. More...
 
MooseObjectID _object_count
 Object id count. More...
 
std::set< std::string > _constructed_types
 Constructed Moose Object types. More...
 

Detailed Description

Generic factory class for build all sorts of objects.

Definition at line 152 of file Factory.h.

Constructor & Destructor Documentation

Factory::Factory ( MooseApp app)

Definition at line 21 of file Factory.C.

21 : _app(app) {}
MooseApp & _app
Reference to the application.
Definition: Factory.h:338
Factory::~Factory ( )
virtual

Definition at line 23 of file Factory.C.

23 {}

Member Function Documentation

std::string Factory::associatedClassName ( const std::string &  name) const

Get the associated class name for an object name.

This will return an empty string if the name was not previously associated with a class name via associateNameToClass()

Definition at line 148 of file Factory.C.

Referenced by Parser::buildJsonSyntaxTree(), and reg().

149 {
150  auto it = _name_to_class.find(name);
151  if (it == _name_to_class.end())
152  return "";
153  else
154  return it->second;
155 }
std::map< std::string, std::string > _name_to_class
Object name to class name association.
Definition: Factory.h:349
void Factory::associateNameToClass ( const std::string &  name,
const std::string &  class_name 
)

Associates an object name with a class name.

Primarily used with the registerNamed* macros to store the mapping between the object name and the class that implements the object.

Definition at line 142 of file Factory.C.

Referenced by reg().

143 {
144  _name_to_class[name] = class_name;
145 }
std::map< std::string, std::string > _name_to_class
Object name to class name association.
Definition: Factory.h:349
MooseObjectPtr Factory::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>())

Parameters
obj_nameType of the object being constructed
nameName for the object
parametersParameters this object should have
tidThe thread id that this copy will be created for
print_deprecatedcontrols the deprecated message
Returns
The created object

Definition at line 46 of file Factory.C.

Referenced by CreateExecutionerAction::act(), PartitionerAction::act(), SetupMeshAction::act(), CreateProblemAction::act(), SetupPreconditionerAction::act(), SetupPredictorAction::act(), SetupTimeStepperAction::act(), CreateDisplacedProblemAction::act(), MaterialDerivativeTestAction::act(), AddOutputAction::act(), AddControlAction::act(), NonlinearSystemBase::addBoundaryCondition(), NonlinearSystemBase::addConstraint(), NonlinearSystemBase::addDamper(), NonlinearSystemBase::addDGKernel(), NonlinearSystemBase::addDiracKernel(), FEProblemBase::addDistribution(), FEProblemBase::addFunction(), FEProblemBase::addIndicator(), FEProblemBase::addInitialCondition(), NonlinearSystemBase::addInterfaceKernel(), MooseEigenSystem::addKernel(), AuxiliarySystem::addKernel(), NonlinearSystemBase::addKernel(), FEProblemBase::addMarker(), FEProblemBase::addMaterial(), MooseApp::addMeshModifier(), FEProblemBase::addMultiApp(), NonlinearSystemBase::addNodalKernel(), FEProblemBase::addPredictor(), FEProblemBase::addSampler(), AuxiliarySystem::addScalarKernel(), NonlinearSystemBase::addScalarKernel(), NonlinearSystemBase::addSplit(), AuxiliarySystem::addTimeIntegrator(), NonlinearSystemBase::addTimeIntegrator(), FEProblemBase::addTransfer(), FEProblemBase::addUserObject(), AStableDirk4::AStableDirk4(), create(), Transient::init(), and regReplaced().

51 {
52  if (print_deprecated)
53  mooseDeprecated("Factory::create() is deprecated, please use Factory::create<T>() instead");
54 
55  // Pointer to the object constructor
56  std::map<std::string, buildPtr>::iterator it = _name_to_build_pointer.find(obj_name);
57 
58  // Check if the object is registered
59  if (it == _name_to_build_pointer.end())
60  reportUnregisteredError(obj_name);
61 
62  // Print out deprecated message, if it exists
63  deprecatedMessage(obj_name);
64 
65  // Create the actual parameters object that the object will reference
66  InputParameters & params =
67  _app.getInputParameterWarehouse().addInputParameters(name, parameters, tid);
68 
69  // Check to make sure that all required parameters are supplied
70  params.checkParams(name);
71 
72  // register type name as constructed
73  _constructed_types.insert(obj_name);
74 
75  // Actually call the function pointer. You can do this in one line,
76  // but it's a bit more obvious what's happening if you do it in two...
77  buildPtr & func = it->second;
78  return (*func)(params);
79 }
MooseApp & _app
Reference to the application.
Definition: Factory.h:338
InputParameterWarehouse & getInputParameterWarehouse()
Get the InputParameterWarehouse for MooseObjects.
Definition: MooseApp.C:1121
void mooseDeprecated(Args &&...args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:202
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
void deprecatedMessage(const std::string obj_name)
Show the appropriate message for deprecated objects.
Definition: Factory.C:88
void checkParams(const std::string &parsing_syntax)
This function checks parameters stored in the object to make sure they are in the correct state as th...
InputParameters & addInputParameters(const std::string &name, InputParameters parameters, THREAD_ID tid=0)
Method for adding a new InputParameters object.
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
template<typename T >
std::shared_ptr<T> Factory::create ( const std::string &  obj_name,
const std::string &  name,
InputParameters  parameters,
THREAD_ID  tid = 0 
)
inline

Build an object (must be registered)

Parameters
obj_nameType of the object being constructed
nameName for the object
parametersParameters this object should have
tidThe thread id that this copy will be created for
Returns
The created object

Definition at line 279 of file Factory.h.

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  }
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
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
void Factory::deprecatedMessage ( const std::string  obj_name)
protected

Show the appropriate message for deprecated objects.

Parameters
obj_nameName of the deprecated object

Definition at line 88 of file Factory.C.

Referenced by create(), getValidParams(), and registeredObjectsEnd().

89 {
90  // If the object is not deprecated return
91  auto iter = _deprecated.find(obj_name);
92  if (iter == _deprecated.end())
93  return;
94 
95  // Build the basic message
96  std::ostringstream msg;
97  msg << "Deprecated Object: " << obj_name << "\n";
98 
99  // Append replacement object, if it exsits
100  auto map_iter = _deprecated_with_replace.find(obj_name);
101  if (map_iter != _deprecated_with_replace.end())
102  msg << "Replaced " << obj_name << " with " << map_iter->second;
103 
104  // Produce the error message
105  mooseDeprecated(msg.str());
106 }
void mooseDeprecated(Args &&...args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:202
std::map< std::string, std::string > _deprecated_with_replace
Storage for the deprecated objects that have replacements.
Definition: Factory.h:355
std::set< std::string > _deprecated
Storage for deprecated objects.
Definition: Factory.h:352
void Factory::deprecateObject ( const std::string &  name)

Allow objects to be deprecated via function call.

Definition at line 158 of file Factory.C.

Referenced by deprecateObject(), regDeprecated(), registeredObjectsEnd(), Moose::registerObjects(), and regReplaced().

159 {
160  _deprecated.insert(name);
161 }
std::set< std::string > _deprecated
Storage for deprecated objects.
Definition: Factory.h:352
void Factory::deprecateObject ( const std::string &  name,
const std::string &  replacement 
)

Definition at line 164 of file Factory.C.

165 {
166  deprecateObject(name);
167  _deprecated_with_replace[name] = replacement;
168 }
std::map< std::string, std::string > _deprecated_with_replace
Storage for the deprecated objects that have replacements.
Definition: Factory.h:355
void deprecateObject(const std::string &name)
Allow objects to be deprecated via function call.
Definition: Factory.C:158
std::vector< std::string > Factory::getConstructedObjects ( ) const

Get a list of all constructed Moose Object types.

Definition at line 127 of file Factory.C.

Referenced by registeredObjectsEnd(), and MooseApp::runInputFile().

128 {
129  std::vector<std::string> list;
130  for (const auto & name : _constructed_types)
131  list.push_back(name);
132  return list;
133 }
std::set< std::string > _constructed_types
Constructed Moose Object types.
Definition: Factory.h:364
FileLineInfo Factory::getLineInfo ( const std::string &  name) const

Gets file and line information where an object was initially registered.

Parameters
nameObject name
Returns
The FileLineInfo associated with name

Definition at line 136 of file Factory.C.

Referenced by Parser::buildJsonSyntaxTree(), and reg().

137 {
138  return _name_to_line.getInfo(name);
139 }
FileLineInfoMap _name_to_line
Definition: Factory.h:346
FileLineInfo getInfo(const std::string &key0) const
Get file/line info for a key.
Definition: FileLineInfo.C:81
InputParameters Factory::getValidParams ( const std::string &  name)

Get valid parameters for the object.

Parameters
nameName of the object whose parameter we are requesting
Returns
Parameters of the object

Definition at line 26 of file Factory.C.

Referenced by AB2PredictorCorrector::AB2PredictorCorrector(), CreateDisplacedProblemAction::act(), SetupResidualDebugAction::act(), MaterialDerivativeTestAction::act(), AddNodalNormalsAction::act(), Executioner::addAttributeReporter(), AStableDirk4::AStableDirk4(), FEProblemBase::getFunction(), Transient::init(), regReplaced(), and Transient::setupTimeIntegrator().

27 {
28  std::map<std::string, paramsPtr>::iterator it = _name_to_params_pointer.find(obj_name);
29 
30  // Check if the object is registered
31  if (it == _name_to_params_pointer.end())
32  reportUnregisteredError(obj_name);
33 
34  // Print out deprecated message, if it exists
35  deprecatedMessage(obj_name);
36 
37  // Return the parameters
38  paramsPtr & func = it->second;
39  InputParameters params = (*func)();
40  params.addPrivateParam("_moose_app", &_app);
41 
42  return params;
43 }
MooseApp & _app
Reference to the application.
Definition: Factory.h:338
void addPrivateParam(const std::string &name, const T &value)
These method add a parameter to the InputParameters object which can be retrieved like any other para...
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
InputParameters(*)( paramsPtr)
alias for validParams function
Definition: AppFactory.h:40
void deprecatedMessage(const std::string obj_name)
Show the appropriate message for deprecated objects.
Definition: Factory.C:88
std::map< std::string, paramsPtr > _name_to_params_pointer
Storage for pointers to the parameters objects.
Definition: Factory.h:344
template<typename T >
void Factory::reg ( const std::string &  obj_name,
const std::string &  file = "",
int  line = -1 
)
inline

Register a new object.

Parameters
obj_nameName of the object to register

Definition at line 163 of file Factory.h.

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  }
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
void addInfo(const std::string &key0, const std::string &file, int line)
Associate a key with file/line info.
Definition: FileLineInfo.C:40
std::set< std::string > _registerable_objects
The list of objects that may be registered.
Definition: Factory.h:358
std::map< std::string, paramsPtr > _name_to_params_pointer
Storage for pointers to the parameters objects.
Definition: Factory.h:344
std::map< std::string, buildPtr > _name_to_build_pointer
Storage for pointers to the object.
Definition: Factory.h:341
template<typename T >
void Factory::regDeprecated ( const std::string &  obj_name,
const std::string &  file,
int  line 
)
inline

Register a deprecated object that expires.

Parameters
obj_nameThe name of the object to register
t_strString containing the expiration date for the object

Note: Params file and line are supplied by the macro

Definition at line 218 of file Factory.h.

221  {
222  // Register the name
223  reg<T>(obj_name, file, line);
224  deprecateObject(obj_name);
225  }
void deprecateObject(const std::string &name)
Allow objects to be deprecated via function call.
Definition: Factory.C:158
registeredMooseObjectIterator Factory::registeredObjectsBegin ( )
inline

Access to registered object iterator (begin)

Definition at line 305 of file Factory.h.

Referenced by Parser::buildFullTree(), and Parser::buildJsonSyntaxTree().

305 { return _name_to_params_pointer.begin(); }
std::map< std::string, paramsPtr > _name_to_params_pointer
Storage for pointers to the parameters objects.
Definition: Factory.h:344
registeredMooseObjectIterator Factory::registeredObjectsEnd ( )
inline

Access to registered object iterator (end)

Definition at line 310 of file Factory.h.

Referenced by Parser::buildFullTree(), and Parser::buildJsonSyntaxTree().

310 { return _name_to_params_pointer.end(); }
std::map< std::string, paramsPtr > _name_to_params_pointer
Storage for pointers to the parameters objects.
Definition: Factory.h:344
template<typename T >
void Factory::regReplaced ( const std::string &  dep_obj,
const std::string &  replacement_name,
const std::string &  file,
int  line 
)
inline

Registers an object as deprecated and associates it with the replacement name.

Parameters
dep_obj- The name (type) of the object being registered (the deprecated type)
replacement_name- The name of the object replacing the deprecated object (new name)
time_str- Time at which the deprecated message prints as an error "MM/DD/YYYY HH:MM"

Note: Params file and line are supplied by the macro

Definition at line 236 of file Factory.h.

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  }
void deprecateObject(const std::string &name)
Allow objects to be deprecated via function call.
Definition: Factory.C:158
void Factory::reportUnregisteredError ( const std::string &  obj_name) const
protected

Prints error information when an object is not registered.

Definition at line 109 of file Factory.C.

Referenced by create(), getValidParams(), and registeredObjectsEnd().

110 {
111  std::ostringstream oss;
112  std::set<std::string> paths = _app.getLoadedLibraryPaths();
113 
114  oss << "A '" + obj_name + "' is not a registered object.\n"
115  << "\nWe loaded objects from the following libraries and still couldn't find your "
116  "object:\n\t";
117  std::copy(paths.begin(), paths.end(), infix_ostream_iterator<std::string>(oss, "\n\t"));
118  if (paths.empty())
119  oss << "(NONE)\n";
120  oss << "\n\nMake sure you have compiled the library and either set the \"library_path\" variable "
121  << "in your input file or exported \"MOOSE_LIBRARY_PATH\".";
122 
123  mooseError(oss.str());
124 }
MooseApp & _app
Reference to the application.
Definition: Factory.h:338
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
std::set< std::string > getLoadedLibraryPaths() const
Return the loaded library filenames in a std::vector.
Definition: MooseApp.C:1110
void Factory::restrictRegisterableObjects ( const std::vector< std::string > &  names)

Calling this object with a non-empty vector will cause this factory to ignore registrations from any object not contained within the list.

Parameters
namesa vector containing the names of objects that this factory will register

Definition at line 82 of file Factory.C.

Referenced by create(), and DynamicObjectRegistrationAction::DynamicObjectRegistrationAction().

83 {
84  _registerable_objects.insert(names.begin(), names.end());
85 }
std::set< std::string > _registerable_objects
The list of objects that may be registered.
Definition: Factory.h:358

Member Data Documentation

MooseApp& Factory::_app
protected

Reference to the application.

Definition at line 338 of file Factory.h.

Referenced by create(), getValidParams(), and reportUnregisteredError().

std::set<std::string> Factory::_constructed_types
protected

Constructed Moose Object types.

Definition at line 364 of file Factory.h.

Referenced by create(), and getConstructedObjects().

std::set<std::string> Factory::_deprecated
protected

Storage for deprecated objects.

Definition at line 352 of file Factory.h.

Referenced by deprecatedMessage(), and deprecateObject().

std::map<std::string, std::string> Factory::_deprecated_with_replace
protected

Storage for the deprecated objects that have replacements.

Definition at line 355 of file Factory.h.

Referenced by deprecatedMessage(), and deprecateObject().

std::map<std::string, buildPtr> Factory::_name_to_build_pointer
protected

Storage for pointers to the object.

Definition at line 341 of file Factory.h.

Referenced by create(), and reg().

std::map<std::string, std::string> Factory::_name_to_class
protected

Object name to class name association.

Definition at line 349 of file Factory.h.

Referenced by associatedClassName(), and associateNameToClass().

FileLineInfoMap Factory::_name_to_line
protected

Definition at line 346 of file Factory.h.

Referenced by getLineInfo(), and reg().

std::map<std::string, paramsPtr> Factory::_name_to_params_pointer
protected

Storage for pointers to the parameters objects.

Definition at line 344 of file Factory.h.

Referenced by getValidParams(), reg(), registeredObjectsBegin(), and registeredObjectsEnd().

MooseObjectID Factory::_object_count
protected

Object id count.

Definition at line 361 of file Factory.h.

std::set<std::string> Factory::_registerable_objects
protected

The list of objects that may be registered.

Definition at line 358 of file Factory.h.

Referenced by reg(), and restrictRegisterableObjects().


The documentation for this class was generated from the following files: