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 #include "MooseUtils.h"
15 
16 #include "libmesh/libmesh_common.h"
17 
18 #include <memory>
19 
20 Registry &
22 {
23  // We need a naked new here (_not_ a smart pointer or object instance) due to what seems like a
24  // bug in clang's static object destruction when using dynamic library loading.
25  static Registry * registry_singleton = nullptr;
26  if (!registry_singleton)
27  registry_singleton = new Registry();
28  return *registry_singleton;
29 }
30 
31 void
32 Registry::registerObjectsTo(Factory & f, const std::set<std::string> & labels)
33 {
34  auto & r = getRegistry();
35 
36  for (const auto & label : labels)
37  {
38  r._known_labels.insert(label);
39  if (r._per_label_objects.count(label) == 0)
40  continue;
41 
42  for (const auto & obj : r._per_label_objects[label])
43  {
44  const auto name = obj->name();
45  r._name_to_entry[name] = obj;
46 
47  f.reg(obj);
48  if (!obj->_alias.empty())
49  f.associateNameToClass(name, obj->_classname);
50  }
51  }
52 }
53 
54 const RegistryEntryBase &
55 Registry::objData(const std::string & name)
56 {
57  auto & r = getRegistry();
58 
59  if (const auto it = r._name_to_entry.find(name); it != r._name_to_entry.end())
60  return *it->second;
61  else
62  mooseError("Object ", name, " is not registered yet");
63 }
64 
65 void
66 Registry::registerActionsTo(ActionFactory & f, const std::set<std::string> & labels)
67 {
68  auto & r = getRegistry();
69 
70  for (const auto & label : labels)
71  {
72  r._known_labels.insert(label);
73  if (r._per_label_actions.count(label) == 0)
74  continue;
75 
76  for (const auto & obj : r._per_label_actions[label])
77  f.reg(obj);
78  }
79 }
80 
81 char
82 Registry::addKnownLabel(const std::string & label)
83 {
84  getRegistry()._known_labels.insert(label);
85  return 0;
86 }
87 
88 void
89 Registry::addDataFilePath(const std::string & fullpath)
90 {
91  auto & dfp = getRegistry()._data_file_paths;
92 
93  // split the *App.C filename from its containing directory
94  const auto path = MooseUtils::splitFileName(fullpath).first;
95 
96  // This works for both build/unity_src/ and src/base/ as the *App.C file location,
97  // in case __FILE__ doesn't get overriden in unity build
98  const auto data_dir = MooseUtils::pathjoin(path, "../../data");
99 
100  // if the data directory exists and hasn't been added before, add it
101  if (MooseUtils::pathIsDirectory(data_dir) &&
102  std::find(dfp.begin(), dfp.end(), data_dir) == dfp.end())
103  dfp.push_back(data_dir);
104 }
static const RegistryEntryBase & objData(const std::string &name)
Definition: Registry.C:55
static Registry & getRegistry()
Get the global Registry singleton.
Definition: Registry.C:21
Generic factory class for build all sorts of objects.
Definition: Factory.h:28
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:284
bool pathIsDirectory(const std::string &path)
Definition: MooseUtils.C:247
void reg(std::shared_ptr< RegistryEntryBase > obj)
Definition: ActionFactory.C:21
std::pair< std::filesystem::path, std::filesystem::path > splitFileName(const T &full_file)
Function for splitting path and filename.
Definition: MooseUtils.h:231
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:32
Registry()
Definition: Registry.h:242
virtual const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:56
static void addDataFilePath(const std::string &path)
register search paths for built-in data files
Definition: Registry.C:89
std::vector< std::string > _data_file_paths
Definition: Registry.h:248
void reg(std::shared_ptr< RegistryEntryBase > obj)
Definition: Factory.C:22
void associateNameToClass(const std::string &name, const std::string &class_name)
Associates an object name with a class name.
Definition: Factory.C:297
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:66
Specialized factory for generic Action System objects.
Definition: ActionFactory.h:50
std::set< std::string > _known_labels
Definition: Registry.h:247
std::filesystem::path pathjoin(const std::filesystem::path &p)
Definition: MooseUtils.C:58
The registry is used as a global singleton to collect information on all available MooseObject and Ac...
Definition: Registry.h:150
static char addKnownLabel(const std::string &label)
addKnownLabel whitelists a label as valid for purposes of the checkLabels function.
Definition: Registry.C:82