https://mooseframework.inl.gov
Registry.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 "InputParameters.h"
13 
14 #include <string>
15 #include <vector>
16 #include <set>
17 #include <map>
18 #include <memory>
19 #include <optional>
20 
21 #include "libmesh/utility.h"
22 
23 #ifdef MOOSE_UNIT_TEST
24 #include <gtest/gtest.h>
25 #endif
26 
27 #define combineNames1(X, Y) X##Y
28 #define combineNames(X, Y) combineNames1(X, Y)
29 
34 #define registerKnownLabel(X) \
35  [[maybe_unused]] static char combineNames(dummy_var_for_known_label, __COUNTER__) = \
36  Registry::addKnownLabel(X)
37 
40 #define registerMooseAction(app, classname, task) \
41  [[maybe_unused]] static char combineNames(dummyvar_for_registering_action_##classname, \
42  __COUNTER__) = \
43  Registry::addAction<classname>({app, #classname, "", task, __FILE__, __LINE__, "", ""})
44 
47 #define registerMooseObject(app, classname) \
48  [[maybe_unused]] static char combineNames(dummyvar_for_registering_obj_##classname, \
49  __COUNTER__) = \
50  Registry::add<classname>({app, #classname, "", "", __FILE__, __LINE__, "", ""})
51 
52 #define registerADMooseObject(app, classname) registerMooseObject(app, classname)
53 
56 #define registerMooseObjectAliased(app, classname, alias) \
57  [[maybe_unused]] static char combineNames(dummyvar_for_registering_obj_##classname, \
58  __COUNTER__) = \
59  Registry::add<classname>({app, #classname, alias, "", __FILE__, __LINE__, "", ""})
60 
63 #define registerMooseObjectDeprecated(app, classname, time) \
64  [[maybe_unused]] static char combineNames(dummyvar_for_registering_obj_##classname, \
65  __COUNTER__) = \
66  Registry::add<classname>({app, #classname, "", "", __FILE__, __LINE__, time, ""})
67 
68 #define registerADMooseObjectDeprecated(app, classname, time) \
69  registerMooseObjectDeprecated(app, classname, time)
70 
73 #define registerMooseObjectReplaced(app, classname, time, replacement) \
74  [[maybe_unused]] static char combineNames(dummyvar_for_registering_obj_##classname, \
75  __COUNTER__) = \
76  Registry::add<classname>({app, #classname, "", "", __FILE__, __LINE__, time, #replacement})
77 
82 #define registerMooseObjectRenamed(app, orig_class, time, new_class) \
83  [[maybe_unused]] static char combineNames(dummyvar_for_registering_obj_##orig_class, \
84  __COUNTER__) = \
85  Registry::add<new_class>( \
86  {app, #new_class, #orig_class, #orig_class, __FILE__, __LINE__, time, #new_class})
87 
88 #define registerADMooseObjectRenamed(app, orig_class, time, new_class) \
89  registerMooseObjectRenamed(app, orig_class, time, new_class)
90 
92 #define registerNonAppDataFilePath(name, path) Registry::addDataFilePath(name, path, false)
93 #define registerNonAppDataFilePathWithInfo(name, path, info) \
95  Registry::addDataFilePath(name, path, false, info)
96 #define registerAppDataFilePath(app) Registry::addAppDataFilePath(app, __FILE__)
100 #define registerMissingDataFilePath(name, info) Registry::addMissingDataFilePath(name, info)
102 #define registerDataFilePath() Registry::addDeprecatedAppDataFilePath(__FILE__)
104 
105 #define registerRepository(repo_name, repo_url) Registry::addRepository(repo_name, repo_url);
106 
107 class Factory;
108 class ActionFactory;
109 class MooseObject;
110 class Action;
111 struct RegistryEntryBase;
112 
118 {
120  std::string _label;
122  std::string _classname;
125  std::string _alias;
127  std::string _name;
129  std::string _file;
131  int _line;
133  std::string _deprecated_time;
135  std::string _replaced_by;
136 };
137 
139 {
141  virtual ~RegistryEntryBase() {}
143  virtual std::unique_ptr<MooseObject> build(const InputParameters & parameters) = 0;
144  virtual std::shared_ptr<MooseObject> buildShared(const InputParameters & parameters) = 0;
145  virtual std::shared_ptr<Action> buildAction(const InputParameters & parameters) = 0;
146  virtual InputParameters buildParameters() = 0;
148  std::string name() const
149  {
150  std::string name = _name;
151  if (name.empty())
152  name = _alias;
153  if (name.empty())
154  name = _classname;
155  return name;
156  }
157 };
158 
159 template <typename T>
161 {
162  RegistryEntry(const RegistryEntryData & data);
163  virtual std::unique_ptr<MooseObject> build(const InputParameters & parameters) override;
164  virtual std::shared_ptr<MooseObject> buildShared(const InputParameters & parameters) override;
165  virtual std::shared_ptr<Action> buildAction(const InputParameters & parameters) override;
166  virtual InputParameters buildParameters() override;
167 };
168 
178 class Registry
179 {
180 public:
184  static Registry & getRegistry();
185 
189  template <typename T>
190  static char add(const RegistryEntryData & base_info)
191  {
192  const auto info = std::make_shared<RegistryEntry<T>>(base_info);
193  getRegistry()._per_label_objects[info->_label].push_back(info);
194  getRegistry()._type_to_classname[typeid(T).name()] = info->name();
195  return 0;
196  }
197 
201  template <typename T>
202  static char addAction(const RegistryEntryData & base_info)
203  {
204  const auto info = std::make_shared<RegistryEntry<T>>(base_info);
205  getRegistry()._per_label_actions[info->_label].push_back(info);
206  getRegistry()._type_to_classname[typeid(T).name()] = info->_classname;
207  return 0;
208  }
209 
210  template <typename T>
211  static std::string getClassName()
212  {
213  return libmesh_map_find(getRegistry()._type_to_classname, typeid(T).name());
214  }
215 
218  static void registerObjectsTo(Factory & f, const std::set<std::string> & labels);
219 
222  static void registerActionsTo(ActionFactory & f, const std::set<std::string> & labels);
223 
225  static char addKnownLabel(const std::string & label);
226 
228  static void addDataFilePath(const std::string & name,
229  const std::string & in_tree_path,
230  const bool app = true,
231  const std::optional<std::string> & info = {});
234  static void addAppDataFilePath(const std::string & app_name, const std::string & app_path);
236  static void addMissingDataFilePath(const std::string & name, const std::string & info);
238  static void addDeprecatedAppDataFilePath(const std::string & app_path);
239 
241  static void addRepository(const std::string & repo_name, const std::string & repo_url);
242 
246  static void
247  addAppCitation(const std::string & app_name, const std::string & key, const std::string & bibtex);
248 
250  static const std::map<std::string, std::vector<std::shared_ptr<RegistryEntryBase>>> & allObjects()
251  {
253  }
255  static const std::map<std::string, std::vector<std::shared_ptr<RegistryEntryBase>>> & allActions()
256  {
258  }
259 
260  static const RegistryEntryBase & objData(const std::string & name);
261 
265  static bool isRegisteredObj(const std::string & name)
266  {
267  return getRegistry()._name_to_entry.count(name);
268  }
269 
271  static const std::map<std::string, std::string> & getDataFilePaths()
272  {
273  return getRegistry()._data_file_paths;
274  }
280  static std::string getDataFilePath(const std::string & name);
281 
283  static const std::string & getRepositoryURL(const std::string & repo_name);
287  static const std::map<std::string, std::string> & getRepos() { return getRegistry()._repos; }
288 
291  static const std::map<std::string, std::map<std::string, std::string>> & getCitations()
292  {
293  return getRegistry()._app_citations;
294  }
299  static const std::map<std::string, std::string> & getCitations(const std::string & app_name);
300 
302  template <typename T>
303  static std::string getRegisteredName();
304 
306  Registry(Registry const &) = delete;
307  Registry & operator=(Registry const &) = delete;
308 
309  Registry(Registry &&) = delete;
310  Registry & operator=(Registry &&) = delete;
312 
313 private:
314 #ifdef MOOSE_UNIT_TEST
315  friend class RegistryTest;
318  friend class DataFileUtilsTest;
319  FRIEND_TEST(RegistryTest, determineFilePath);
320  FRIEND_TEST(RegistryTest, determineFilePathFailed);
322  FRIEND_TEST(RegistryTest, appNameFromAppPathFailed);
325 #endif
326 
327  Registry() {};
328 
334  static void setDataFilePaths(const std::map<std::string, std::string> & data_file_paths)
335  {
336  getRegistry()._data_file_paths = data_file_paths;
337  }
343  static void setRepos(const std::map<std::string, std::string> & repos)
344  {
345  getRegistry()._repos = repos;
346  }
347 
349  static std::string determineDataFilePath(const std::string & name,
350  const std::string & in_tree_path);
351 
354  static std::string appNameFromAppPath(const std::string & app_path);
355 
357  static void checkDataFilePathName(const std::string & name);
358 
360  static void addDataFilePathCapability(const std::string & name,
361  const std::optional<std::string> & path = {},
362  const std::optional<std::string> & extra_info = {});
363 
364  std::map<std::string, std::shared_ptr<RegistryEntryBase>> _name_to_entry;
365  std::map<std::string, std::vector<std::shared_ptr<RegistryEntryBase>>> _per_label_objects;
366  std::map<std::string, std::vector<std::shared_ptr<RegistryEntryBase>>> _per_label_actions;
367  std::set<std::string> _known_labels;
369  std::map<std::string, std::string> _data_file_paths;
371  std::map<std::string, std::string> _repos;
372  std::map<std::string, std::string> _type_to_classname;
374  std::map<std::string, std::map<std::string, std::string>> _app_citations;
375 };
376 
377 template <typename T>
378 std::string
380 {
381  mooseDeprecated("Use Registry::getClassName() instead.");
382  return getClassName<T>();
383 }
384 
385 template <typename T>
387 {
388  static_assert(std::is_base_of_v<MooseObject, T> || std::is_base_of_v<Action, T>,
389  "Not derived from MooseObject or Action");
390 }
391 
392 template <typename T>
393 std::unique_ptr<MooseObject>
395 {
396  if constexpr (std::is_base_of_v<MooseObject, T>)
397  return std::make_unique<T>(parameters);
398  mooseError(MooseUtils::prettyCppType<T>(), " to be built is not a MooseObject.");
399 }
400 template <typename T>
401 std::shared_ptr<MooseObject>
403 {
404  if constexpr (std::is_base_of_v<MooseObject, T>)
405  return std::make_shared<T>(parameters);
406  mooseError(MooseUtils::prettyCppType<T>(), " to be built is not a MooseObject.");
407 }
408 
409 template <typename T>
410 std::shared_ptr<Action>
412 {
413  if constexpr (!std::is_base_of_v<Action, T>)
414  mooseError("The action to be built is not derived from Action.");
415  else
416  return std::make_shared<T>(parameters);
417 }
418 
419 template <typename T>
422 {
423  auto params = T::validParams();
424  return params;
425 }
std::string name(const ElemQuality q)
static std::string appNameFromAppPath(const std::string &app_path)
Internal helper for getting an application name from its path, for example: /path/to/FooBarBazApp.C -> foo_bar_baz, for use in addDeprecatedAppDataFilePath.
Definition: Registry.C:246
std::map< std::string, std::map< std::string, std::string > > _app_citations
App/module name -> (citation key -> BibTeX entry text) for that app.
Definition: Registry.h:374
static const std::map< std::string, std::map< std::string, std::string > > & getCitations()
Returns the registered citations, keyed by app/module name and then by BibTeX key (app/module name ->...
Definition: Registry.h:291
virtual std::shared_ptr< MooseObject > buildShared(const InputParameters &parameters) override
Definition: Registry.h:402
virtual std::shared_ptr< Action > buildAction(const InputParameters &parameters) override
Definition: Registry.h:411
virtual std::unique_ptr< MooseObject > build(const InputParameters &parameters)=0
proxy functions
static const RegistryEntryBase & objData(const std::string &name)
Definition: Registry.C:58
std::map< std::string, std::vector< std::shared_ptr< RegistryEntryBase > > > _per_label_actions
Definition: Registry.h:366
static Registry & getRegistry()
Get the global Registry singleton.
Definition: Registry.C:24
static void addDataFilePath(const std::string &name, const std::string &in_tree_path, const bool app=true, const std::optional< std::string > &info={})
register general search paths; "app" is whether or not the path is an app path
Definition: Registry.C:92
virtual InputParameters buildParameters()=0
MPI_Info info
Generic factory class for build all sorts of objects.
Definition: Factory.h:28
static std::string getDataFilePath(const std::string &name)
Gets a data path for the registered name.
Definition: Registry.C:166
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
static void addAppDataFilePath(const std::string &app_name, const std::string &app_path)
register search paths for an application (path determined relative to app_path); app_path should be p...
Definition: Registry.C:144
Holds details and meta-data info for a particular MooseObject or Action for use in the use in the reg...
Definition: Registry.h:117
static void addAppCitation(const std::string &app_name, const std::string &key, const std::string &bibtex)
Register a citation (the full BibTeX bibtex text, identified by key) tied to the app app_name; emitte...
Definition: Registry.C:198
static const std::map< std::string, std::vector< std::shared_ptr< RegistryEntryBase > > > & allActions()
Returns a per-label keyed map of all Actions in the registry.
Definition: Registry.h:255
static std::string getRegisteredName()
returns the name() for a registered class
Definition: Registry.h:379
std::string _classname
name of the c++ class for the object.
Definition: Registry.h:122
static std::string determineDataFilePath(const std::string &name, const std::string &in_tree_path)
Internal helper for determing a root data file path (in-tree vs installed)
Definition: Registry.C:219
friend class RegistryTest
Friends for unit testing.
Definition: Registry.h:317
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
static void registerObjectsTo(Factory &f, const std::set< std::string > &labels)
This registers all MooseObjects known to the registry that have the given label(s) with the factory f...
Definition: Registry.C:35
virtual std::shared_ptr< Action > buildAction(const InputParameters &parameters)=0
RegistryEntryBase(const RegistryEntryData &data)
Definition: Registry.h:140
Registry()
Definition: Registry.h:327
static const std::map< std::string, std::vector< std::shared_ptr< RegistryEntryBase > > > & allObjects()
Returns a per-label keyed map of all MooseObjects in the registry.
Definition: Registry.h:250
std::string _file
file path for the c++ file the object or action was added to the registry in.
Definition: Registry.h:129
static char addAction(const RegistryEntryData &base_info)
Adds information on an Action object to the registry.
Definition: Registry.h:202
Base class for actions.
Definition: Action.h:34
virtual ~RegistryEntryBase()
Definition: Registry.h:141
friend class DataFileUtilsTest
Definition: Registry.h:318
static void setDataFilePaths(const std::map< std::string, std::string > &data_file_paths)
Manually set the data file paths.
Definition: Registry.h:334
std::string _name
name that the object will be registered to factories under. If unspecified, _alias is used...
Definition: Registry.h:127
virtual std::shared_ptr< MooseObject > buildShared(const InputParameters &parameters)=0
static void addMissingDataFilePath(const std::string &name, const std::string &info)
register a data file path as missing, along with info about how it can be added
Definition: Registry.C:137
FRIEND_TEST(RegistryTest, determineFilePath)
static void registerActionsTo(ActionFactory &f, const std::set< std::string > &labels)
This registers all Actions known to the registry that have the given label(s) with the factory f...
Definition: Registry.C:69
static char add(const RegistryEntryData &base_info)
Adds information on a MooseObject to the registry.
Definition: Registry.h:190
std::string name() const
resolve the name from _classname, _alias, and _name
Definition: Registry.h:148
std::string _alias
an alternate name to register the object to factories under.
Definition: Registry.h:125
std::string _replaced_by
class name for an object that replaces this object if deprecated, blank otherwise.
Definition: Registry.h:135
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:28
static void addDataFilePathCapability(const std::string &name, const std::optional< std::string > &path={}, const std::optional< std::string > &extra_info={})
Add a data file path capability.
Definition: Registry.C:277
std::map< std::string, std::string > _data_file_paths
Data file registry; name -> in-tree path.
Definition: Registry.h:369
std::map< std::string, std::shared_ptr< RegistryEntryBase > > _name_to_entry
Definition: Registry.h:364
Specialized factory for generic Action System objects.
Definition: ActionFactory.h:48
static void checkDataFilePathName(const std::string &name)
Check a data file path for valid characters.
Definition: Registry.C:268
static std::string getClassName()
Definition: Registry.h:211
static void addRepository(const std::string &repo_name, const std::string &repo_url)
register a repository
Definition: Registry.C:176
virtual std::unique_ptr< MooseObject > build(const InputParameters &parameters) override
proxy functions
Definition: Registry.h:394
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:363
int _line
line number in the c++ file the object or action was added to the registry on.
Definition: Registry.h:131
static void setRepos(const std::map< std::string, std::string > &repos)
Manually set the repos.
Definition: Registry.h:343
std::string _label
label (usually app name - e.g. "YourAnimalApp") that the object or action is associated with...
Definition: Registry.h:120
std::set< std::string > _known_labels
Definition: Registry.h:367
std::map< std::string, std::string > _type_to_classname
Definition: Registry.h:372
static bool isRegisteredObj(const std::string &name)
Definition: Registry.h:265
std::string _deprecated_time
time in "mm/dd/yyyy HH:MM" format that the object is/becomes deprecated, blank otherwise.
Definition: Registry.h:133
RegistryEntry(const RegistryEntryData &data)
Definition: Registry.h:386
std::map< std::string, std::string > _repos
Repository name -> repository URL; used for mooseDocumentedError.
Definition: Registry.h:371
virtual InputParameters buildParameters() override
Definition: Registry.h:421
static const std::string & getRepositoryURL(const std::string &repo_name)
Returns the repository URL associated with repo_name.
Definition: Registry.C:189
static const std::map< std::string, std::string > & getDataFilePaths()
Returns a map of all registered data file paths (name -> path)
Definition: Registry.h:271
Registry & operator=(Registry const &)=delete
static void addDeprecatedAppDataFilePath(const std::string &app_path)
deprecated method; use addAppDataFilePath instead
Definition: Registry.C:154
The registry is used as a global singleton to collect information on all available MooseObject and Ac...
Definition: Registry.h:178
static const std::map< std::string, std::string > & getRepos()
Returns a map of all registered repositories.
Definition: Registry.h:287
std::map< std::string, std::vector< std::shared_ptr< RegistryEntryBase > > > _per_label_objects
Definition: Registry.h:365
InputParameters validParams()
static char addKnownLabel(const std::string &label)
addKnownLabel whitelists a label as valid for purposes of the checkLabels function.
Definition: Registry.C:85