www.mooseframework.org
Factory.C
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 #include "Factory.h"
16 #include "InfixIterator.h"
18 // Just for testing...
19 #include "Diffusion.h"
20 
21 Factory::Factory(MooseApp & app) : _app(app) {}
22 
24 
26 Factory::getValidParams(const std::string & obj_name)
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 }
44 
46 Factory::create(const std::string & obj_name,
47  const std::string & name,
48  InputParameters parameters,
49  THREAD_ID tid /* =0 */,
50  bool print_deprecated /* =true */)
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 }
80 
81 void
82 Factory::restrictRegisterableObjects(const std::vector<std::string> & names)
83 {
84  _registerable_objects.insert(names.begin(), names.end());
85 }
86 
87 void
88 Factory::deprecatedMessage(const std::string obj_name)
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 }
107 
108 void
109 Factory::reportUnregisteredError(const std::string & obj_name) const
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 }
125 
126 std::vector<std::string>
128 {
129  std::vector<std::string> list;
130  for (const auto & name : _constructed_types)
131  list.push_back(name);
132  return list;
133 }
134 
136 Factory::getLineInfo(const std::string & name) const
137 {
138  return _name_to_line.getInfo(name);
139 }
140 
141 void
142 Factory::associateNameToClass(const std::string & name, const std::string & class_name)
143 {
144  _name_to_class[name] = class_name;
145 }
146 
147 std::string
148 Factory::associatedClassName(const std::string & name) const
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 }
156 
157 void
158 Factory::deprecateObject(const std::string & name)
159 {
160  _deprecated.insert(name);
161 }
162 
163 void
164 Factory::deprecateObject(const std::string & name, const std::string & replacement)
165 {
166  deprecateObject(name);
167  _deprecated_with_replace[name] = replacement;
168 }
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
MooseApp & _app
Reference to the application.
Definition: Factory.h:338
Factory(MooseApp &app)
Definition: Factory.C:21
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...
InputParameters getValidParams(const std::string &name)
Get valid parameters for the object.
Definition: Factory.C:26
InputParameterWarehouse & getInputParameterWarehouse()
Get the InputParameterWarehouse for MooseObjects.
Definition: MooseApp.C:1121
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
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
void mooseDeprecated(Args &&...args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:202
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...
std::set< std::string > getLoadedLibraryPaths() const
Return the loaded library filenames in a std::vector.
Definition: MooseApp.C:1110
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
MooseObjectPtr(*)(const InputParameters &parameters) buildPtr
alias for method to build objects
Definition: Factory.h:132
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 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.
Holds file and line information.
Definition: FileLineInfo.h:24
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
std::set< std::string > _deprecated
Storage for deprecated objects.
Definition: Factory.h:352
std::shared_ptr< MooseObject > MooseObjectPtr
alias to wrap shared pointer type
Definition: Factory.h:122
FileLineInfo getInfo(const std::string &key0) const
Get file/line info for a key.
Definition: FileLineInfo.C:81
unsigned int THREAD_ID
Definition: MooseTypes.h:79