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 "Registry.h"
18 #include "MooseObject.h"
19 #include "MooseTypes.h"
20 #include "FileLineInfo.h"
21 
22 // Forward declarations
23 class InputParameters;
24 
28 class Factory
29 {
30 public:
31  Factory(MooseApp & app);
32  virtual ~Factory();
33 
34  void reg(std::shared_ptr<RegistryEntryBase> obj);
35 
41  FileLineInfo getLineInfo(const std::string & name) const;
42 
48  void associateNameToClass(const std::string & name, const std::string & class_name);
49 
55  std::string associatedClassName(const std::string & name) const;
56 
62  InputParameters getValidParams(const std::string & name) const;
63 
73  std::unique_ptr<MooseObject> createUnique(const std::string & obj_name,
75  const std::string & name,
76  const InputParameters & parameters,
77  THREAD_ID tid = 0,
78  bool print_deprecated = true);
79  std::shared_ptr<MooseObject> create(const std::string & obj_name,
80  const std::string & name,
81  const InputParameters & parameters,
82  THREAD_ID tid = 0,
83  bool print_deprecated = true);
85 
94  template <typename T>
96  std::unique_ptr<T> createUnique(const std::string & obj_name,
97  const std::string & name,
98  const InputParameters & parameters,
99  const THREAD_ID tid = 0);
100  template <typename T>
101  std::shared_ptr<T> create(const std::string & obj_name,
102  const std::string & name,
103  const InputParameters & parameters,
104  const THREAD_ID tid = 0);
106 
115  template <typename T>
116  std::unique_ptr<T> clone(const T & object);
117 
125  template <typename T>
126  std::unique_ptr<T> copyConstruct(const T & object);
127 
134  void releaseSharedObjects(const MooseObject & moose_object, THREAD_ID tid = 0);
135 
142  void restrictRegisterableObjects(const std::vector<std::string> & names);
143 
147  const auto & registeredObjects() const { return _name_to_object; }
148 
152  bool isRegistered(const std::string & obj_name) const { return _name_to_object.count(obj_name); }
153 
157  std::vector<std::string> getConstructedObjects() const;
158 
159  MooseApp & app() { return _app; }
160 
167  const InputParameters * currentlyConstructing() const;
168 
169 private:
176  std::time_t parseTime(std::string);
177 
182  void deprecatedMessage(const std::string obj_name) const;
183 
187  void reportUnregisteredError(const std::string & obj_name) const;
188 
193  InputParameters & initialize(const std::string & type,
194  const std::string & name,
195  const InputParameters & from_params,
196  const THREAD_ID tid);
197 
204  void finalize(const std::string & type, const MooseObject & object);
205 
208 
210  std::map<std::string, std::shared_ptr<RegistryEntryBase>> _name_to_object;
211 
213 
215  std::map<std::string, std::string> _name_to_class;
216 
218  std::map<std::string, std::time_t> _deprecated_time;
219 
221  std::map<std::string, std::string> _deprecated_name;
222 
224  std::set<std::string> _registerable_objects;
225 
227  std::set<std::string> _constructed_types;
228 
230  mutable std::set<std::string> _deprecated_types;
231 
235  std::set<std::pair<std::string, std::string>> _objects_by_label;
236 
240  std::vector<const InputParameters *> _currently_constructing;
241 
244  std::map<const MooseObject *, unsigned int> _clone_counter;
245 };
246 
247 template <typename T>
248 std::unique_ptr<T>
249 Factory::createUnique(const std::string & obj_name,
250  const std::string & name,
251  const InputParameters & parameters,
252  const THREAD_ID tid)
253 {
254  auto object = createUnique(obj_name, name, parameters, tid, false);
255  if (!dynamic_cast<T *>(object.get()))
256  mooseError("We expected to create an object of type '" + demangle(typeid(T).name()) +
257  "'.\nInstead we received a parameters object for type '" + obj_name +
258  "'.\nDid you call the wrong \"add\" method in your Action?");
259 
260  return std::unique_ptr<T>(static_cast<T *>(object.release()));
261 }
262 
263 template <typename T>
264 std::shared_ptr<T>
265 Factory::create(const std::string & obj_name,
266  const std::string & name,
267  const InputParameters & parameters,
268  const THREAD_ID tid)
269 {
270  return std::move(createUnique<T>(obj_name, name, parameters, tid));
271 }
272 
273 template <typename T>
274 std::unique_ptr<T>
275 Factory::clone(const T & object)
276 {
277  static_assert(std::is_base_of_v<MooseObject, T>, "Not a MooseObject");
278 
279  const auto tid = object.template getParam<THREAD_ID>("_tid");
280  if (tid != 0)
281  mooseError("Factory::clone(): The object ",
282  object.typeAndName(),
283  " is threaded but cloning does not work with threaded objects");
284 
285  // Clone the parameters; we can't copy construct InputParameters
286  InputParameters cloned_params = emptyInputParameters();
287  cloned_params += object.parameters();
288  if (const auto hit_node = object.parameters().getHitNode())
289  cloned_params.setHitNode(*hit_node, {});
290 
291  // Fill the new parameters in the warehouse
292  const auto type = static_cast<const MooseBase &>(object).type();
293  const auto clone_count = _clone_counter[&object]++;
294  const auto name = object.name() + "_clone" + std::to_string(clone_count);
295  const auto & params = initialize(type, name, cloned_params, 0);
296 
297  // Construct the object
298  _currently_constructing.push_back(&params);
299  auto cloned_object = std::make_unique<T>(params);
300  _currently_constructing.pop_back();
301 
302  // Do some sanity checking
303  finalize(type, *cloned_object);
304 
305  return cloned_object;
306 }
307 
308 template <typename T>
309 std::unique_ptr<T>
310 Factory::copyConstruct(const T & object)
311 {
312  static_assert(std::is_base_of_v<MooseObject, T>, "Not a MooseObject");
313 
314  const auto type = static_cast<const MooseBase &>(object).type();
315  const auto base = object.parameters().getBase();
316  if (!base || (*base != "MooseMesh" && *base != "RelationshipManager"))
317  mooseError("Copy construction of ", type, " objects is not supported.");
318 
319  _currently_constructing.push_back(&object.parameters());
320  auto cloned_object = std::make_unique<T>(object);
321  _currently_constructing.pop_back();
322 
323  finalize(type, *cloned_object);
324 
325  return cloned_object;
326 }
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:235
std::string name(const ElemQuality q)
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:132
void reportUnregisteredError(const std::string &obj_name) const
Prints error information when an object is not registered.
Definition: Factory.C:221
bool isRegistered(const std::string &obj_name) const
Returns a Boolean indicating whether an object type has been registered.
Definition: Factory.h:152
std::set< std::string > _deprecated_types
Set of deprecated object types that have been printed.
Definition: Factory.h:230
MooseApp & _app
Reference to the application.
Definition: Factory.h:207
Base class for everything in MOOSE with a name and a type.
Definition: MooseBase.h:32
Generic factory class for build all sorts of objects.
Definition: Factory.h:28
Factory(MooseApp &app)
Definition: Factory.C:17
std::time_t parseTime(std::string)
Parse time string (mm/dd/yyyy HH:MM)
Definition: Factory.C:138
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:284
MooseApp & app()
Definition: Factory.h:159
std::string associatedClassName(const std::string &name) const
Get the associated class name for an object name.
Definition: Factory.C:271
FileLineInfoMap _name_to_line
Definition: Factory.h:212
virtual ~Factory()
Definition: Factory.C:19
std::shared_ptr< MooseObject > create(const std::string &obj_name, const std::string &name, const InputParameters &parameters, THREAD_ID tid=0, bool print_deprecated=true)
Definition: Factory.C:110
InputParameters getValidParams(const std::string &name) const
Get valid parameters for the object.
Definition: Factory.C:67
Base class for MOOSE-based applications.
Definition: MooseApp.h:73
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::unique_ptr< T > copyConstruct(const T &object)
Copy constructs the object object.
Definition: Factory.h:310
FileLineInfo getLineInfo(const std::string &name) const
Gets file and line information where an object was initially registered.
Definition: Factory.C:259
InputParameters emptyInputParameters()
void deprecatedMessage(const std::string obj_name) const
Show the appropriate message for deprecated objects.
Definition: Factory.C:160
InputParameters & initialize(const std::string &type, const std::string &name, const InputParameters &from_params, const THREAD_ID tid)
Initializes the data structures and the parameters (in the InputParameterWarehouse) for the object wi...
Definition: Factory.C:281
std::map< std::string, std::time_t > _deprecated_time
Storage for deprecated object experiation dates.
Definition: Factory.h:218
void reg(std::shared_ptr< RegistryEntryBase > obj)
Definition: Factory.C:22
void associateNameToClass(const std::string &name, const std::string &class_name)
Associates an object name with a class name.
Definition: Factory.C:265
std::set< std::string > _registerable_objects
The list of objects that may be registered.
Definition: Factory.h:224
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:33
const auto & registeredObjects() const
Returns a reference to the map from names to RegistryEntryBase pointers.
Definition: Factory.h:147
std::vector< const InputParameters * > _currently_constructing
The object&#39;s parameters that are currently being constructed (if any).
Definition: Factory.h:240
std::map< std::string, std::string > _deprecated_name
Storage for the deprecated objects that have replacements.
Definition: Factory.h:221
std::string demangle(const char *name)
Holds file and line information.
Definition: FileLineInfo.h:18
std::map< std::string, std::string > _name_to_class
Object name to class name association.
Definition: Factory.h:215
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:126
std::map< std::string, std::shared_ptr< RegistryEntryBase > > _name_to_object
Storage for pointers to the object registry entry.
Definition: Factory.h:210
void finalize(const std::string &type, const MooseObject &object)
Finalizes the creaction of object of type type.
Definition: Factory.C:325
std::vector< std::string > getConstructedObjects() const
Get a list of all constructed Moose Object types.
Definition: Factory.C:244
std::set< std::string > _constructed_types
Constructed Moose Object types.
Definition: Factory.h:227
std::map< const MooseObject *, unsigned int > _clone_counter
Counter for keeping track of the number of times an object with a given name has been cloned so that ...
Definition: Factory.h:244
void setHitNode(const std::string &param, const hit::Node &node, const SetParamHitNodeKey)
Sets the hit node associated with the parameter param to node.
std::unique_ptr< T > clone(const T &object)
Clones the object object.
Definition: Factory.h:275
A mapping between a series of keys to a FileLineInfo.
Definition: FileLineInfo.h:40
std::unique_ptr< MooseObject > createUnique(const std::string &obj_name, const std::string &name, const 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:86
const InputParameters * currentlyConstructing() const
Definition: Factory.C:253
unsigned int THREAD_ID
Definition: MooseTypes.h:198