www.mooseframework.org
Registry.C
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 #include "InputParameters.h"
11 #include "Registry.h"
12 #include "Factory.h"
13 #include "ActionFactory.h"
14 
15 #include "libmesh/libmesh_common.h"
16 
17 static Registry &
19 {
20  static std::unique_ptr<Registry> _singleton;
21  if (!_singleton)
22  _singleton = libmesh_make_unique<Registry>();
23  return *_singleton;
24 }
25 
26 const std::map<std::string, std::vector<RegistryEntry>> &
28 {
30 }
31 const std::map<std::string, std::vector<RegistryEntry>> &
33 {
35 }
36 
37 void
39 {
40  auto & r = getRegistry();
41  r._per_label_objects[info._label].push_back(info);
42 }
43 
44 void
46 {
47  auto & r = getRegistry();
48  r._per_label_actions[info._label].push_back(info);
49 }
50 
51 void
52 Registry::registerObjectsTo(Factory & f, const std::set<std::string> & labels)
53 {
54  auto & r = getRegistry();
55  for (const auto & label : labels)
56  {
57  r._known_labels.insert(label);
58  if (r._per_label_objects.count(label) == 0)
59  continue;
60 
61  for (const auto & obj : r._per_label_objects[label])
62  {
63  std::string name = obj._name;
64  if (name.empty())
65  name = obj._alias;
66  if (name.empty())
67  name = obj._classname;
68 
69  r._name_to_entry[name] = obj;
70 
71  f.reg(obj._label,
72  name,
73  obj._build_ptr,
74  obj._params_ptr,
75  obj._deprecated_time,
76  obj._replaced_by,
77  obj._file,
78  obj._line);
79 
80  if (!obj._alias.empty())
81  f.associateNameToClass(name, obj._classname);
82  }
83  }
84 }
85 
87 Registry::objData(const std::string & name)
88 {
89  auto & r = getRegistry();
90 
91  auto it = r._name_to_entry.find(name);
92 
93  if (it != r._name_to_entry.end())
94  return it->second;
95  else
96  mooseError("Object ", name, " is not registered yet");
97 }
98 
99 bool
100 Registry::isRegisteredObj(const std::string & name)
101 {
102  auto & r = getRegistry();
103 
104  auto it = r._name_to_entry.find(name);
105 
106  return it != r._name_to_entry.end();
107 }
108 
109 bool
110 Registry::isADObj(const std::string & name)
111 {
112  auto & r = getRegistry();
113 
114  auto it = r._name_to_entry.find(name);
115 
116  return (it != r._name_to_entry.end()) && it->second._is_ad;
117 }
118 
119 void
120 Registry::registerActionsTo(ActionFactory & f, const std::set<std::string> & labels)
121 {
122  auto & r = getRegistry();
123  for (const auto & label : labels)
124  {
125  r._known_labels.insert(label);
126  if (r._per_label_actions.count(label) == 0)
127  continue;
128 
129  for (const auto & obj : r._per_label_actions[label])
130  f.reg(
131  obj._classname, obj._name, obj._build_action_ptr, obj._params_ptr, obj._file, obj._line);
132  }
133 }
134 
135 void
136 Registry::checkLabels(const std::set<std::string> & known_labels)
137 {
138  auto & r = getRegistry();
139  std::vector<RegistryEntry> orphs;
140 
141  for (auto & entry : r._per_label_objects)
142  if (known_labels.count(entry.first) == 0 && r._known_labels.count(entry.first) == 0)
143  orphs.insert(orphs.end(), entry.second.begin(), entry.second.end());
144  for (auto & entry : r._per_label_actions)
145  if (known_labels.count(entry.first) == 0 && r._known_labels.count(entry.first) == 0)
146  orphs.insert(orphs.end(), entry.second.begin(), entry.second.end());
147 
148  if (orphs.size() > 0)
149  {
150  std::stringstream lst;
151  for (auto & orph : orphs)
152  lst << "\n\t" << orph._classname << " (app='" << orph._label << "')";
153  mooseError("The following objects/actions have been registered to unknown applications/labels:",
154  lst.str());
155  }
156 }
157 
158 char
159 Registry::addKnownLabel(const std::string & label)
160 {
161  auto & r = getRegistry();
162  r._known_labels.insert(label);
163  return 0;
164 }
void reg(const std::string &name, const std::string &task, const std::string &file="", int line=-1)
Definition: ActionFactory.h:74
static const std::map< std::string, std::vector< RegistryEntry > > & allActions()
Returns a per-label keyed map of all Actions in the registry.
Definition: Registry.C:32
std::map< std::string, std::vector< RegistryEntry > > _per_label_actions
Definition: Registry.h:288
static void checkLabels(const std::set< std::string > &known_labels={})
This runs error checking to make sure that all objects and actions in the registry have valid labels...
Definition: Registry.C:136
Generic factory class for build all sorts of objects.
Definition: Factory.h:153
if(nl->nonlinearSolver() ->matvec &&nl->nonlinearSolver() ->residual_and_jacobian_object)
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
std::map< std::string, std::vector< RegistryEntry > > _per_label_objects
Definition: Registry.h:287
static bool isADObj(const std::string &name)
Definition: Registry.C:110
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:52
void associateNameToClass(const std::string &name, const std::string &class_name)
Associates an object name with a class name.
Definition: Factory.C:292
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:120
Specialized factory for generic Action System objects.
Definition: ActionFactory.h:64
void reg(const std::string &obj_name, const std::string &file="", int line=-1)
Register a new object.
Definition: Factory.h:164
static void addActionInner(const RegistryEntry &info)
Definition: Registry.C:45
static Registry & getRegistry()
Definition: Registry.C:18
static RegistryEntry & objData(const std::string &name)
Definition: Registry.C:87
std::string _label
label (usually app name - e.g. "YourAnimalApp") that the object or action is associated with...
Definition: Registry.h:178
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:59
static void addInner(const RegistryEntry &info)
Definition: Registry.C:38
Holds details and meta-data info for a particular MooseObject or Action for use in the registry...
Definition: Registry.h:175
static const std::map< std::string, std::vector< RegistryEntry > > & allObjects()
Returns a per-label keyed map of all MooseObjects in the registry.
Definition: Registry.C:27
The registry is used as a global singleton to collect information on all available MooseObject and Ac...
Definition: Registry.h:227
static bool isRegisteredObj(const std::string &name)
Definition: Registry.C:100
static char addKnownLabel(const std::string &label)
addKnownLabel whitelists a label as valid for purposes of the checkLabels function.
Definition: Registry.C:159