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 
20 #include "libmesh/utility.h"
21 
22 #include <gtest/gtest.h>
23 
24 #define combineNames1(X, Y) X##Y
25 #define combineNames(X, Y) combineNames1(X, Y)
26 
31 #define registerKnownLabel(X) \
32  static char combineNames(dummy_var_for_known_label, __COUNTER__) = Registry::addKnownLabel(X)
33 
36 #define registerMooseAction(app, classname, task) \
37  static char combineNames(dummyvar_for_registering_action_##classname, __COUNTER__) = \
38  Registry::addAction<classname>({app, #classname, "", task, __FILE__, __LINE__, "", ""})
39 
42 #define registerMooseObject(app, classname) \
43  static char combineNames(dummyvar_for_registering_obj_##classname, __COUNTER__) = \
44  Registry::add<classname>({app, #classname, "", "", __FILE__, __LINE__, "", ""})
45 
46 #define registerADMooseObject(app, classname) registerMooseObject(app, classname)
47 
50 #define registerMooseObjectAliased(app, classname, alias) \
51  static char combineNames(dummyvar_for_registering_obj_##classname, __COUNTER__) = \
52  Registry::add<classname>({app, #classname, alias, "", __FILE__, __LINE__, "", ""})
53 
56 #define registerMooseObjectDeprecated(app, classname, time) \
57  static char combineNames(dummyvar_for_registering_obj_##classname, __COUNTER__) = \
58  Registry::add<classname>({app, #classname, "", "", __FILE__, __LINE__, time, ""})
59 
60 #define registerADMooseObjectDeprecated(app, classname, time) \
61  registerMooseObjectDeprecated(app, classname, time)
62 
65 #define registerMooseObjectReplaced(app, classname, time, replacement) \
66  static char combineNames(dummyvar_for_registering_obj_##classname, __COUNTER__) = \
67  Registry::add<classname>({app, #classname, "", "", __FILE__, __LINE__, time, #replacement})
68 
73 #define registerMooseObjectRenamed(app, orig_class, time, new_class) \
74  static char combineNames(dummyvar_for_registering_obj_##orig_class, __COUNTER__) = \
75  Registry::add<new_class>( \
76  {app, #new_class, #orig_class, #orig_class, __FILE__, __LINE__, time, #new_class})
77 
78 #define registerADMooseObjectRenamed(app, orig_class, time, new_class) \
79  registerMooseObjectRenamed(app, orig_class, time, new_class)
80 
82 #define registerNonAppDataFilePath(name, path) Registry::addDataFilePath(name, path)
83 #define registerAppDataFilePath(app) Registry::addAppDataFilePath(app, __FILE__)
87 #define registerDataFilePath() Registry::addDeprecatedAppDataFilePath(__FILE__)
89 
90 #define registerRepository(repo_name, repo_url) Registry::addRepository(repo_name, repo_url);
91 
92 class Factory;
93 class ActionFactory;
94 class MooseObject;
95 class Action;
96 struct RegistryEntryBase;
97 
103 {
105  std::string _label;
107  std::string _classname;
110  std::string _alias;
112  std::string _name;
114  std::string _file;
116  int _line;
118  std::string _deprecated_time;
120  std::string _replaced_by;
121 };
122 
124 {
126  virtual ~RegistryEntryBase() {}
128  virtual std::unique_ptr<MooseObject> build(const InputParameters & parameters) = 0;
129  virtual std::shared_ptr<MooseObject> buildShared(const InputParameters & parameters) = 0;
130  virtual std::shared_ptr<Action> buildAction(const InputParameters & parameters) = 0;
131  virtual InputParameters buildParameters() = 0;
133  std::string name() const
134  {
135  std::string name = _name;
136  if (name.empty())
137  name = _alias;
138  if (name.empty())
139  name = _classname;
140  return name;
141  }
142 };
143 
144 template <typename T>
146 {
147  RegistryEntry(const RegistryEntryData & data);
148  virtual std::unique_ptr<MooseObject> build(const InputParameters & parameters) override;
149  virtual std::shared_ptr<MooseObject> buildShared(const InputParameters & parameters) override;
150  virtual std::shared_ptr<Action> buildAction(const InputParameters & parameters) override;
151  virtual InputParameters buildParameters() override;
152 };
153 
163 class Registry
164 {
165 public:
169  static Registry & getRegistry();
170 
174  template <typename T>
175  static char add(const RegistryEntryData & base_info)
176  {
177  const auto info = std::make_shared<RegistryEntry<T>>(base_info);
178  getRegistry()._per_label_objects[info->_label].push_back(info);
179  getRegistry()._type_to_classname[typeid(T).name()] = info->name();
180  return 0;
181  }
182 
186  template <typename T>
187  static char addAction(const RegistryEntryData & base_info)
188  {
189  const auto info = std::make_shared<RegistryEntry<T>>(base_info);
190  getRegistry()._per_label_actions[info->_label].push_back(info);
191  getRegistry()._type_to_classname[typeid(T).name()] = info->_classname;
192  return 0;
193  }
194 
195  template <typename T>
196  static std::string getClassName()
197  {
198  return libmesh_map_find(getRegistry()._type_to_classname, typeid(T).name());
199  }
200 
203  static void registerObjectsTo(Factory & f, const std::set<std::string> & labels);
204 
207  static void registerActionsTo(ActionFactory & f, const std::set<std::string> & labels);
208 
210  static char addKnownLabel(const std::string & label);
211 
213  static void addDataFilePath(const std::string & name, const std::string & in_tree_path);
216  static void addAppDataFilePath(const std::string & app_name, const std::string & app_path);
218  static void addDeprecatedAppDataFilePath(const std::string & app_path);
219 
221  static void addRepository(const std::string & repo_name, const std::string & repo_url);
222 
224  static const std::map<std::string, std::vector<std::shared_ptr<RegistryEntryBase>>> & allObjects()
225  {
227  }
229  static const std::map<std::string, std::vector<std::shared_ptr<RegistryEntryBase>>> & allActions()
230  {
232  }
233 
234  static const RegistryEntryBase & objData(const std::string & name);
235 
239  static bool isRegisteredObj(const std::string & name)
240  {
241  return getRegistry()._name_to_entry.count(name);
242  }
243 
245  static const std::map<std::string, std::string> & getDataFilePaths()
246  {
247  return getRegistry()._data_file_paths;
248  }
254  static std::string getDataFilePath(const std::string & name);
255 
257  static const std::string & getRepositoryURL(const std::string & repo_name);
261  static const std::map<std::string, std::string> & getRepos() { return getRegistry()._repos; }
262 
264  template <typename T>
265  static std::string getRegisteredName();
266 
268  Registry(Registry const &) = delete;
269  Registry & operator=(Registry const &) = delete;
270 
271  Registry(Registry &&) = delete;
272  Registry & operator=(Registry &&) = delete;
274 
275 private:
278  friend class RegistryTest;
279  friend class DataFileUtilsTest;
280  FRIEND_TEST(RegistryTest, determineFilePath);
281  FRIEND_TEST(RegistryTest, determineFilePathFailed);
283  FRIEND_TEST(RegistryTest, appNameFromAppPathFailed);
285 
286  Registry() {};
287 
293  static void setDataFilePaths(const std::map<std::string, std::string> & data_file_paths)
294  {
295  getRegistry()._data_file_paths = data_file_paths;
296  }
302  static void setRepos(const std::map<std::string, std::string> & repos)
303  {
304  getRegistry()._repos = repos;
305  }
306 
308  static std::string determineDataFilePath(const std::string & name,
309  const std::string & in_tree_path);
310 
313  static std::string appNameFromAppPath(const std::string & app_path);
314 
315  std::map<std::string, std::shared_ptr<RegistryEntryBase>> _name_to_entry;
316  std::map<std::string, std::vector<std::shared_ptr<RegistryEntryBase>>> _per_label_objects;
317  std::map<std::string, std::vector<std::shared_ptr<RegistryEntryBase>>> _per_label_actions;
318  std::set<std::string> _known_labels;
320  std::map<std::string, std::string> _data_file_paths;
322  std::map<std::string, std::string> _repos;
323  std::map<std::string, std::string> _type_to_classname;
324 };
325 
326 template <typename T>
327 std::string
329 {
330  mooseDeprecated("Use Registry::getClassName() instead.");
331  return getClassName<T>();
332 }
333 
334 template <typename T>
336 {
337  static_assert(std::is_base_of_v<MooseObject, T> || std::is_base_of_v<Action, T>,
338  "Not derived from MooseObject or Action");
339 }
340 
341 template <typename T>
342 std::unique_ptr<MooseObject>
344 {
345  if constexpr (std::is_base_of_v<MooseObject, T>)
346  return std::make_unique<T>(parameters);
347  mooseError(MooseUtils::prettyCppType<T>(), " to be built is not a MooseObject.");
348 }
349 template <typename T>
350 std::shared_ptr<MooseObject>
352 {
353  if constexpr (std::is_base_of_v<MooseObject, T>)
354  return std::make_shared<T>(parameters);
355  mooseError(MooseUtils::prettyCppType<T>(), " to be built is not a MooseObject.");
356 }
357 
358 template <typename T>
359 std::shared_ptr<Action>
361 {
362  if constexpr (!std::is_base_of_v<Action, T>)
363  mooseError("The action to be built is not derived from Action.");
364  else
365  return std::make_shared<T>(parameters);
366 }
367 
368 template <typename T>
371 {
372  auto params = T::validParams();
373  return params;
374 }
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:209
virtual std::shared_ptr< MooseObject > buildShared(const InputParameters &parameters) override
Definition: Registry.h:351
virtual std::shared_ptr< Action > buildAction(const InputParameters &parameters) override
Definition: Registry.h:360
virtual std::unique_ptr< MooseObject > build(const InputParameters &parameters)=0
proxy functions
static const RegistryEntryBase & objData(const std::string &name)
Definition: Registry.C:57
std::map< std::string, std::vector< std::shared_ptr< RegistryEntryBase > > > _per_label_actions
Definition: Registry.h:317
static Registry & getRegistry()
Get the global Registry singleton.
Definition: Registry.C:23
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:150
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:323
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:128
Holds details and meta-data info for a particular MooseObject or Action for use in the use in the reg...
Definition: Registry.h:102
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:229
static std::string getRegisteredName()
returns the name() for a registered class
Definition: Registry.h:328
std::string _classname
name of the c++ class for the object.
Definition: Registry.h:107
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:182
friend class RegistryTest
Friends for unit testing.
Definition: Registry.h:278
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:34
virtual std::shared_ptr< Action > buildAction(const InputParameters &parameters)=0
RegistryEntryBase(const RegistryEntryData &data)
Definition: Registry.h:125
Registry()
Definition: Registry.h:286
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:224
std::string _file
file path for the c++ file the object or action was added to the registry in.
Definition: Registry.h:114
static char addAction(const RegistryEntryData &base_info)
Adds information on an Action object to the registry.
Definition: Registry.h:187
InputParameters validParams()
Base class for actions.
Definition: Action.h:34
virtual ~RegistryEntryBase()
Definition: Registry.h:126
friend class DataFileUtilsTest
Definition: Registry.h:279
static void setDataFilePaths(const std::map< std::string, std::string > &data_file_paths)
Manually set the data file paths.
Definition: Registry.h:293
std::string _name
name that the object will be registered to factories under. If unspecified, _alias is used...
Definition: Registry.h:112
virtual std::shared_ptr< MooseObject > buildShared(const InputParameters &parameters)=0
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:68
static char add(const RegistryEntryData &base_info)
Adds information on a MooseObject to the registry.
Definition: Registry.h:175
std::string name() const
resolve the name from _classname, _alias, and _name
Definition: Registry.h:133
std::string _alias
an alternate name to register the object to factories under.
Definition: Registry.h:110
std::string _replaced_by
class name for an object that replaces this object if deprecated, blank otherwise.
Definition: Registry.h:120
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:28
std::map< std::string, std::string > _data_file_paths
Data file registry; name -> in-tree path.
Definition: Registry.h:320
std::map< std::string, std::shared_ptr< RegistryEntryBase > > _name_to_entry
Definition: Registry.h:315
Specialized factory for generic Action System objects.
Definition: ActionFactory.h:48
static std::string getClassName()
Definition: Registry.h:196
static void addRepository(const std::string &repo_name, const std::string &repo_url)
register a repository
Definition: Registry.C:160
virtual std::unique_ptr< MooseObject > build(const InputParameters &parameters) override
proxy functions
Definition: Registry.h:343
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:374
int _line
line number in the c++ file the object or action was added to the registry on.
Definition: Registry.h:116
static void setRepos(const std::map< std::string, std::string > &repos)
Manually set the repos.
Definition: Registry.h:302
std::string _label
label (usually app name - e.g. "YourAnimalApp") that the object or action is associated with...
Definition: Registry.h:105
std::set< std::string > _known_labels
Definition: Registry.h:318
std::map< std::string, std::string > _type_to_classname
Definition: Registry.h:323
static bool isRegisteredObj(const std::string &name)
Definition: Registry.h:239
std::string _deprecated_time
time in "mm/dd/yyyy HH:MM" format that the object is/becomes deprecated, blank otherwise.
Definition: Registry.h:118
RegistryEntry(const RegistryEntryData &data)
Definition: Registry.h:335
std::map< std::string, std::string > _repos
Repository name -> repository URL; used for mooseDocumentedError.
Definition: Registry.h:322
virtual InputParameters buildParameters() override
Definition: Registry.h:370
static const std::string & getRepositoryURL(const std::string &repo_name)
Returns the repository URL associated with repo_name.
Definition: Registry.C:173
static const std::map< std::string, std::string > & getDataFilePaths()
Returns a map of all registered data file paths (name -> path)
Definition: Registry.h:245
Registry & operator=(Registry const &)=delete
static void addDeprecatedAppDataFilePath(const std::string &app_path)
deprecated method; use addAppDataFilePath instead
Definition: Registry.C:138
The registry is used as a global singleton to collect information on all available MooseObject and Ac...
Definition: Registry.h:163
static const std::map< std::string, std::string > & getRepos()
Returns a map of all registered repositories.
Definition: Registry.h:261
std::map< std::string, std::vector< std::shared_ptr< RegistryEntryBase > > > _per_label_objects
Definition: Registry.h:316
static char addKnownLabel(const std::string &label)
addKnownLabel whitelists a label as valid for purposes of the checkLabels function.
Definition: Registry.C:84
static void addDataFilePath(const std::string &name, const std::string &in_tree_path)
register general search paths (folder name must be data)
Definition: Registry.C:91