https://mooseframework.inl.gov
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
Moose::ParameterRegistry Class Reference

Registry that allows for the typeless setting of a parameter value from a hit field. More...

#include <ParameterRegistry.h>

Public Member Functions

template<class T , class F >
char add (F &&f)
 Add a parameter. More...
 
void set (libMesh::Parameters::Value &value, const hit::Field &field) const
 Sets a parameter value given a hit field. More...
 

Static Public Member Functions

static ParameterRegistryget ()
 Get the singleton registry. More...
 

Private Member Functions

 ParameterRegistry ()
 Constructor; private so that it can only be created with the singleton. More...
 
 FRIEND_TEST (::ParameterRegistryTest, add)
 
 FRIEND_TEST (::ParameterRegistryTest, addExists)
 
 FRIEND_TEST (::ParameterRegistryTest, set)
 
 FRIEND_TEST (::ParameterRegistryTest, setNotRegistered)
 
 FRIEND_TEST (::ParameterRegistryTest, setCatchMooseError)
 

Private Attributes

std::unordered_map< std::type_index, std::function< void(libMesh::Parameters::Value &value, const hit::Field &)> > _registry
 Registration map of type -> function to fill each type. More...
 

Detailed Description

Registry that allows for the typeless setting of a parameter value from a hit field.

Definition at line 43 of file ParameterRegistry.h.

Constructor & Destructor Documentation

◆ ParameterRegistry()

Moose::ParameterRegistry::ParameterRegistry ( )
inlineprivate

Constructor; private so that it can only be created with the singleton.

Definition at line 66 of file ParameterRegistry.h.

Referenced by get().

66 {};

Member Function Documentation

◆ add()

template<class T , class F >
char Moose::ParameterRegistry::add ( F &&  f)

Add a parameter.

Definition at line 84 of file ParameterRegistry.h.

85 {
86  static_assert(std::is_invocable_r_v<void, F &, T &, const hit::Field &>,
87  "Setter function must be callable with (T &, const hit::Field &) and return void");
88 
89  // We register a function that stores T &, but the registry will only ever be
90  // called from a bare libMesh::Parameters::Value. Thus, we need to cast the
91  // bare Value to the derived value and then call the setter.
92  auto setter = [f = std::forward<F>(f)](libMesh::Parameters::Value & param_value,
93  const hit::Field & field) -> void
94  {
95  auto cast_param_value = dynamic_cast<libMesh::Parameters::Parameter<T> *>(&param_value);
96  mooseAssert(cast_param_value, "Cast failed");
97  f(cast_param_value->set(), field);
98  };
99 
100  const std::type_index key(typeid(T));
101  const auto it_inserted_pair = _registry.emplace(key, std::move(setter));
102 
103  if (!it_inserted_pair.second)
104  mooseError("ParameterRegistry: Parameter with type '",
105  MooseUtils::prettyCppType<T>(),
106  "' is already registered");
107 
108  return 0;
109 }
std::unordered_map< std::type_index, std::function< void(libMesh::Parameters::Value &value, const hit::Field &)> > _registry
Registration map of type -> function to fill each type.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:323

◆ FRIEND_TEST() [1/5]

Moose::ParameterRegistry::FRIEND_TEST ( ::ParameterRegistryTest  ,
add   
)
private

◆ FRIEND_TEST() [2/5]

Moose::ParameterRegistry::FRIEND_TEST ( ::ParameterRegistryTest  ,
addExists   
)
private

◆ FRIEND_TEST() [3/5]

Moose::ParameterRegistry::FRIEND_TEST ( ::ParameterRegistryTest  ,
set   
)
private

◆ FRIEND_TEST() [4/5]

Moose::ParameterRegistry::FRIEND_TEST ( ::ParameterRegistryTest  ,
setNotRegistered   
)
private

◆ FRIEND_TEST() [5/5]

Moose::ParameterRegistry::FRIEND_TEST ( ::ParameterRegistryTest  ,
setCatchMooseError   
)
private

◆ get()

ParameterRegistry & Moose::ParameterRegistry::get ( )
static

Get the singleton registry.

Definition at line 17 of file ParameterRegistry.C.

Referenced by Moose::Builder::extractParams(), and MooseApp::registerInterfaceObject().

18 {
19  static ParameterRegistry * registry = nullptr;
20  if (!registry)
22  return *registry;
23 }
static Moose::ParameterRegistry & registry
ParameterRegistry()
Constructor; private so that it can only be created with the singleton.

◆ set()

void Moose::ParameterRegistry::set ( libMesh::Parameters::Value value,
const hit::Field &  field 
) const

Sets a parameter value given a hit field.

Definition at line 26 of file ParameterRegistry.C.

Referenced by Moose::Builder::extractParams().

27 {
28  const std::type_index key(value.type_info());
29  const auto it = _registry.find(key);
30  if (it == _registry.end())
31  mooseError("ParameterRegistry::set(): Parameter type '",
33  "' is not registered");
34 
35  // Catch all mooseErrors so that they can be accumulated during
36  // parsing and building instead of ending the run
37  Moose::ScopedThrowOnError scoped_throw_on_error;
38 
39  it->second(value, field);
40 }
std::unordered_map< std::type_index, std::function< void(libMesh::Parameters::Value &value, const hit::Field &)> > _registry
Registration map of type -> function to fill each type.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:323
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
Scoped helper for setting Moose::_throw_on_error during this scope.
Definition: Moose.h:294
std::string prettyCppType(const std::string &cpp_type)
Definition: MooseUtils.C:1147

Member Data Documentation

◆ _registry

std::unordered_map<std::type_index, std::function<void(libMesh::Parameters::Value & value, const hit::Field &)> > Moose::ParameterRegistry::_registry
private

Registration map of type -> function to fill each type.

Definition at line 79 of file ParameterRegistry.h.

Referenced by add(), and set().


The documentation for this class was generated from the following files: