https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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)
94#define registerNonAppDataFilePathWithInfo(name, path, info) \
95 Registry::addDataFilePath(name, path, false, info)
99#define registerAppDataFilePath(app) Registry::addAppDataFilePath(app, __FILE__)
101#define registerMissingDataFilePath(name, info) Registry::addMissingDataFilePath(name, info)
103#define registerDataFilePath() Registry::addDeprecatedAppDataFilePath(__FILE__)
104
105#define registerRepository(repo_name, repo_url) Registry::addRepository(repo_name, repo_url);
106
107class Factory;
108class ActionFactory;
109class MooseObject;
110class Action;
111struct 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{
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;
148 std::string name() const
149 {
150 std::string name = _name;
151 if (name.empty())
152 name = _alias;
153 if (name.empty())
155 return name;
156 }
157};
158
159template <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
179{
180public:
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 {
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 {
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;
312
313private:
314#ifdef MOOSE_UNIT_TEST
317 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
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
377template <typename T>
378std::string
380{
381 mooseDeprecated("Use Registry::getClassName() instead.");
382 return getClassName<T>();
383}
384
385template <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
392template <typename T>
393std::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}
400template <typename T>
401std::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
409template <typename T>
410std::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
419template <typename T>
422{
423 auto params = T::validParams();
424 return params;
425}
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition MooseError.h:363
Specialized factory for generic Action System objects.
Base class for actions.
Definition Action.h:38
Generic factory class for build all sorts of objects.
Definition Factory.h:29
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
Every object that can be built by the factory should be derived from this class.
Definition MooseObject.h:31
The registry is used as a global singleton to collect information on all available MooseObject and Ac...
Definition Registry.h:179
Registry(Registry const &)=delete
Don't allow creation through copy/move construction or assignment.
static std::string getClassName()
Definition Registry.h:211
std::map< std::string, std::string > _repos
Repository name -> repository URL; used for mooseDocumentedError.
Definition Registry.h:371
static const RegistryEntryBase & objData(const std::string &name)
Definition Registry.C:58
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
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 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
static std::string appNameFromAppPath(const std::string &app_path)
Internal helper for getting an application name from its path, for example: /path/to/FooBarBazApp....
Definition Registry.C:246
friend class DataFileUtilsTest
Definition Registry.h:318
FRIEND_TEST(RegistryTest, addDataFilePathCapability)
static const std::map< std::string, std::string > & getRepos()
Returns a map of all registered repositories.
Definition Registry.h:287
static void checkDataFilePathName(const std::string &name)
Check a data file path for valid characters.
Definition Registry.C:268
FRIEND_TEST(RegistryTest, determineFilePath)
static const std::map< std::string, std::string > & getDataFilePaths()
Returns a map of all registered data file paths (name -> path)
Definition Registry.h:271
static void setDataFilePaths(const std::map< std::string, std::string > &data_file_paths)
Manually set the data file paths.
Definition Registry.h:334
std::map< std::string, std::shared_ptr< RegistryEntryBase > > _name_to_entry
Definition Registry.h:364
static void addDeprecatedAppDataFilePath(const std::string &app_path)
deprecated method; use addAppDataFilePath instead
Definition Registry.C:154
Registry & operator=(Registry const &)=delete
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
static char addAction(const RegistryEntryData &base_info)
Adds information on an Action object to the registry.
Definition Registry.h:202
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 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
Registry & operator=(Registry &&)=delete
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
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 Registry & getRegistry()
Get the global Registry singleton.
Definition Registry.C:24
Registry(Registry &&)=delete
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 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
static std::string getRegisteredName()
returns the name() for a registered class
Definition Registry.h:379
static bool isRegisteredObj(const std::string &name)
Definition Registry.h:265
static std::string getDataFilePath(const std::string &name)
Gets a data path for the registered name.
Definition Registry.C:166
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, appNameFromAppPathFailed)
FRIEND_TEST(RegistryTest, appNameFromAppPath)
std::set< std::string > _known_labels
Definition Registry.h:367
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::vector< std::shared_ptr< RegistryEntryBase > > > _per_label_actions
Definition Registry.h:366
static const std::string & getRepositoryURL(const std::string &repo_name)
Returns the repository URL associated with repo_name.
Definition Registry.C:189
FRIEND_TEST(RegistryTest, determineFilePathFailed)
std::map< std::string, std::vector< std::shared_ptr< RegistryEntryBase > > > _per_label_objects
Definition Registry.h:365
friend class RegistryTest
Friends for unit testing.
Definition Registry.h:317
std::map< std::string, std::string > _type_to_classname
Definition Registry.h:372
static void addRepository(const std::string &repo_name, const std::string &repo_url)
register a repository
Definition Registry.C:176
static char add(const RegistryEntryData &base_info)
Adds information on a MooseObject to the registry.
Definition Registry.h:190
static void setRepos(const std::map< std::string, std::string > &repos)
Manually set the repos.
Definition Registry.h:343
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
static char addKnownLabel(const std::string &label)
addKnownLabel whitelists a label as valid for purposes of the checkLabels function.
Definition Registry.C:85
virtual std::shared_ptr< MooseObject > buildShared(const InputParameters &parameters)=0
RegistryEntryBase(const RegistryEntryData &data)
Definition Registry.h:140
virtual InputParameters buildParameters()=0
virtual std::shared_ptr< Action > buildAction(const InputParameters &parameters)=0
std::string name() const
resolve the name from _classname, _alias, and _name
Definition Registry.h:148
virtual std::unique_ptr< MooseObject > build(const InputParameters &parameters)=0
proxy functions
virtual ~RegistryEntryBase()
Definition Registry.h:141
Holds details and meta-data info for a particular MooseObject or Action for use in the use in the reg...
Definition Registry.h:118
std::string _label
label (usually app name - e.g. "YourAnimalApp") that the object or action is associated with.
Definition Registry.h:120
std::string _classname
name of the c++ class for the object.
Definition Registry.h:122
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
std::string _file
file path for the c++ file the object or action was added to the registry in.
Definition Registry.h:129
std::string _deprecated_time
time in "mm/dd/yyyy HH:MM" format that the object is/becomes deprecated, blank otherwise.
Definition Registry.h:133
std::string _name
name that the object will be registered to factories under. If unspecified, _alias is used.
Definition Registry.h:127
int _line
line number in the c++ file the object or action was added to the registry on.
Definition Registry.h:131
virtual std::shared_ptr< MooseObject > buildShared(const InputParameters &parameters) override
Definition Registry.h:402
virtual std::unique_ptr< MooseObject > build(const InputParameters &parameters) override
proxy functions
Definition Registry.h:394
RegistryEntry(const RegistryEntryData &data)
Definition Registry.h:386
virtual std::shared_ptr< Action > buildAction(const InputParameters &parameters) override
Definition Registry.h:411
virtual InputParameters buildParameters() override
Definition Registry.h:421