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::map< std::string, 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 40 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 63 of file ParameterRegistry.h.

Referenced by get().

63 {};

Member Function Documentation

◆ add()

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

Add a parameter.

Definition at line 80 of file ParameterRegistry.h.

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

◆ 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 auto key = value.type();
29  const auto it = _registry.find(key);
30  if (it == _registry.end())
31  mooseError("ParameterRegistry::set(): Parameter type '", key, "' is not registered");
32 
33  // Catch all mooseErrors so that they can be accumulated during
34  // parsing and building instead of ending the run
35  Moose::ScopedThrowOnError scoped_throw_on_error;
36 
37  it->second(value, field);
38 }
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)
std::map< std::string, std::function< void(libMesh::Parameters::Value &value, const hit::Field &)> > _registry
Registration map of type -> function to fill each type.
Scoped helper for setting Moose::_throw_on_error during this scope.
Definition: Moose.h:294

Member Data Documentation

◆ _registry

std::map<std::string, 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 75 of file ParameterRegistry.h.

Referenced by add(), and set().


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