https://mooseframework.inl.gov
Factory.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
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:
175  template <class T, class SmartPtr>
176  void createCheckObjectType(const SmartPtr & object, const std::string & obj_name) const;
177 
202  template <class ptr_type>
204  ptr_type createTempl(const std::string & obj_name,
205  const std::string & name,
206  const InputParameters & parameters,
207  const THREAD_ID tid,
208  const std::optional<std::string> & deprecated_method_name);
209 
216  std::time_t parseTime(std::string);
217 
222  void deprecatedMessage(const std::string obj_name) const;
223 
227  void reportUnregisteredError(const std::string & obj_name) const;
228 
233  InputParameters & initialize(const std::string & type,
234  const std::string & name,
235  const InputParameters & from_params,
236  const THREAD_ID tid);
237 
244  void finalize(const std::string & type, const MooseObject & object);
245 
248 
250  std::map<std::string, std::shared_ptr<RegistryEntryBase>> _name_to_object;
251 
253 
255  std::map<std::string, std::string> _name_to_class;
256 
258  std::map<std::string, std::time_t> _deprecated_time;
259 
261  std::map<std::string, std::string> _deprecated_name;
262 
264  std::set<std::string> _registerable_objects;
265 
267  std::set<std::string> _constructed_types;
268 
270  mutable std::set<std::string> _deprecated_types;
271 
275  std::set<std::pair<std::string, std::string>> _objects_by_label;
276 
280  std::vector<const InputParameters *> _currently_constructing;
281 
284  std::map<const MooseObject *, unsigned int> _clone_counter;
285 };
286 
287 template <class T, class SmartPtr>
288 void
289 Factory::createCheckObjectType(const SmartPtr & object, const std::string & obj_name) const
290 {
291  if (!dynamic_cast<T *>(object.get()))
292  mooseError("We expected to create an object of type '" + MooseUtils::prettyCppType<T>() +
293  "'.\nInstead we received a parameters object for type '" + obj_name +
294  "'.\nDid you call the wrong \"add\" method in your Action?");
295 }
296 
297 template <typename T>
298 std::unique_ptr<T>
299 Factory::createUnique(const std::string & obj_name,
300  const std::string & name,
301  const InputParameters & parameters,
302  const THREAD_ID tid)
303 {
304  auto object = createUnique(obj_name, name, parameters, tid, false);
305  createCheckObjectType<T>(object, obj_name);
306  return std::unique_ptr<T>(static_cast<T *>(object.release()));
307 }
308 
309 template <typename T>
310 std::shared_ptr<T>
311 Factory::create(const std::string & obj_name,
312  const std::string & name,
313  const InputParameters & parameters,
314  const THREAD_ID tid)
315 {
316  auto object = create(obj_name, name, parameters, tid, false);
317  createCheckObjectType<T>(object, obj_name);
318  return std::static_pointer_cast<T>(object);
319 }
320 
321 template <typename T>
322 std::unique_ptr<T>
323 Factory::clone(const T & object)
324 {
325  static_assert(std::is_base_of_v<MooseObject, T>, "Not a MooseObject");
326 
327  const auto tid = object.template getParam<THREAD_ID>("_tid");
328  if (tid != 0)
329  mooseError("Factory::clone(): The object ",
330  object.typeAndName(),
331  " is threaded but cloning does not work with threaded objects");
332 
333  // Clone the parameters; we can't copy construct InputParameters
334  InputParameters cloned_params = emptyInputParameters();
335  cloned_params += object.parameters();
336  if (const auto hit_node = object.parameters().getHitNode())
337  cloned_params.setHitNode(*hit_node, {});
338 
339  // Fill the new parameters in the warehouse
340  const auto type = static_cast<const MooseBase &>(object).type();
341  const auto clone_count = _clone_counter[&object]++;
342  const auto name = object.name() + "_clone" + std::to_string(clone_count);
343  const auto & params = initialize(type, name, cloned_params, 0);
344 
345  // Construct the object
346  _currently_constructing.push_back(&params);
347  auto cloned_object = std::make_unique<T>(params);
348  _currently_constructing.pop_back();
349 
350  // Do some sanity checking
351  finalize(type, *cloned_object);
352 
353  return cloned_object;
354 }
355 
356 template <typename T>
357 std::unique_ptr<T>
358 Factory::copyConstruct(const T & object)
359 {
360  static_assert(std::is_base_of_v<MooseObject, T>, "Not a MooseObject");
361 
362  const auto type = static_cast<const MooseBase &>(object).type();
363  if (object.hasBase())
364  {
365  const auto & base = object.getBase();
366  if (base != "MooseMesh" && base != "RelationshipManager")
367  mooseError("Copy construction of ", type, " objects is not supported.");
368  }
369 
370  _currently_constructing.push_back(&object.parameters());
371  auto cloned_object = std::make_unique<T>(object);
372  _currently_constructing.pop_back();
373 
374  finalize(type, *cloned_object);
375 
376  return cloned_object;
377 }
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:275
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:162
void reportUnregisteredError(const std::string &obj_name) const
Prints error information when an object is not registered.
Definition: Factory.C:251
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:270
MooseApp & _app
Reference to the application.
Definition: Factory.h:247
Base class for everything in MOOSE with a name and a type.
Definition: MooseBase.h:49
Generic factory class for build all sorts of objects.
Definition: Factory.h:28
Factory(MooseApp &app)
Definition: Factory.C:18
std::time_t parseTime(std::string)
Parse time string (mm/dd/yyyy HH:MM)
Definition: Factory.C:168
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:323
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:301
FileLineInfoMap _name_to_line
Definition: Factory.h:252
virtual ~Factory()
Definition: Factory.C:20
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:142
InputParameters getValidParams(const std::string &name) const
Get valid parameters for the object.
Definition: Factory.C:68
Base class for MOOSE-based applications.
Definition: MooseApp.h:103
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:358
FileLineInfo getLineInfo(const std::string &name) const
Gets file and line information where an object was initially registered.
Definition: Factory.C:289
InputParameters emptyInputParameters()
void deprecatedMessage(const std::string obj_name) const
Show the appropriate message for deprecated objects.
Definition: Factory.C:190
void createCheckObjectType(const SmartPtr &object, const std::string &obj_name) const
Internal method for checking if the created smart pointer is of the correct expected type...
Definition: Factory.h:289
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:311
std::map< std::string, std::time_t > _deprecated_time
Storage for deprecated object expiration dates.
Definition: Factory.h:258
void reg(std::shared_ptr< RegistryEntryBase > obj)
Definition: Factory.C:23
void associateNameToClass(const std::string &name, const std::string &class_name)
Associates an object name with a class name.
Definition: Factory.C:295
ptr_type createTempl(const std::string &obj_name, const std::string &name, const InputParameters &parameters, const THREAD_ID tid, const std::optional< std::string > &deprecated_method_name)
Internal method for creating a MooseObject, either as a shared_ptr or as a unique_ptr.
Definition: Factory.C:88
std::set< std::string > _registerable_objects
The list of objects that may be registered.
Definition: Factory.h:264
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:28
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:280
std::map< std::string, std::string > _deprecated_name
Storage for the deprecated objects that have replacements.
Definition: Factory.h:261
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:255
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:156
std::map< std::string, std::shared_ptr< RegistryEntryBase > > _name_to_object
Storage for pointers to the object registry entry.
Definition: Factory.h:250
void finalize(const std::string &type, const MooseObject &object)
Finalizes the creaction of object of type type.
Definition: Factory.C:355
std::vector< std::string > getConstructedObjects() const
Get a list of all constructed Moose Object types.
Definition: Factory.C:274
std::set< std::string > _constructed_types
Constructed Moose Object types.
Definition: Factory.h:267
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:284
void setHitNode(const std::string &param, const hit::Node &node, const SetParamHitNodeKey)
Sets the hit node associated with the parameter param to node.
const std::string & getBase() const
Definition: MooseBase.h:147
std::unique_ptr< T > clone(const T &object)
Clones the object object.
Definition: Factory.h:323
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:128
const InputParameters * currentlyConstructing() const
Definition: Factory.C:283
unsigned int THREAD_ID
Definition: MooseTypes.h:237