https://mooseframework.inl.gov
Public Types | Public Member Functions | Protected Member Functions | Private Attributes | List of all members
Moose::MFEM::NamedFieldsMap< T > Class Template Reference

Lightweight adaptor over an std::map from strings to pointer to T. More...

#include <MFEMContainers.h>

Public Types

using MapType = std::map< std::string, std::shared_ptr< T > >
 
using const_iterator = typename MapType::const_iterator
 

Public Member Functions

 NamedFieldsMap ()=default
 Default initializer. More...
 
 ~NamedFieldsMap ()
 Destructor. More...
 
template<class FieldType , class... FieldArgs>
void Register (const std::string &field_name, FieldArgs &&... args)
 Construct new field with name field_name and register. More...
 
void Register (const std::string &field_name, std::shared_ptr< T > field)
 Register association between field and field_name. More...
 
void Deregister (const std::string &field_name)
 Unregister association between a field and the field_name. More...
 
bool Has (const std::string &field_name) const
 Predicate to check if a field is registered with name field_name. More...
 
std::shared_ptr< T > GetShared (const std::string &field_name) const
 Returns a shared pointer to the field. This is guaranteed to return a non-null shared pointer. More...
 
T & GetRef (const std::string &field_name) const
 Returns a reference to a field. More...
 
T * Get (const std::string &field_name) const
 Returns a non-owning pointer to the field. This is guaranteed to return a non-null pointer. More...
 
template<typename TDerived >
TDerived * Get (const std::string &field_name) const
 Returns a non-owning pointer to the field where TDerived is a derived class of class T. More...
 
std::vector< T * > Get (const std::vector< std::string > &keys) const
 Returns a vector containing all values for supplied keys. More...
 
const_iterator begin () const
 Returns a begin const iterator to the registered fields. More...
 
const_iterator end () const
 Returns an end const iterator to the registered fields. More...
 
int size ()
 Returns the number of elements in the map. More...
 

Protected Member Functions

const_iterator FindField (const std::string &field_name) const
 Returns a const iterator to the field. More...
 
void CheckFieldIsRegistrable ([[maybe_unused]] const std::string &field_name, [[maybe_unused]] T *field) const
 Check that the field pointer is valid and the field has not already been registered. More...
 
void CheckFieldIsRegistered (const std::string &field_name) const
 Check that a field exists in the map. More...
 
std::shared_ptr< T > EnsureFieldPointerIsNonNull (const_iterator &iterator) const
 Ensure that a returned shared pointer is valid. More...
 
template<typename TDerived >
TDerived * EnsurePointerCastIsNonNull (T *ptr) const
 Ensure that a dynamic cast is successful. More...
 
void DeregisterAll ()
 Clear all associations between names and fields. More...
 

Private Attributes

MapType _field_map {}
 

Detailed Description

template<typename T>
class Moose::MFEM::NamedFieldsMap< T >

Lightweight adaptor over an std::map from strings to pointer to T.

Definition at line 24 of file MFEMContainers.h.

Member Typedef Documentation

◆ const_iterator

template<typename T>
using Moose::MFEM::NamedFieldsMap< T >::const_iterator = typename MapType::const_iterator

Definition at line 28 of file MFEMContainers.h.

◆ MapType

template<typename T>
using Moose::MFEM::NamedFieldsMap< T >::MapType = std::map<std::string, std::shared_ptr<T> >

Definition at line 27 of file MFEMContainers.h.

Constructor & Destructor Documentation

◆ NamedFieldsMap()

template<typename T>
Moose::MFEM::NamedFieldsMap< T >::NamedFieldsMap ( )
default

Default initializer.

◆ ~NamedFieldsMap()

template<typename T>
Moose::MFEM::NamedFieldsMap< T >::~NamedFieldsMap ( )
inline

Destructor.

Definition at line 34 of file MFEMContainers.h.

34 { DeregisterAll(); }
void DeregisterAll()
Clear all associations between names and fields.

Member Function Documentation

◆ begin()

template<typename T>
const_iterator Moose::MFEM::NamedFieldsMap< T >::begin ( ) const
inline

Returns a begin const iterator to the registered fields.

Definition at line 109 of file MFEMContainers.h.

109 { return _field_map.begin(); }

◆ CheckFieldIsRegistered()

template<typename T>
void Moose::MFEM::NamedFieldsMap< T >::CheckFieldIsRegistered ( const std::string &  field_name) const
inlineprotected

Check that a field exists in the map.

Definition at line 135 of file MFEMContainers.h.

Referenced by Moose::MFEM::NamedFieldsMap< mfem::ParNonlinearForm >::GetShared().

136  {
137  if (!Has(field_name))
138  mooseError("The field '" + field_name + "' has not been registered.");
139  }
bool Has(const std::string &field_name) const
Predicate to check if a field is registered with name field_name.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311

◆ CheckFieldIsRegistrable()

template<typename T>
void Moose::MFEM::NamedFieldsMap< T >::CheckFieldIsRegistrable ( [[maybe_unused] ] const std::string &  field_name,
[[maybe_unused] ] T *  field 
) const
inlineprotected

Check that the field pointer is valid and the field has not already been registered.

Definition at line 126 of file MFEMContainers.h.

Referenced by Moose::MFEM::NamedFieldsMap< mfem::ParNonlinearForm >::Register().

128  {
129  mooseAssert(field, "Cannot register NULL field with name '" + field_name + "'.");
130  mooseAssert(!Has(field_name) || Get(field_name) != field,
131  "The field '" + field_name + "' is already registered.");
132  }
bool Has(const std::string &field_name) const
Predicate to check if a field is registered with name field_name.
T * Get(const std::string &field_name) const
Returns a non-owning pointer to the field. This is guaranteed to return a non-null pointer...

◆ Deregister()

template<typename T>
void Moose::MFEM::NamedFieldsMap< T >::Deregister ( const std::string &  field_name)
inline

Unregister association between a field and the field_name.

Definition at line 54 of file MFEMContainers.h.

Referenced by Moose::MFEM::NamedFieldsMap< mfem::ParNonlinearForm >::Register().

54 { _field_map.erase(field_name); }

◆ DeregisterAll()

template<typename T>
void Moose::MFEM::NamedFieldsMap< T >::DeregisterAll ( )
inlineprotected

Clear all associations between names and fields.

Definition at line 159 of file MFEMContainers.h.

Referenced by Moose::MFEM::NamedFieldsMap< mfem::ParNonlinearForm >::~NamedFieldsMap().

159 { _field_map.clear(); }

◆ end()

template<typename T>
const_iterator Moose::MFEM::NamedFieldsMap< T >::end ( ) const
inline

Returns an end const iterator to the registered fields.

Definition at line 113 of file MFEMContainers.h.

Referenced by Moose::MFEM::NamedFieldsMap< mfem::ParNonlinearForm >::Has().

113 { return _field_map.end(); }

◆ EnsureFieldPointerIsNonNull()

template<typename T>
std::shared_ptr<T> Moose::MFEM::NamedFieldsMap< T >::EnsureFieldPointerIsNonNull ( const_iterator iterator) const
inlineprotected

Ensure that a returned shared pointer is valid.

Definition at line 142 of file MFEMContainers.h.

Referenced by Moose::MFEM::NamedFieldsMap< mfem::ParNonlinearForm >::GetShared().

143  {
144  auto owned_ptr = iterator->second;
145  mooseAssert(owned_ptr, "The field '" + iterator->first + "' is NULL.");
146  return owned_ptr;
147  }

◆ EnsurePointerCastIsNonNull()

template<typename T>
template<typename TDerived >
TDerived* Moose::MFEM::NamedFieldsMap< T >::EnsurePointerCastIsNonNull ( T *  ptr) const
inlineprotected

Ensure that a dynamic cast is successful.

Definition at line 151 of file MFEMContainers.h.

152  {
153  auto derived_ptr = dynamic_cast<TDerived *>(ptr);
154  mooseAssert(derived_ptr, "The dynamic cast performed on the field pointer failed.");
155  return derived_ptr;
156  }

◆ FindField()

template<typename T>
const_iterator Moose::MFEM::NamedFieldsMap< T >::FindField ( const std::string &  field_name) const
inlineprotected

Returns a const iterator to the field.

Definition at line 120 of file MFEMContainers.h.

Referenced by Moose::MFEM::NamedFieldsMap< mfem::ParNonlinearForm >::GetShared(), and Moose::MFEM::NamedFieldsMap< mfem::ParNonlinearForm >::Has().

121  {
122  return _field_map.find(field_name);
123  }

◆ Get() [1/3]

template<typename T>
T* Moose::MFEM::NamedFieldsMap< T >::Get ( const std::string &  field_name) const
inline

Returns a non-owning pointer to the field. This is guaranteed to return a non-null pointer.

Definition at line 79 of file MFEMContainers.h.

Referenced by Moose::MFEM::TimeDependentEquationSystem::AddKernel(), Moose::MFEM::NamedFieldsMap< mfem::ParNonlinearForm >::CheckFieldIsRegistrable(), Moose::MFEM::TimeDependentEquationSystem::EliminateCoupledVariables(), Moose::MFEM::EquationSystem::EliminateCoupledVariables(), Moose::MFEM::EigenproblemEquationSystem::FormEigenproblemMatrix(), Moose::MFEM::EquationSystem::FormJacobianMatrix(), Moose::MFEM::ComplexEquationSystem::FormSystemMatrix(), Moose::MFEM::EquationSystem::FormSystemMatrix(), Moose::MFEM::ComplexEquationSystem::FormSystemOperator(), Moose::MFEM::EquationSystem::FormSystemOperator(), Moose::MFEM::NamedFieldsMap< mfem::ParNonlinearForm >::Get(), MFEMProblem::getMeshDisplacementGridFunction(), MFEMProblem::getMFEMVariableMesh(), Moose::MFEM::ComplexEquationSystem::Init(), Moose::MFEM::EquationSystem::Init(), Moose::MFEM::EquationSystem::PrepareLinearSolver(), Moose::MFEM::EigenproblemESProblemOperator::RecoverEigenproblemSolution(), Moose::MFEM::ProblemOperatorBase::SetGridFunctions(), Moose::MFEM::ComplexEquationSystemProblemOperator::SetGridFunctions(), Moose::MFEM::EquationSystem::SetTrialVariablesFromTrueVectors(), and Moose::MFEM::ComplexEquationSystem::SetTrialVariablesFromTrueVectors().

80  {
81  return GetShared(field_name).get();
82  }
std::shared_ptr< T > GetShared(const std::string &field_name) const
Returns a shared pointer to the field. This is guaranteed to return a non-null shared pointer...

◆ Get() [2/3]

template<typename T>
template<typename TDerived >
TDerived* Moose::MFEM::NamedFieldsMap< T >::Get ( const std::string &  field_name) const
inline

Returns a non-owning pointer to the field where TDerived is a derived class of class T.

Definition at line 86 of file MFEMContainers.h.

87  {
88  auto ptr = Get(field_name);
89 
90  return EnsurePointerCastIsNonNull<TDerived>(ptr);
91  }
T * Get(const std::string &field_name) const
Returns a non-owning pointer to the field. This is guaranteed to return a non-null pointer...

◆ Get() [3/3]

template<typename T>
std::vector<T *> Moose::MFEM::NamedFieldsMap< T >::Get ( const std::vector< std::string > &  keys) const
inline

Returns a vector containing all values for supplied keys.

Definition at line 94 of file MFEMContainers.h.

95  {
96  std::vector<T *> values;
97 
98  for (const auto & key : keys)
99  {
100  values.push_back(Get(key));
101  }
102 
103  values.shrink_to_fit();
104  return values;
105  }
T * Get(const std::string &field_name) const
Returns a non-owning pointer to the field. This is guaranteed to return a non-null pointer...

◆ GetRef()

template<typename T>
T& Moose::MFEM::NamedFieldsMap< T >::GetRef ( const std::string &  field_name) const
inline

◆ GetShared()

template<typename T>
std::shared_ptr<T> Moose::MFEM::NamedFieldsMap< T >::GetShared ( const std::string &  field_name) const
inline

Returns a shared pointer to the field. This is guaranteed to return a non-null shared pointer.

Definition at line 63 of file MFEMContainers.h.

Referenced by Moose::MFEM::TimeDependentEquationSystem::BuildBilinearForms(), Moose::MFEM::ComplexEquationSystem::BuildBilinearForms(), Moose::MFEM::EquationSystem::BuildBilinearForms(), Moose::MFEM::ComplexEquationSystem::BuildLinearForms(), Moose::MFEM::EquationSystem::BuildLinearForms(), Moose::MFEM::TimeDependentEquationSystem::BuildNonlinearForms(), Moose::MFEM::EquationSystem::BuildNonlinearForms(), Moose::MFEM::EquationSystem::ComputeNonlinearResidual(), Moose::MFEM::NamedFieldsMap< mfem::ParNonlinearForm >::Get(), MFEMProblem::getComplexGridFunction(), MFEMProblem::getGridFunction(), Moose::MFEM::NamedFieldsMap< mfem::ParNonlinearForm >::GetRef(), Moose::MFEM::ComplexEquationSystem::Init(), and Moose::MFEM::EquationSystem::Init().

64  {
65  CheckFieldIsRegistered(field_name);
66 
67  auto it = FindField(field_name);
68 
69  return EnsureFieldPointerIsNonNull(it);
70  }
void CheckFieldIsRegistered(const std::string &field_name) const
Check that a field exists in the map.
const_iterator FindField(const std::string &field_name) const
Returns a const iterator to the field.
std::shared_ptr< T > EnsureFieldPointerIsNonNull(const_iterator &iterator) const
Ensure that a returned shared pointer is valid.

◆ Has()

template<typename T>
bool Moose::MFEM::NamedFieldsMap< T >::Has ( const std::string &  field_name) const
inline

◆ Register() [1/2]

template<typename T>
template<class FieldType , class... FieldArgs>
void Moose::MFEM::NamedFieldsMap< T >::Register ( const std::string &  field_name,
FieldArgs &&...  args 
)
inline

◆ Register() [2/2]

template<typename T>
void Moose::MFEM::NamedFieldsMap< T >::Register ( const std::string &  field_name,
std::shared_ptr< T >  field 
)
inline

Register association between field and field_name.

Definition at line 44 of file MFEMContainers.h.

45  {
46  CheckFieldIsRegistrable(field_name, field.get());
47 
48  Deregister(field_name);
49 
50  _field_map[field_name] = std::move(field);
51  }
void CheckFieldIsRegistrable([[maybe_unused]] const std::string &field_name, [[maybe_unused]] T *field) const
Check that the field pointer is valid and the field has not already been registered.
void Deregister(const std::string &field_name)
Unregister association between a field and the field_name.

◆ size()

template<typename T>
int Moose::MFEM::NamedFieldsMap< T >::size ( )
inline

Returns the number of elements in the map.

Definition at line 116 of file MFEMContainers.h.

Referenced by Moose::MFEM::ComplexEquationSystem::Init(), and Moose::MFEM::EquationSystem::Init().

116 { return _field_map.size(); }

Member Data Documentation

◆ _field_map

template<typename T>
MapType Moose::MFEM::NamedFieldsMap< T >::_field_map {}
private

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